Theming: enable day/night mode incl. app-wide setting.

This commit is contained in:
Dennis Guse
2020-09-06 16:06:13 +02:00
parent 1688076457
commit 4150d060ee
8 changed files with 56 additions and 1 deletions
@@ -6,6 +6,7 @@ import android.util.Log;
import java.util.Locale; import java.util.Locale;
import de.dennisguse.opentracks.util.ActivityUtils;
import de.dennisguse.opentracks.util.PreferencesUtils; import de.dennisguse.opentracks.util.PreferencesUtils;
/** /**
@@ -32,6 +33,8 @@ public class Startup extends Application {
PreferencesUtils.setString(this, R.string.stats_units_key, statsUnits); PreferencesUtils.setString(this, R.string.stats_units_key, statsUnits);
} }
ActivityUtils.applyNightMode(this);
//TODO Workaround to reset recordingTrackId on app startup as the TrackRecordingService (likely) crashed. //TODO Workaround to reset recordingTrackId on app startup as the TrackRecordingService (likely) crashed.
if (PreferencesUtils.isRecording(this)) { if (PreferencesUtils.isRecording(this)) {
Log.e(TAG, "Reset recordingTrackId; likely the TrackRecordingService crashed."); Log.e(TAG, "Reset recordingTrackId; likely the TrackRecordingService crashed.");
@@ -21,6 +21,7 @@ import de.dennisguse.opentracks.io.file.TrackFileFormat;
import de.dennisguse.opentracks.settings.bluetooth.BluetoothLeCyclingCadenceAndSpeedPreference; import de.dennisguse.opentracks.settings.bluetooth.BluetoothLeCyclingCadenceAndSpeedPreference;
import de.dennisguse.opentracks.settings.bluetooth.BluetoothLeHeartRatePreference; import de.dennisguse.opentracks.settings.bluetooth.BluetoothLeHeartRatePreference;
import de.dennisguse.opentracks.settings.bluetooth.BluetoothLeSensorPreference; import de.dennisguse.opentracks.settings.bluetooth.BluetoothLeSensorPreference;
import de.dennisguse.opentracks.util.ActivityUtils;
import de.dennisguse.opentracks.util.BluetoothUtils; import de.dennisguse.opentracks.util.BluetoothUtils;
import de.dennisguse.opentracks.util.HackUtils; import de.dennisguse.opentracks.util.HackUtils;
import de.dennisguse.opentracks.util.PreferencesUtils; import de.dennisguse.opentracks.util.PreferencesUtils;
@@ -65,6 +66,9 @@ public class SettingsActivity extends AppCompatActivity implements ChooseActivit
if (PreferencesUtils.isKey(getActivity(), R.string.stats_units_key, key)) { if (PreferencesUtils.isKey(getActivity(), R.string.stats_units_key, key)) {
getActivity().runOnUiThread(this::updateUnits); getActivity().runOnUiThread(this::updateUnits);
} }
if (PreferencesUtils.isKey(getActivity(), R.string.night_mode_key, key)) {
getActivity().runOnUiThread(() -> ActivityUtils.applyNightMode(getContext()));
}
}; };
// Used to forward update from ChooseActivityTypeDialogFragment; TODO Could be replaced with LiveData. // Used to forward update from ChooseActivityTypeDialogFragment; TODO Could be replaced with LiveData.
@@ -16,6 +16,7 @@ import android.widget.AbsListView;
import android.widget.ListView; import android.widget.ListView;
import android.widget.Toast; import android.widget.Toast;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.appcompat.widget.SearchView; import androidx.appcompat.widget.SearchView;
import java.util.ArrayList; import java.util.ArrayList;
@@ -151,4 +152,7 @@ public class ActivityUtils {
toast.show(); toast.show();
} }
public static void applyNightMode(Context context) {
AppCompatDelegate.setDefaultNightMode(PreferencesUtils.getDefaultNightMode(context));
}
} }
@@ -108,6 +108,8 @@ public class PreferencesUtils {
* @param keyId the key id * @param keyId the key id
* @param value the value * @param value the value
*/ */
//TODO Don't use; this function is only to be used TrackRecordingService and will be removed.
@VisibleForTesting
public static void setBoolean(Context context, int keyId, boolean value) { public static void setBoolean(Context context, int keyId, boolean value) {
SharedPreferences sharedPreferences = getSharedPreferences(context); SharedPreferences sharedPreferences = getSharedPreferences(context);
Editor editor = sharedPreferences.edit(); Editor editor = sharedPreferences.edit();
@@ -162,6 +164,8 @@ public class PreferencesUtils {
* @param keyId the key id * @param keyId the key id
* @param value the value * @param value the value
*/ */
//TODO Don't use; this function is only to be used TrackRecordingService and will be removed.
@Deprecated
@VisibleForTesting @VisibleForTesting
public static void setLong(Context context, int keyId, long value) { public static void setLong(Context context, int keyId, long value) {
SharedPreferences sharedPreferences = getSharedPreferences(context); SharedPreferences sharedPreferences = getSharedPreferences(context);
@@ -189,6 +193,7 @@ public class PreferencesUtils {
* @param keyId the key id * @param keyId the key id
* @param value the value * @param value the value
*/ */
@VisibleForTesting
public static void setString(Context context, int keyId, String value) { public static void setString(Context context, int keyId, String value) {
SharedPreferences sharedPreferences = getSharedPreferences(context); SharedPreferences sharedPreferences = getSharedPreferences(context);
Editor editor = sharedPreferences.edit(); Editor editor = sharedPreferences.edit();
@@ -347,6 +352,16 @@ public class PreferencesUtils {
return getBoolean(context, R.string.import_prevent_reimport_key, defaultValue); return getBoolean(context, R.string.import_prevent_reimport_key, defaultValue);
} }
/**
* @return {@link androidx.appcompat.app.AppCompatDelegate}.MODE_*
*/
public static int getDefaultNightMode(Context context) {
final String defaultValue = getKey(context, R.string.night_mode_default);
final String value = getString(context, R.string.night_mode_key, defaultValue);
return Integer.parseInt(value);
}
@Deprecated //Use TrackRecordingService @Deprecated //Use TrackRecordingService
public static boolean isRecording(Context context) { public static boolean isRecording(Context context) {
return isRecording(getRecordingTrackId(context)); return isRecording(getRecordingTrackId(context));
+17
View File
@@ -221,4 +221,21 @@
<string name="settings_recording_key" translatable="false">settingsRecordingKey</string> <string name="settings_recording_key" translatable="false">settingsRecordingKey</string>
<integer name="buttonDelayMillis">1500</integer> <integer name="buttonDelayMillis">1500</integer>
<!-- androidx.appcompat.app.AppCompatDelegate.setDefaultNightMode() -->
<string name="night_mode_key" translatable="false">nightMode</string>
<string name="night_mode_default" translatable="false">@string/night_mode_no_value</string>
<string name="night_mode_no_value" translatable="false">1</string>
<string name="night_mode_yes_value" translatable="false">2</string>
<string name="night_mode_system_value" translatable="false">-1</string>
<string-array name="night_mode_values">
<item>@string/night_mode_system_value</item>
<item>@string/night_mode_no_value</item>
<item>@string/night_mode_yes_value</item>
</string-array>
<string-array name="night_mode_options">
<item>@string/settings_night_mode_option_system</item>
<item>@string/settings_night_mode_option_day</item>
<item>@string/settings_night_mode_option_night</item>
</string-array>
</resources> </resources>
+5
View File
@@ -371,6 +371,11 @@ limitations under the License.
<string name="settings_recording_track_name_date_local_option">Date (local)</string> <string name="settings_recording_track_name_date_local_option">Date (local)</string>
<string name="settings_recording_track_name_number_option">Number</string> <string name="settings_recording_track_name_number_option">Number</string>
<string name="settings_recording_track_name_title">Default track name</string> <string name="settings_recording_track_name_title">Default track name</string>
<string name="settings_night_mode_title">UI Theme</string>
<string name="settings_night_mode_option_system">System</string>
<string name="settings_night_mode_option_day">Day</string>
<string name="settings_night_mode_option_night">Night</string>
<!-- Settings Reset --> <!-- Settings Reset -->
<string name="settings_reset">Reset</string> <string name="settings_reset">Reset</string>
<string name="settings_reset_confirm_message">All settings will be reverted to the default values. This will not delete any tracks on the device.</string> <string name="settings_reset_confirm_message">All settings will be reverted to the default values. This will not delete any tracks on the device.</string>
+1 -1
View File
@@ -15,7 +15,7 @@ limitations under the License.
--> -->
<resources xmlns:android="http://schemas.android.com/apk/res/android"> <resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="ThemeCustom" parent="Theme.AppCompat.NoActionBar"> <style name="ThemeCustom" parent="Theme.AppCompat.DayNight.NoActionBar">
<!-- TODO Cleanup necessary due to change to appcompat --> <!-- TODO Cleanup necessary due to change to appcompat -->
<!-- Auto generated --> <!-- Auto generated -->
<item name="android:activatedBackgroundIndicator">@drawable/activated_background_holo_dark <item name="android:activatedBackgroundIndicator">@drawable/activated_background_holo_dark
+7
View File
@@ -51,6 +51,13 @@ limitations under the License.
android:defaultValue="@bool/stats_show_coordinate_default" android:defaultValue="@bool/stats_show_coordinate_default"
android:key="@string/stats_show_coordinate_key" android:key="@string/stats_show_coordinate_key"
android:title="@string/settings_stats_coordinate" /> android:title="@string/settings_stats_coordinate" />
<ListPreference
android:key="@string/night_mode_key"
android:title="@string/settings_night_mode_title"
android:defaultValue="@string/night_mode_default"
android:entries="@array/night_mode_options"
android:entryValues="@array/night_mode_values"
app:useSimpleSummaryProvider="true" />
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory <PreferenceCategory