1 /* Copyright 2016 Braden Farmer
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.farmerbb.taskbar.fragment;
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;
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;
51 public class AdvancedFragment extends SettingsFragment implements Preference.OnPreferenceClickListener {
53 boolean secondScreenPrefEnabled = false;
56 public void onCreate(Bundle savedInstanceState) {
57 finishedLoadingPrefs = false;
59 super.onCreate(savedInstanceState);
62 addPreferencesFromResource(R.xml.pref_advanced);
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()));
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;
78 getPreferenceScreen().removePreference(findPreference("secondscreen"));
80 bindPreferenceSummaryToValue(findPreference("dashboard"));
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));
87 findPreference("launcher").setEnabled(!lockHomeToggle);
89 finishedLoadingPrefs = true;
93 public void onActivityCreated(Bundle savedInstanceState) {
94 super.onActivityCreated(savedInstanceState);
96 AppCompatActivity activity = (AppCompatActivity) getActivity();
97 activity.setTitle(R.string.pref_header_advanced);
98 ActionBar actionBar = activity.getSupportActionBar();
100 actionBar.setDisplayHomeAsUpEnabled(true);
104 public void onResume() {
107 if(secondScreenPrefEnabled) {
108 findPreference("secondscreen").setTitle(
109 getSecondScreenPackageName() == null
110 ? R.string.pref_secondscreen_title_install
111 : R.string.pref_secondscreen_title_open);
114 updateDashboardGridSize(false);
117 @SuppressLint("SetTextI18n")
119 public boolean onPreferenceClick(final Preference p) {
120 final SharedPreferences pref = U.getSharedPreferences(getActivity());
123 case "clear_pinned_apps":
124 Intent clearIntent = null;
126 switch(pref.getString("theme", "light")) {
128 clearIntent = new Intent(getActivity(), ClearDataActivity.class);
131 clearIntent = new Intent(getActivity(), ClearDataActivityDark.class);
135 startActivity(clearIntent);
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);
144 U.showPermissionDialog(getActivity());
145 ((CheckBoxPreference) p).setChecked(false);
148 if(!((CheckBoxPreference) p).isChecked())
149 LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(new Intent("com.farmerbb.taskbar.KILL_HOME_ACTIVITY"));
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);
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);
161 boolean isPortrait = getActivity().getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
162 boolean isLandscape = getActivity().getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
165 int editText2Id = -1;
168 editTextId = R.id.fragmentEditText2;
169 editText2Id = R.id.fragmentEditText1;
173 editTextId = R.id.fragmentEditText1;
174 editText2Id = R.id.fragmentEditText2;
177 final EditText editText = U.findViewById(dialogLayout, editTextId);
178 final EditText editText2 = U.findViewById(dialogLayout, editText2Id);
180 builder.setView(dialogLayout)
181 .setTitle(R.string.dashboard_grid_size)
182 .setPositiveButton(R.string.action_ok, (dialog, id) -> {
183 boolean successfullyUpdated = false;
185 String widthString = editText.getText().toString();
186 String heightString = editText2.getText().toString();
188 if(widthString.length() > 0 && heightString.length() > 0) {
189 int width = Integer.parseInt(widthString);
190 int height = Integer.parseInt(heightString);
192 if(width > 0 && height > 0) {
193 SharedPreferences.Editor editor = pref.edit();
194 editor.putInt("dashboard_width", width);
195 editor.putInt("dashboard_height", height);
198 updateDashboardGridSize(true);
199 successfullyUpdated = true;
203 if(!successfullyUpdated)
204 U.showToast(getActivity(), R.string.invalid_grid_size);
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);
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))));
215 AlertDialog dialog = builder.create();
218 new Handler().post(() -> {
219 InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
220 imm.showSoftInput(editText2, InputMethodManager.SHOW_IMPLICIT);
224 case "navigation_bar_buttons":
225 Intent intent = null;
227 switch(pref.getString("theme", "light")) {
229 intent = new Intent(getActivity(), NavigationBarButtonsActivity.class);
232 intent = new Intent(getActivity(), NavigationBarButtonsActivityDark.class);
236 startActivity(intent);
239 PackageManager packageManager = getActivity().getPackageManager();
240 String packageName = getSecondScreenPackageName();
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"));
247 intent2 = packageManager.getLaunchIntentForPackage(packageName);
249 if(intent2 != null) {
250 intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
253 startActivity(intent2);
254 } catch (ActivityNotFoundException e) { /* Gracefully fail */ }
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));
268 boolean isPortrait = getActivity().getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
269 boolean isLandscape = getActivity().getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
284 findPreference("dashboard_grid_size").setSummary(getString(R.string.dashboard_grid_description, first, second));
286 if(restartTaskbar) U.restartTaskbar(getActivity());
289 private String getSecondScreenPackageName() {
290 PackageManager pm = getActivity().getPackageManager();
294 packageName = "com.farmerbb.secondscreen.free";
295 pm.getPackageInfo(packageName, 0);
296 } catch (PackageManager.NameNotFoundException e) {
298 packageName = "com.farmerbb.secondscreen";
299 pm.getPackageInfo(packageName, 0);
300 } catch (PackageManager.NameNotFoundException e1) {