Fix dark mode application on devices lower than Q

This commit is contained in:
Billy Brawner 2019-05-18 08:49:03 -07:00 committed by William Brawner
parent 8713c1e5e6
commit f9fd55b369
7 changed files with 84 additions and 83 deletions

View file

@ -19,7 +19,7 @@ android {
exclude 'META-INF/LICENSE' exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES' exclude 'META-INF/DEPENDENCIES'
} }
compileSdkVersion 'android-Q' compileSdkVersion 28
buildToolsVersion '28.0.3' buildToolsVersion '28.0.3'
compileOptions { compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8 sourceCompatibility JavaVersion.VERSION_1_8
@ -27,8 +27,8 @@ android {
} }
defaultConfig { defaultConfig {
applicationId "com.wbrawner.simplemarkdown" applicationId "com.wbrawner.simplemarkdown"
minSdkVersion 20 minSdkVersion 21
targetSdkVersion 'Q' targetSdkVersion 28
multiDexEnabled true multiDexEnabled true
versionCode 18 versionCode 18
versionName "0.6.0" versionName "0.6.0"
@ -93,7 +93,7 @@ dependencies {
implementation 'androidx.multidex:multidex:2.0.1' implementation 'androidx.multidex:multidex:2.0.1'
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version" implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
kapt "androidx.lifecycle:lifecycle-compiler:$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 "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0' implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
} }

View file

@ -44,10 +44,6 @@ class MainActivity : AppCompatActivity(), ActivityCompat.OnRequestPermissionsRes
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
) )
// setOnApplyWindowInsetsListener { v, insets ->
// pager.updatePadding(top = insets.systemWindowInsetTop)
// insets
// }
} }
(application as MarkdownApplication).component.inject(this) (application as MarkdownApplication).component.inject(this)
pager.adapter = EditPagerAdapter(supportFragmentManager, this@MainActivity) pager.adapter = EditPagerAdapter(supportFragmentManager, this@MainActivity)

View file

@ -2,18 +2,19 @@ package com.wbrawner.simplemarkdown.view.activity;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import com.crashlytics.android.Crashlytics; import com.crashlytics.android.Crashlytics;
import com.wbrawner.simplemarkdown.BuildConfig; import com.wbrawner.simplemarkdown.BuildConfig;
import com.wbrawner.simplemarkdown.MarkdownApplication; import com.wbrawner.simplemarkdown.MarkdownApplication;
import com.wbrawner.simplemarkdown.R; import com.wbrawner.simplemarkdown.R;
import com.wbrawner.simplemarkdown.presentation.MarkdownPresenter; import com.wbrawner.simplemarkdown.presentation.MarkdownPresenter;
import com.wbrawner.simplemarkdown.utility.Constants;
import com.wbrawner.simplemarkdown.utility.Utils; import com.wbrawner.simplemarkdown.utility.Utils;
import javax.inject.Inject; import javax.inject.Inject;
@ -35,6 +36,27 @@ public class SplashActivity extends AppCompatActivity {
} }
((MarkdownApplication) getApplication()).getComponent().inject(this); ((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); String defaultName = Utils.getDefaultFileName(this);
Intent intent = getIntent(); Intent intent = getIntent();
@ -45,16 +67,6 @@ public class SplashActivity extends AppCompatActivity {
} }
Intent startIntent = new Intent(this, MainActivity.class); 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); startActivity(startIntent);
finish(); finish();
} }

View file

