OSDN Git Service

Moved Added User-Visible Strings from Code to Strings XML
[android-x86/packages-apps-Taskbar.git] / app / src / main / java / com / farmerbb / taskbar / fragment / AppearanceFragment.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.app.Activity;
20 import android.content.ActivityNotFoundException;
21 import android.content.Intent;
22 import android.content.SharedPreferences;
23 import android.content.pm.PackageManager;
24 import android.graphics.Color;
25 import android.net.Uri;
26 import android.os.Bundle;
27 import android.preference.CheckBoxPreference;
28 import android.preference.Preference;
29 import android.preference.PreferenceManager;
30 import android.support.v7.app.ActionBar;
31 import android.support.v7.app.AlertDialog;
32 import android.support.v7.app.AppCompatActivity;
33 import android.view.View;
34 import android.widget.ScrollView;
35 import android.widget.SeekBar;
36 import android.widget.TextView;
37
38 import com.farmerbb.taskbar.BuildConfig;
39 import com.farmerbb.taskbar.R;
40 import com.farmerbb.taskbar.activity.IconPackActivity;
41 import com.farmerbb.taskbar.activity.dark.IconPackActivityDark;
42 import com.farmerbb.taskbar.util.U;
43
44 public class AppearanceFragment extends SettingsFragment implements Preference.OnPreferenceClickListener {
45     private int alpha, red, green, blue;
46
47     private enum ColorPickerType { BACKGROUND_TINT, ACCENT_COLOR }
48
49     @Override
50     public void onCreate(Bundle savedInstanceState) {
51         finishedLoadingPrefs = false;
52
53         super.onCreate(savedInstanceState);
54
55         // Add preferences
56         addPreferencesFromResource(R.xml.pref_appearance);
57
58         // Set OnClickListeners for certain preferences
59         findPreference("icon_pack_list").setOnPreferenceClickListener(this);
60         findPreference("reset_colors").setOnPreferenceClickListener(this);
61         findPreference("background_tint_pref").setOnPreferenceClickListener(this);
62         findPreference("accent_color_pref").setOnPreferenceClickListener(this);
63         findPreference("app_drawer_icon_image").setOnPreferenceClickListener(this);
64
65         bindPreferenceSummaryToValue(findPreference("theme"));
66         bindPreferenceSummaryToValue(findPreference("invisible_button"));
67         bindPreferenceSummaryToValue(findPreference("app_drawer_icon"));
68         bindPreferenceSummaryToValue(findPreference("app_drawer_icon_custom"));
69         bindPreferenceSummaryToValue(findPreference("app_drawer_icon_image"));
70         bindPreferenceSummaryToValue(findPreference("icon_pack_use_mask"));
71         bindPreferenceSummaryToValue(findPreference("visual_feedback"));
72         bindPreferenceSummaryToValue(findPreference("shortcut_icon"));
73         bindPreferenceSummaryToValue(findPreference("transparent_start_menu"));
74
75         findPreference("background_tint_pref").setSummary("#" + String.format("%08x", U.getBackgroundTint(getActivity())).toUpperCase());
76         findPreference("accent_color_pref").setSummary("#" + String.format("%08x", U.getAccentColor(getActivity())).toUpperCase());
77
78         if(U.isBlissOs(getActivity()))
79             findPreference("app_drawer_icon").setTitle(R.string.pref_title_app_drawer_icon_bliss);
80
81         finishedLoadingPrefs = true;
82     }
83
84     @Override
85     public void onActivityCreated(Bundle savedInstanceState) {
86         super.onActivityCreated(savedInstanceState);
87
88         AppCompatActivity activity = (AppCompatActivity) getActivity();
89         activity.setTitle(R.string.pref_header_appearance);
90         ActionBar actionBar = activity.getSupportActionBar();
91         if(actionBar != null)
92             actionBar.setDisplayHomeAsUpEnabled(true);
93     }
94
95     @Override
96     public void onResume() {
97         super.onResume();
98
99         Preference iconPackListPref = findPreference("icon_pack_list");
100         if(iconPackListPref != null) {
101             SharedPreferences pref = U.getSharedPreferences(getActivity());
102             String iconPackPackage = pref.getString("icon_pack", BuildConfig.APPLICATION_ID);
103             PackageManager pm = getActivity().getPackageManager();
104
105             boolean iconPackValid = true;
106             try {
107                 pm.getPackageInfo(iconPackPackage, 0);
108             } catch (PackageManager.NameNotFoundException e) {
109                 iconPackValid = false;
110             }
111
112             if(!iconPackValid || iconPackPackage.equals(BuildConfig.APPLICATION_ID)) {
113                 iconPackListPref.setSummary(getString(R.string.icon_pack_none));
114             } else {
115                 try {
116                     iconPackListPref.setSummary(pm.getApplicationLabel(pm.getApplicationInfo(iconPackPackage, 0)));
117                 } catch (PackageManager.NameNotFoundException e) { /* Gracefully fail */ }
118             }
119         }
120     }
121
122     @Override
123     public boolean onPreferenceClick(final Preference p) {
124         final SharedPreferences pref = U.getSharedPreferences(getActivity());
125
126         switch(p.getKey()) {
127             case "icon_pack_list":
128                 Intent intent = null;
129
130                 switch(pref.getString("theme", "light")) {
131                     case "light":
132                         intent = new Intent(getActivity(), IconPackActivity.class);
133                         break;
134                     case "dark":
135                         intent = new Intent(getActivity(), IconPackActivityDark.class);
136                         break;
137                 }
138
139                 startActivityForResult(intent, 123);
140                 break;
141             case "reset_colors":
142                 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
143                 builder.setTitle(R.string.reset_colors)
144                         .setMessage(R.string.are_you_sure)
145                         .setNegativeButton(R.string.action_cancel, null)
146                         .setPositiveButton(R.string.action_ok, (dialog, which) -> {
147                             finishedLoadingPrefs = false;
148
149                             pref.edit().remove("background_tint").remove("accent_color").apply();
150
151                             findPreference("background_tint_pref").setSummary("#" + String.format("%08x", U.getBackgroundTint(getActivity())).toUpperCase());
152                             findPreference("accent_color_pref").setSummary("#" + String.format("%08x", U.getAccentColor(getActivity())).toUpperCase());
153
154                             finishedLoadingPrefs = true;
155                             U.restartTaskbar(getActivity());
156                         });
157
158                 AlertDialog dialog = builder.create();
159                 dialog.show();
160                 break;
161             case "background_tint_pref":
162                 showColorPicker(ColorPickerType.BACKGROUND_TINT);
163                 break;
164             case "accent_color_pref":
165                 showColorPicker(ColorPickerType.ACCENT_COLOR);
166                 break;
167             case "app_drawer_icon_image":
168                 showFileChooser();
169                 break;
170         }
171
172         return true;
173     }
174
175     private void showFileChooser() {
176         Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
177         intent.setType("image/*");
178         intent.addCategory(Intent.CATEGORY_OPENABLE);
179
180         try {
181             startActivityForResult(Intent.createChooser(intent, getResources().getString(R.string.filepicker_select_an_image_file)), 1001);
182         } catch (ActivityNotFoundException ex) {
183             // Potentially direct the user to the Market with a Dialog
184             U.showToast(getActivity(), getResources().getString(R.string.filepicker_install_file_manager), 50);
185         }
186     }
187
188     @Override
189     public void onActivityResult(int requestCode, int resultCode, Intent data) {
190         if(requestCode == 123 && resultCode == Activity.RESULT_OK) {
191             U.refreshPinnedIcons(getActivity());
192             U.restartTaskbar(getActivity());
193         }
194
195         if (requestCode == 1001 && resultCode == Activity.RESULT_OK) {
196             Uri currFileURI = data.getData();
197
198             if (currFileURI == null) {
199                 return;
200             }
201
202             final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
203             Boolean customStartImageBoolValue = prefs.getBoolean("app_drawer_icon_custom", true);
204
205             if(customStartImageBoolValue == false) {
206                 prefs.edit().putBoolean("app_drawer_icon_custom", true).commit();
207                 ((CheckBoxPreference) findPreference("app_drawer_icon_custom")).setChecked(true);
208             }
209
210             prefs.edit().putString("app_drawer_icon_image", currFileURI.toString()).commit();
211             bindPreferenceSummaryToValue(findPreference("app_drawer_icon_image"));
212             U.restartTaskbar(getActivity());
213         }
214     }
215
216     @SuppressLint("SetTextI18n")
217     private void showColorPicker(ColorPickerType type) {
218         int color = -1;
219         int dialogTitle = -1;
220
221         switch(type) {
222             case BACKGROUND_TINT:
223                 color = U.getBackgroundTint(getActivity());
224                 dialogTitle = R.string.pref_title_background_tint;
225                 break;
226             case ACCENT_COLOR:
227                 color = U.getAccentColor(getActivity());
228                 dialogTitle = R.string.pref_title_accent_color;
229                 break;
230         }
231
232         alpha = Color.alpha(color);
233         red = Color.red(color);
234         green = Color.green(color);
235         blue = Color.blue(color);
236
237         ScrollView dialogLayout = (ScrollView) View.inflate(getActivity(), R.layout.color_picker_pref, null);
238
239         View colorPreview = dialogLayout.findViewById(R.id.color_preview);
240         colorPreview.setBackgroundColor(Color.argb(alpha, red, green, blue));
241
242         TextView hexPreview = dialogLayout.findViewById(R.id.hex_preview);
243         hexPreview.setText("#" + String.format("%08x", Color.argb(alpha, red, green, blue)).toUpperCase());
244
245         final TextView alphaValue = dialogLayout.findViewById(R.id.alpha_value);
246         alphaValue.setText("0");
247
248         final SeekBar alphaSeekBar = dialogLayout.findViewById(R.id.alpha_seekbar);
249         alphaSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
250             @Override
251             public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
252                 alpha = progress;
253
254                 alphaValue.setText(Integer.toString(alpha));
255                 colorPreview.setBackgroundColor(Color.argb(alpha, red, green, blue));
256                 hexPreview.setText("#" + String.format("%08x", Color.argb(alpha, red, green, blue)).toUpperCase());
257             }
258
259             @Override
260             public void onStartTrackingTouch(SeekBar seekBar) {}
261
262             @Override
263             public void onStopTrackingTouch(SeekBar seekBar) {}
264         });
265
266         alphaSeekBar.setProgress(Color.alpha(color));
267
268         final TextView redValue = dialogLayout.findViewById(R.id.red_value);
269         redValue.setText("0");
270
271         final SeekBar redSeekBar = dialogLayout.findViewById(R.id.red_seekbar);
272         redSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
273             @Override
274             public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
275                 red = progress;
276
277                 redValue.setText(Integer.toString(red));
278                 colorPreview.setBackgroundColor(Color.argb(alpha, red, green, blue));
279                 hexPreview.setText("#" + String.format("%08x", Color.argb(alpha, red, green, blue)).toUpperCase());
280             }
281
282             @Override
283             public void onStartTrackingTouch(SeekBar seekBar) {}
284
285             @Override
286             public void onStopTrackingTouch(SeekBar seekBar) {}
287         });
288
289         redSeekBar.setProgress(Color.red(color));
290
291         final TextView greenValue = dialogLayout.findViewById(R.id.green_value);
292         greenValue.setText("0");
293
294         final SeekBar greenSeekBar = dialogLayout.findViewById(R.id.green_seekbar);
295         greenSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
296             @Override
297             public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
298                 green = progress;
299
300                 greenValue.setText(Integer.toString(green));
301                 colorPreview.setBackgroundColor(Color.argb(alpha, red, green, blue));
302                 hexPreview.setText("#" + String.format("%08x", Color.argb(alpha, red, green, blue)).toUpperCase());
303             }
304
305             @Override
306             public void onStartTrackingTouch(SeekBar seekBar) {}
307
308             @Override
309             public void onStopTrackingTouch(SeekBar seekBar) {}
310         });
311
312         greenSeekBar.setProgress(Color.green(color));
313
314         final TextView blueValue = dialogLayout.findViewById(R.id.blue_value);
315         blueValue.setText("0");
316
317         final SeekBar blueSeekBar = dialogLayout.findViewById(R.id.blue_seekbar);
318         blueSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
319             @Override
320             public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
321                 blue = progress;
322
323                 blueValue.setText(Integer.toString(blue));
324                 colorPreview.setBackgroundColor(Color.argb(alpha, red, green, blue));
325                 hexPreview.setText("#" + String.format("%08x", Color.argb(alpha, red, green, blue)).toUpperCase());
326             }
327
328             @Override
329             public void onStartTrackingTouch(SeekBar seekBar) {}
330
331             @Override
332             public void onStopTrackingTouch(SeekBar seekBar) {}
333         });
334
335         blueSeekBar.setProgress(Color.blue(color));
336
337         AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
338         builder.setView(dialogLayout)
339                 .setTitle(dialogTitle)
340                 .setPositiveButton(R.string.action_ok, (dialog, which) -> {
341                     String preferenceId = null;
342                     switch(type) {
343                         case BACKGROUND_TINT:
344                             preferenceId = "background_tint";
345                             break;
346                         case ACCENT_COLOR:
347                             preferenceId = "accent_color";
348                             break;
349                     }
350
351                     SharedPreferences pref = U.getSharedPreferences(getActivity());
352                     pref.edit().putInt(preferenceId, Color.argb(alpha, red, green, blue)).apply();
353
354                     findPreference(preferenceId + "_pref").setSummary("#" + String.format("%08x", Color.argb(alpha, red, green, blue)).toUpperCase());
355
356                     U.restartTaskbar(getActivity());
357                 })
358                 .setNegativeButton(R.string.action_cancel, null);
359
360         AlertDialog dialog = builder.create();
361         dialog.show();
362     }
363 }