state of night mode is now remebered corrextly
This commit is contained in:
		
							parent
							
								
									616741725a
								
							
						
					
					
						commit
						fd8dad3c74
					
				
					 5 changed files with 52 additions and 58 deletions
				
			
		|  | @ -339,8 +339,7 @@ public class MainActivityTrackFragment extends Fragment implements AdapterView.O | |||
|             case RESULT_EXPORT_DIALOG: | ||||
|                 if (resultCode == Activity.RESULT_OK) { | ||||
|                     // User chose EXPORT | ||||
|                     ExportHelper exportHelper = new ExportHelper(mActivity); | ||||
|                     exportHelper.exportToGpx(mTrack); | ||||
|                     ExportHelper.exportToGpx(mActivity, mTrack); | ||||
|                 } else if (resultCode == Activity.RESULT_CANCELED){ | ||||
|                     // User chose CANCEL | ||||
|                     LogHelper.v(LOG_TAG, "Export to GPX: User chose CANCEL."); | ||||
|  | @ -536,11 +535,8 @@ public class MainActivityTrackFragment extends Fragment implements AdapterView.O | |||
|                 String recordingStartDate = df.format(mTrack.getRecordingStart()); | ||||
|                 String dialogMessage; | ||||
| 
 | ||||
|                 // create an ExportHelper | ||||
|                 final ExportHelper exportHelper = new ExportHelper(mActivity); | ||||
| 
 | ||||
|                 // get text elements for delete dialog | ||||
|                 if (exportHelper.gpxFileExists(mTrack)) { | ||||
|                 if (ExportHelper.gpxFileExists(mTrack)) { | ||||
|                     // CASE: OVERWRITE - GPX file exists | ||||
|                     dialogTitle = R.string.dialog_export_title_overwrite; | ||||
|                     dialogMessage = getString(R.string.dialog_export_content_overwrite) + " (" + recordingStartDate + " | " + mTrack.getTrackDistanceString() + ")"; | ||||
|  |  | |||
|  | @ -38,69 +38,59 @@ import java.util.TimeZone; | |||
| /** | ||||
|  * ExportHelper class | ||||
|  */ | ||||
| public class ExportHelper implements TrackbookKeys { | ||||
| public final class ExportHelper implements TrackbookKeys { | ||||
| 
 | ||||
|     /* Define log tag */ | ||||
|     private static final String LOG_TAG = ExportHelper.class.getSimpleName(); | ||||
| 
 | ||||
| 
 | ||||
|     /* Main class variables */ | ||||
| //    private final Track mTrack; | ||||
|     private final Context mContext; | ||||
|     private final File mFolder; | ||||
| 
 | ||||
| 
 | ||||
|     /* Constructor */ | ||||
|     public ExportHelper(Context context) { | ||||
|         mContext = context; | ||||
|         mFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     /* Checks if a GPX file for given track is already present */ | ||||
|     public boolean gpxFileExists(Track track) { | ||||
|         return createFile(track).exists(); | ||||
|     public static boolean gpxFileExists(Track track) { | ||||
|         File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); | ||||
|         return createFile(track, folder).exists(); | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     /* Exports given track to GPX */ | ||||
|     public boolean exportToGpx(Track track) { | ||||
|     public static boolean exportToGpx(Context context, Track track) { | ||||
|         // get "Download" folder | ||||
|         File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); | ||||
| 
 | ||||
|         // create "Download" folder if necessary | ||||
|         if (mFolder != null && !mFolder.exists()) { | ||||
|             LogHelper.v(LOG_TAG, "Creating new folder: " + mFolder.toString()); | ||||
|             mFolder.mkdirs(); | ||||
|         if (folder != null && !folder.exists()) { | ||||
|             LogHelper.v(LOG_TAG, "Creating new folder: " + folder.toString()); | ||||
|             folder.mkdirs(); | ||||
|         } | ||||
| 
 | ||||
|         // get file for given track | ||||
|         File gpxFile = createFile(track); | ||||
|         File gpxFile = createFile(track, folder); | ||||
| 
 | ||||
|         // get GPX string representation for given track | ||||
|         String gpxString = createGpxString(track); | ||||
| 
 | ||||
|         // write GPX file | ||||
|         if (writeGpxToFile(gpxString, gpxFile)) { | ||||
|             String toastMessage = mContext.getResources().getString(R.string.toast_message_export_success) + " " + gpxFile.toString(); | ||||
|             Toast.makeText(mContext, toastMessage, Toast.LENGTH_LONG).show(); | ||||
|             String toastMessage = context.getResources().getString(R.string.toast_message_export_success) + " " + gpxFile.toString(); | ||||
|             Toast.makeText(context, toastMessage, Toast.LENGTH_LONG).show(); | ||||
|             return true; | ||||
|         } else { | ||||
|             String toastMessage = mContext.getResources().getString(R.string.toast_message_export_fail) + " " + gpxFile.toString(); | ||||
|             Toast.makeText(mContext, toastMessage, Toast.LENGTH_LONG).show(); | ||||
|             String toastMessage = context.getResources().getString(R.string.toast_message_export_fail) + " " + gpxFile.toString(); | ||||
|             Toast.makeText(context, toastMessage, Toast.LENGTH_LONG).show(); | ||||
|             return false; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     /* Return a GPX filepath for a given track */ | ||||
|     private File createFile(Track track) { | ||||
|     private static File createFile(Track track, File folder) { | ||||
|         Date recordingStart = track.getRecordingStart(); | ||||
|         DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.US); | ||||
|         return new File(mFolder, dateFormat.format(recordingStart) + FILE_TYPE_GPX_EXTENSION); | ||||
|         return new File(folder, dateFormat.format(recordingStart) + FILE_TYPE_GPX_EXTENSION); | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     /* Writes given GPX string to Download folder */ | ||||
|     private boolean writeGpxToFile (String gpxString, File gpxFile) { | ||||
|     private static boolean writeGpxToFile (String gpxString, File gpxFile) { | ||||
|         // write track | ||||
|         try (BufferedWriter bw = new BufferedWriter(new FileWriter(gpxFile))) { | ||||
|             LogHelper.v(LOG_TAG, "Saving track to external storage: " + gpxFile.toString()); | ||||
|  | @ -114,7 +104,7 @@ public class ExportHelper implements TrackbookKeys { | |||
| 
 | ||||
| 
 | ||||
|     /* Creates GPX formatted string */ | ||||
|     private String createGpxString(Track track) { | ||||
|     private static String createGpxString(Track track) { | ||||
|         String gpxString; | ||||
| 
 | ||||
|         // add header | ||||
|  | @ -134,7 +124,7 @@ public class ExportHelper implements TrackbookKeys { | |||
| 
 | ||||
| 
 | ||||
|     /* Creates Track */ | ||||
|     private String addTrack(Track track) { | ||||
|     private static String addTrack(Track track) { | ||||
|         StringBuilder gpxTrack = new StringBuilder(""); | ||||
|         DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US); | ||||
|         dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); | ||||
|  |  | |||
|  | @ -31,7 +31,6 @@ import android.view.View; | |||
|  */ | ||||
| public final class NightModeHelper implements TrackbookKeys { | ||||
| 
 | ||||
| 
 | ||||
|     /* Define log tag */ | ||||
|     private static final String LOG_TAG = NightModeHelper.class.getSimpleName(); | ||||
| 
 | ||||
|  | @ -62,21 +61,21 @@ public final class NightModeHelper implements TrackbookKeys { | |||
|         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 | ||||
|         if (savedNightModeState != -1 && savedNightModeState != currentNightModeState) { | ||||
| //            switch (savedNightModeState) { | ||||
| //                case Configuration.UI_MODE_NIGHT_NO: | ||||
| //                     // turn off night mode | ||||
| //                    deactivateNightMode(activity); | ||||
| //                    break; | ||||
| //                case Configuration.UI_MODE_NIGHT_YES: | ||||
| //                    // turn on night mode | ||||
| //                    activateNightMode(activity); | ||||
| //                    break; | ||||
| //                case Configuration.UI_MODE_NIGHT_UNDEFINED: | ||||
| //                    // turn off night mode | ||||
| //                    deactivateNightMode(activity); | ||||
| //                    break; | ||||
| //            } | ||||
| //            activity.recreate(); // todo check if necessary | ||||
|             switch (savedNightModeState) { | ||||
|                 case Configuration.UI_MODE_NIGHT_NO: | ||||
|                     // turn off night mode | ||||
|                     deactivateNightMode(activity); | ||||
|                     break; | ||||
|                 case Configuration.UI_MODE_NIGHT_YES: | ||||
|                     // turn on night mode | ||||
|                     activateNightMode(activity); | ||||
|                     break; | ||||
|                 case Configuration.UI_MODE_NIGHT_UNDEFINED: | ||||
|                     // turn off night mode | ||||
|                     deactivateNightMode(activity); | ||||
|                     break; | ||||
|             } | ||||
|             activity.recreate(); // todo check if necessary | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|  | @ -89,23 +88,32 @@ public final class NightModeHelper implements TrackbookKeys { | |||
| 
 | ||||
|     /* Activates Night Mode */ | ||||
|     private static void activateNightMode(Activity activity) { | ||||
|         AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); | ||||
|         saveNightModeState(activity, Configuration.UI_MODE_NIGHT_YES); | ||||
| 
 | ||||
|         // revert to normal status bar | ||||
|         View decorView = activity.getWindow().getDecorView(); | ||||
|         decorView.setSystemUiVisibility(0); | ||||
|         saveNightModeState(activity, Configuration.UI_MODE_NIGHT_NO); | ||||
| 
 | ||||
|         // switch to Nighh Mode | ||||
|         AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     /* Deactivates Night Mode */ | ||||
|     private static void deactivateNightMode(Activity activity) { | ||||
|         AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); | ||||
|         // 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(activity, Configuration.UI_MODE_NIGHT_YES); | ||||
| 
 | ||||
|         // switch to Day Mode | ||||
|         AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|  |  | |||
|  | @ -39,7 +39,7 @@ import org.y20k.trackbook.core.Track; | |||
| /** | ||||
|  * NotificationHelper class | ||||
|  */ | ||||
| public class NotificationHelper implements TrackbookKeys { | ||||
| public final class NotificationHelper implements TrackbookKeys { | ||||
| 
 | ||||
|     /* Define log tag */ | ||||
|     private static final String LOG_TAG = NotificationHelper.class.getSimpleName(); | ||||
|  |  | |||
|  | @ -34,7 +34,7 @@ | |||
|         android:layout_width="@dimen/bottom_sheet_width" | ||||
|         android:layout_height="match_parent" | ||||
|         android:layout_gravity="center_horizontal" | ||||
|         android:background="@color/statistic_sheet_background_expanded" | ||||
|         android:background="@color/statistic_sheet_background_collapsed" | ||||
|         app:behavior_hideable="false" | ||||
|         app:behavior_peekHeight="54dp" | ||||
|         app:layout_behavior="android.support.design.widget.BottomSheetBehavior"> | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue
	
	 y20k
						y20k