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.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;
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;
44 public class AppearanceFragment extends SettingsFragment implements Preference.OnPreferenceClickListener {
45 private int alpha, red, green, blue;
47 private enum ColorPickerType { BACKGROUND_TINT, ACCENT_COLOR }
50 public void onCreate(Bundle savedInstanceState) {
51 finishedLoadingPrefs = false;
53 super.onCreate(savedInstanceState);
56 addPreferencesFromResource(R.xml.pref_appearance);
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);
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"));
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());
78 if(U.isBlissOs(getActivity()))
79 findPreference("app_drawer_icon").setTitle(R.string.pref_title_app_drawer_icon_bliss);
81 finishedLoadingPrefs = true;
85 public void onActivityCreated(Bundle savedInstanceState) {
86 super.onActivityCreated(savedInstanceState);
88 AppCompatActivity activity = (AppCompatActivity) getActivity();
89 activity.setTitle(R.string.pref_header_appearance);
90 ActionBar actionBar = activity.getSupportActionBar();
92 actionBar.setDisplayHomeAsUpEnabled(true);
96 public void onResume() {
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();
105 boolean iconPackValid = true;
107 pm.getPackageInfo(iconPackPackage, 0);
108 } catch (PackageManager.NameNotFoundException e) {
109 iconPackValid = false;
112 if(!iconPackValid || iconPackPackage.equals(BuildConfig.APPLICATION_ID)) {
113 iconPackListPref.setSummary(getString(R.string.icon_pack_none));
116 iconPackListPref.setSummary(pm.getApplicationLabel(pm.getApplicationInfo(iconPackPackage, 0)));
117 } catch (PackageManager.NameNotFoundException e) { /* Gracefully fail */ }
123 public boolean onPreferenceClick(final Preference p) {
124 final SharedPreferences pref = U.getSharedPreferences(getActivity());
127 case "icon_pack_list":
128 Intent intent = null;
130 switch(pref.getString("theme", "light")) {
132 intent = new Intent(getActivity(), IconPackActivity.class);
135 intent = new Intent(getActivity(), IconPackActivityDark.class);
139 startActivityForResult(intent, 123);
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;
149 pref.edit().remove("background_tint").remove("accent_color").apply();
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());
154 finishedLoadingPrefs = true;
155 U.restartTaskbar(getActivity());
158 AlertDialog dialog = builder.create();
161 case "background_tint_pref":
162 showColorPicker(ColorPickerType.BACKGROUND_TINT);
164 case "accent_color_pref":
165 showColorPicker(ColorPickerType.ACCENT_COLOR);
167 case "app_drawer_icon_image":
175 private void showFileChooser() {
176 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
177 intent.setType("image/*");
178 intent.addCategory(Intent.CATEGORY_OPENABLE);
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);
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());
195 if (requestCode == 1001 && resultCode == Activity.RESULT_OK) {
196 Uri currFileURI = data.getData();
198 if (currFileURI == null) {
202 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
203 Boolean customStartImageBoolValue = prefs.getBoolean("app_drawer_icon_custom", true);
205 if(customStartImageBoolValue == false) {
206 prefs.edit().putBoolean("app_drawer_icon_custom", true).commit();
207 ((CheckBoxPreference) findPreference("app_drawer_icon_custom")).setChecked(true);
210 prefs.edit().putString("app_drawer_icon_image", currFileURI.toString()).commit();
211 bindPreferenceSummaryToValue(findPreference("app_drawer_icon_image"));
212 U.restartTaskbar(getActivity());
216 @SuppressLint("SetTextI18n")
217 private void showColorPicker(ColorPickerType type) {
219 int dialogTitle = -1;
222 case BACKGROUND_TINT:
223 color = U.getBackgroundTint(getActivity());
224 dialogTitle = R.string.pref_title_background_tint;
227 color = U.getAccentColor(getActivity());
228 dialogTitle = R.string.pref_title_accent_color;
232 alpha = Color.alpha(color);
233 red = Color.red(color);
234 green = Color.green(color);
235 blue = Color.blue(color);
237 ScrollView dialogLayout = (ScrollView) View.inflate(getActivity(), R.layout.color_picker_pref, null);
239 View colorPreview = dialogLayout.findViewById(R.id.color_preview);
240 colorPreview.setBackgroundColor(Color.argb(alpha, red, green, blue));
242 TextView hexPreview = dialogLayout.findViewById(R.id.hex_preview);
243 hexPreview.setText("#" + String.format("%08x", Color.argb(alpha, red, green, blue)).toUpperCase());
245 final TextView alphaValue = dialogLayout.findViewById(R.id.alpha_value);
246 alphaValue.setText("0");
248 final SeekBar alphaSeekBar = dialogLayout.findViewById(R.id.alpha_seekbar);
249 alphaSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
251 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
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());
260 public void onStartTrackingTouch(SeekBar seekBar) {}
263 public void onStopTrackingTouch(SeekBar seekBar) {}
266 alphaSeekBar.setProgress(Color.alpha(color));
268 final TextView redValue = dialogLayout.findViewById(R.id.red_value);
269 redValue.setText("0");
271 final SeekBar redSeekBar = dialogLayout.findViewById(R.id.red_seekbar);
272 redSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
274 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
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());
283 public void onStartTrackingTouch(SeekBar seekBar) {}
286 public void onStopTrackingTouch(SeekBar seekBar) {}
289 redSeekBar.setProgress(Color.red(color));
291 final TextView greenValue = dialogLayout.findViewById(R.id.green_value);
292 greenValue.setText("0");
294 final SeekBar greenSeekBar = dialogLayout.findViewById(R.id.green_seekbar);
295 greenSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
297 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
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());
306 public void onStartTrackingTouch(SeekBar seekBar) {}
309 public void onStopTrackingTouch(SeekBar seekBar) {}
312 greenSeekBar.setProgress(Color.green(color));
314 final TextView blueValue = dialogLayout.findViewById(R.id.blue_value);
315 blueValue.setText("0");
317 final SeekBar blueSeekBar = dialogLayout.findViewById(R.id.blue_seekbar);
318 blueSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
320 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
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());
329 public void onStartTrackingTouch(SeekBar seekBar) {}
332 public void onStopTrackingTouch(SeekBar seekBar) {}
335 blueSeekBar.setProgress(Color.blue(color));
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;
343 case BACKGROUND_TINT:
344 preferenceId = "background_tint";
347 preferenceId = "accent_color";
351 SharedPreferences pref = U.getSharedPreferences(getActivity());
352 pref.edit().putInt(preferenceId, Color.argb(alpha, red, green, blue)).apply();
354 findPreference(preferenceId + "_pref").setSummary("#" + String.format("%08x", Color.argb(alpha, red, green, blue)).toUpperCase());
356 U.restartTaskbar(getActivity());
358 .setNegativeButton(R.string.action_cancel, null);
360 AlertDialog dialog = builder.create();