From cbcb8b4b89061293582deaebbac2752fca6658d4 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Tue, 29 Dec 2020 01:37:20 +1000 Subject: [PATCH] Android: Add dark theme --- .../stenzek/duckstation/MainActivity.java | 32 +++++++++++++++++-- .../app/src/main/res/values-night/colors.xml | 8 +++++ android/app/src/main/res/values/arrays.xml | 10 ++++++ android/app/src/main/res/values/colors.xml | 4 +-- android/app/src/main/res/values/strings.xml | 1 + android/app/src/main/res/values/styles.xml | 8 ++--- .../src/main/res/xml/general_preferences.xml | 8 +++++ 7 files changed, 63 insertions(+), 8 deletions(-) create mode 100644 android/app/src/main/res/values-night/colors.xml diff --git a/android/app/src/main/java/com/github/stenzek/duckstation/MainActivity.java b/android/app/src/main/java/com/github/stenzek/duckstation/MainActivity.java index c0af5169a..8192a5be0 100644 --- a/android/app/src/main/java/com/github/stenzek/duckstation/MainActivity.java +++ b/android/app/src/main/java/com/github/stenzek/duckstation/MainActivity.java @@ -9,6 +9,7 @@ import android.content.pm.PackageManager; import android.content.res.Configuration; import android.content.res.Resources; import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.util.Log; import android.view.Gravity; @@ -22,6 +23,7 @@ import android.widget.Toast; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.app.AppCompatDelegate; import androidx.appcompat.widget.Toolbar; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; @@ -40,6 +42,7 @@ public class MainActivity extends AppCompatActivity { private static final int REQUEST_ADD_DIRECTORY_TO_GAME_LIST = 2; private static final int REQUEST_IMPORT_BIOS_IMAGE = 3; private static final int REQUEST_START_FILE = 4; + private static final int REQUEST_SETTINGS = 5; private GameList mGameList; private ListView mGameListView; @@ -64,6 +67,26 @@ public class MainActivity extends AppCompatActivity { res.updateConfiguration(config, res.getDisplayMetrics()); } + private void setTheme() { + String theme = PreferenceManager.getDefaultSharedPreferences(this).getString("Main/Theme", "follow_system"); + if (theme == null) + return; + + if (theme.equals("follow_system")) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) + AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM); + } else if (theme.equals("light")) { + AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); + } else if (theme.equals("dark")) { + AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); + } + } + + private void loadSettings() { + setLanguage(); + setTheme(); + } + private boolean shouldResumeStateByDefault() { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); return prefs.getBoolean("Main/SaveStateOnExit", true); @@ -81,7 +104,7 @@ public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setLanguage(); + loadSettings(); setContentView(R.layout.activity_main); Toolbar toolbar = findViewById(R.id.toolbar); @@ -193,7 +216,7 @@ public class MainActivity extends AppCompatActivity { importBIOSImage(); } else if (id == R.id.action_settings) { Intent intent = new Intent(this, SettingsActivity.class); - startActivity(intent); + startActivityForResult(intent, REQUEST_SETTINGS); return true; } else if (id == R.id.action_show_version) { showVersion(); @@ -287,6 +310,11 @@ public class MainActivity extends AppCompatActivity { startEmulation(path, shouldResumeStateByDefault()); } break; + + case REQUEST_SETTINGS: { + loadSettings(); + } + break; } } diff --git a/android/app/src/main/res/values-night/colors.xml b/android/app/src/main/res/values-night/colors.xml new file mode 100644 index 000000000..523e487ed --- /dev/null +++ b/android/app/src/main/res/values-night/colors.xml @@ -0,0 +1,8 @@ + + + #000000 + #000000 + #03A9F4 + + #66000000 + diff --git a/android/app/src/main/res/values/arrays.xml b/android/app/src/main/res/values/arrays.xml index 72d1c53e5..9fc39365c 100644 --- a/android/app/src/main/res/values/arrays.xml +++ b/android/app/src/main/res/values/arrays.xml @@ -440,4 +440,14 @@ it-IT nl-NL + + Follow System + Light + Dark + + + follow_system + light + dark + diff --git a/android/app/src/main/res/values/colors.xml b/android/app/src/main/res/values/colors.xml index cbd5bb00e..a0ede428c 100644 --- a/android/app/src/main/res/values/colors.xml +++ b/android/app/src/main/res/values/colors.xml @@ -1,7 +1,7 @@ - #008577 - #00574B + #6200EE + #3700B3 #D81B60 #66000000 diff --git a/android/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xml index 17723f2fb..9c19b87b4 100644 --- a/android/app/src/main/res/values/strings.xml +++ b/android/app/src/main/res/values/strings.xml @@ -159,4 +159,5 @@ Stop Editing Reset Layout Touchscreen controller is not active. + Theme diff --git a/android/app/src/main/res/values/styles.xml b/android/app/src/main/res/values/styles.xml index 6d6374064..dd376a7fd 100644 --- a/android/app/src/main/res/values/styles.xml +++ b/android/app/src/main/res/values/styles.xml @@ -1,10 +1,10 @@ - @@ -15,7 +15,7 @@