OSDN Git Service

Move custom BuildConfig fields out of build.gradle
[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.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.SharedPreferences;
23 import android.content.pm.PackageManager;
24 import android.content.res.Configuration;
25 import android.os.Bundle;
26 import android.os.Handler;
27 import android.preference.CheckBoxPreference;
28 import android.preference.Preference;
29 import android.support.v4.content.LocalBroadcastManager;
30 import android.support.v7.app.ActionBar;
31 import android.support.v7.app.AlertDialog;
32 import android.support.v7.app.AppCompatActivity;
33 import android.text.Spannable;
34 import android.view.View;
35 import android.view.inputmethod.InputMethodManager;
36 import android.widget.EditText;
37 import android.widget.LinearLayout;
38
39 import com.farmerbb.taskbar.R;
40 import com.farmerbb.taskbar.activity.ClearDataActivity;
41 import com.farmerbb.taskbar.activity.NavigationBarButtonsActivity;
42 import com.farmerbb.taskbar.activity.dark.ClearDataActivityDark;
43 import com.farmerbb.taskbar.activity.HomeActivity;
44 import com.farmerbb.taskbar.activity.KeyboardShortcutActivity;
45 import com.farmerbb.taskbar.activity.dark.NavigationBarButtonsActivityDark;
46 import com.farmerbb.taskbar.util.U;
47
48 import java.lang.reflect.Method;
49
50 public class AdvancedFragment extends SettingsFragment implements Preference.OnPreferenceClickListener {
51
52     @Override
53     public void onActivityCreated(Bundle savedInstanceState) {
54         finishedLoadingPrefs = false;
55
56         super.onActivityCreated(savedInstanceState);
57
58         if(findPreference("dummy") == null) {
59             // Add preferences
60             addPreferencesFromResource(R.xml.pref_advanced);
61
62             // Set OnClickListeners for certain preferences
63             findPreference("clear_pinned_apps").setOnPreferenceClickListener(this);
64             findPreference("launcher").setOnPreferenceClickListener(this);
65             findPreference("keyboard_shortcut").setOnPreferenceClickListener(this);
66             findPreference("dashboard_grid_size").setOnPreferenceClickListener(this);
67             findPreference("navigation_bar_buttons").setOnPreferenceClickListener(this);
68             findPreference("keyboard_shortcut").setSummary(getKeyboardShortcutSummary());
69
70             bindPreferenceSummaryToValue(findPreference("dashboard"));
71
72             SharedPreferences pref = U.getSharedPreferences(getActivity());
73             boolean lockHomeToggle = U.hasSupportLibrary(getActivity()) && pref.getBoolean("launcher", false);
74
75             findPreference("launcher").setEnabled(!lockHomeToggle);
76         }
77
78         AppCompatActivity activity = (AppCompatActivity) getActivity();
79         activity.setTitle(R.string.pref_header_advanced);
80         ActionBar actionBar = activity.getSupportActionBar();
81         if(actionBar != null)
82             actionBar.setDisplayHomeAsUpEnabled(true);
83
84         finishedLoadingPrefs = true;
85     }
86
87     @Override
88     public void onResume() {
89         super.onResume();
90
91         updateDashboardGridSize(false);
92     }
93
94     @SuppressLint("SetTextI18n")
95     @Override
96     public boolean onPreferenceClick(final Preference p) {
97         final SharedPreferences pref = U.getSharedPreferences(getActivity());
98
99         switch(p.getKey()) {
100             case "clear_pinned_apps":
101                 Intent clearIntent = null;
102
103                 switch(pref.getString("theme", "light")) {
104                     case "light":
105                         clearIntent = new Intent(getActivity(), ClearDataActivity.class);
106                         break;
107                     case "dark":
108                         clearIntent = new Intent(getActivity(), ClearDataActivityDark.class);
109                         break;
110                 }
111
112                 startActivity(clearIntent);
113                 break;
114             case "launcher":
115                 if(U.canDrawOverlays(getActivity())) {
116                     ComponentName component = new ComponentName(getActivity(), HomeActivity.class);
117                     getActivity().getPackageManager().setComponentEnabledSetting(component,
118                             ((CheckBoxPreference) p).isChecked() ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
119                             PackageManager.DONT_KILL_APP);
120                 } else {
121                     U.showPermissionDialog(getActivity());
122                     ((CheckBoxPreference) p).setChecked(false);
123                 }
124
125                 if(!((CheckBoxPreference) p).isChecked())
126                     LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(new Intent("com.farmerbb.taskbar.KILL_HOME_ACTIVITY"));
127                 break;
128             case "keyboard_shortcut":
129                 ComponentName component = new ComponentName(getActivity(), KeyboardShortcutActivity.class);
130                 getActivity().getPackageManager().setComponentEnabledSetting(component,
131                         ((CheckBoxPreference) p).isChecked() ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
132                         PackageManager.DONT_KILL_APP);
133                 break;
134             case "dashboard_grid_size":
135                 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
136                 LinearLayout dialogLayout = (LinearLayout) View.inflate(getActivity(), R.layout.dashboard_size_dialog, null);
137
138                 boolean isPortrait = getActivity().getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
139                 boolean isLandscape = getActivity().getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
140
141                 int editTextId = -1;
142                 int editText2Id = -1;
143
144                 if(isPortrait) {
145                     editTextId = R.id.fragmentEditText2;
146                     editText2Id = R.id.fragmentEditText1;
147                 }
148
149                 if(isLandscape) {
150                     editTextId = R.id.fragmentEditText1;
151                     editText2Id = R.id.fragmentEditText2;
152                 }
153
154                 final EditText editText = (EditText) dialogLayout.findViewById(editTextId);
155                 final EditText editText2 = (EditText) dialogLayout.findViewById(editText2Id);
156
157                 builder.setView(dialogLayout)
158                         .setTitle(R.string.dashboard_grid_size)
159                         .setPositiveButton(R.string.action_ok, (dialog, id) -> {
160                             boolean successfullyUpdated = false;
161
162                             String widthString = editText.getText().toString();
163                             String heightString = editText2.getText().toString();
164
165                             if(widthString.length() > 0 && heightString.length() > 0) {
166                                 int width = Integer.parseInt(widthString);
167                                 int height = Integer.parseInt(heightString);
168
169                                 if(width > 0 && height > 0) {
170                                     SharedPreferences.Editor editor = pref.edit();
171                                     editor.putInt("dashboard_width", width);
172                                     editor.putInt("dashboard_height", height);
173                                     editor.apply();
174
175                                     updateDashboardGridSize(true);
176                                     successfullyUpdated = true;
177                                 }
178                             }
179
180                             if(!successfullyUpdated)
181                                 U.showToast(getActivity(), R.string.invalid_grid_size);
182                         })
183                         .setNegativeButton(R.string.action_cancel, null);
184
185                 editText.setText(Integer.toString(pref.getInt("dashboard_width", getActivity().getApplicationContext().getResources().getInteger(R.integer.dashboard_width))));
186                 editText2.setText(Integer.toString(pref.getInt("dashboard_height", getActivity().getApplicationContext().getResources().getInteger(R.integer.dashboard_height))));
187
188                 AlertDialog dialog = builder.create();
189                 dialog.show();
190
191                 new Handler().post(() -> {
192                     InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
193                     imm.showSoftInput(editText2, InputMethodManager.SHOW_IMPLICIT);
194                 });
195
196                 break;
197             case "navigation_bar_buttons":
198                 Intent intent = null;
199
200                 switch(pref.getString("theme", "light")) {
201                     case "light":
202                         intent = new Intent(getActivity(), NavigationBarButtonsActivity.class);
203                         break;
204                     case "dark":
205                         intent = new Intent(getActivity(), NavigationBarButtonsActivityDark.class);
206                         break;
207                 }
208
209                 startActivity(intent);
210                 break;
211         }
212
213         return true;
214     }
215
216     private void updateDashboardGridSize(boolean restartTaskbar) {
217         SharedPreferences pref = U.getSharedPreferences(getActivity());
218         int width = pref.getInt("dashboard_width", getActivity().getApplicationContext().getResources().getInteger(R.integer.dashboard_width));
219         int height = pref.getInt("dashboard_height", getActivity().getApplicationContext().getResources().getInteger(R.integer.dashboard_height));
220
221         boolean isPortrait = getActivity().getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
222         boolean isLandscape = getActivity().getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
223
224         int first = -1;
225         int second = -1;
226
227         if(isPortrait) {
228             first = height;
229             second = width;
230         }
231
232         if(isLandscape) {
233             first = width;
234             second = height;
235         }
236
237         findPreference("dashboard_grid_size").setSummary(getString(R.string.dashboard_grid_description, first, second));
238
239         if(restartTaskbar) U.restartTaskbar(getActivity());
240     }
241
242     private CharSequence getKeyboardShortcutSummary() {
243         try {
244             Class iconicsClass = Class.forName("com.mikepenz.iconics.Iconics$IconicsBuilder");
245             Class iconicsViewClass = Class.forName("com.mikepenz.iconics.Iconics$IconicsBuilderString");
246             Method method = iconicsClass.getMethod("ctx", Context.class);
247             Method method2 = iconicsClass.getMethod("on", String.class);
248             Method method3 = iconicsViewClass.getMethod("build");
249
250             Object iconicsBuilder = iconicsClass.newInstance();
251             method.invoke(iconicsBuilder, getActivity());
252
253             Object iconicsView = method2.invoke(iconicsBuilder, getString(R.string.pref_description_keyboard_shortcut));
254             return (Spannable) method3.invoke(iconicsView);
255         } catch (Exception e) {
256             return getString(R.string.pref_description_keyboard_shortcut_alt);
257         }
258     }
259 }