1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
package it.niedermann.owncloud.notes.branding;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.util.Log;
import android.view.MenuItem;
import android.widget.EditText;
import androidx.annotation.ColorInt;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.preference.PreferenceManager;
import it.niedermann.owncloud.notes.NotesApplication;
import it.niedermann.owncloud.notes.R;
import static it.niedermann.owncloud.notes.shared.util.NotesColorUtil.contrastRatioIsSufficient;
public class BrandingUtil {
private static final String TAG = BrandingUtil.class.getSimpleName();
private static final String pref_key_branding_main = "branding_main";
private static final String pref_key_branding_text = "branding_text";
private BrandingUtil() {
}
@ColorInt
public static int readBrandMainColor(@NonNull Context context) {
final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
Log.v(TAG, "--- Read: shared_preference_theme_main");
return sharedPreferences.getInt(pref_key_branding_main, context.getApplicationContext().getResources().getColor(R.color.defaultBrand));
}
@ColorInt
public static int readBrandTextColor(@NonNull Context context) {
final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
Log.v(TAG, "--- Read: shared_preference_theme_text");
return sharedPreferences.getInt(pref_key_branding_text, Color.WHITE);
}
public static void saveBrandColors(@NonNull Context context, @ColorInt int mainColor, @ColorInt int textColor) {
final int previousMainColor = readBrandMainColor(context);
final int previousTextColor = readBrandTextColor(context);
final SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
Log.v(TAG, "--- Write: shared_preference_theme_main" + " | " + mainColor);
Log.v(TAG, "--- Write: shared_preference_theme_text" + " | " + textColor);
editor.putInt(pref_key_branding_main, mainColor);
editor.putInt(pref_key_branding_text, textColor);
editor.apply();
if (context instanceof BrandedActivity) {
if (mainColor != previousMainColor || textColor != previousTextColor) {
final BrandedActivity activity = (BrandedActivity) context;
activity.runOnUiThread(() -> ActivityCompat.recreate(activity));
}
}
}
/**
* Since we may collide with dark theme in this area, we have to make sure that the color is visible depending on the background
*/
@ColorInt
public static int getSecondaryForegroundColorDependingOnTheme(@NonNull Context context, @ColorInt int mainColor) {
final int primaryColor = ContextCompat.getColor(context, R.color.primary);
final boolean isDarkTheme = NotesApplication.isDarkThemeActive(context);
if (isDarkTheme && !contrastRatioIsSufficient(mainColor, primaryColor)) {
Log.v(TAG, "Contrast ratio between brand color " + String.format("#%06X", (0xFFFFFF & mainColor)) + " and dark theme is too low. Falling back to WHITE as brand color.");
return Color.WHITE;
} else if (!isDarkTheme && !contrastRatioIsSufficient(mainColor, primaryColor)) {
Log.v(TAG, "Contrast ratio between brand color " + String.format("#%06X", (0xFFFFFF & mainColor)) + " and light theme is too low. Falling back to BLACK as brand color.");
return Color.BLACK;
} else {
return mainColor;
}
}
public static void applyBrandToEditText(@ColorInt int mainColor, @ColorInt int textColor, @NonNull EditText editText) {
@ColorInt final int finalMainColor = getSecondaryForegroundColorDependingOnTheme(editText.getContext(), mainColor);
DrawableCompat.setTintList(editText.getBackground(), new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_active},
new int[]{android.R.attr.state_activated},
new int[]{android.R.attr.state_focused},
new int[]{android.R.attr.state_pressed},
new int[]{}
},
new int[]{
finalMainColor,
finalMainColor,
finalMainColor,
finalMainColor,
editText.getContext().getResources().getColor(R.color.fg_default_low)
}
));
}
public static void tintMenuIcon(@NonNull MenuItem menuItem, @ColorInt int color) {
Drawable drawable = menuItem.getIcon();
if (drawable != null) {
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable, color);
menuItem.setIcon(drawable);
}
}
public static void applyBrandToLayerDrawable(@NonNull LayerDrawable check, @IdRes int areaToColor, @ColorInt int mainColor) {
final Drawable drawable = check.findDrawableByLayerId(areaToColor);
if (drawable == null) {
Log.e(TAG, "Could not find areaToColor (" + areaToColor + "). Cannot apply brand.");
} else {
DrawableCompat.setTint(drawable, mainColor);
}
}
}
|