Files
arcanechat-android/src/org/thoughtcrime/securesms/util/DynamicTheme.java
T
Hocuri 1ff6a3a18d Dark theme / DayNight: Add option to follow system
fix maybe #1748. Maybe not, because actually I don't understand why
issue #1748 even happened because we don't use the system background for
the bubble but an own one (see conversation_item_sent.xml:50). Probably
the system is somehow trying to forcefully create a dark mode.

So, let's see whether this PR fixes the issue, if not, I'll try again.
2020-12-19 19:51:55 +01:00

93 lines
2.5 KiB
Java

package org.thoughtcrime.securesms.util;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import org.thoughtcrime.securesms.R;
public class DynamicTheme {
public static final String DARK = "dark";
public static final String LIGHT = "light";
public static final String SYSTEM = "system";
//private static boolean isDarkTheme;
private int currentTheme;
public void onCreate(Activity activity) {
//boolean wasDarkTheme = isDarkTheme;
currentTheme = getSelectedTheme(activity);
//isDarkTheme = isDarkTheme(activity);
activity.setTheme(currentTheme);
// In case you introduce a CachedInflater and there are problems with the dark mode, uncomment
// this line and the line in onResume():
//if (isDarkTheme != wasDarkTheme) {
//CachedInflater.from(activity).clear();
//}
}
public void onResume(Activity activity) {
if (currentTheme != getSelectedTheme(activity)) {
Intent intent = activity.getIntent();
activity.finish();
OverridePendingTransition.invoke(activity);
activity.startActivity(intent);
OverridePendingTransition.invoke(activity);
//CachedInflater.from(activity).clear();
}
}
private @StyleRes int getSelectedTheme(Activity activity) {
if (isDarkTheme(activity)) {
return getDarkThemeStyle();
} else {
return getLightThemeStyle();
}
}
protected @StyleRes int getLightThemeStyle() {
return R.style.TextSecure_LightTheme;
}
protected @StyleRes int getDarkThemeStyle() {
return R.style.TextSecure_DarkTheme;
}
static boolean systemThemeAvailable() {
return Build.VERSION.SDK_INT >= 29;
}
/**
* Takes the system theme into account.
*/
public static boolean isDarkTheme(@NonNull Context context) {
String theme = Prefs.getTheme(context);
if (theme.equals(SYSTEM) && systemThemeAvailable()) {
return isSystemInDarkTheme(context);
} else {
return theme.equals(DARK);
}
}
private static boolean isSystemInDarkTheme(@NonNull Context context) {
return (context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
}
private static final class OverridePendingTransition {
static void invoke(Activity activity) {
activity.overridePendingTransition(0, 0);
}
}
}