2018-06-05 01:49:54 +02:00
|
|
|
/*
|
|
|
|
* Copyright © 2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.wireguard.android.activity;
|
|
|
|
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.content.res.Resources;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
|
import android.support.v7.app.AppCompatDelegate;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2018-06-07 04:12:42 +02:00
|
|
|
import com.wireguard.android.Application;
|
|
|
|
|
2018-06-05 01:49:54 +02:00
|
|
|
import java.lang.reflect.Field;
|
|
|
|
|
|
|
|
public abstract class ThemeChangeAwareActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
|
|
|
|
private static final String TAG = "WireGuard/" + ThemeChangeAwareActivity.class.getSimpleName();
|
|
|
|
|
|
|
|
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
|
|
|
|
protected void onCreate(final Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2018-06-07 04:12:42 +02:00
|
|
|
Application.getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
|
2018-06-05 01:49:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
2018-06-07 04:12:42 +02:00
|
|
|
Application.getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
|
2018-06-05 01:49:54 +02:00
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
@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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|