Add new Theme "Use System Design" + fixes to night mode (#1069)

* Add theme system

A theme which follows system design.
See: https://www.xda-developers.com/samsung-galaxy-s9-update-night-mode-schedule/

* update

to be in line with https://github.com/tuskyapp/Tusky/pull/1060/files

* Update ThemeUtils.java

* update

* Cleanup

* Update Deps

* Cleanup

* Update PreferencesActivity.kt

* Workaround to make MODE_NIGHT_FOLLOW_SYSTEM work

* Update ThemeUtils.java

* Use ThemeUtils.THEME_SYSTEM

* Update SplashActivity.kt

* Update strings.xml

* Update Deps

* Update build.gradle

* Update build.gradle

* fix tests
This commit is contained in:
Bernd 2019-03-07 21:33:29 +01:00 committed by Konrad Pozniak
commit 507ffb1b41
9 changed files with 45 additions and 41 deletions

View file

@ -56,6 +56,8 @@ public abstract class BaseActivity extends AppCompatActivity implements Injectab
@Inject
public AccountManager accountManager;
ThemeUtils themeUtils = new ThemeUtils();
protected static final int PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 1;
@Override
@ -72,7 +74,8 @@ public abstract class BaseActivity extends AppCompatActivity implements Injectab
if (theme.equals("black")) {
setTheme(R.style.TuskyBlackTheme);
}
ThemeUtils.setAppNightMode(theme, this);
themeUtils.setAppNightMode(theme, this);
/* set the taskdescription programmatically, the theme would turn it blue */
String appName = getString(R.string.app_name);

View file

@ -34,6 +34,7 @@ import dagger.android.support.HasSupportFragmentInjector
import kotlinx.android.synthetic.main.toolbar_basic.*
import java.lang.IllegalArgumentException
import javax.inject.Inject
import androidx.appcompat.app.AppCompatDelegate
class PreferencesActivity : BaseActivity(), SharedPreferences.OnSharedPreferenceChangeListener, HasSupportFragmentInjector {
@ -123,7 +124,7 @@ class PreferencesActivity : BaseActivity(), SharedPreferences.OnSharedPreference
"appTheme" -> {
val theme = sharedPreferences.getNonNullString("appTheme", ThemeUtils.APP_THEME_DEFAULT)
Log.d("activeTheme", theme)
ThemeUtils.setAppNightMode(theme, this)
ThemeUtils().setAppNightMode(theme, this)
restartActivitiesOnExit = true
// recreate() could be used instead, but it doesn't have an animation B).
@ -135,7 +136,13 @@ class PreferencesActivity : BaseActivity(), SharedPreferences.OnSharedPreference
finish()
overridePendingTransition(R.anim.fade_in, R.anim.fade_out)
restartActivitiesOnExit = true
// MODE_NIGHT_FOLLOW_SYSTEM workaround part 2 :/
when(theme){
ThemeUtils.THEME_SYSTEM -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
}
}
//workaround end
}
"statusTextSize" -> {

View file

@ -17,18 +17,10 @@ package com.keylesspalace.tusky
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.keylesspalace.tusky.db.AccountManager
import com.keylesspalace.tusky.di.Injectable
import com.keylesspalace.tusky.util.NotificationHelper
import javax.inject.Inject
class SplashActivity : AppCompatActivity(), Injectable {
@Inject
lateinit var accountManager: AccountManager
class SplashActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

View file

@ -106,14 +106,12 @@ public class ConversationViewHolder extends StatusBaseViewHolder {
private void setConversationName(List<ConversationAccountEntity> accounts) {
Context context = conversationNameTextView.getContext();
String conversationName;
if(accounts.size() == 0) {
conversationName = " ";
}else if(accounts.size() == 1) {
String conversationName = "";
if(accounts.size() == 1) {
conversationName = context.getString(R.string.conversation_1_recipients, accounts.get(0).getUsername());
} else if(accounts.size() == 2) {
conversationName = context.getString(R.string.conversation_2_recipients, accounts.get(0).getUsername(), accounts.get(1).getUsername());
} else {
} else if (accounts.size() > 2){
conversationName = context.getString(R.string.conversation_more_recipients, accounts.get(0).getUsername(), accounts.get(1).getUsername(), accounts.size() - 2);
}

View file

@ -15,12 +15,10 @@
package com.keylesspalace.tusky.util;
import android.app.UiModeManager;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Build;
import androidx.annotation.AttrRes;
import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
@ -28,8 +26,8 @@ import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDelegate;
import android.provider.Settings;
import android.util.TypedValue;
import android.widget.ImageView;
/**
* Provides runtime compatibility to obtain theme information and re-theme views, especially where
@ -42,6 +40,7 @@ public class ThemeUtils {
private static final String THEME_DAY = "day";
private static final String THEME_BLACK = "black";
private static final String THEME_AUTO = "auto";
public static final String THEME_SYSTEM = "auto_system";
public static Drawable getDrawable(@NonNull Context context, @AttrRes int attribute,
@DrawableRes int fallbackDrawable) {
@ -85,10 +84,6 @@ public class ThemeUtils {
ResourcesUtils.getResourceIdentifier(context, "attr", name));
}
public static void setImageViewTint(ImageView view, @AttrRes int attribute) {
view.setColorFilter(getColor(view.getContext(), attribute), PorterDuff.Mode.SRC_IN);
}
/** this can be replaced with drawableTint in xml once minSdkVersion >= 23 */
public static @Nullable Drawable getTintedDrawable(@NonNull Context context, @DrawableRes int drawableId, @AttrRes int colorAttr) {
Drawable drawable = context.getDrawable(drawableId);
@ -103,30 +98,31 @@ public class ThemeUtils {
drawable.setColorFilter(getColor(context, attribute), PorterDuff.Mode.SRC_IN);
}
public static void setAppNightMode(String flavor, Context context) {
int mode;
public void setAppNightMode(String flavor, Context context) {
switch (flavor) {
default:
case THEME_NIGHT:
mode = UiModeManager.MODE_NIGHT_YES;
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
break;
case THEME_DAY:
mode = UiModeManager.MODE_NIGHT_NO;
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
break;
case THEME_BLACK:
mode = UiModeManager.MODE_NIGHT_YES;
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
break;
case THEME_AUTO:
mode = UiModeManager.MODE_NIGHT_AUTO;
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
break;
case THEME_SYSTEM:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
//stupid workaround to make MODE_NIGHT_FOLLOW_SYSTEM work :(
if((Settings.System.getInt(context.getContentResolver(), "display_night_theme", 0) == 1)) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else if ((Settings.System.getInt(context.getContentResolver(), "display_night_theme", 0) == 0)) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
break;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
UiModeManager uiModeManager = (UiModeManager)context.getApplicationContext().getSystemService(Context.UI_MODE_SERVICE);
uiModeManager.setNightMode(mode);
} else {
AppCompatDelegate.setDefaultNightMode(mode);
}
}
}