From f9fd55b369302005655a13344b2e632f44df43d0 Mon Sep 17 00:00:00 2001 From: Billy Brawner Date: Sat, 18 May 2019 08:49:03 -0700 Subject: [PATCH] Fix dark mode application on devices lower than Q --- app/build.gradle | 8 +-- .../view/activity/MainActivity.kt | 4 -- .../view/activity/SplashActivity.java | 34 +++++++++---- .../view/fragment/SettingsFragment.kt | 51 +++++++++++-------- app/src/main/res/menu/menu_explorer.xml | 16 ------ app/src/main/res/values/strings.xml | 32 ++++++------ app/src/main/res/xml/pref_general.xml | 22 ++++---- 7 files changed, 84 insertions(+), 83 deletions(-) delete mode 100644 app/src/main/res/menu/menu_explorer.xml diff --git a/app/build.gradle b/app/build.gradle index 0b6637c..c877b71 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -19,7 +19,7 @@ android { exclude 'META-INF/LICENSE' exclude 'META-INF/DEPENDENCIES' } - compileSdkVersion 'android-Q' + compileSdkVersion 28 buildToolsVersion '28.0.3' compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 @@ -27,8 +27,8 @@ android { } defaultConfig { applicationId "com.wbrawner.simplemarkdown" - minSdkVersion 20 - targetSdkVersion 'Q' + minSdkVersion 21 + targetSdkVersion 28 multiDexEnabled true versionCode 18 versionName "0.6.0" @@ -93,7 +93,7 @@ dependencies { implementation 'androidx.multidex:multidex:2.0.1' implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version" kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version" - compile "androidx.core:core-ktx:1.0.2" + implementation "androidx.core:core-ktx:1.0.2" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0' } diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/MainActivity.kt b/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/MainActivity.kt index ef6bf4f..e337714 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/MainActivity.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/MainActivity.kt @@ -44,10 +44,6 @@ class MainActivity : AppCompatActivity(), ActivityCompat.OnRequestPermissionsRes or View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN ) -// setOnApplyWindowInsetsListener { v, insets -> -// pager.updatePadding(top = insets.systemWindowInsetTop) -// insets -// } } (application as MarkdownApplication).component.inject(this) pager.adapter = EditPagerAdapter(supportFragmentManager, this@MainActivity) diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/SplashActivity.java b/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/SplashActivity.java index 25a5f9c..2c42837 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/SplashActivity.java +++ b/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/SplashActivity.java @@ -2,18 +2,19 @@ package com.wbrawner.simplemarkdown.view.activity; import android.content.Intent; import android.content.SharedPreferences; +import android.os.Build; import android.os.Bundle; import android.preference.PreferenceManager; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.app.AppCompatDelegate; import com.crashlytics.android.Crashlytics; import com.wbrawner.simplemarkdown.BuildConfig; import com.wbrawner.simplemarkdown.MarkdownApplication; import com.wbrawner.simplemarkdown.R; import com.wbrawner.simplemarkdown.presentation.MarkdownPresenter; -import com.wbrawner.simplemarkdown.utility.Constants; import com.wbrawner.simplemarkdown.utility.Utils; import javax.inject.Inject; @@ -35,6 +36,27 @@ public class SplashActivity extends AppCompatActivity { } ((MarkdownApplication) getApplication()).getComponent().inject(this); + String darkModeValue = sharedPreferences.getString( + getString(R.string.pref_key_dark_mode), + getString(R.string.pref_value_auto) + ); + + int darkMode; + if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) { + darkMode = AppCompatDelegate.MODE_NIGHT_AUTO; + } else { + darkMode = AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM; + } + + if (darkModeValue != null && !darkModeValue.isEmpty()) { + if (darkModeValue.equalsIgnoreCase(getString(R.string.pref_value_light))) { + darkMode = AppCompatDelegate.MODE_NIGHT_NO; + } else if (darkModeValue.equalsIgnoreCase(getString(R.string.pref_value_dark))) { + darkMode = AppCompatDelegate.MODE_NIGHT_YES; + } + } + AppCompatDelegate.setDefaultNightMode(darkMode); + String defaultName = Utils.getDefaultFileName(this); Intent intent = getIntent(); @@ -45,16 +67,6 @@ public class SplashActivity extends AppCompatActivity { } Intent startIntent = new Intent(this, MainActivity.class); - String startScreen = PreferenceManager.getDefaultSharedPreferences(this) - .getString( - getString(R.string.key_default_view), - Constants.VALUE_EDIT_VIEW - ); - switch (startScreen) { - case Constants.VALUE_FILE_VIEW: - startIntent.putExtra(Constants.EXTRA_EXPLORER, true); - break; - } startActivity(startIntent); finish(); } diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/SettingsFragment.kt b/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/SettingsFragment.kt index 3f34877..0657d61 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/SettingsFragment.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/SettingsFragment.kt @@ -1,16 +1,16 @@ package com.wbrawner.simplemarkdown.view.fragment import android.content.SharedPreferences +import android.os.Build import android.os.Bundle import android.preference.ListPreference import android.preference.Preference import android.preference.PreferenceFragment import android.preference.PreferenceManager -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup - +import androidx.appcompat.app.AppCompatDelegate import com.wbrawner.simplemarkdown.BuildConfig import com.wbrawner.simplemarkdown.R @@ -22,42 +22,51 @@ class SettingsFragment : PreferenceFragment(), SharedPreferences.OnSharedPrefere sharedPreferences.registerOnSharedPreferenceChangeListener(this) setListPreferenceSummary( sharedPreferences, - findPreference(getString(R.string.key_default_view)) + findPreference(getString(R.string.pref_key_dark_mode)) ) if (!BuildConfig.ENABLE_CUSTOM_CSS) { preferenceScreen.removePreference(findPreference(getString(R.string.pref_custom_css))) } } - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, - savedInstanceState: Bundle?): View? { - + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { return inflater.inflate(R.layout.preference_list_fragment_safe, container, false) } override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) { + if (!isAdded) return val preference = findPreference(key) - if (preference is ListPreference) { setListPreferenceSummary(sharedPreferences, preference) } + if (preference.key == getString(R.string.pref_key_dark_mode)) { + var darkMode: Int = if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) { + AppCompatDelegate.MODE_NIGHT_AUTO + } else { + AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM + } + val darkModeValue = sharedPreferences.getString(preference.key, null) + if (darkModeValue != null && !darkModeValue.isEmpty()) { + if (darkModeValue.equals(getString(R.string.pref_value_light), ignoreCase = true)) { + darkMode = AppCompatDelegate.MODE_NIGHT_NO + } else if (darkModeValue.equals(getString(R.string.pref_value_dark), ignoreCase = true)) { + darkMode = AppCompatDelegate.MODE_NIGHT_YES + } + } + AppCompatDelegate.setDefaultNightMode(darkMode) + activity?.recreate() + } } private fun setListPreferenceSummary(sharedPreferences: SharedPreferences, preference: Preference) { val listPreference = preference as ListPreference - val storedValue = sharedPreferences.getString(preference.getKey(), "") - if (storedValue!!.isEmpty()) { - return - } - var index = 0 - try { - index = Integer.valueOf(storedValue) - } catch (e: NumberFormatException) { - // TODO: Report this? - Log.e("SimpleMarkdown", "Unable to parse $storedValue to integer") - } - - val summary = listPreference.entries[index].toString() - preference.setSummary(summary) + val storedValue = sharedPreferences.getString(preference.getKey(), null) ?: return + val index = listPreference.findIndexOfValue(storedValue) + if (index < 0) return + preference.setSummary(listPreference.entries[index].toString()) } } diff --git a/app/src/main/res/menu/menu_explorer.xml b/app/src/main/res/menu/menu_explorer.xml deleted file mode 100644 index b94358b..0000000 --- a/app/src/main/res/menu/menu_explorer.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 37ee8e6..1b15902 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -12,7 +12,6 @@ Markdown here… Save Share - File name Export Unable to share file - no capable apps installed Share file to… @@ -26,9 +25,7 @@ An error occurred while writing the file An error occurred while opening the file Libraries - ExplorerActivity New - Use SD Card Done Open Yes @@ -37,14 +34,7 @@ Enable autosave Automatically save files when closing the app Lock Swiping - defaultView - Decide what you see first when you open the app - Default View - Edit View - File Explorer View - Default Root Directory Select - \u2191 Go up Privacy crashlytics.enable Enable automated error reports @@ -58,12 +48,22 @@ pre {overflow:scroll; padding:15px; background: #F1F1F1;} body{background: #000000;color: #F1F1F1;}a{color:#7b91ff;}pre{background:#111111;} - - @string/value_edit_view - @string/value_explorer_view + darkMode + Dark Mode + Light + Dark + Auto + light + dark + auto + + @string/pref_value_light + @string/pref_value_dark + @string/pref_value_auto - - 0 - 1 + + @string/pref_key_dark_mode_light + @string/pref_key_dark_mode_dark + @string/pref_key_dark_mode_auto diff --git a/app/src/main/res/xml/pref_general.xml b/app/src/main/res/xml/pref_general.xml index 32f6fdc..0aff4b7 100644 --- a/app/src/main/res/xml/pref_general.xml +++ b/app/src/main/res/xml/pref_general.xml @@ -3,25 +3,25 @@ + + android:entries="@array/pref_entries_dark_mode" + android:entryValues="@array/pref_values_dark_mode" + android:defaultValue="@string/pref_value_auto" + android:key="@string/pref_key_dark_mode" + android:title="@string/title_dark_mode" /> -