simplifies the initial theme setting (no need for "recreate()")
This commit is contained in:
		
							parent
							
								
									fd8dad3c74
								
							
						
					
					
						commit
						aa8f9e2f2d
					
				
					 4 changed files with 75 additions and 41 deletions
				
			
		|  | @ -17,6 +17,7 @@ | |||
|     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | ||||
| 
 | ||||
|     <application | ||||
|         android:name=".Trackbook" | ||||
|         android:allowBackup="true" | ||||
|         android:fullBackupContent="@xml/backupscheme" | ||||
|         android:icon="@mipmap/ic_launcher" | ||||
|  |  | |||
|  | @ -43,7 +43,6 @@ import android.support.v4.app.FragmentPagerAdapter; | |||
| import android.support.v4.content.ContextCompat; | ||||
| import android.support.v4.content.LocalBroadcastManager; | ||||
| import android.support.v7.app.AppCompatActivity; | ||||
| import android.support.v7.app.AppCompatDelegate; | ||||
| import android.support.v7.widget.CardView; | ||||
| import android.util.SparseArray; | ||||
| import android.view.MenuItem; | ||||
|  | @ -94,12 +93,6 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys { | |||
|     private int mSelectedTab; | ||||
| 
 | ||||
| 
 | ||||
|     /* Sets day / night mode */ | ||||
|     static { | ||||
|         AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM); | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     @Override | ||||
|     protected void onCreate(Bundle savedInstanceState) { | ||||
|         super.onCreate(savedInstanceState); | ||||
|  | @ -461,6 +454,7 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys { | |||
|             public boolean onLongClick(View v) { | ||||
|                 longPressFeedback(R.string.toastmessage_long_press_night_mode_switch); | ||||
|                 NightModeHelper.switchToOpposite(MainActivity.this); | ||||
|                 recreate(); | ||||
|                 return true; | ||||
|             } | ||||
|         }); | ||||
|  |  | |||
							
								
								
									
										58
									
								
								app/src/main/java/org/y20k/trackbook/Trackbook.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								app/src/main/java/org/y20k/trackbook/Trackbook.java
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,58 @@ | |||
| /** | ||||
|  * Trackbook.java | ||||
|  * Implements the Trackbook class | ||||
|  * Trackbook starts up the app and sets up the basic theme (Day / Night) | ||||
|  * | ||||
|  * This file is part of | ||||
|  * TRACKBOOK - Movement Recorder for Android | ||||
|  * | ||||
|  * Copyright (c) 2016-18 - Y20K.org | ||||
|  * Licensed under the MIT-License | ||||
|  * http://opensource.org/licenses/MIT | ||||
|  * | ||||
|  * Trackbook uses osmdroid - OpenStreetMap-Tools for Android | ||||
|  * https://github.com/osmdroid/osmdroid | ||||
|  */ | ||||
| 
 | ||||
| package org.y20k.trackbook; | ||||
| 
 | ||||
| import android.app.Application; | ||||
| import android.os.Build; | ||||
| import android.support.v7.app.AppCompatDelegate; | ||||
| 
 | ||||
| import org.y20k.trackbook.helpers.LogHelper; | ||||
| import org.y20k.trackbook.helpers.NightModeHelper; | ||||
| 
 | ||||
| 
 | ||||
| /** | ||||
|  * Trackbook.class | ||||
|  */ | ||||
| public class Trackbook extends Application { | ||||
| 
 | ||||
|     /* Define log tag */ | ||||
|     private static final String LOG_TAG = Trackbook.class.getSimpleName(); | ||||
| 
 | ||||
| 
 | ||||
|     @Override | ||||
|     public void onCreate() { | ||||
|         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); | ||||
|         } | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     @Override | ||||
|     public void onTerminate() { | ||||
|         super.onTerminate(); | ||||
|         LogHelper.v(LOG_TAG, "Trackbook application terminated."); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | @ -16,14 +16,11 @@ | |||
| 
 | ||||
| package org.y20k.trackbook.helpers; | ||||
| 
 | ||||
| import android.app.Activity; | ||||
| import android.content.Context; | ||||
| import android.content.SharedPreferences; | ||||
| import android.content.res.Configuration; | ||||
| import android.os.Build; | ||||
| import android.preference.PreferenceManager; | ||||
| import android.support.v7.app.AppCompatDelegate; | ||||
| import android.view.View; | ||||
| 
 | ||||
| 
 | ||||
| /** | ||||
|  | @ -36,46 +33,43 @@ public final class NightModeHelper implements TrackbookKeys { | |||
| 
 | ||||
| 
 | ||||
|     /* Switches to opposite theme */ | ||||
|     public static void switchToOpposite(Activity activity) { | ||||
|         switch (getCurrentNightModeState(activity)) { | ||||
|     public static void switchToOpposite(Context context) { | ||||
|         switch (getCurrentNightModeState(context)) { | ||||
|             case Configuration.UI_MODE_NIGHT_NO: | ||||
|                 // night mode is currently not active - turn on night mode | ||||
|                 activateNightMode(activity); | ||||
|                 activateNightMode(context); | ||||
|                 break; | ||||
|             case Configuration.UI_MODE_NIGHT_YES: | ||||
|                 // night mode is currently active - turn off night mode | ||||
|                 deactivateNightMode(activity); | ||||
|                 deactivateNightMode(context); | ||||
|                 break; | ||||
|             case Configuration.UI_MODE_NIGHT_UNDEFINED: | ||||
|                 // don't know what mode is active - turn off night mode | ||||
|                 deactivateNightMode(activity); | ||||
|                 deactivateNightMode(context); | ||||
|                 break; | ||||
|         } | ||||
|         activity.recreate(); // todo check if necessary | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     /* Sets night mode / dark theme */ | ||||
|     public static void restoreSavedState(Activity activity) { | ||||
|         int savedNightModeState = loadNightModeState(activity); | ||||
|         int currentNightModeState = getCurrentNightModeState(activity); | ||||
|         LogHelper.i(LOG_TAG, "Saved state of Night Mode = " + savedNightModeState + " || current state of Night Mode = " + currentNightModeState + " || NO=16 & YES=32"); // todo remove | ||||
|     public static void restoreSavedState(Context context) { | ||||
|         int savedNightModeState = loadNightModeState(context); | ||||
|         int currentNightModeState = getCurrentNightModeState(context); | ||||
|         if (savedNightModeState != -1 && savedNightModeState != currentNightModeState) { | ||||
|             switch (savedNightModeState) { | ||||
|                 case Configuration.UI_MODE_NIGHT_NO: | ||||
|                     // turn off night mode | ||||
|                     deactivateNightMode(activity); | ||||
|                     deactivateNightMode(context); | ||||
|                     break; | ||||
|                 case Configuration.UI_MODE_NIGHT_YES: | ||||
|                     // turn on night mode | ||||
|                     activateNightMode(activity); | ||||
|                     activateNightMode(context); | ||||
|                     break; | ||||
|                 case Configuration.UI_MODE_NIGHT_UNDEFINED: | ||||
|                     // turn off night mode | ||||
|                     deactivateNightMode(activity); | ||||
|                     deactivateNightMode(context); | ||||
|                     break; | ||||
|             } | ||||
|             activity.recreate(); // todo check if necessary | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|  | @ -87,12 +81,8 @@ public final class NightModeHelper implements TrackbookKeys { | |||
| 
 | ||||
| 
 | ||||
|     /* Activates Night Mode */ | ||||
|     private static void activateNightMode(Activity activity) { | ||||
|         saveNightModeState(activity, Configuration.UI_MODE_NIGHT_YES); | ||||
| 
 | ||||
|         // revert to normal status bar | ||||
|         View decorView = activity.getWindow().getDecorView(); | ||||
|         decorView.setSystemUiVisibility(0); | ||||
|     private static void activateNightMode(Context context) { | ||||
|         saveNightModeState(context, Configuration.UI_MODE_NIGHT_YES); | ||||
| 
 | ||||
|         // switch to Nighh Mode | ||||
|         AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); | ||||
|  | @ -100,17 +90,9 @@ public final class NightModeHelper implements TrackbookKeys { | |||
| 
 | ||||
| 
 | ||||
|     /* Deactivates Night Mode */ | ||||
|     private static void deactivateNightMode(Activity activity) { | ||||
|     private static void deactivateNightMode(Context context) { | ||||
|         // save the new state | ||||
|         saveNightModeState(activity, Configuration.UI_MODE_NIGHT_NO); | ||||
| 
 | ||||
|         // switch to white status bar - if possible | ||||
|         View decorView = activity.getWindow().getDecorView(); | ||||
|         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | ||||
|             decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); | ||||
|         } else { | ||||
|             decorView.setSystemUiVisibility(0); | ||||
|         } | ||||
|         saveNightModeState(context, Configuration.UI_MODE_NIGHT_NO); | ||||
| 
 | ||||
|         // switch to Day Mode | ||||
|         AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); | ||||
|  | @ -132,4 +114,3 @@ public final class NightModeHelper implements TrackbookKeys { | |||
|     } | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue
	
	 y20k
						y20k