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.app.Activity;
20 import android.app.FragmentTransaction;
21 import android.content.ActivityNotFoundException;
22 import android.content.BroadcastReceiver;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.SharedPreferences;
26 import android.content.pm.PackageManager;
27 import android.content.res.Configuration;
28 import android.net.Uri;
29 import android.os.Build;
30 import android.os.Bundle;
31 import android.os.Handler;
32 import android.preference.CheckBoxPreference;
33 import android.preference.Preference;
34 import androidx.appcompat.app.ActionBar;
35 import androidx.appcompat.app.AlertDialog;
36 import androidx.appcompat.app.AppCompatActivity;
38 import android.view.View;
39 import android.view.inputmethod.InputMethodManager;
40 import android.widget.EditText;
41 import android.widget.LinearLayout;
43 import com.farmerbb.taskbar.BuildConfig;
44 import com.farmerbb.taskbar.R;
45 import com.farmerbb.taskbar.activity.KeyboardShortcutActivityLockDevice;
46 import com.farmerbb.taskbar.activity.NavigationBarButtonsActivity;
47 import com.farmerbb.taskbar.activity.HomeActivity;
48 import com.farmerbb.taskbar.activity.KeyboardShortcutActivity;
49 import com.farmerbb.taskbar.activity.dark.NavigationBarButtonsActivityDark;
50 import com.farmerbb.taskbar.content.TaskbarIntent;
51 import com.farmerbb.taskbar.util.DependencyUtils;
52 import com.farmerbb.taskbar.util.U;
54 public class AdvancedFragment extends SettingsFragment {
56 boolean secondScreenPrefEnabled = false;
58 private BroadcastReceiver homeToggleReceiver = new BroadcastReceiver() {
60 public void onReceive(Context context, Intent intent) {
61 SharedPreferences pref = U.getSharedPreferences(getActivity());
62 CheckBoxPreference checkBox = (CheckBoxPreference) findPreference("launcher");
63 checkBox.setChecked(pref.getBoolean("launcher", false));
68 public void onCreate(Bundle savedInstanceState) {
69 finishedLoadingPrefs = false;
71 super.onCreate(savedInstanceState);
74 addPreferencesFromResource(R.xml.tb_pref_advanced);
76 // Set OnClickListeners for certain preferences
77 findPreference("dashboard_grid_size").setOnPreferenceClickListener(this);
78 findPreference("keyboard_shortcut").setSummary(DependencyUtils.getKeyboardShortcutSummary(getActivity()));
80 boolean isLibrary = U.isLibrary(getActivity());
81 boolean isAndroidx86 = getActivity().getPackageName().equals(BuildConfig.ANDROIDX86_APPLICATION_ID);
83 SharedPreferences pref = U.getSharedPreferences(getActivity());
84 boolean lockHomeToggle = (pref.getBoolean("launcher", false)
85 && U.isLauncherPermanentlyEnabled(getActivity()))
86 || pref.getBoolean("desktop_mode", false);
89 getPreferenceScreen().removePreference(findPreference("tasker_enabled"));
90 getPreferenceScreen().removePreference(findPreference("launcher"));
91 getPreferenceScreen().removePreference(findPreference("keyboard_shortcut"));
92 getPreferenceScreen().removePreference(findPreference("navigation_bar_buttons"));
93 getPreferenceScreen().removePreference(findPreference("manage_app_data"));
95 findPreference("clear_pinned_apps").setOnPreferenceClickListener(this);
97 findPreference("launcher").setEnabled(!lockHomeToggle);
98 findPreference("launcher").setOnPreferenceClickListener(this);
99 findPreference("keyboard_shortcut").setOnPreferenceClickListener(this);
100 findPreference("navigation_bar_buttons").setOnPreferenceClickListener(this);
101 findPreference("manage_app_data").setOnPreferenceClickListener(this);
103 getPreferenceScreen().removePreference(findPreference("clear_pinned_apps"));
106 if(!isAndroidx86 && !isLibrary
107 && U.isPlayStoreInstalled(getActivity())
108 && U.isPlayStoreRelease(getActivity())) {
109 findPreference("secondscreen").setOnPreferenceClickListener(this);
110 secondScreenPrefEnabled = true;
112 getPreferenceScreen().removePreference(findPreference("secondscreen"));
114 bindPreferenceSummaryToValue(findPreference("dashboard"));
116 finishedLoadingPrefs = true;
120 public void onActivityCreated(Bundle savedInstanceState) {
121 super.onActivityCreated(savedInstanceState);
123 AppCompatActivity activity = (AppCompatActivity) getActivity();
124 activity.setTitle(R.string.tb_pref_header_advanced);
125 ActionBar actionBar = activity.getSupportActionBar();
126 if(actionBar != null)
127 actionBar.setDisplayHomeAsUpEnabled(true);
131 public void onResume() {
134 if(secondScreenPrefEnabled) {
135 findPreference("secondscreen").setTitle(
136 U.getSecondScreenPackageName(getActivity()) == null
137 ? R.string.tb_pref_secondscreen_title_install
138 : R.string.tb_pref_secondscreen_title_open);
141 updateDashboardGridSize(false);
145 public void onAttach(Activity activity) {
146 super.onAttach(activity);
148 U.registerReceiver(getActivity(), homeToggleReceiver, TaskbarIntent.ACTION_LAUNCHER_PREF_CHANGED);
152 public void onDetach() {
153 U.unregisterReceiver(getActivity(), homeToggleReceiver);
158 @SuppressLint("SetTextI18n")
159 @SuppressWarnings("deprecation")
161 public boolean onPreferenceClick(final Preference p) {
162 final SharedPreferences pref = U.getSharedPreferences(getActivity());
166 if(U.canDrawOverlays(getActivity())) {
167 U.setComponentEnabled(getActivity(), HomeActivity.class,
168 ((CheckBoxPreference) p).isChecked());
170 U.showPermissionDialog(getActivity());
171 ((CheckBoxPreference) p).setChecked(false);
174 if(!((CheckBoxPreference) p).isChecked()) {
175 U.sendBroadcast(getActivity(), TaskbarIntent.ACTION_KILL_HOME_ACTIVITY);
178 case "keyboard_shortcut":
179 U.setComponentEnabled(getActivity(), KeyboardShortcutActivity.class,
180 ((CheckBoxPreference) p).isChecked());
182 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
183 U.setComponentEnabled(getActivity(), KeyboardShortcutActivityLockDevice.class,
184 ((CheckBoxPreference) p).isChecked());
187 case "dashboard_grid_size":
188 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
189 LinearLayout dialogLayout = (LinearLayout) View.inflate(getActivity(), R.layout.tb_dashboard_size_dialog, null);
191 boolean isPortrait = getActivity().getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
192 boolean isLandscape = getActivity().getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
195 int editText2Id = -1;
198 editTextId = R.id.fragmentEditText2;
199 editText2Id = R.id.fragmentEditText1;
203 editTextId = R.id.fragmentEditText1;
204 editText2Id = R.id.fragmentEditText2;
207 final EditText editText = dialogLayout.findViewById(editTextId);
208 final EditText editText2 = dialogLayout.findViewById(editText2Id);
210 builder.setView(dialogLayout)
211 .setTitle(R.string.tb_dashboard_grid_size)
212 .setPositiveButton(R.string.tb_action_ok, (dialog, id) -> {
213 boolean successfullyUpdated = false;
215 String widthString = editText.getText().toString();
216 String heightString = editText2.getText().toString();
218 if(widthString.length() > 0 && heightString.length() > 0) {
219 int width = Integer.parseInt(widthString);
220 int height = Integer.parseInt(heightString);
222 if(width > 0 && height > 0) {
223 SharedPreferences.Editor editor = pref.edit();
224 editor.putInt("dashboard_width", width);
225 editor.putInt("dashboard_height", height);
228 updateDashboardGridSize(true);
229 successfullyUpdated = true;
233 if(!successfullyUpdated)
234 U.showToast(getActivity(), R.string.tb_invalid_grid_size);
236 .setNegativeButton(R.string.tb_action_cancel, null)
237 .setNeutralButton(R.string.tb_use_default, (dialog, id) -> {
238 pref.edit().remove("dashboard_width").remove("dashboard_height").apply();
239 updateDashboardGridSize(true);
242 editText.setText(Integer.toString(pref.getInt("dashboard_width", getActivity().getApplicationContext().getResources().getInteger(R.integer.tb_dashboard_width))));
243 editText2.setText(Integer.toString(pref.getInt("dashboard_height", getActivity().getApplicationContext().getResources().getInteger(R.integer.tb_dashboard_height))));
245 AlertDialog dialog = builder.create();
248 new Handler().post(() -> {
249 InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
250 imm.showSoftInput(editText2, InputMethodManager.SHOW_IMPLICIT);
254 case "navigation_bar_buttons":
255 Intent intent = null;
257 switch(pref.getString("theme", "light")) {
259 intent = new Intent(getActivity(), NavigationBarButtonsActivity.class);
262 intent = new Intent(getActivity(), NavigationBarButtonsActivityDark.class);
266 startActivity(intent);
269 PackageManager packageManager = getActivity().getPackageManager();
270 String packageName = U.getSecondScreenPackageName(getActivity());
273 if(packageName == null) {
274 intent2 = new Intent(Intent.ACTION_VIEW);
275 intent2.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.farmerbb.secondscreen.free"));
277 intent2 = packageManager.getLaunchIntentForPackage(packageName);
279 if(intent2 != null) {
280 intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
283 startActivity(intent2);
284 } catch (ActivityNotFoundException e) { /* Gracefully fail */ }
288 case "manage_app_data":
291 .replace(R.id.fragmentContainer, new ManageAppDataFragment(), "ManageAppDataFragment")
292 .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
297 return super.onPreferenceClick(p);
300 private void updateDashboardGridSize(boolean restartTaskbar) {
301 SharedPreferences pref = U.getSharedPreferences(getActivity());
302 int width = pref.getInt("dashboard_width", getActivity().getApplicationContext().getResources().getInteger(R.integer.tb_dashboard_width));
303 int height = pref.getInt("dashboard_height", getActivity().getApplicationContext().getResources().getInteger(R.integer.tb_dashboard_height));
305 boolean isPortrait = getActivity().getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
306 boolean isLandscape = getActivity().getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
321 findPreference("dashboard_grid_size").setSummary(getString(R.string.tb_dashboard_grid_description, first, second));
323 if(restartTaskbar) U.restartTaskbar(getActivity());