Improve the Android 12 splash screen.

This commit is contained in:
Greyson Parrelli
2022-04-27 14:43:41 -04:00
parent 39a11ce26c
commit 11db59d8a1
14 changed files with 159 additions and 18 deletions

View File

@@ -24,6 +24,7 @@ import org.thoughtcrime.securesms.util.CachedInflater;
import org.thoughtcrime.securesms.util.CommunicationActions;
import org.thoughtcrime.securesms.util.DynamicNoActionBarTheme;
import org.thoughtcrime.securesms.util.DynamicTheme;
import org.thoughtcrime.securesms.util.SplashScreenUtil;
import org.thoughtcrime.securesms.util.WindowUtil;
public class MainActivity extends PassphraseRequiredActivity implements VoiceNoteMediaControllerOwner {
@@ -100,6 +101,12 @@ public class MainActivity extends PassphraseRequiredActivity implements VoiceNot
updateTabVisibility();
}
@Override
protected void onStop() {
super.onStop();
SplashScreenUtil.setSplashScreenThemeIfNecessary(this, SignalStore.settings().getTheme());
}
@Override
public void onBackPressed() {
if (!navigator.onBackPressed()) {

View File

@@ -8,6 +8,7 @@ import org.thoughtcrime.securesms.components.settings.DSLSettingsAdapter
import org.thoughtcrime.securesms.components.settings.DSLSettingsFragment
import org.thoughtcrime.securesms.components.settings.DSLSettingsText
import org.thoughtcrime.securesms.components.settings.configure
import org.thoughtcrime.securesms.keyvalue.SettingsValues
import org.thoughtcrime.securesms.util.navigation.safeNavigate
class AppearanceSettingsFragment : DSLSettingsFragment(R.string.preferences__appearance) {
@@ -36,9 +37,9 @@ class AppearanceSettingsFragment : DSLSettingsFragment(R.string.preferences__app
radioListPref(
title = DSLSettingsText.from(R.string.preferences__theme),
listItems = themeLabels,
selected = themeValues.indexOf(state.theme),
selected = themeValues.indexOf(state.theme.serialize()),
onSelected = {
viewModel.setTheme(themeValues[it])
viewModel.setTheme(activity, SettingsValues.Theme.deserialize(themeValues[it]))
}
)

View File

@@ -1,7 +1,9 @@
package org.thoughtcrime.securesms.components.settings.app.appearance
import org.thoughtcrime.securesms.keyvalue.SettingsValues
data class AppearanceSettingsState(
val theme: String,
val theme: SettingsValues.Theme,
val messageFontSize: Int,
val language: String
)

View File

@@ -1,9 +1,12 @@
package org.thoughtcrime.securesms.components.settings.app.appearance
import android.app.Activity
import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import org.thoughtcrime.securesms.jobs.EmojiSearchIndexDownloadJob
import org.thoughtcrime.securesms.keyvalue.SettingsValues.Theme
import org.thoughtcrime.securesms.keyvalue.SignalStore
import org.thoughtcrime.securesms.util.SplashScreenUtil
import org.thoughtcrime.securesms.util.livedata.Store
class AppearanceSettingsViewModel : ViewModel() {
@@ -21,9 +24,10 @@ class AppearanceSettingsViewModel : ViewModel() {
val state: LiveData<AppearanceSettingsState> = store.stateLiveData
fun setTheme(theme: String) {
fun setTheme(activity: Activity?, theme: Theme) {
store.update { it.copy(theme = theme) }
SignalStore.settings().theme = theme
SplashScreenUtil.setSplashScreenThemeIfNecessary(activity, theme)
}
fun setLanguage(language: String) {

View File

@@ -181,12 +181,12 @@ public final class SettingsValues extends SignalStoreValues {
return CallBandwidthMode.fromCode(getInteger(CALL_BANDWIDTH_MODE, CallBandwidthMode.HIGH_ALWAYS.getCode()));
}
public @NonNull String getTheme() {
return getString(THEME, TextSecurePreferences.getTheme(ApplicationDependencies.getApplication()));
public @NonNull Theme getTheme() {
return Theme.deserialize(getString(THEME, TextSecurePreferences.getTheme(ApplicationDependencies.getApplication())));
}
public void setTheme(@NonNull String theme) {
putString(THEME, theme);
public void setTheme(@NonNull Theme theme) {
putString(THEME, theme.serialize());
onConfigurationSettingChanged.postValue(THEME);
}
@@ -432,4 +432,27 @@ public final class SettingsValues extends SignalStoreValues {
return value;
}
}
public enum Theme {
SYSTEM("system"), LIGHT("light"), DARK("dark");
private final String value;
Theme(String value) {
this.value = value;
}
public @NonNull String serialize() {
return value;
}
public static @NonNull Theme deserialize(@NonNull String value) {
switch (value) {
case "system": return SYSTEM;
case "light": return LIGHT;
case "dark": return DARK;
default: throw new IllegalArgumentException("Unrecognized value " + value);
}
}
}
}

View File

@@ -11,16 +11,14 @@ import androidx.appcompat.app.AppCompatDelegate;
import org.signal.core.util.logging.Log;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.keyvalue.SettingsValues;
import org.thoughtcrime.securesms.keyvalue.SettingsValues.Theme;
import org.thoughtcrime.securesms.keyvalue.SignalStore;
public class DynamicTheme {
private static final String TAG = Log.tag(DynamicTheme.class);
public static final String DARK = "dark";
public static final String LIGHT = "light";
public static final String SYSTEM = "system";
private static int globalNightModeConfiguration;
private int onCreateNightModeConfiguration;
@@ -55,9 +53,9 @@ public class DynamicTheme {
}
public static void setDefaultDayNightMode(@NonNull Context context) {
String theme = SignalStore.settings().getTheme();
Theme theme = SignalStore.settings().getTheme();
if (theme.equals(SYSTEM)) {
if (theme == Theme.SYSTEM) {
Log.d(TAG, "Setting to follow system expecting: " + ConfigurationUtil.getNightModeConfiguration(context.getApplicationContext()));
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
} else if (DynamicTheme.isDarkTheme(context)) {
@@ -75,12 +73,12 @@ public class DynamicTheme {
* Takes the system theme into account.
*/
public static boolean isDarkTheme(@NonNull Context context) {
String theme = SignalStore.settings().getTheme();
Theme theme = SignalStore.settings().getTheme();
if (theme.equals(SYSTEM) && systemThemeAvailable()) {
if (theme == Theme.SYSTEM && systemThemeAvailable()) {
return isSystemInDarkTheme(context);
} else {
return theme.equals(DARK);
return theme == Theme.DARK;
}
}

View File

@@ -0,0 +1,36 @@
package org.thoughtcrime.securesms.util;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Build;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.keyvalue.SettingsValues;
public final class SplashScreenUtil {
private SplashScreenUtil() {}
/**
* Sets the splash screen for Android 12+ devices based on the passed-in theme.
*/
public static void setSplashScreenThemeIfNecessary(@Nullable Activity activity, @NonNull SettingsValues.Theme theme) {
if (Build.VERSION.SDK_INT < 31 || activity == null) {
return;
}
switch (theme) {
case LIGHT:
activity.getSplashScreen().setSplashScreenTheme(R.style.Theme_Signal_DayNight_NoActionBar_LightSplash);
break;
case DARK:
activity.getSplashScreen().setSplashScreenTheme(R.style.Theme_Signal_DayNight_NoActionBar_DarkSplash);
break;
case SYSTEM:
activity.getSplashScreen().setSplashScreenTheme(Resources.ID_NULL);
break;
}
}
}

View File

@@ -774,7 +774,7 @@ public class TextSecurePreferences {
* @deprecated Use {@link SettingsValues#getTheme()} via {@link org.thoughtcrime.securesms.keyvalue.SignalStore} instead.
*/
public static String getTheme(Context context) {
return getStringPreference(context, THEME_PREF, DynamicTheme.systemThemeAvailable() ? DynamicTheme.SYSTEM : DynamicTheme.LIGHT);
return getStringPreference(context, THEME_PREF, DynamicTheme.systemThemeAvailable() ? "system" : "light");
}
public static boolean isShowInviteReminders(Context context) {