Better way to create and handle night mode
This commit is contained in:
parent
a943fc3f08
commit
3506264347
6 changed files with 66 additions and 77 deletions
|
@ -155,7 +155,7 @@ dependencies {
|
|||
implementation 'com.google.firebase:firebase-messaging:19.0.1'
|
||||
}
|
||||
implementation 'androidx.recyclerview:recyclerview:1.0.0'
|
||||
implementation 'androidx.appcompat:appcompat:1.0.2'
|
||||
implementation 'androidx.appcompat:appcompat:1.1.0'
|
||||
implementation 'com.google.android:flexbox:1.1.0'
|
||||
implementation 'com.github.bumptech.glide:glide:4.9.0'
|
||||
|
||||
|
|
|
@ -64,7 +64,7 @@
|
|||
android:largeHeap="true"
|
||||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:resizeableActivity="true"
|
||||
android:theme="@style/LinphoneStyleLight">
|
||||
android:theme="@style/LinphoneStyle">
|
||||
|
||||
<!-- Starting activities -->
|
||||
|
||||
|
|
|
@ -20,56 +20,39 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|||
*/
|
||||
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.content.res.Configuration;
|
||||
import android.os.Bundle;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.appcompat.app.AppCompatDelegate;
|
||||
import org.linphone.R;
|
||||
import org.linphone.core.tools.Log;
|
||||
import org.linphone.settings.LinphonePreferences;
|
||||
|
||||
public abstract class ThemeableActivity extends AppCompatActivity {
|
||||
private int mTheme;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
mTheme = R.style.LinphoneStyleLight;
|
||||
if (LinphonePreferences.instance().isDarkModeEnabled()) {
|
||||
mTheme = R.style.LinphoneStyleDark;
|
||||
setTheme(R.style.LinphoneStyleDark);
|
||||
}
|
||||
|
||||
if (getResources().getBoolean(R.bool.orientation_portrait_only)) {
|
||||
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
|
||||
}
|
||||
|
||||
super.onCreate(savedInstanceState);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSaveInstanceState(Bundle outState) {
|
||||
outState.putInt("Theme", mTheme);
|
||||
super.onSaveInstanceState(outState);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onRestoreInstanceState(Bundle savedInstanceState) {
|
||||
mTheme = savedInstanceState.getInt("Theme");
|
||||
super.onRestoreInstanceState(savedInstanceState);
|
||||
int nightMode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
|
||||
switch (nightMode) {
|
||||
case Configuration.UI_MODE_NIGHT_NO:
|
||||
case Configuration.UI_MODE_NIGHT_UNDEFINED:
|
||||
if (LinphonePreferences.instance().isDarkModeEnabled()) {
|
||||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
|
||||
}
|
||||
case Configuration.UI_MODE_NIGHT_YES:
|
||||
if (!LinphonePreferences.instance().isDarkModeEnabled()) {
|
||||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
|
||||
if (LinphonePreferences.instance().isDarkModeEnabled()) {
|
||||
if (mTheme != R.style.LinphoneStyleDark) {
|
||||
Log.w("[Themeable Activity] Recreate Activity cause theme doesn't match");
|
||||
recreate();
|
||||
}
|
||||
} else {
|
||||
if (mTheme != R.style.LinphoneStyleLight) {
|
||||
Log.w("[Themeable Activity] Recreate Activity cause theme doesn't match");
|
||||
recreate();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,6 +30,7 @@ import android.view.LayoutInflater;
|
|||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AppCompatDelegate;
|
||||
import org.linphone.LinphoneService;
|
||||
import org.linphone.R;
|
||||
import org.linphone.compatibility.Compatibility;
|
||||
|
@ -147,7 +148,10 @@ public class AdvancedSettingsFragment extends SettingsFragment {
|
|||
@Override
|
||||
public void onBoolValueChanged(boolean newValue) {
|
||||
mPrefs.enableDarkMode(newValue);
|
||||
getActivity().recreate();
|
||||
AppCompatDelegate.setDefaultNightMode(
|
||||
newValue
|
||||
? AppCompatDelegate.MODE_NIGHT_YES
|
||||
: AppCompatDelegate.MODE_NIGHT_NO);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
44
app/src/main/res/values-night/styles.xml
Normal file
44
app/src/main/res/values-night/styles.xml
Normal file
|
@ -0,0 +1,44 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<style name="LinphoneStyle" parent="Theme.AppCompat.DayNight.NoActionBar">
|
||||
<!-- Disable animations between activities -->
|
||||
<item name="android:windowAnimationStyle">@null</item>
|
||||
<item name="android:windowBackground">@drawable/launch_screen</item>
|
||||
|
||||
<!-- Android theme override -->
|
||||
<item name="colorAccent">@color/primary_color</item>
|
||||
<item name="android:colorPrimary">@color/primary_color</item>
|
||||
<!-- Comment out the following line to disable system's status bar coloring -->
|
||||
<item name="android:colorPrimaryDark">@color/primary_dark_color</item>
|
||||
<item name="android:textColorPrimary">@color/dark_primary_text_color</item>
|
||||
<item name="android:textColorSecondary">@color/header_background_color</item>
|
||||
<item name="android:textColorHint">@color/toolbar_color</item>
|
||||
<item name="android:colorBackground">@color/dark_grey_color</item>
|
||||
<item name="android:textColorPrimaryInverse">@color/dark_grey_color</item>
|
||||
|
||||
<item name="accentColor">@color/primary_color</item>
|
||||
<item name="accentColorLight30">@color/primary_light_color</item>
|
||||
<item name="accentTextColor">@color/white_color</item>
|
||||
<item name="primaryTextColor">@color/dark_primary_text_color</item>
|
||||
<item name="secondaryTextColor">@color/dark_grey_color</item>
|
||||
<item name="primarySubtextDarkColor">@color/header_background_color</item>
|
||||
<item name="primarySubtextLightColor">@color/toolbar_color</item>
|
||||
|
||||
<item name="backgroundColor">@color/dark_grey_color</item>
|
||||
<item name="backgroundColor2">@color/grey_color</item>
|
||||
<item name="backgroundContastColor">@color/white_color</item>
|
||||
<item name="dividerColor">@color/light_grey_color</item>
|
||||
|
||||
<item name="drawableTintColor">@color/white_color</item>
|
||||
<item name="drawableTintDisabledColor">@color/light_grey_color</item>
|
||||
<item name="drawableTintOverColor">@color/primary_color</item>
|
||||
|
||||
<item name="lighToolbarBackgroundColor">@color/dark_grey_color</item>
|
||||
<item name="lighToolbarBackgroundDisabledColor">@color/light_grey_color</item>
|
||||
<item name="lighToolbarTextColor">@color/white_color</item>
|
||||
<item name="darkToolbarBackgroundColor">@color/dark_grey_color</item>
|
||||
<item name="darkToolbarTextColor">@color/white_color</item>
|
||||
|
||||
<item name="button_background_drawable">@drawable/button_background_dark</item>
|
||||
</style>
|
||||
</resources>
|
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<style name="LinphoneStyleLight" parent="Theme.AppCompat.Light.NoActionBar">
|
||||
<style name="LinphoneStyle" parent="Theme.AppCompat.DayNight.NoActionBar">
|
||||
<!-- Disable animations between activities -->
|
||||
<item name="android:windowAnimationStyle">@null</item>
|
||||
<item name="android:windowBackground">@drawable/launch_screen</item>
|
||||
|
@ -42,48 +42,6 @@
|
|||
<item name="button_background_drawable">@drawable/button_background_light</item>
|
||||
</style>
|
||||
|
||||
<style name="LinphoneStyleDark" parent="Theme.AppCompat.NoActionBar">
|
||||
<!-- Disable animations between activities -->
|
||||
<item name="android:windowAnimationStyle">@null</item>
|
||||
<item name="android:windowBackground">@drawable/launch_screen</item>
|
||||
|
||||
<!-- Android theme override -->
|
||||
<item name="colorAccent">@color/primary_color</item>
|
||||
<item name="android:colorPrimary">@color/primary_color</item>
|
||||
<!-- Comment out the following line to disable system's status bar coloring -->
|
||||
<item name="android:colorPrimaryDark">@color/primary_dark_color</item>
|
||||
<item name="android:textColorPrimary">@color/dark_primary_text_color</item>
|
||||
<item name="android:textColorSecondary">@color/header_background_color</item>
|
||||
<item name="android:textColorHint">@color/toolbar_color</item>
|
||||
<item name="android:colorBackground">@color/dark_grey_color</item>
|
||||
<item name="android:textColorPrimaryInverse">@color/dark_grey_color</item>
|
||||
|
||||
<item name="accentColor">@color/primary_color</item>
|
||||
<item name="accentColorLight30">@color/primary_light_color</item>
|
||||
<item name="accentTextColor">@color/white_color</item>
|
||||
<item name="primaryTextColor">@color/dark_primary_text_color</item>
|
||||
<item name="secondaryTextColor">@color/dark_grey_color</item>
|
||||
<item name="primarySubtextDarkColor">@color/header_background_color</item>
|
||||
<item name="primarySubtextLightColor">@color/toolbar_color</item>
|
||||
|
||||
<item name="backgroundColor">@color/dark_grey_color</item>
|
||||
<item name="backgroundColor2">@color/grey_color</item>
|
||||
<item name="backgroundContastColor">@color/white_color</item>
|
||||
<item name="dividerColor">@color/light_grey_color</item>
|
||||
|
||||
<item name="drawableTintColor">@color/white_color</item>
|
||||
<item name="drawableTintDisabledColor">@color/light_grey_color</item>
|
||||
<item name="drawableTintOverColor">@color/primary_color</item>
|
||||
|
||||
<item name="lighToolbarBackgroundColor">@color/dark_grey_color</item>
|
||||
<item name="lighToolbarBackgroundDisabledColor">@color/light_grey_color</item>
|
||||
<item name="lighToolbarTextColor">@color/white_color</item>
|
||||
<item name="darkToolbarBackgroundColor">@color/dark_grey_color</item>
|
||||
<item name="darkToolbarTextColor">@color/white_color</item>
|
||||
|
||||
<item name="button_background_drawable">@drawable/button_background_dark</item>
|
||||
</style>
|
||||
|
||||
<!-- Assistant related -->
|
||||
|
||||
<style name="assistant_input_field_error_font" parent="@android:style/TextAppearance.Small">
|
||||
|
|
Loading…
Reference in a new issue