OSDN Git Service

Implement custom color picker dialogs
authorBraden Farmer <farmerbb@gmail.com>
Fri, 16 Jun 2017 04:45:47 +0000 (22:45 -0600)
committerBraden Farmer <farmerbb@gmail.com>
Fri, 16 Jun 2017 04:45:47 +0000 (22:45 -0600)
app/build.gradle
app/src/main/java/com/farmerbb/taskbar/MainActivity.java
app/src/main/java/com/farmerbb/taskbar/fragment/AppearanceFragment.java
app/src/main/java/com/farmerbb/taskbar/util/U.java
app/src/main/res/layout/color_picker_pref.xml [new file with mode: 0644]
app/src/main/res/values/colors.xml
app/src/main/res/values/strings.xml
app/src/main/res/xml/pref_appearance.xml

index f904b07..d21b935 100644 (file)
@@ -1,5 +1,5 @@
 ext {
-    SUPPORT_LIBRARY_VERSION = "25.3.1"
+    SUPPORT_LIBRARY_VERSION = "25.4.0"
 }
 
 buildscript {
@@ -90,8 +90,7 @@ dependencies {
     compile fileTree(dir: 'libs', include: ['*.jar'])
     compile "com.android.support:appcompat-v7:${SUPPORT_LIBRARY_VERSION}"
     compile "com.android.support:design:${SUPPORT_LIBRARY_VERSION}"
-    compile 'com.github.enricocid:cpl:1.0.2'
-    compile 'com.mikepenz:iconics-core:2.8.3@aar'
+    compile 'com.mikepenz:iconics-core:2.8.6@aar'
     compile 'com.mikepenz:foundation-icons-typeface:3.0.0.2@aar'
     compile 'moe.banana:toast-compat:1.0.5'
 }
index 1a53aa4..8280cdd 100644 (file)
@@ -35,13 +35,11 @@ import android.net.Uri;
 import android.os.Build;
 import android.os.Bundle;
 import android.provider.Settings;
-import android.support.v4.app.DialogFragment;
 import android.support.v4.content.LocalBroadcastManager;
 import android.support.v7.app.ActionBar;
 import android.support.v7.app.AppCompatActivity;
 import android.support.v7.widget.SwitchCompat;
 
-import com.enrico.colorpicker.colorDialog;
 import com.farmerbb.taskbar.activity.HomeActivity;
 import com.farmerbb.taskbar.activity.ImportSettingsActivity;
 import com.farmerbb.taskbar.activity.KeyboardShortcutActivity;
@@ -49,7 +47,6 @@ import com.farmerbb.taskbar.activity.ShortcutActivity;
 import com.farmerbb.taskbar.activity.StartTaskbarActivity;
 import com.farmerbb.taskbar.fragment.AboutFragment;
 import com.farmerbb.taskbar.fragment.AppearanceFragment;
-import com.farmerbb.taskbar.fragment.SettingsFragment;
 import com.farmerbb.taskbar.service.DashboardService;
 import com.farmerbb.taskbar.service.NotificationService;
 import com.farmerbb.taskbar.service.StartMenuService;
@@ -62,7 +59,7 @@ import com.farmerbb.taskbar.util.U;
 import java.io.File;
 import java.util.Arrays;
 
-public class MainActivity extends AppCompatActivity implements colorDialog.ColorSelectedListener {
+public class MainActivity extends AppCompatActivity {
 
     private SwitchCompat theSwitch;
 
@@ -73,9 +70,6 @@ public class MainActivity extends AppCompatActivity implements colorDialog.Color
         }
     };
 
-    public final int BACKGROUND_TINT = 1;
-    public final int ACCENT_COLOR = 2;
-
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -361,26 +355,4 @@ public class MainActivity extends AppCompatActivity implements colorDialog.Color
                     .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE)
                     .commit();
     }
-
-    @Override
-    public void onColorSelection(DialogFragment dialogFragment, int color) {
-        SharedPreferences pref = U.getSharedPreferences(this);
-        String preferenceId = null;
-
-        switch(Integer.parseInt(dialogFragment.getTag())) {
-            case BACKGROUND_TINT:
-                preferenceId = "background_tint";
-                break;
-            case ACCENT_COLOR:
-                preferenceId = "accent_color";
-                break;
-        }
-
-        pref.edit().putInt(preferenceId, color).apply();
-
-        SettingsFragment fragment = (SettingsFragment) getFragmentManager().findFragmentById(R.id.fragmentContainer);
-        colorDialog.setColorPreferenceSummary(fragment.findPreference(preferenceId + "_pref"), color, this, getResources());
-
-        U.restartTaskbar(this);
-    }
 }
