OSDN Git Service

Abstract logic for setting components enabled / disabled
[android-x86/packages-apps-Taskbar.git] / app / src / main / java / com / farmerbb / taskbar / fragment / SettingsFragment.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.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;
30
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;
38
39 public abstract class SettingsFragment extends PreferenceFragment implements Preference.OnPreferenceClickListener {
40
41     boolean finishedLoadingPrefs;
42     boolean showReminderToast = false;
43     boolean restartNotificationService = false;
44
45     @SuppressWarnings("deprecation")
46     @Override
47     public void onCreate(Bundle savedInstanceState) {
48         super.onCreate(savedInstanceState);
49
50         if(U.isLibrary(getActivity()))
51             getPreferenceManager().setSharedPreferencesName(BuildConfig.APPLICATION_ID + "_preferences");
52
53         // Set values
54         setRetainInstance(true);
55         setHasOptionsMenu(true);
56     }
57
58     @Override
59     public void onStart() {
60         super.onStart();
61
62         ((MainActivity) getActivity()).updateHelpButton(this);
63     }
64
65     @Override
66     public void onActivityCreated(Bundle savedInstanceState) {
67         super.onActivityCreated(savedInstanceState);
68
69         // Remove dividers
70         View rootView = getView();
71         if(rootView != null) {
72             ListView list = rootView.findViewById(android.R.id.list);
73             if(list != null) list.setDivider(null);
74         }
75     }
76
77     private Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
78         @Override
79         public boolean onPreferenceChange(Preference preference, Object value) {
80             String stringValue = value.toString();
81
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);
87
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);
94             }
95
96             if(finishedLoadingPrefs) {
97                 switch(preference.getKey()) {
98                     case "theme":
99                         if(U.isLibrary(getActivity())) break;
100
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);
107                         break;
108                     case "chrome_os_context_menu_fix":
109                         FreeformHackHelper helper = FreeformHackHelper.getInstance();
110                         helper.setFreeformHackActive(false);
111                         helper.setInFreeformWorkspace(false);
112
113                         U.sendBroadcast(getActivity(), TaskbarIntent.ACTION_FINISH_FREEFORM_ACTIVITY);
114
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()));
118                         break;
119                     case "start_button_image":
120                         if(stringValue.equals("custom"))
121                             ((AppearanceFragment) SettingsFragment.this).showFileChooser();
122                         break;
123                 }
124
125                 U.restartTaskbar(getActivity());
126             }
127
128             return true;
129         }
130     };
131
132     void bindPreferenceSummaryToValue(Preference preference) {
133         // Set the listener to watch for value changes.
134         preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
135
136         // Trigger the listener immediately with the preference's
137         // current value.
138         if(!(preference instanceof CheckBoxPreference))
139             sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
140                     U.getSharedPreferences(preference.getContext()).getString(preference.getKey(), ""));
141     }
142
143     @Override
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();
149                 return true;
150             default:
151                 return super.onOptionsItemSelected(item);
152         }
153     }
154
155     @Override
156     public void onResume() {
157         super.onResume();
158
159         if(restartNotificationService) {
160             restartNotificationService = false;
161
162             U.restartNotificationService(getActivity());
163         }
164     }
165
166     @SuppressLint("SetTextI18n")
167     @Override
168     public boolean onPreferenceClick(final Preference p) {
169         if(p.getKey().equals("clear_pinned_apps")) {
170             Intent clearIntent = null;
171
172             SharedPreferences pref = U.getSharedPreferences(getActivity());
173             switch(pref.getString("theme", "light")) {
174                 case "light":
175                     clearIntent = new Intent(getActivity(), ClearDataActivity.class);
176                     break;
177                 case "dark":
178                     clearIntent = new Intent(getActivity(), ClearDataActivityDark.class);
179                     break;
180             }
181
182             startActivity(clearIntent);
183         }
184
185         return true;
186     }
187 }