@ -1,16 +1,16 @@
package com.wbrawner.simplemarkdown.view.fragment package com.wbrawner.simplemarkdown.view.fragment
import android.content.SharedPreferences import android.content.SharedPreferences
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.preference.ListPreference import android.preference.ListPreference
import android.preference.Preference import android.preference.Preference
import android.preference.PreferenceFragment import android.preference.PreferenceFragment
import android.preference.PreferenceManager import android.preference.PreferenceManager
import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.appcompat.app.AppCompatDelegate
import com.wbrawner.simplemarkdown.BuildConfig import com.wbrawner.simplemarkdown.BuildConfig
import com.wbrawner.simplemarkdown.R import com.wbrawner.simplemarkdown.R
@ -22,42 +22,51 @@ class SettingsFragment : PreferenceFragment(), SharedPreferences.OnSharedPrefere
sharedPreferences.registerOnSharedPreferenceChangeListener(this) sharedPreferences.registerOnSharedPreferenceChangeListener(this)
setListPreferenceSummary( setListPreferenceSummary(
sharedPreferences, sharedPreferences,
findPreference(getString(R.string.key_default_view)) findPreference(getString(R.string.pref_key_dark_mode))
) )
if (!BuildConfig.ENABLE_CUSTOM_CSS) { if (!BuildConfig.ENABLE_CUSTOM_CSS) {
preferenceScreen.removePreference(findPreference(getString(R.string.pref_custom_css))) preferenceScreen.removePreference(findPreference(getString(R.string.pref_custom_css)))
} }
} }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, override fun onCreateView(
savedInstanceState: Bundle?): View? { inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.preference_list_fragment_safe, container, false) return inflater.inflate(R.layout.preference_list_fragment_safe, container, false)
} }
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) { override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) {
if (!isAdded) return
val preference = findPreference(key) val preference = findPreference(key)
if (preference is ListPreference) { if (preference is ListPreference) {
setListPreferenceSummary(sharedPreferences, preference) 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) { private fun setListPreferenceSummary(sharedPreferences: SharedPreferences, preference: Preference) {
val listPreference = preference as ListPreference val listPreference = preference as ListPreference
val storedValue = sharedPreferences.getString(preference.getKey(), "") val storedValue = sharedPreferences.getString(preference.getKey(), null) ?: return
if (storedValue!!.isEmpty()) { val index = listPreference.findIndexOfValue(storedValue)
return if (index < 0) return
} preference.setSummary(listPreference.entries[index].toString())
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)
} }
} }

View file

@ -1,16 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_select"
android:icon="@drawable/ic_action_select"
android:title="@string/action_select"
android:visible="false"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_use_sdcard"
android:checkable="true"
android:title="@string/action_use_sdcard"
android:visible="false"
app:showAsAction="never" />
</menu>

View file