\ No newline at end of file
index 87c6ae8..e8e724c 100644 (file)
 
 package com.farmerbb.taskbar.fragment;
 
+import android.annotation.SuppressLint;
 import android.app.Activity;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.content.pm.PackageManager;
+import android.graphics.Color;
 import android.os.Bundle;
 import android.preference.Preference;
 import android.support.v7.app.ActionBar;
 import android.support.v7.app.AlertDialog;
 import android.support.v7.app.AppCompatActivity;
+import android.view.View;
+import android.widget.ScrollView;
+import android.widget.SeekBar;
+import android.widget.TextView;
 
-import com.enrico.colorpicker.colorDialog;
 import com.farmerbb.taskbar.BuildConfig;
-import com.farmerbb.taskbar.MainActivity;
 import com.farmerbb.taskbar.R;
 import com.farmerbb.taskbar.activity.IconPackActivity;
 import com.farmerbb.taskbar.activity.dark.IconPackActivityDark;
 import com.farmerbb.taskbar.util.U;
 
 public class AppearanceFragment extends SettingsFragment implements Preference.OnPreferenceClickListener {
+    private int alpha, red, green, blue;
+
+    private enum ColorPickerType { BACKGROUND_TINT, ACCENT_COLOR }
 
     @Override
     public void onActivityCreated(Bundle savedInstanceState) {
@@ -59,8 +66,8 @@ public class AppearanceFragment extends SettingsFragment implements Preference.O
             bindPreferenceSummaryToValue(findPreference("shortcut_icon"));
             bindPreferenceSummaryToValue(findPreference("transparent_start_menu"));
 
-            colorDialog.setColorPreferenceSummary(findPreference("background_tint_pref"), U.getBackgroundTint(getActivity()), getActivity(), getResources());
-            colorDialog.setColorPreferenceSummary(findPreference("accent_color_pref"), U.getAccentColor(getActivity()), getActivity(), getResources());
+            findPreference("background_tint_pref").setSummary("#" + String.format("%08x", U.getBackgroundTint(getActivity())).toUpperCase());
+            findPreference("accent_color_pref").setSummary("#" + String.format("%08x", U.getAccentColor(getActivity())).toUpperCase());
         }
 
         AppCompatActivity activity = (AppCompatActivity) getActivity();
@@ -128,8 +135,8 @@ public class AppearanceFragment extends SettingsFragment implements Preference.O
 
                             pref.edit().remove("background_tint").remove("accent_color").apply();
 
-                            colorDialog.setColorPreferenceSummary(findPreference("background_tint_pref"), U.getBackgroundTint(getActivity()), getActivity(), getResources());
-                            colorDialog.setColorPreferenceSummary(findPreference("accent_color_pref"), U.getAccentColor(getActivity()), getActivity(), getResources());
+                            findPreference("background_tint_pref").setSummary("#" + String.format("%08x", U.getBackgroundTint(getActivity())).toUpperCase());
+                            findPreference("accent_color_pref").setSummary("#" + String.format("%08x", U.getAccentColor(getActivity())).toUpperCase());
 
                             finishedLoadingPrefs = true;
                             U.restartTaskbar(getActivity());
@@ -139,20 +146,10 @@ public class AppearanceFragment extends SettingsFragment implements Preference.O
                 dialog.show();
                 break;
             case "background_tint_pref":
-                U.cancelToast();
-
-                MainActivity activity = (MainActivity) getActivity();
-
-                colorDialog.setPickerColor(activity, activity.BACKGROUND_TINT, U.getBackgroundTint(activity));
-                colorDialog.showColorPicker(activity, activity.BACKGROUND_TINT);
+                showColorPicker(ColorPickerType.BACKGROUND_TINT);
                 break;
             case "accent_color_pref":
-                U.cancelToast();
-
-                MainActivity activity2 = (MainActivity) getActivity();
-
-                colorDialog.setPickerColor(activity2, activity2.ACCENT_COLOR, U.getAccentColor(activity2));
-                colorDialog.showColorPicker(activity2, activity2.ACCENT_COLOR);
+                showColorPicker(ColorPickerType.ACCENT_COLOR);
                 break;
         }
 
