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.Intent;
20 import android.content.SharedPreferences;
21 import android.os.Bundle;
22 import android.os.Handler;
23 import android.preference.CheckBoxPreference;
24 import android.preference.ListPreference;
25 import android.preference.Preference;
26 import android.preference.PreferenceFragment;
27 import android.view.MenuItem;
28 import android.view.View;
29 import android.widget.ListView;
31 import com.farmerbb.taskbar.BuildConfig;
32 import com.farmerbb.taskbar.activity.ClearDataActivity;
33 import com.farmerbb.taskbar.activity.MainActivity;
34 import com.farmerbb.taskbar.activity.dark.ClearDataActivityDark;
35 import com.farmerbb.taskbar.content.TaskbarIntent;
36 import com.farmerbb.taskbar.util.FreeformHackHelper;
37 import com.farmerbb.taskbar.util.U;
39 public abstract class SettingsFragment extends PreferenceFragment implements Preference.OnPreferenceClickListener {
41 boolean finishedLoadingPrefs;
42 boolean showReminderToast = false;
43 boolean restartNotificationService = false;
45 @SuppressWarnings("deprecation")
47 public void onCreate(Bundle savedInstanceState) {
48 super.onCreate(savedInstanceState);
50 if(U.isLibrary(getActivity()))
51 getPreferenceManager().setSharedPreferencesName(BuildConfig.APPLICATION_ID + "_preferences");
54 setRetainInstance(true);
55 setHasOptionsMenu(true);
59 public void onStart() {
62 ((MainActivity) getActivity()).updateHelpButton(this);
66 public void onActivityCreated(Bundle savedInstanceState) {
67 super.onActivityCreated(savedInstanceState);
70 View rootView = getView();
71 if(rootView != null) {
72 ListView list = rootView.findViewById(android.R.id.list);
73 if(list != null) list.setDivider(null);
77 private Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
79 public boolean onPreferenceChange(Preference preference, Object value) {
80 String stringValue = value.toString();
82 if(preference instanceof ListPreference) {
83 // For list preferences, look up the correct display value in
84 // the preference's 'entries' list.
85 ListPreference listPreference = (ListPreference) preference;
86 int index = listPreference.findIndexOfValue(stringValue);
88 // Set the summary to reflect the new value.
89 preference.setSummary(index >= 0 ? listPreference.getEntries()[index] : null);
90 } else if(!(preference instanceof CheckBoxPreference)) {
91 // For all other preferences, set the summary to the value's
92 // simple string representation.
93 preference.setSummary(stringValue);
96 if(finishedLoadingPrefs) {
97 switch(preference.getKey()) {
99 if(U.isLibrary(getActivity())) break;
101 // Restart MainActivity
102 Intent intent = new Intent(getActivity(), MainActivity.class);
103 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
104 intent.putExtra("theme_change", true);
105 startActivity(intent);
106 getActivity().overridePendingTransition(0, 0);
108 case "chrome_os_context_menu_fix":
109 FreeformHackHelper helper = FreeformHackHelper.getInstance();
110 helper.setFreeformHackActive(false);
111 helper.setInFreeformWorkspace(false);
113 U.sendBroadcast(getActivity(), TaskbarIntent.ACTION_FINISH_FREEFORM_ACTIVITY);
115 SharedPreferences pref = U.getSharedPreferences(getActivity());
116 if(pref.getBoolean("taskbar_active", false) && !pref.getBoolean("is_hidden", false))
117 new Handler().post(() -> U.startFreeformHack(getActivity()));
119 case "start_button_image":
120 if(stringValue.equals("custom"))
121 ((AppearanceFragment) SettingsFragment.this).showFileChooser();
125 U.restartTaskbar(getActivity());
132 void bindPreferenceSummaryToValue(Preference preference) {
133 // Set the listener to watch for value changes.
134 preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
136 // Trigger the listener immediately with the preference's
138 if(!(preference instanceof CheckBoxPreference))
139 sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
140 U.getSharedPreferences(preference.getContext()).getString(preference.getKey(), ""));
144 public boolean onOptionsItemSelected(MenuItem item) {
145 switch (item.getItemId()) {
146 case android.R.id.home:
147 // Override default Android "up" behavior to instead mimic the back button
148 getActivity().onBackPressed();
151 return super.onOptionsItemSelected(item);
156 public void onResume() {
159 if(restartNotificationService) {
160 restartNotificationService = false;
162 U.restartNotificationService(getActivity());
166 @SuppressLint("SetTextI18n")
168 public boolean onPreferenceClick(final Preference p) {
169 if(p.getKey().equals("clear_pinned_apps")) {
170 Intent clearIntent = null;
172 SharedPreferences pref = U.getSharedPreferences(getActivity());
173 switch(pref.getString("theme", "light")) {
175 clearIntent = new Intent(getActivity(), ClearDataActivity.class);
178 clearIntent = new Intent(getActivity(), ClearDataActivityDark.class);
182 startActivity(clearIntent);