Эх сурвалжийг харах

Dark Mode Improvements

Removes conflicts between high contrast server themes and the dark mode.
Made primaryAccentColor() aware of the device theme so that it can
adjust the lightness accordingly.
Switch preference's track and thumb are more visible in device dark
mode.

Signed-off-by: Daniel Bailey <daniel.bailey@grappleIT.co.uk>
Daniel Bailey 5 жил өмнө
parent
commit
28d35f26af

+ 9 - 3
src/main/java/com/owncloud/android/ui/ThemeableSwitchPreference.java

@@ -32,11 +32,13 @@ import android.widget.Switch;
 import com.owncloud.android.R;
 import com.owncloud.android.utils.ThemeUtils;
 
+import androidx.appcompat.app.AppCompatDelegate;
 import androidx.core.graphics.drawable.DrawableCompat;
 
 
 /**
  * Themeable switch preference
+ * TODO Migrate to androidx
  */
 public class ThemeableSwitchPreference extends SwitchPreference {
 
@@ -72,12 +74,16 @@ public class ThemeableSwitchPreference extends SwitchPreference {
                 Switch switchView = (Switch) child;
 
                 if(thumbColorStateList == null && trackColorStateList == null) {
-                    int color = ThemeUtils.primaryAccentColor(getContext());
-                    int trackColor = Color.argb(77, Color.red(color), Color.green(color), Color.blue(color));
+                    int thumbColor = ThemeUtils.primaryAccentColor(getContext());
+                    if (ThemeUtils.darkTheme(getContext()) &&
+                        AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
+                        thumbColor = Color.WHITE;
+                    }
+                    int trackColor = Color.argb(77, Color.red(thumbColor), Color.green(thumbColor), Color.blue(thumbColor));
                     int trackColorUnchecked = getContext().getResources().getColor(R.color.switch_track_color_unchecked);
                     thumbColorStateList = new ColorStateList(
                             new int[][]{new int[]{android.R.attr.state_checked}, new int[]{}},
-                            new int[]{color, Color.WHITE});
+                            new int[]{thumbColor, getContext().getResources().getColor(R.color.switch_thumb_color_unchecked)});
                     trackColorStateList = new ColorStateList(
                             new int[][]{new int[]{android.R.attr.state_checked},
                                 new int[]{}},

+ 2 - 1
src/main/java/com/owncloud/android/ui/activity/SettingsActivity.java

@@ -751,7 +751,8 @@ public class SettingsActivity extends ThemedPreferenceActivity
             actionBar.setBackgroundDrawable(new ColorDrawable(ThemeUtils.primaryColor(this)));
 
             Drawable backArrow = getResources().getDrawable(R.drawable.ic_arrow_back);
-            actionBar.setHomeAsUpIndicator(ThemeUtils.tintDrawable(backArrow, ThemeUtils.fontColor(this, true)));
+            actionBar.setHomeAsUpIndicator(ThemeUtils.tintDrawable(backArrow, ThemeUtils.fontColor(this,
+                                                                                                   !ThemeUtils.darkTheme(this))));
         }
 
         Window window = getWindow();

+ 1 - 1
src/main/java/com/owncloud/android/ui/activity/ToolbarActivity.java

@@ -65,7 +65,7 @@ public abstract class ToolbarActivity extends BaseActivity {
      */
     protected void setupToolbar(boolean useBackgroundImage) {
         int primaryColor = ThemeUtils.primaryColor(this, false);
-        int fontColor = ThemeUtils.fontColor(this, true);
+        int fontColor = ThemeUtils.fontColor(this, !ThemeUtils.darkTheme(getApplicationContext()));
 
         Toolbar toolbar = findViewById(R.id.toolbar);
         setSupportActionBar(toolbar);

+ 19 - 27
src/main/java/com/owncloud/android/utils/ThemeUtils.java

@@ -94,7 +94,12 @@ public final class ThemeUtils {
         try {
             float adjust;
             if (darkTheme(context)) {
-                adjust = +0.1f;
+                if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
+                    adjust = +0.5f;
+//                    return adjustLightness(adjust, Color.parseColor(capability.getServerColor()), -1);
+                } else {
+                    adjust = +0.1f;
+                }
             } else {
                 adjust = -0.1f;
             }
@@ -134,7 +139,7 @@ public final class ThemeUtils {
         try {
             int color = Color.parseColor(getCapability(account, context).getServerColor());
             if (replaceWhite && Color.WHITE == color) {
-                return Color.GRAY;
+                return getNeutralGrey(context);
             } else {
                 return color;
             }
@@ -143,6 +148,11 @@ public final class ThemeUtils {
         }
     }
 
+    public static int getNeutralGrey(Context context) {
+        return darkTheme(context) ? context.getResources().getColor(R.color.fg_contrast) :
+                                    Color.GRAY;
+    }
+
     public static int elementColor(Context context) {
         return elementColor(null, context);
     }
@@ -238,7 +248,7 @@ public final class ThemeUtils {
                 actionBar.setTitle(title);
             } else {
                 Spannable text = new SpannableString(title);
-                text.setSpan(new ForegroundColorSpan(fontColor(context, true)),
+                text.setSpan(new ForegroundColorSpan(fontColor(context, !darkTheme(context))),
                              0,
                              text.length(),
                              Spannable.SPAN_INCLUSIVE_INCLUSIVE);
@@ -247,6 +257,10 @@ public final class ThemeUtils {
         }
     }
 
+    public static void setColoredTitle(@Nullable ActionBar actionBar, int titleId, Context context) {
+        setColoredTitle(actionBar, context.getString(titleId), context);
+    }
+
     /**
      * Set color of subtitle to white/black depending on background color
      *
@@ -293,28 +307,6 @@ public final class ThemeUtils {
         return text;
     }
 
-    /**
-     * Set color of title to white/black depending on background color
-     *
-     * @param actionBar actionBar to be used
-     * @param titleId   title to be shown
-     */
-    public static void setColoredTitle(@Nullable ActionBar actionBar, int titleId, Context context) {
-        if (actionBar != null) {
-            if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
-                actionBar.setTitle(titleId);
-            } else {
-                String title = context.getString(titleId);
-                Spannable text = new SpannableString(title);
-                text.setSpan(new ForegroundColorSpan(fontColor(context, true)),
-                             0,
-                             text.length(),
-                             Spannable.SPAN_INCLUSIVE_INCLUSIVE);
-                actionBar.setTitle(text);
-            }
-        }
-    }
-
     public static String getDefaultDisplayNameForRootFolder(Context context) {
         OCCapability capability = getCapability(context);
 
@@ -534,7 +526,7 @@ public final class ThemeUtils {
      */
     public static void themeSearchView(SearchView searchView, boolean themedBackground, Context context) {
         // hacky as no default way is provided
-        int fontColor = ThemeUtils.fontColor(context, true);
+        int fontColor = ThemeUtils.fontColor(context, !darkTheme(context));
         SearchView.SearchAutoComplete editText = searchView.findViewById(R.id.search_src_text);
         themeEditText(context, editText, themedBackground);
 
@@ -581,7 +573,7 @@ public final class ThemeUtils {
         // setting the track color
         DrawableCompat.setTintList(switchView.getTrackDrawable(), new ColorStateList(
                 new int[][]{new int[]{android.R.attr.state_checked}, new int[]{}},
-                new int[]{trackColor, Color.parseColor("#4D000000")}));
+                new int[]{trackColor, MainApp.getAppContext().getResources().getColor(R.color.switch_track_color_unchecked)}));
     }
 
     public static Drawable tintDrawable(@DrawableRes int id, int color) {

+ 2 - 0
src/main/res/values-night/colors.xml

@@ -25,6 +25,7 @@
 
     <!-- Colors -->
     <color name="bg_default">#151515</color>
+    <color name="fg_contrast">#717171</color>
     <color name="primary_button_text_color">#000000</color>
 
     <color name="uploader_list_separator_color">#2a2a2a</color>
@@ -35,6 +36,7 @@
 
     <color name="drawer_menu_icon">#ffffff</color>
     <color name="bg_fallback_highlight">#737373</color>
+    <color name="switch_thumb_color_unchecked">#2a2a2a</color>
     <color name="switch_track_color_unchecked">#B3FFFFFF</color>
     <color name="drawer_active_item_background">@color/white</color>
 

+ 1 - 0
src/main/res/values/colors.xml

@@ -77,6 +77,7 @@
 
     <color name="drawer_menu_icon">#757575</color>
     <color name="bg_fallback_highlight">#616161</color>
+    <color name="switch_thumb_color_unchecked">#FFFFFF</color>
     <color name="switch_track_color_unchecked">#4D000000</color>
     <color name="drawer_active_item_background">#80000000</color>
 

+ 1 - 0
src/main/res/xml/preferences.xml

@@ -50,6 +50,7 @@
 			android:dialogTitle="@string/prefs_lock_title"
 			android:defaultValue="none"/>
 		<com.owncloud.android.ui.ThemeableSwitchPreference
+
 			android:title="@string/prefs_show_hidden_files"
 			android:key="show_hidden_files"/>
         <com.owncloud.android.ui.ThemeableSwitchPreference