@@ -166,4 +163,152 @@ public class AppearanceFragment extends SettingsFragment implements Preference.O
             U.restartTaskbar(getActivity());
         }
     }
+
+    @SuppressLint("SetTextI18n")
+    private void showColorPicker(ColorPickerType type) {
+        int color = -1;
+        int dialogTitle = -1;
+
+        switch(type) {
+            case BACKGROUND_TINT:
+                color = U.getBackgroundTint(getActivity());
+                dialogTitle = R.string.pref_title_background_tint;
+                break;
+            case ACCENT_COLOR:
+                color = U.getAccentColor(getActivity());
+                dialogTitle = R.string.pref_title_accent_color;
+                break;
+        }
+
+        alpha = Color.alpha(color);
+        red = Color.red(color);
+        green = Color.green(color);
+        blue = Color.blue(color);
+
+        ScrollView dialogLayout = (ScrollView) View.inflate(getActivity(), R.layout.color_picker_pref, null);
+
+        View colorPreview = dialogLayout.findViewById(R.id.color_preview);
+        colorPreview.setBackgroundColor(Color.argb(alpha, red, green, blue));
+
+        TextView hexPreview = (TextView) dialogLayout.findViewById(R.id.hex_preview);
+        hexPreview.setText("#" + String.format("%08x", Color.argb(alpha, red, green, blue)).toUpperCase());
+
+        final TextView alphaValue = (TextView) dialogLayout.findViewById(R.id.alpha_value);
+        alphaValue.setText("0");
+
+        final SeekBar alphaSeekBar = (SeekBar) dialogLayout.findViewById(R.id.alpha_seekbar);
+        alphaSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
+            @Override
+            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
+                alpha = progress;
+
+                alphaValue.setText(Integer.toString(alpha));
+                colorPreview.setBackgroundColor(Color.argb(alpha, red, green, blue));
+                hexPreview.setText("#" + String.format("%08x", Color.argb(alpha, red, green, blue)).toUpperCase());
+            }
+
+            @Override
+            public void onStartTrackingTouch(SeekBar seekBar) {}
+
+            @Override
+            public void onStopTrackingTouch(SeekBar seekBar) {}
+        });
+
+        alphaSeekBar.setProgress(Color.alpha(color));
+
+        final TextView redValue = (TextView) dialogLayout.findViewById(R.id.red_value);
+        redValue.setText("0");
+
+        final SeekBar redSeekBar = (SeekBar) dialogLayout.findViewById(R.id.red_seekbar);
+        redSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
+            @Override
+            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
+                red = progress;
+
+                redValue.setText(Integer.toString(red));
+                colorPreview.setBackgroundColor(Color.argb(alpha, red, green, blue));
+                hexPreview.setText("#" + String.format("%08x", Color.argb(alpha, red, green, blue)).toUpperCase());
+            }
+
+            @Override
+            public void onStartTrackingTouch(SeekBar seekBar) {}
+
+            @Override
+            public void onStopTrackingTouch(SeekBar seekBar) {}
+        });
+
+        redSeekBar.setProgress(Color.red(color));
+
+        final TextView greenValue = (TextView) dialogLayout.findViewById(R.id.green_value);
+        greenValue.setText("0");
+
+        final SeekBar greenSeekBar = (SeekBar) dialogLayout.findViewById(R.id.green_seekbar);
+        greenSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
+            @Override
+            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
+                green = progress;
+
+                greenValue.setText(Integer.toString(green));
+                colorPreview.setBackgroundColor(Color.argb(alpha, red, green, blue));
+                hexPreview.setText("#" + String.format("%08x", Color.argb(alpha, red, green, blue)).toUpperCase());
+            }
+
+            @Override
+            public void onStartTrackingTouch(SeekBar seekBar) {}
+
+            @Override
+            public void onStopTrackingTouch(SeekBar seekBar) {}
+        });
+
+        greenSeekBar.setProgress(Color.green(color));
+
+        final TextView blueValue = (TextView) dialogLayout.findViewById(R.id.blue_value);
+        blueValue.setText("0");
+
+        final SeekBar blueSeekBar = (SeekBar) dialogLayout.findViewById(R.id.blue_seekbar);
+        blueSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
+            @Override
+            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
+                blue = progress;
+
+                blueValue.setText(Integer.toString(blue));
+                colorPreview.setBackgroundColor(Color.argb(alpha, red, green, blue));
+                hexPreview.setText("#" + String.format("%08x", Color.argb(alpha, red, green, blue)).toUpperCase());
+            }
+
+            @Override
+            public void onStartTrackingTouch(SeekBar seekBar) {}
+
+            @Override
+            public void onStopTrackingTouch(SeekBar seekBar) {}
+        });
+
+        blueSeekBar.setProgress(Color.blue(color));
+
+        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
+        builder.setView(dialogLayout)
+                .setTitle(dialogTitle)
+                .setPositiveButton(R.string.action_ok, (dialog, which) -> {
+                    String preferenceId = null;
+                    switch(type) {
+                        case BACKGROUND_TINT:
+                            preferenceId = "background_tint";
+                            break;
+                        case ACCENT_COLOR:
+                            preferenceId = "accent_color";
+                            break;
+                    }
+
+                    SharedPreferences pref = U.getSharedPreferences(getActivity());
+                    pref.edit().putInt(preferenceId, Color.argb(alpha, red, green, blue)).apply();
+
+                    findPreference(preferenceId + "_pref").setSummary("#" + String.format("%08x", Color.argb(alpha, red, green, blue)).toUpperCase());
+
+                    U.restartTaskbar(getActivity());
+                })
+                .setNegativeButton(R.string.action_cancel, null);
+
+        AlertDialog dialog = builder.create();
+        dialog.show();
+    }
 }
