ThemeChangeAwareActivity: reintroduce cache buster
This is still needed by certain icons, like the trash icon in the peer editor. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
parent
e437ac389e
commit
b8052bd8fb
@ -6,14 +6,49 @@
|
|||||||
package com.wireguard.android.activity;
|
package com.wireguard.android.activity;
|
||||||
|
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
|
import android.content.res.Resources;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
import android.support.v7.app.AppCompatDelegate;
|
import android.support.v7.app.AppCompatDelegate;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import com.wireguard.android.Application;
|
import com.wireguard.android.Application;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
|
||||||
public abstract class ThemeChangeAwareActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
|
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
|
@Override
|
||||||
@ -31,10 +66,12 @@ public abstract class ThemeChangeAwareActivity extends AppCompatActivity impleme
|
|||||||
@Override
|
@Override
|
||||||
public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key) {
|
public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key) {
|
||||||
if ("dark_theme".equals(key)) {
|
if ("dark_theme".equals(key)) {
|
||||||
|
final boolean darkMode = sharedPreferences.getBoolean(key, false);
|
||||||
AppCompatDelegate.setDefaultNightMode(
|
AppCompatDelegate.setDefaultNightMode(
|
||||||
sharedPreferences.getBoolean(key, false) ?
|
sharedPreferences.getBoolean(key, false) ?
|
||||||
AppCompatDelegate.MODE_NIGHT_YES :
|
AppCompatDelegate.MODE_NIGHT_YES :
|
||||||
AppCompatDelegate.MODE_NIGHT_NO);
|
AppCompatDelegate.MODE_NIGHT_NO);
|
||||||
|
invalidateDrawableCache(getResources(), darkMode);
|
||||||
recreate();
|
recreate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user