aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/app/src/main/java/com/wireguard
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-07-25 03:32:54 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2018-07-25 04:05:41 +0200
commit07de269d65ec69dc1b86fac04f7f43231ef5e4b1 (patch)
treea9d7ddb3893fe9e45020048daf5b9ef0fd7b4b22 /app/src/main/java/com/wireguard
parentVersion bump (diff)
downloadwireguard-android-07de269d65ec69dc1b86fac04f7f43231ef5e4b1.tar.xz
wireguard-android-07de269d65ec69dc1b86fac04f7f43231ef5e4b1.zip
ThemeChangeAwareActivity: reintroduce cache buster
This is still needed by certain icons, like the trash icon in the peer editor.
Diffstat (limited to 'app/src/main/java/com/wireguard')
-rw-r--r--app/src/main/java/com/wireguard/android/activity/ThemeChangeAwareActivity.java37
1 files changed, 37 insertions, 0 deletions
diff --git a/app/src/main/java/com/wireguard/android/activity/ThemeChangeAwareActivity.java b/app/src/main/java/com/wireguard/android/activity/ThemeChangeAwareActivity.java
index 75b7251b..d85a0170 100644
--- a/app/src/main/java/com/wireguard/android/activity/ThemeChangeAwareActivity.java
+++ b/app/src/main/java/com/wireguard/android/activity/ThemeChangeAwareActivity.java
@@ -6,14 +6,49 @@
package com.wireguard.android.activity;
import android.content.SharedPreferences;
+import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
+import android.util.Log;
import com.wireguard.android.Application;
+import java.lang.reflect.Field;
+
public abstract class ThemeChangeAwareActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
+ private static final String TAG = "WireGuard/" + ThemeChangeAwareActivity.class.getSimpleName();
+
+ @Nullable private static Resources lastResources;
+ private static boolean lastDarkMode;
+ private static synchronized void invalidateDrawableCache(final Resources resources, final boolean darkMode) {
+ if (resources == lastResources && darkMode == lastDarkMode)
+ return;
+
+ try {
+ Field f;
+ Object o = resources;
+ try {
+ f = o.getClass().getDeclaredField("mResourcesImpl");
+ f.setAccessible(true);
+ o = f.get(o);
+ } catch (final Exception ignored) { }
+ f = o.getClass().getDeclaredField("mDrawableCache");
+ f.setAccessible(true);
+ o = f.get(o);
+ try {
+ o.getClass().getMethod("onConfigurationChange", int.class).invoke(o, -1);
+ } catch (final Exception ignored) {
+ o.getClass().getMethod("clear").invoke(o);
+ }
+ } catch (final Exception e) {
+ Log.e(TAG, "Failed to flush drawable cache", e);
+ }
+
+ lastResources = resources;
+ lastDarkMode = darkMode;
+ }
@Override
@@ -31,10 +66,12 @@ public abstract class ThemeChangeAwareActivity extends AppCompatActivity impleme
@Override
public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key) {
if ("dark_theme".equals(key)) {
+ final boolean darkMode = sharedPreferences.getBoolean(key, false);
AppCompatDelegate.setDefaultNightMode(
sharedPreferences.getBoolean(key, false) ?
AppCompatDelegate.MODE_NIGHT_YES :
AppCompatDelegate.MODE_NIGHT_NO);
+ invalidateDrawableCache(getResources(), darkMode);
recreate();
}
}