index 2da2985..b8b8903 100644 (file)
@@ -34,6 +34,7 @@ import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
 import android.content.pm.ShortcutInfo;
 import android.content.res.Configuration;
+import android.graphics.Color;
 import android.graphics.Rect;
 import android.graphics.drawable.BitmapDrawable;
 import android.hardware.display.DisplayManager;
@@ -193,7 +194,7 @@ public class U {
         ToastHelper.getInstance().setLastToast(toast);
     }
 
-    public static void cancelToast() {
+    private static void cancelToast() {
         ToastCompat toast = ToastHelper.getInstance().getLastToast();
         if(toast != null) toast.cancel();
     }
@@ -839,7 +840,7 @@ public class U {
             SharedPreferences.Editor editor = pref.edit();
 
             if(!pref.getBoolean("show_background", true))
-                editor.putInt("background_tint", 0).apply();
+                editor.putInt("background_tint", Color.TRANSPARENT).apply();
 
             editor.remove("show_background");
             editor.apply();
diff --git a/app/src/main/res/layout/color_picker_pref.xml b/app/src/main/res/layout/color_picker_pref.xml
new file mode 100644 (file)
index 0000000..a5b79ac
--- /dev/null
@@ -0,0 +1,170 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright 2017 Braden Farmer
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
+              android:layout_width="match_parent"
+              android:layout_height="match_parent"
+              android:orientation="vertical"
+              android:padding="24dp" >
+
+    <LinearLayout
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:orientation="vertical" >
+
+    <View
+        android:id="@+id/color_preview"
+        android:layout_width="match_parent"
+        android:layout_height="96dp"
+        android:layout_marginBottom="16dp"/>
+
+    <LinearLayout
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:paddingBottom="16dp"
+        android:orientation="horizontal" >
+
+        <TextView
+            android:gravity="start"
+            android:layout_gravity="center_vertical"
+            android:layout_width="40dp"
+            android:layout_height="wrap_content"
+            android:text="@string/color_picker_alpha" />
+        
+        <SeekBar
+            android:id="@+id/alpha_seekbar"
+            android:layout_width="0dp"
+            android:layout_height="wrap_content"
+            android:layout_weight="1"
+            android:max="255"
+            android:thumbTint="@color/alpha"
+            android:progressTint="@color/alpha"
+            android:progressBackgroundTint="@color/alpha" />
+
+        <TextView
+            android:id="@+id/alpha_value"
+            android:gravity="end"
+            android:layout_gravity="center_vertical"
+            android:layout_width="24dp"
+            android:layout_height="wrap_content" />
+
+    </LinearLayout>
+
+    <LinearLayout
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:paddingBottom="16dp"
+        android:orientation="horizontal" >
+
+        <TextView
+            android:gravity="start"
+            android:layout_gravity="center_vertical"
+            android:layout_width="40dp"
+            android:layout_height="wrap_content"
+            android:text="@string/color_picker_red" />
+
+        <SeekBar
+            android:id="@+id/red_seekbar"
+            android:layout_width="0dp"
+            android:layout_height="wrap_content"
+            android:layout_weight="1"
+            android:max="255"
+            android:thumbTint="@color/red"
+            android:progressTint="@color/red"
+            android:progressBackgroundTint="@color/red" />
+
+        <TextView
+            android:id="@+id/red_value"
+            android:gravity="end"
+            android:layout_gravity="center_vertical"
+            android:layout_width="24dp"
+            android:layout_height="wrap_content" />
+
+    </LinearLayout>
+
+    <LinearLayout
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:paddingBottom="16dp"
+        android:orientation="horizontal" >
+
+        <TextView
+            android:gravity="start"
+            android:layout_gravity="center_vertical"
+            android:layout_width="40dp"
+            android:layout_height="wrap_content"
+            android:text="@string/color_picker_green" />
+
+        <SeekBar
+            android:id="@+id/green_seekbar"
+            android:layout_width="0dp"
+            android:layout_height="wrap_content"
+            android:layout_weight="1"
+            android:max="255"
+            android:thumbTint="@color/green"
+            android:progressTint="@color/green"
+            android:progressBackgroundTint="@color/green" />
+
+        <TextView
+            android:id="@+id/green_value"
+            android:gravity="end"
+            android:layout_gravity="center_vertical"
+            android:layout_width="24dp"
+            android:layout_height="wrap_content" />
+
+    </LinearLayout>
+
+    <LinearLayout
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:paddingBottom="16dp"
+        android:orientation="horizontal" >
+
+        <TextView
+            android:gravity="start"
+            android:layout_gravity="center_vertical"
+            android:layout_width="40dp"
+            android:layout_height="wrap_content"
+            android:text="@string/color_picker_blue" />
+
+        <SeekBar
+            android:id="@+id/blue_seekbar"
+            android:layout_width="0dp"
+            android:layout_height="wrap_content"
+            android:layout_weight="1"
+            android:max="255"
+            android:thumbTint="@color/blue"
+            android:progressTint="@color/blue"
+            android:progressBackgroundTint="@color/blue" />
+
+        <TextView
+            android:id="@+id/blue_value"
+            android:gravity="end"
+            android:layout_gravity="center_vertical"
+            android:layout_width="24dp"
+            android:layout_height="wrap_content" />
+
+    </LinearLayout>
+
+    <TextView
+        android:id="@+id/hex_preview"
+        android:gravity="center"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content" />
+
+    </LinearLayout>
+
+</ScrollView>
\ No newline at end of file
index 6605e3f..88976f2 100644 (file)
@@ -27,4 +27,9 @@
     <color name="main_activity_background_dark">#252525</color>
     <color name="settings_icon_color">#000000</color>
     <color name="settings_icon_color_dark">#FFFFFF</color>
+
+    <color name="red">#FF0000</color>
+    <color name="green">#00FF00</color>
+    <color name="blue">#0000FF</color>
+    <color name="alpha">#40000000</color>
 </resources>
index 49dca87..9f36e11 100644 (file)
 
     <string name="freeform_dialog_message_alt">In order for Taskbar to launch apps in freeform window mode, please follow these steps:\n\n&#8226; Go to Developer Options and enable USB debugging\n\n&#8226; Connect your device to a computer with the Android SDK installed, and run the following adb shell command:\n\nsettings put global enable_freeform_support 1\n\n&#8226; Reboot your device</string>
 
+    <string name="color_picker_alpha">Alpha</string>
+    <string name="color_picker_red">Red</string>
+    <string name="color_picker_green">Green</string>
+    <string name="color_picker_blue">Blue</string>
+
 </resources>
index f185392..e203aec 100644 (file)
 
     <Preference
         android:key="background_tint_pref"
-        android:defaultValue="@integer/translucent_gray"
         android:title="@string/pref_title_background_tint" />
 
     <Preference
         android:key="accent_color_pref"
-        android:defaultValue="@integer/translucent_white"
         android:title="@string/pref_title_accent_color" />
 
     <CheckBoxPreference