This repository was archived by the owner on Dec 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathATE.java
More file actions
305 lines (268 loc) · 12.7 KB
/
ATE.java
File metadata and controls
305 lines (268 loc) · 12.7 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package com.afollestad.appthemeengine;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.Canvas;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import com.afollestad.appthemeengine.customizers.ATEActivityThemeCustomizer;
import com.afollestad.appthemeengine.customizers.ATETaskDescriptionCustomizer;
import com.afollestad.appthemeengine.inflation.PostInflationApplier;
import com.afollestad.appthemeengine.inflation.ViewInterface;
import com.afollestad.appthemeengine.util.ATEUtil;
import com.afollestad.appthemeengine.util.MDUtil;
import com.afollestad.appthemeengine.util.TintHelper;
import com.afollestad.appthemeengine.viewprocessors.ViewProcessor;
import java.lang.reflect.Field;
import java.util.ArrayList;
/**
* @author Aidan Follestad (afollestad)
*/
public final class ATE extends ATEBase {
public static final String IGNORE_TAG = "ate_ignore";
public static final int USE_DEFAULT = Integer.MAX_VALUE;
/**
* @hide
*/
public static <T extends View> void addPostInflationView(T view) {
if (mPostInflationApply == null)
mPostInflationApply = new ArrayList<>();
mPostInflationApply.add(view);
}
private static ArrayList<View> mPostInflationApply;
@SuppressWarnings("unchecked")
private static void performDefaultProcessing(@NonNull Context context, @NonNull View current, @Nullable String key) {
if (current.getTag() != null && current.getTag() instanceof String) {
// Apply default processor to view if view's tag is a String
ViewProcessor viewProcessor = getViewProcessor(null); // gets default viewProcessor
if (viewProcessor != null)
viewProcessor.process(context, key, current, null);
}
}
public static void cleanup() {
if (mPostInflationApply != null) {
mPostInflationApply.clear();
mPostInflationApply = null;
}
}
public static Config config(@NonNull Context context, @Nullable String key) {
return new Config(context, key);
}
@SuppressLint("CommitPrefEdits")
private static boolean didValuesChange(@NonNull Context context, long updateTime, @Nullable String key) {
return ATE.config(context, key).isConfigured() && Config.prefs(context, key).getLong(Config.VALUES_CHANGED, -1) > updateTime;
}
public static boolean invalidateActivity(final @NonNull Activity activity, long updateTime, @Nullable String ateKey) {
if (ATE.didValuesChange(activity, updateTime, ateKey)) {
// hack to prevent java.lang.RuntimeException: Performing pause of activity that is not resumed
// makes sure recreate() is called right after and not in onResume()
new Handler().post(new Runnable() {
@Override
public void run() {
activity.recreate();
}
});
return true;
}
return false;
}
public static void preApply(@NonNull Activity activity, @Nullable String key) {
didPreApply = activity.getClass();
synchronized (IGNORE_TAG) {
if (mPostInflationApply != null) {
mPostInflationApply.clear();
mPostInflationApply = null;
}
}
int activityTheme = activity instanceof ATEActivityThemeCustomizer ?
((ATEActivityThemeCustomizer) activity).getActivityTheme() : Config.activityTheme(activity, key);
if (activityTheme != 0) activity.setTheme(activityTheme);
final LayoutInflater li = activity.getLayoutInflater();
ATEUtil.setInflaterFactory(li, activity);
}
public static View getRootView(Activity activity) {
return ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
}
@SuppressWarnings("unchecked")
private static void performMainTheming(@NonNull Activity activity, @Nullable String key) {
final View rootView = getRootView(activity);
final boolean rootSetsStatusBarColor = rootView != null && rootView instanceof ViewInterface &&
((ViewInterface) rootView).setsStatusBarColor();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
final Window window = activity.getWindow();
if (!rootSetsStatusBarColor) {
if (Config.coloredStatusBar(activity, key))
window.setStatusBarColor(Config.statusBarColor(activity, key));
else window.setStatusBarColor(Color.BLACK);
invalidateLightStatusBar(activity, key);
}
if (Config.coloredNavigationBar(activity, key))
window.setNavigationBarColor(Config.navigationBarColor(activity, key));
else window.setNavigationBarColor(Color.BLACK);
applyTaskDescription(activity, key);
}
}
public static void invalidateLightStatusBar(@NonNull Activity activity, @Nullable String key) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
final View decorView = activity.getWindow().getDecorView();
boolean lightStatusEnabled = false;
if (Config.coloredStatusBar(activity, key)) {
final int lightStatusMode = Config.lightStatusBarMode(activity, key);
switch (lightStatusMode) {
case Config.LIGHT_STATUS_BAR_OFF:
default:
break;
case Config.LIGHT_STATUS_BAR_ON:
lightStatusEnabled = true;
break;
case Config.LIGHT_STATUS_BAR_AUTO:
lightStatusEnabled = ATEUtil.isColorLight(Config.primaryColor(activity, key));
break;
}
}
final int systemUiVisibility = decorView.getSystemUiVisibility();
if (lightStatusEnabled) {
decorView.setSystemUiVisibility(systemUiVisibility | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
} else {
decorView.setSystemUiVisibility(systemUiVisibility & ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
}
}
public static void themeView(@NonNull View view, @Nullable String key) {
if (view.getContext() == null)
throw new IllegalStateException("View has no Context, use apply(Context, View, String) instead.");
themeView(view.getContext(), view, key);
}
@SuppressWarnings("unchecked")
public static void themeView(@NonNull Context context, @NonNull View view, @Nullable String key) {
if (IGNORE_TAG.equals(view.getTag())) return;
performDefaultProcessing(context, view, key);
final ViewProcessor viewProcessor = getViewProcessor(view.getClass());
if (viewProcessor != null) {
// Apply view theming using processors, if any match
viewProcessor.process(context, key, view, null);
}
}
/**
* @deprecated use postApply() instead. This method will throw an Exception.
*/
@Deprecated
public static void apply(@NonNull Activity activity, @Nullable String key) throws IllegalStateException {
throw new IllegalStateException("ATE.apply() is no longer used, ATE intercepts views at inflation time. Use postApply() here instead.");
}
@SuppressWarnings("unchecked")
public static void postApply(@NonNull Activity activity, @Nullable String key) {
if (didPreApply == null)
preApply(activity, key);
performMainTheming(activity, key);
final View rootView = getRootView(activity);
final boolean rootSetsToolbarColor = rootView != null && rootView instanceof ViewInterface &&
((ViewInterface) rootView).setsToolbarColor();
if (!rootSetsToolbarColor && Config.coloredActionBar(activity, key)) {
if (activity instanceof AppCompatActivity) {
final AppCompatActivity aca = (AppCompatActivity) activity;
if (aca.getSupportActionBar() != null) {
ViewProcessor toolbarViewProcessor = getViewProcessor(Toolbar.class);
if (toolbarViewProcessor != null) {
// The processor handles retrieving the toolbar from the support action bar
toolbarViewProcessor.process(activity, key, null, null);
}
}
} else if (activity.getActionBar() != null) {
activity.getActionBar().setBackgroundDrawable(new ColorDrawable(Config.toolbarColor(activity, key, null)));
}
}
if (mPostInflationApply != null) {
synchronized (IGNORE_TAG) {
for (View view : mPostInflationApply) {
if (view instanceof PostInflationApplier)
((PostInflationApplier) view).postApply();
else ATE.themeView(activity, view, key);
}
}
}
if (ATEUtil.isInClassPath(MDUtil.MAIN_CLASS))
MDUtil.initMdSupport(activity, key);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static void applyTaskDescription(@NonNull Activity activity, @Nullable String key) {
int color = 0;
Bitmap icon = null;
if (activity instanceof ATETaskDescriptionCustomizer) {
final ATETaskDescriptionCustomizer customizer = (ATETaskDescriptionCustomizer) activity;
color = customizer.getTaskDescriptionColor();
icon = customizer.getTaskDescriptionIcon();
if (color == ATE.USE_DEFAULT)
color = Config.primaryColor(activity, key);
} else {
color = Config.primaryColor(activity, key);
}
// Task description requires fully opaque color
color = ATEUtil.stripAlpha(color);
// Default is app's launcher icon
if (icon == null)
icon = (getBitmapFromDrawable(activity.getApplicationInfo().loadIcon(activity.getPackageManager())));
// Sets color of entry in the system recents page
ActivityManager.TaskDescription td = new ActivityManager.TaskDescription(
(String) activity.getTitle(), icon, color);
activity.setTaskDescription(td);
}
@Nullable
private static Toolbar getPostInflationToolbar() {
synchronized (IGNORE_TAG) {
if (mPostInflationApply == null) return null;
for (View view : mPostInflationApply) {
if (view instanceof Toolbar)
return (Toolbar) view;
}
return null;
}
}
@SuppressWarnings("unchecked")
public static void themeOverflow(@NonNull Activity activity, @Nullable String key) {
final Toolbar toolbar = getPostInflationToolbar();
final int toolbarColor = Config.toolbarColor(activity, key, toolbar);
final int tintColor = Config.getToolbarTitleColor(activity, toolbar, key, toolbarColor);
// The collapse icon displays when action views are expanded (e.g. SearchView)
try {
final Field field = Toolbar.class.getDeclaredField("mCollapseIcon");
field.setAccessible(true);
Drawable collapseIcon = (Drawable) field.get(toolbar);
if (collapseIcon != null)
field.set(toolbar, TintHelper.createTintedDrawable(collapseIcon, tintColor));
} catch (Exception e) {
e.printStackTrace();
}
if (toolbar != null && toolbar.getParent() instanceof CollapsingToolbarLayout)
return; // collapsing toolbar handles the overflow color
ATEUtil.setOverflowButtonColor(activity, toolbar, tintColor);
}
@NonNull
private static Bitmap getBitmapFromDrawable(@NonNull Drawable drawable) {
Bitmap bmp = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bmp;
}
private ATE() {
}
}