@ -12,7 +12,6 @@
<string name="markdown_here">Markdown here…</string> <string name="markdown_here">Markdown here…</string>
<string name="action_save">Save</string> <string name="action_save">Save</string>
<string name="action_share">Share</string> <string name="action_share">Share</string>
<string name="hint_filename">File name</string>
<string name="action_export">Export</string> <string name="action_export">Export</string>
<string name="no_shareable_apps">Unable to share file - no capable apps installed</string> <string name="no_shareable_apps">Unable to share file - no capable apps installed</string>
<string name="share_file">Share file to…</string> <string name="share_file">Share file to…</string>
@ -26,9 +25,7 @@
<string name="error_write">An error occurred while writing the file</string> <string name="error_write">An error occurred while writing the file</string>
<string name="file_load_error">An error occurred while opening the file</string> <string name="file_load_error">An error occurred while opening the file</string>
<string name="action_libraries">Libraries</string> <string name="action_libraries">Libraries</string>
<string name="title_activity_explorer">ExplorerActivity</string>
<string name="action_new">New</string> <string name="action_new">New</string>
<string name="action_use_sdcard">Use SD Card</string>
<string name="action_done">Done</string> <string name="action_done">Done</string>
<string name="action_open">Open</string> <string name="action_open">Open</string>
<string name="yes">Yes</string> <string name="yes">Yes</string>
@ -37,14 +34,7 @@
<string name="pref_title_autosave">Enable autosave</string> <string name="pref_title_autosave">Enable autosave</string>
<string name="pref_description_autosave">Automatically save files when closing the app</string> <string name="pref_description_autosave">Automatically save files when closing the app</string>
<string name="action_lock_swipe">Lock Swiping</string> <string name="action_lock_swipe">Lock Swiping</string>
<string name="key_default_view">defaultView</string>
<string name="pref_description_default_view">Decide what you see first when you open the app</string>
<string name="pref_title_default_view">Default View</string>
<string name="value_edit_view">Edit View</string>
<string name="value_explorer_view">File Explorer View</string>
<string name="pref_description_default_root">Default Root Directory</string>
<string name="action_select">Select</string> <string name="action_select">Select</string>
<string name="directory_up">\u2191 Go up</string>
<string name="action_privacy">Privacy</string> <string name="action_privacy">Privacy</string>
<string name="error_reports_enabled">crashlytics.enable</string> <string name="error_reports_enabled">crashlytics.enable</string>
<string name="pref_title_error_reports">Enable automated error reports</string> <string name="pref_title_error_reports">Enable automated error reports</string>
@ -58,12 +48,22 @@
<string name="pref_custom_css_default" translatable="false">pre {overflow:scroll; padding:15px; background: #F1F1F1;}</string> <string name="pref_custom_css_default" translatable="false">pre {overflow:scroll; padding:15px; background: #F1F1F1;}</string>
<string name="pref_custom_css_default_dark" translatable="false">body{background: <string name="pref_custom_css_default_dark" translatable="false">body{background:
#000000;color: #F1F1F1;}a{color:#7b91ff;}pre{background:#111111;}</string> #000000;color: #F1F1F1;}a{color:#7b91ff;}pre{background:#111111;}</string>
<string-array name="pref_entries_default_view"> <string name="pref_key_dark_mode">darkMode</string>
<item>@string/value_edit_view</item> <string name="title_dark_mode">Dark Mode</string>
<item>@string/value_explorer_view</item> <string name="pref_value_light">Light</string>
<string name="pref_value_dark">Dark</string>
<string name="pref_value_auto">Auto</string>
<string name="pref_key_dark_mode_light" translatable="false">light</string>
<string name="pref_key_dark_mode_dark" translatable="false">dark</string>
<string name="pref_key_dark_mode_auto" translatable="false">auto</string>
<string-array name="pref_entries_dark_mode">
<item>@string/pref_value_light</item>
<item>@string/pref_value_dark</item>
<item>@string/pref_value_auto</item>
</string-array> </string-array>
<string-array name="pref_values_default_view"> <string-array name="pref_values_dark_mode">
<item>0</item> <item>@string/pref_key_dark_mode_light</item>
<item>1</item> <item>@string/pref_key_dark_mode_dark</item>
<item>@string/pref_key_dark_mode_auto</item>
</string-array> </string-array>
</resources> </resources>

View file

@ -3,25 +3,25 @@
<SwitchPreference <SwitchPreference
android:defaultValue="true" android:defaultValue="true"
android:key="autosave" android:key="autosave"
android:summaryOn="@string/pref_autosave_on"
android:summaryOff="@string/pref_autosave_off" android:summaryOff="@string/pref_autosave_off"
android:summaryOn="@string/pref_autosave_on"
android:title="@string/pref_title_autosave" /> android:title="@string/pref_title_autosave" />
<EditTextPreference
android:defaultValue="@string/pref_custom_css_default"
android:key="@string/pref_custom_css"
android:summary="@string/pref_description_custom_css"
android:title="@string/pref_title_custom_css" />
<ListPreference <ListPreference
android:defaultValue="@string/value_edit_view" android:entries="@array/pref_entries_dark_mode"
android:entries="@array/pref_entries_default_view" android:entryValues="@array/pref_values_dark_mode"
android:entryValues="@array/pref_values_default_view" android:defaultValue="@string/pref_value_auto"
android:key="@string/key_default_view" android:key="@string/pref_key_dark_mode"
android:title="@string/pref_title_default_view" /> android:title="@string/title_dark_mode" />
<SwitchPreference <SwitchPreference
android:defaultValue="true" android:defaultValue="true"
android:key="@string/error_reports_enabled" android:key="@string/error_reports_enabled"
android:summaryOff="@string/pref_error_reports_off" android:summaryOff="@string/pref_error_reports_off"
android:summaryOn="@string/pref_error_reports_on" android:summaryOn="@string/pref_error_reports_on"
android:title="@string/pref_title_error_reports" /> android:title="@string/pref_title_error_reports" />
<EditTextPreference
android:defaultValue="@string/pref_custom_css_default"
android:key="@string/pref_custom_css"
android:summary="@string/pref_description_custom_css"
android:title="@string/pref_title_custom_css" />
</PreferenceScreen> </PreferenceScreen>