adjusted the secret night mode switch behavior

This commit is contained in:
y20k 2019-01-02 15:37:56 +01:00
parent 705d38651e
commit 2750533e5c
9 changed files with 92 additions and 46 deletions

View file

@ -534,11 +534,16 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
mainActivityMapFragment.handleShowMyLocation();
}
});
// secret night mode switch
mFloatingActionButtonLocation.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
longPressFeedback(R.string.toast_message_long_press_night_mode_switch);
NightModeHelper.switchToOpposite(MainActivity.this);
NightModeHelper.switchMode(MainActivity.this);
// vibrate 50 milliseconds
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(50);
// recreate activity
recreate();
return true;
}

View file

@ -17,13 +17,10 @@
package org.y20k.trackbook;
import android.app.Application;
import android.os.Build;
import org.y20k.trackbook.helpers.LogHelper;
import org.y20k.trackbook.helpers.NightModeHelper;
import androidx.appcompat.app.AppCompatDelegate;
/**
* Trackbook.class
@ -39,13 +36,16 @@ public class Trackbook extends Application {
super.onCreate();
// set Day / Night theme state
if (Build.VERSION.SDK_INT >= 28) {
// Android P might introduce a system wide theme option - in that case: follow system (28 = Build.VERSION_CODES.P)
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
} else {
// try t0 get last state the user chose
NightModeHelper.restoreSavedState(this);
}
NightModeHelper.restoreSavedState(this);
// todo remove
// if (Build.VERSION.SDK_INT >= 28) {
// // Android P might introduce a system wide theme option - in that case: follow system (28 = Build.VERSION_CODES.P)
// AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
// } else {
// // try to get last state the user chose
// NightModeHelper.restoreSavedState(this);
// }
}

View file

@ -23,6 +23,9 @@ import android.content.res.Configuration;
import android.os.Build;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Toast;
import org.y20k.trackbook.R;
import androidx.appcompat.app.AppCompatDelegate;
@ -36,23 +39,24 @@ public final class NightModeHelper implements TrackbookKeys {
private static final String LOG_TAG = NightModeHelper.class.getSimpleName();
/* Switches to opposite theme */
public static void switchToOpposite(Activity activity) {
switch (getCurrentNightModeState(activity)) {
case Configuration.UI_MODE_NIGHT_NO:
// night mode is currently not active - turn on night mode
/* Switches between modes: day, night, undefined */
public static void switchMode(Activity activity) {
// SWITCH: undefined -> night / night -> day / day - undefined
switch (AppCompatDelegate.getDefaultNightMode()) {
case AppCompatDelegate.MODE_NIGHT_NO:
// currently: day mode -> switch to: follow system
displayDefaultStatusBar(activity); // necessary hack :-/
activateNightMode(activity);
activateFollowSystemMode(activity, true);
break;
case Configuration.UI_MODE_NIGHT_YES:
// night mode is currently active - turn off night mode
case AppCompatDelegate.MODE_NIGHT_YES:
// currently: night mode -> switch to: day mode
displayLightStatusBar(activity); // necessary hack :-/
deactivateNightMode(activity);
activateDayMode(activity, true);
break;
case Configuration.UI_MODE_NIGHT_UNDEFINED:
// don't know what mode is active - turn off night mode
default:
// currently: follow system / undefined -> switch to: day mode
displayLightStatusBar(activity); // necessary hack :-/
deactivateNightMode(activity);
activateNightMode(activity, true);
break;
}
}
@ -61,20 +65,20 @@ public final class NightModeHelper implements TrackbookKeys {
/* Sets night mode / dark theme */
public static void restoreSavedState(Context context) {
int savedNightModeState = loadNightModeState(context);
int currentNightModeState = getCurrentNightModeState(context);
if (savedNightModeState != -1 && savedNightModeState != currentNightModeState) {
int currentNightModeState = AppCompatDelegate.getDefaultNightMode();
if (savedNightModeState != currentNightModeState) {
switch (savedNightModeState) {
case Configuration.UI_MODE_NIGHT_NO:
// turn off night mode
deactivateNightMode(context);
case AppCompatDelegate.MODE_NIGHT_NO:
// turn on day mode
activateDayMode(context, false);
break;
case Configuration.UI_MODE_NIGHT_YES:
case AppCompatDelegate.MODE_NIGHT_YES:
// turn on night mode
activateNightMode(context);
activateNightMode(context, false);
break;
case Configuration.UI_MODE_NIGHT_UNDEFINED:
// turn off night mode
deactivateNightMode(context);
default:
// turn on mode "follow system"
activateFollowSystemMode(context, false);
break;
}
}
@ -88,21 +92,46 @@ public final class NightModeHelper implements TrackbookKeys {
/* Activates Night Mode */
private static void activateNightMode(Context context) {
saveNightModeState(context, Configuration.UI_MODE_NIGHT_YES);
private static void activateNightMode(Context context, Boolean notifyUser) {
saveNightModeState(context, AppCompatDelegate.MODE_NIGHT_YES);
// switch to Night Mode
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
// notify user
if (notifyUser) {
Toast.makeText(context, context.getText(R.string.toast_message_theme_night), Toast.LENGTH_SHORT).show();
}
}
/* Deactivates Night Mode */
private static void deactivateNightMode(Context context) {
/* Activates Day Mode */
private static void activateDayMode(Context context, Boolean notifyUser) {
// save the new state
saveNightModeState(context, Configuration.UI_MODE_NIGHT_NO);
saveNightModeState(context, AppCompatDelegate.MODE_NIGHT_NO);
// switch to Day Mode
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
// notify user
if (notifyUser) {
Toast.makeText(context, context.getText(R.string.toast_message_theme_day), Toast.LENGTH_LONG).show();
}
}
/* Activate Mode "Follow System" */
private static void activateFollowSystemMode(Context context, Boolean notifyUser) {
// save the new state
saveNightModeState(context, AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
// switch to Undefined Mode / Follow System
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
// notify user
if (notifyUser) {
Toast.makeText(context, context.getText(R.string.toast_message_theme_follow_system), Toast.LENGTH_LONG).show();
}
}
@ -135,7 +164,7 @@ public final class NightModeHelper implements TrackbookKeys {
/* Load state of Night Mode */
private static int loadNightModeState(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context).getInt(PREF_NIGHT_MODE_STATE, -1);
return PreferenceManager.getDefaultSharedPreferences(context).getInt(PREF_NIGHT_MODE_STATE, AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
}
}

View file

@ -63,8 +63,10 @@
<string name="toast_message_export_success">GPX-Export erfolgreich:</string>
<string name="toast_message_export_fail">GPX-Export fehlgeschlagen:</string>
<string name="toast_message_elevation_info">Hinweis: Die Genauogkeit der Höhenmeter-Werte ist geräteabhängig. Gemessen werden die Steigungen und Gefälle der Gesamtstrecke.</string>
<string name="toast_message_long_press_night_mode_switch">Schalter für Nachtmodus (Längeres Drücken erkannt)</string>
<string name="toast_message_install_file_helper">Bitte zunächst einen Dateimanager oder GPX-Betrachter installieren.</string>
<string name="toast_message_theme_night">Nachtmodus aktiviert (Längeres Drücken erkannt)</string>
<string name="toast_message_theme_day">Tagmodus aktiviert (Längeres Drücken erkannt)</string>
<string name="toast_message_theme_follow_system">Modus Systemeinstellung Beachten aktiviert (Längeres Drücken erkannt)</string>
<!-- map markers -->
<string name="marker_description_source">Quelle</string>

View file

@ -63,8 +63,10 @@
<string name="toast_message_export_success">GPX export successful:</string>
<string name="toast_message_export_fail">GPX export failed:</string>
<string name="toast_message_elevation_info">Hint: The accuracy of elevation data depends on your device. The uphill and downhill elevation of the whole route is measured.</string>
<string name="toast_message_long_press_night_mode_switch">Night mode switch (long press detected)</string>
<string name="toast_message_install_file_helper">Please install a file manager or a GPX track viewer first.</string>
<string name="toast_message_theme_night">Switching to Night mode (long press detected)</string>
<string name="toast_message_theme_day">Switching to Day mode (long press detected)</string>
<string name="toast_message_theme_follow_system">Switching to Follow System Setting mode (long press detected)</string>
<!-- map markers -->
<string name="marker_description_source">Source</string>

View file

@ -63,8 +63,10 @@
<string name="toast_message_export_success">L\'esportazione GPX è riuscita:</string>
<string name="toast_message_export_fail">L\'esportazione GPX non è riuscita:</string>
<string name="toast_message_elevation_info">Hint: The accuracy of elevation data depends on your device. The uphill and downhill elevation of the whole route is measured.</string>
<string name="toast_message_long_press_night_mode_switch">Night mode switch (long press detected)</string>
<string name="toast_message_install_file_helper">Please install a file manager or a GPX track viewer first.</string>
<string name="toast_message_theme_night">Switching to Night mode (long press detected)</string>
<string name="toast_message_theme_day">Switching to Day mode (long press detected)</string>
<string name="toast_message_theme_follow_system">Switching to Follow System Setting mode (long press detected)</string>
<!-- map markers -->
<string name="marker_description_source">Fonte</string>

View file

@ -63,8 +63,10 @@
<string name="toast_message_export_success">GPX-eksportering vellykket:</string>
<string name="toast_message_export_fail">GPX-eksportering mislyktes:</string>
<string name="toast_message_elevation_info">Hint: Høydedataens nøyaktighet avhenger av enheten din. Opp og ned-stigningen for hele ruten måles.</string>
<string name="toast_message_long_press_night_mode_switch">Nattmodusveksling (langt trykk oppdaget)</string>
<string name="toast_message_install_file_helper">Please install a file manager or a GPX track viewer first.</string>
<string name="toast_message_theme_night">Switching to Night mode (long press detected)</string>
<string name="toast_message_theme_day">Switching to Day mode (long press detected)</string>
<string name="toast_message_theme_follow_system">Switching to Follow System Setting mode (long press detected)</string>
<!-- map markers -->
<string name="marker_description_source">Kilde</string>

View file

@ -63,8 +63,10 @@
<string name="toast_message_export_success">GPX export successful:</string>
<string name="toast_message_export_fail">GPX export failed:</string>
<string name="toast_message_elevation_info">Hint: The accuracy of elevation data depends on your device. The uphill and downhill elevation of the whole route is measured.</string>
<string name="toast_message_long_press_night_mode_switch">Night mode switch (long press detected)</string>
<string name="toast_message_install_file_helper">Please install a file manager or a GPX track viewer first.</string>
<string name="toast_message_theme_night">Switching to Night mode (long press detected)</string>
<string name="toast_message_theme_day">Switching to Day mode (long press detected)</string>
<string name="toast_message_theme_follow_system">Switching to Follow System Setting mode (long press detected)</string>
<!-- map markers -->
<string name="marker_description_source">Bron</string>

View file

@ -63,8 +63,10 @@
<string name="toast_message_export_success">GPX export successful:</string>
<string name="toast_message_export_fail">GPX export failed:</string>
<string name="toast_message_elevation_info">Hint: The accuracy of elevation data depends on your device. The uphill and downhill elevation of the whole route is measured.</string>
<string name="toast_message_long_press_night_mode_switch">Night mode switch (long press detected)</string>
<string name="toast_message_install_file_helper">Please install a file manager or a GPX track viewer first.</string>
<string name="toast_message_theme_night">Switching to Night mode (long press detected)</string>
<string name="toast_message_theme_day">Switching to Day mode (long press detected)</string>
<string name="toast_message_theme_follow_system">Switching to Follow System Setting mode (long press detected)</string>
<!-- map markers -->
<string name="marker_description_source">Source</string>