OSDN Git Service

e91c62bcd814f0cbd8030bddcd661517fa3e6fc2
[android-x86/packages-apps-Taskbar.git] / app / src / main / java / com / farmerbb / taskbar / fragment / AdvancedFragment.java
1 /* Copyright 2016 Braden Farmer
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 package com.farmerbb.taskbar.fragment;
17
18 import android.annotation.SuppressLint;
19 import android.content.ActivityNotFoundException;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.SharedPreferences;
24 import android.content.pm.PackageManager;
25 import android.content.res.Configuration;
26 import android.net.Uri;
27 import android.os.Bundle;
28 import android.os.Handler;
29 import android.preference.CheckBoxPreference;
30 import android.preference.Preference;
31 import android.support.v4.content.LocalBroadcastManager;
32 import android.support.v7.app.ActionBar;
33 import android.support.v7.app.AlertDialog;
34 import android.support.v7.app.AppCompatActivity;
35 import android.view.View;
36 import android.view.inputmethod.InputMethodManager;
37 import android.widget.EditText;
38 import android.widget.LinearLayout;
39
40 import com.farmerbb.taskbar.BuildConfig;
41 import com.farmerbb.taskbar.R;
42 import com.farmerbb.taskbar.activity.ClearDataActivity;
43 import com.farmerbb.taskbar.activity.NavigationBarButtonsActivity;
44 import com.farmerbb.taskbar.activity.dark.ClearDataActivityDark;
45 import com.farmerbb.taskbar.activity.HomeActivity;
46 import com.farmerbb.taskbar.activity.KeyboardShortcutActivity;
47 import com.farmerbb.taskbar.activity.dark.NavigationBarButtonsActivityDark;
48 import com.farmerbb.taskbar.util.DependencyUtils;
49 import com.farmerbb.taskbar.util.U;
50
51 public class AdvancedFragment extends SettingsFragment implements Preference.OnPreferenceClickListener {
52
53     boolean secondScreenPrefEnabled = false;
54
55     @Override
56     public void onCreate(Bundle savedInstanceState) {
57         finishedLoadingPrefs = false;
58
59         super.onCreate(savedInstanceState);
60
61         // Add preferences
62         addPreferencesFromResource(R.xml.pref_advanced);
63
64         // Set OnClickListeners for certain preferences
65         findPreference("clear_pinned_apps").setOnPreferenceClickListener(this);
66         findPreference("launcher").setOnPreferenceClickListener(this);
67         findPreference("keyboard_shortcut").setOnPreferenceClickListener(this);
68         findPreference("dashboard_grid_size").setOnPreferenceClickListener(this);
69         findPreference("navigation_bar_buttons").setOnPreferenceClickListener(this);
70         findPreference("keyboard_shortcut").setSummary(DependencyUtils.getKeyboardShortcutSummary(getActivity()));
71
72         if(!BuildConfig.APPLICATION_ID.equals(BuildConfig.ANDROIDX86_APPLICATION_ID)
73                 && !U.hasSupportLibrary(getActivity())
74                 && U.isPlayStoreInstalled(getActivity())) {
75             findPreference("secondscreen").setOnPreferenceClickListener(this);
76             secondScreenPrefEnabled = true;
77         } else
78             getPreferenceScreen().removePreference(findPreference("secondscreen"));
79
80         bindPreferenceSummaryToValue(findPreference("dashboard"));
81
82         SharedPreferences pref = U.getSharedPreferences(getActivity());
83         boolean lockHomeToggle = pref.getBoolean("launcher", false)
84                 && (U.hasSupportLibrary(getActivity())
85                 || BuildConfig.APPLICATION_ID.equals(BuildConfig.ANDROIDX86_APPLICATION_ID));
86
87         findPreference("launcher").setEnabled(!lockHomeToggle);
88
89         finishedLoadingPrefs = true;
90     }
91
92     @Override
93     public void onActivityCreated(Bundle savedInstanceState) {
94         super.onActivityCreated(savedInstanceState);
95
96         AppCompatActivity activity = (AppCompatActivity) getActivity();
97         activity.setTitle(R.string.pref_header_advanced);
98         ActionBar actionBar = activity.getSupportActionBar();
99         if(actionBar != null)
100             actionBar.setDisplayHomeAsUpEnabled(true);
101     }
102
103     @Override
104     public void onResume() {
105         super.onResume();
106
107         if(secondScreenPrefEnabled) {
108             findPreference("secondscreen").setTitle(
109                     getSecondScreenPackageName() == null
110                             ? R.string.pref_secondscreen_title_install
111                             : R.string.pref_secondscreen_title_open);
112         }
113
114         updateDashboardGridSize(false);
115     }
116
117     @SuppressLint("SetTextI18n")
118     @Override
119     public boolean onPreferenceClick(final Preference p) {
120         final SharedPreferences pref = U.getSharedPreferences(getActivity());
121
122         switch(p.getKey()) {
123             case "clear_pinned_apps":
124                 Intent clearIntent = null;
125
126                 switch(pref.getString("theme", "light")) {
127                     case "light":
128                         clearIntent = new Intent(getActivity(), ClearDataActivity.class);
129                         break;
130                     case "dark":
131                         clearIntent = new Intent(getActivity(), ClearDataActivityDark.class);
132                         break;
133                 }
134
135                 startActivity(clearIntent);
136                 break;
137             case "launcher":
138                 if(U.canDrawOverlays(getActivity())) {
139                     ComponentName component = new ComponentName(getActivity(), HomeActivity.class);
140                     getActivity().getPackageManager().setComponentEnabledSetting(component,
141                             ((CheckBoxPreference) p).isChecked() ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
142                             PackageManager.DONT_KILL_APP);
143                 } else {
144                     U.showPermissionDialog(getActivity());
145                     ((CheckBoxPreference) p).setChecked(false);
146                 }
147
148                 if(!((CheckBoxPreference) p).isChecked())
149                     LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(new Intent("com.farmerbb.taskbar.KILL_HOME_ACTIVITY"));
150                 break;
151             case "keyboard_shortcut":
152                 ComponentName component = new ComponentName(getActivity(), KeyboardShortcutActivity.class);
153                 getActivity().getPackageManager().setComponentEnabledSetting(component,
154                         ((CheckBoxPreference) p).isChecked() ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
155                         PackageManager.DONT_KILL_APP);
156                 break;
157             case "dashboard_grid_size":
158                 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
159                 LinearLayout dialogLayout = (LinearLayout) View.inflate(getActivity(), R.layout.dashboard_size_dialog, null);
160
161                 boolean isPortrait = getActivity().getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
162                 boolean isLandscape = getActivity().getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
163
164                 int editTextId = -1;
165                 int editText2Id = -1;
166
167                 if(isPortrait) {
168                     editTextId = R.id.fragmentEditText2;
169                     editText2Id = R.id.fragmentEditText1;
170                 }
171
172                 if(isLandscape) {
173                     editTextId = R.id.fragmentEditText1;
174                     editText2Id = R.id.fragmentEditText2;
175                 }
176
177                 final EditText editText = U.findViewById(dialogLayout, editTextId);
178                 final EditText editText2 = U.findViewById(dialogLayout, editText2Id);
179
180                 builder.setView(dialogLayout)
181                         .setTitle(R.string.dashboard_grid_size)
182                         .setPositiveButton(R.string.action_ok, (dialog, id) -> {
183                             boolean successfullyUpdated = false;
184
185                             String widthString = editText.getText().toString();
186                             String heightString = editText2.getText().toString();
187
188                             if(widthString.length() > 0 && heightString.length() > 0) {
189                                 int width = Integer.parseInt(widthString);
190                                 int height = Integer.parseInt(heightString);
191
192                                 if(width > 0 && height > 0) {
193                                     SharedPreferences.Editor editor = pref.edit();
194                                     editor.putInt("dashboard_width", width);
195                                     editor.putInt("dashboard_height", height);
196                                     editor.apply();
197
198                                     updateDashboardGridSize(true);
199                                     successfullyUpdated = true;
200                                 }
201                             }
202
203                             if(!successfullyUpdated)
204                                 U.showToast(getActivity(), R.string.invalid_grid_size);
205                         })
206                         .setNegativeButton(R.string.action_cancel, null)
207                         .setNeutralButton(R.string.use_default, (dialog, id) -> {
208                             pref.edit().remove("dashboard_width").remove("dashboard_height").apply();
209                             updateDashboardGridSize(true);
210                         });
211
212                 editText.setText(Integer.toString(pref.getInt("dashboard_width", getActivity().getApplicationContext().getResources().getInteger(R.integer.dashboard_width))));
213                 editText2.setText(Integer.toString(pref.getInt("dashboard_height", getActivity().getApplicationContext().getResources().getInteger(R.integer.dashboard_height))));
214
215                 AlertDialog dialog = builder.create();
216                 dialog.show();
217
218                 new Handler().post(() -> {
219                     InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
220                     imm.showSoftInput(editText2, InputMethodManager.SHOW_IMPLICIT);
221                 });
222
223                 break;
224             case "navigation_bar_buttons":
225                 Intent intent = null;
226
227                 switch(pref.getString("theme", "light")) {
228                     case "light":
229                         intent = new Intent(getActivity(), NavigationBarButtonsActivity.class);
230                         break;
231                     case "dark":
232                         intent = new Intent(getActivity(), NavigationBarButtonsActivityDark.class);
233                         break;
234                 }
235
236                 startActivity(intent);
237                 break;
238             case "secondscreen":
239                 PackageManager packageManager = getActivity().getPackageManager();
240                 String packageName = getSecondScreenPackageName();
241                 Intent intent2;
242
243                 if(packageName == null) {
244                     intent2 = new Intent(Intent.ACTION_VIEW);
245                     intent2.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.farmerbb.secondscreen.free"));
246                 } else
247                     intent2 = packageManager.getLaunchIntentForPackage(packageName);
248
249                 if(intent2 != null) {
250                     intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
251
252                     try {
253                         startActivity(intent2);
254                     } catch (ActivityNotFoundException e) { /* Gracefully fail */ }
255                 }
256
257                 break;
258         }
259
260         return true;
261     }
262
263     private void updateDashboardGridSize(boolean restartTaskbar) {
264         SharedPreferences pref = U.getSharedPreferences(getActivity());
265         int width = pref.getInt("dashboard_width", getActivity().getApplicationContext().getResources().getInteger(R.integer.dashboard_width));
266         int height = pref.getInt("dashboard_height", getActivity().getApplicationContext().getResources().getInteger(R.integer.dashboard_height));
267
268         boolean isPortrait = getActivity().getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
269         boolean isLandscape = getActivity().getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
270
271         int first = -1;
272         int second = -1;
273
274         if(isPortrait) {
275             first = height;
276             second = width;
277         }
278
279         if(isLandscape) {
280             first = width;
281             second = height;
282         }
283
284         findPreference("dashboard_grid_size").setSummary(getString(R.string.dashboard_grid_description, first, second));
285
286         if(restartTaskbar) U.restartTaskbar(getActivity());
287     }
288
289     private String getSecondScreenPackageName() {
290         PackageManager pm = getActivity().getPackageManager();
291         String packageName;
292
293         try {
294             packageName = "com.farmerbb.secondscreen.free";
295             pm.getPackageInfo(packageName, 0);
296         } catch (PackageManager.NameNotFoundException e) {
297             try {
298                 packageName = "com.farmerbb.secondscreen";
299                 pm.getPackageInfo(packageName, 0);
300             } catch (PackageManager.NameNotFoundException e1) {
301                 packageName = null;
302             }
303         }
304
305         return packageName;
306     }
307 }