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.content.ActivityNotFoundException;
21 import android.content.BroadcastReceiver;
22 import android.content.ComponentName;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.IntentFilter;
26 import android.content.SharedPreferences;
27 import android.content.pm.PackageManager;
28 import android.content.res.Configuration;
29 import android.net.Uri;
30 import android.os.Bundle;
31 import android.os.Handler;
32 import android.preference.CheckBoxPreference;
33 import android.preference.Preference;
34 import android.support.v4.content.LocalBroadcastManager;
35 import android.support.v7.app.ActionBar;
36 import android.support.v7.app.AlertDialog;
37 import android.support.v7.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.ClearDataActivity;
46 import com.farmerbb.taskbar.activity.NavigationBarButtonsActivity;
47 import com.farmerbb.taskbar.activity.dark.ClearDataActivityDark;
48 import com.farmerbb.taskbar.activity.HomeActivity;
49 import com.farmerbb.taskbar.activity.KeyboardShortcutActivity;
50 import com.farmerbb.taskbar.activity.dark.NavigationBarButtonsActivityDark;
51 import com.farmerbb.taskbar.util.DependencyUtils;
52 import com.farmerbb.taskbar.util.U;
54 public class AdvancedFragment extends SettingsFragment implements Preference.OnPreferenceClickListener {
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.pref_advanced);
76 // Set OnClickListeners for certain preferences
77 findPreference("clear_pinned_apps").setOnPreferenceClickListener(this);
78 findPreference("launcher").setOnPreferenceClickListener(this);
79 findPreference("keyboard_shortcut").setOnPreferenceClickListener(this);
80 findPreference("dashboard_grid_size").setOnPreferenceClickListener(this);
81 findPreference("navigation_bar_buttons").setOnPreferenceClickListener(this);
82 findPreference("keyboard_shortcut").setSummary(DependencyUtils.getKeyboardShortcutSummary(getActivity()));
84 boolean isAndroidx86 = BuildConfig.APPLICATION_ID.equals(BuildConfig.ANDROIDX86_APPLICATION_ID);
86 if(!isAndroidx86 && U.isPlayStoreInstalled(getActivity()) && U.isPlayStoreRelease(getActivity())) {
87 findPreference("secondscreen").setOnPreferenceClickListener(this);
88 secondScreenPrefEnabled = true;
90 getPreferenceScreen().removePreference(findPreference("secondscreen"));
93 getPreferenceScreen().removePreference(findPreference("tasker_enabled"));
95 bindPreferenceSummaryToValue(findPreference("dashboard"));
97 SharedPreferences pref = U.getSharedPreferences(getActivity());
98 boolean lockHomeToggle = pref.getBoolean("launcher", false)
99 && U.isLauncherPermanentlyEnabled(getActivity());
101 findPreference("launcher").setEnabled(!lockHomeToggle);
103 finishedLoadingPrefs = true;
107 public void onActivityCreated(Bundle savedInstanceState) {
108 super.onActivityCreated(savedInstanceState);
110 AppCompatActivity activity = (AppCompatActivity) getActivity();
111 activity.setTitle(R.string.pref_header_advanced);
112 ActionBar actionBar = activity.getSupportActionBar();
113 if(actionBar != null)
114 actionBar.setDisplayHomeAsUpEnabled(true);
118 public void onResume() {
121 if(secondScreenPrefEnabled) {
122 findPreference("secondscreen").setTitle(
123 U.getSecondScreenPackageName(getActivity()) == null
124 ? R.string.pref_secondscreen_title_install
125 : R.string.pref_secondscreen_title_open);
128 updateDashboardGridSize(false);
132 public void onAttach(Activity activity) {
133 super.onAttach(activity);
135 LocalBroadcastManager.getInstance(getActivity()).registerReceiver(homeToggleReceiver,
136 new IntentFilter("com.farmerbb.taskbar.LAUNCHER_PREF_CHANGED"));
140 public void onDetach() {
141 LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(homeToggleReceiver);
146 @SuppressLint("SetTextI18n")
148 public boolean onPreferenceClick(final Preference p) {
149 final SharedPreferences pref = U.getSharedPreferences(getActivity());
152 case "clear_pinned_apps":
153 Intent clearIntent = null;
155 switch(pref.getString("theme", "light")) {
157 clearIntent = new Intent(getActivity(), ClearDataActivity.class);
160 clearIntent = new Intent(getActivity(), ClearDataActivityDark.class);
164 startActivity(clearIntent);
167 if(U.canDrawOverlays(getActivity())) {
168 ComponentName component = new ComponentName(getActivity(), HomeActivity.class);
169 getActivity().getPackageManager().setComponentEnabledSetting(component,
170 ((CheckBoxPreference) p).isChecked() ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
171 PackageManager.DONT_KILL_APP);
173 U.showPermissionDialog(getActivity());
174 ((CheckBoxPreference) p).setChecked(false);
177 if(!((CheckBoxPreference) p).isChecked())
178 LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(new Intent("com.farmerbb.taskbar.KILL_HOME_ACTIVITY"));
180 case "keyboard_shortcut":
181 ComponentName component = new ComponentName(getActivity(), KeyboardShortcutActivity.class);
182 getActivity().getPackageManager().setComponentEnabledSetting(component,
183 ((CheckBoxPreference) p).isChecked() ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
184 PackageManager.DONT_KILL_APP);
186 case "dashboard_grid_size":
187 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
188 LinearLayout dialogLayout = (LinearLayout) View.inflate(getActivity(), R.layout.dashboard_size_dialog, null);
190 boolean isPortrait = getActivity().getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
191 boolean isLandscape = getActivity().getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
194 int editText2Id = -1;
197 editTextId = R.id.fragmentEditText2;
198 editText2Id = R.id.fragmentEditText1;
202 editTextId = R.id.fragmentEditText1;
203 editText2Id = R.id.fragmentEditText2;
206 final EditText editText = U.findViewById(dialogLayout, editTextId);
207 final EditText editText2 = U.findViewById(dialogLayout, editText2Id);
209 builder.setView(dialogLayout)
210 .setTitle(R.string.dashboard_grid_size)
211 .setPositiveButton(R.string.action_ok, (dialog, id) -> {
212 boolean successfullyUpdated = false;
214 String widthString = editText.getText().toString();
215 String heightString = editText2.getText().toString();
217 if(widthString.length() > 0 && heightString.length() > 0) {
218 int width = Integer.parseInt(widthString);
219 int height = Integer.parseInt(heightString);
221 if(width > 0 && height > 0) {
222 SharedPreferences.Editor editor = pref.edit();
223 editor.putInt("dashboard_width", width);
224 editor.putInt("dashboard_height", height);
227 updateDashboardGridSize(true);
228 successfullyUpdated = true;
232 if(!successfullyUpdated)
233 U.showToast(getActivity(), R.string.invalid_grid_size);
235 .setNegativeButton(R.string.action_cancel, null)
236 .setNeutralButton(R.string.use_default, (dialog, id) -> {
237 pref.edit().remove("dashboard_width").remove("dashboard_height").apply();
238 updateDashboardGridSize(true);
241 editText.setText(Integer.toString(pref.getInt("dashboard_width", getActivity().getApplicationContext().getResources().getInteger(R.integer.dashboard_width))));
242 editText2.setText(Integer.toString(pref.getInt("dashboard_height", getActivity().getApplicationContext().getResources().getInteger(R.integer.dashboard_height))));
244 AlertDialog dialog = builder.create();
247 new Handler().post(() -> {
248 InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
249 imm.showSoftInput(editText2, InputMethodManager.SHOW_IMPLICIT);
253 case "navigation_bar_buttons":
254 Intent intent = null;
256 switch(pref.getString("theme", "light")) {
258 intent = new Intent(getActivity(), NavigationBarButtonsActivity.class);
261 intent = new Intent(getActivity(), NavigationBarButtonsActivityDark.class);
265 startActivity(intent);
268 PackageManager packageManager = getActivity().getPackageManager();
269 String packageName = U.getSecondScreenPackageName(getActivity());
272 if(packageName == null) {
273 intent2 = new Intent(Intent.ACTION_VIEW);
274 intent2.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.farmerbb.secondscreen.free"));
276 intent2 = packageManager.getLaunchIntentForPackage(packageName);
278 if(intent2 != null) {
279 intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
282 startActivity(intent2);
283 } catch (ActivityNotFoundException e) { /* Gracefully fail */ }
292 private void updateDashboardGridSize(boolean restartTaskbar) {
293 SharedPreferences pref = U.getSharedPreferences(getActivity());
294 int width = pref.getInt("dashboard_width", getActivity().getApplicationContext().getResources().getInteger(R.integer.dashboard_width));
295 int height = pref.getInt("dashboard_height", getActivity().getApplicationContext().getResources().getInteger(R.integer.dashboard_height));
297 boolean isPortrait = getActivity().getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
298 boolean isLandscape = getActivity().getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
313 findPreference("dashboard_grid_size").setSummary(getString(R.string.dashboard_grid_description, first, second));
315 if(restartTaskbar) U.restartTaskbar(getActivity());