11package de .nilsfo .lockscreennotes .activity ;
22
3+ import static de .nilsfo .lockscreennotes .LockScreenNotes .PREFS_TAG ;
4+ import static de .nilsfo .lockscreennotes .LockScreenNotes .REQUEST_CODE_INTENT_EXTERNAL_SEARCH ;
5+
36import android .app .Activity ;
47import android .content .DialogInterface ;
58import android .content .Intent ;
69import android .content .SharedPreferences ;
710import android .content .res .Configuration ;
811import android .net .Uri ;
9- import android .os .Build ;
1012import android .os .Bundle ;
1113import android .preference .PreferenceManager ;
1214import android .text .TextUtils ;
2022import android .widget .TextView ;
2123import android .widget .Toast ;
2224
25+ import androidx .annotation .NonNull ;
26+ import androidx .appcompat .app .AlertDialog ;
27+ import androidx .recyclerview .widget .LinearLayoutManager ;
28+ import androidx .recyclerview .widget .RecyclerView ;
29+
2330import com .google .android .material .floatingactionbutton .FloatingActionButton ;
2431import com .google .android .material .snackbar .Snackbar ;
2532import com .skydoves .balloon .ArrowOrientation ;
4552import java .util .concurrent .ExecutorService ;
4653import java .util .concurrent .Executors ;
4754
48- import androidx .annotation .NonNull ;
49- import androidx .appcompat .app .AlertDialog ;
50- import androidx .recyclerview .widget .LinearLayoutManager ;
51- import androidx .recyclerview .widget .RecyclerView ;
52-
5355import de .nilsfo .lockscreennotes .LockScreenNotes ;
5456import de .nilsfo .lockscreennotes .data .Note ;
5557import de .nilsfo .lockscreennotes .data .RelativeTimeTextfieldContainer ;
6971import de .nilsfo .lsn .R ;
7072import timber .log .Timber ;
7173
72- import static de .nilsfo .lockscreennotes .LockScreenNotes .PREFS_TAG ;
73- import static de .nilsfo .lockscreennotes .LockScreenNotes .REQUEST_CODE_INTENT_EXTERNAL_SEARCH ;
74- import static de .nilsfo .lockscreennotes .io .StoragePermissionManager .StoragePermissionState .STATE_MEDIA_ONLY ;
75-
7674public class MainActivity extends NotesActivity implements Observer , NotesRecyclerAdapter .NotesRecyclerAdapterListener {
7775
7876 public static final int IMPORT_NOTE_PREVIEW_SIZE = 35 ;
@@ -85,7 +83,7 @@ public class MainActivity extends NotesActivity implements Observer, NotesRecycl
8583 private NotesRecyclerAdapter noteRecyclerAdapter ;
8684 private RecyclerView notesRecyclerView ;
8785 private ScrollView tutorialView ;
88- private CheckBox tutorialDontShowAgainCB ;
86+ private CheckBox tutorialDoNotShowAgainCB ;
8987 private TextView nothingToDisplayLB ;
9088 private ExecutorService executorService ;
9189
@@ -102,8 +100,8 @@ protected void onCreate(Bundle savedInstanceState) {
102100 tutorialView = (ScrollView ) findViewById (R .id .tutorial_view );
103101 nothingToDisplayLB = (TextView ) findViewById (R .id .nothing_to_display );
104102
105- tutorialDontShowAgainCB = (CheckBox ) findViewById (R .id .tutorial_do_not_show_again_cb );
106- tutorialDontShowAgainCB .setOnCheckedChangeListener (new CompoundButton .OnCheckedChangeListener () {
103+ tutorialDoNotShowAgainCB = (CheckBox ) findViewById (R .id .tutorial_do_not_show_again_cb );
104+ tutorialDoNotShowAgainCB .setOnCheckedChangeListener (new CompoundButton .OnCheckedChangeListener () {
107105 @ Override
108106 public void onCheckedChanged (CompoundButton buttonView , boolean isChecked ) {
109107 onTutorialCBclicked (isChecked );
@@ -190,6 +188,11 @@ public void requestRecoverNote(Note note) {
190188
191189 public void requestDeleteNote (long databaseID ) {
192190 final Note note = Note .getNoteFromDB (databaseID , databaseAdapter );
191+ if (note == null ) {
192+ Toast .makeText (this , R .string .error_internal_error , Toast .LENGTH_LONG ).show ();
193+ return ;
194+ }
195+
193196 Timber .i ("Request to delete note: '" + note .getText () + "'" );
194197 if (PreferenceManager .getDefaultSharedPreferences (this ).getBoolean ("prefs_quick_delete" , true )) {
195198 deleteNote (note .getDatabaseID ());
@@ -325,65 +328,10 @@ private void reviewNotificationPermissions() {
325328 return ;
326329 }
327330
328- if (storagePermissionManager .getPermissionState () == STATE_MEDIA_ONLY ) {
329- // The item is visible because the user needs to update their storage permissions
330- showPermissionDialogStorageUpgrade ();
331- return ;
332- }
333-
334331 // All permissions granted.
335332 Toast .makeText (this , R .string .info_review_notification_permissions_granted , Toast .LENGTH_LONG ).show ();
336333 }
337334
338- public void showPermissionDialogStorageUpgrade () {
339- NotesNotificationManager notesNotificationManager = new NotesNotificationManager (this );
340- final Activity activity = this ;
341- if (Build .VERSION .SDK_INT < Build .VERSION_CODES .R ) {
342- // The user's device is too old. This should not affect them!
343- Toast .makeText (activity , R .string .action_review_storage_upgrade_permissions_wrong_version , Toast .LENGTH_LONG ).show ();
344- return ;
345- }
346-
347- AlertDialog .Builder builder = new AlertDialog .Builder (activity );
348- builder .setTitle (R .string .action_review_storage_upgrade_permissions );
349- builder .setIcon (R .mipmap .ic_launcher );
350- builder .setMessage (getString (R .string .action_review_storage_upgrade_permissions_info ));
351-
352- builder .setNeutralButton (R .string .prefs_review_notifications_settings_system_title , new DialogInterface .OnClickListener () {
353- @ Override
354- public void onClick (DialogInterface dialog , int which ) {
355- dialog .dismiss ();
356- try {
357- Intent intent = LockScreenNotes .BuildPermissionIntentSystemSettings (activity );
358- startActivity (intent );
359- } catch (Exception e ) {
360- Timber .e (e );
361- Toast .makeText (activity , R .string .error_internal_error , Toast .LENGTH_LONG ).show ();
362- }
363- }
364- });
365- builder .setPositiveButton (R .string .action_review_storage_upgrade_permissions_enable_permissions , new DialogInterface .OnClickListener () {
366- @ Override
367- public void onClick (DialogInterface dialog , int which ) {
368- dialog .dismiss ();
369- StoragePermissionManager storagePermissionManager = new StoragePermissionManager (activity );
370- try {
371- storagePermissionManager .requestExternalStoragePermission (activity );
372- } catch (Exception e ) {
373- Timber .e (e );
374- Toast .makeText (activity , R .string .error_internal_error , Toast .LENGTH_LONG ).show ();
375- }
376- }
377- });
378- builder .setNegativeButton (android .R .string .cancel , new DialogInterface .OnClickListener () {
379- @ Override
380- public void onClick (DialogInterface dialog , int which ) {
381- dialog .cancel ();
382- }
383- });
384- builder .show ();
385- }
386-
387335 public void showPermissionDialogNotification () {
388336 NotesNotificationManager notesNotificationManager = new NotesNotificationManager (this );
389337 final Activity activity = this ;
@@ -454,27 +402,14 @@ private void hidePermissionBalloon() {
454402 }
455403 }
456404
457- private void requestExtendStoragePermissionsDialog () {
458- // TODO implement
459- }
460-
461405 private void requestBackupMenu () {
462406 BackupManager backupManager = new BackupManager (this );
463407 StoragePermissionManager storagePermissionManager = new StoragePermissionManager (this );
464- StoragePermissionManager .StoragePermissionState currentState = storagePermissionManager .getPermissionState ();
465- boolean storageManager = storagePermissionManager .isExternalStorageManager ();
466- Timber .i ("Current permission state: " + currentState );
467- Timber .i ("Is storage manager: " + storageManager );
468-
469- // Checking if permissions are granted, but need upgrading
470- if (storagePermissionManager .requiresExtendedPermissions () && !storagePermissionManager .hasPermissionsTotallyDenied ()) {
471- Toast .makeText (this , R .string .error_external_permissions_require_upgrade , Toast .LENGTH_LONG ).show ();
472- requestExtendStoragePermissionsDialog ();
473- return ;
474- }
408+ boolean hasExternalStoragePermission = storagePermissionManager .hasExternalStoragePermission ();
409+ Timber .i ("Has external Storage Permission: " + hasExternalStoragePermission );
475410
476411 // Checking if external permissions are granted
477- if (!storagePermissionManager . hasAllCorrectPermissions () ) {
412+ if (!hasExternalStoragePermission ) {
478413 Toast .makeText (this , R .string .warning_no_storage_permission , Toast .LENGTH_LONG ).show ();
479414 storagePermissionManager .requestExternalStoragePermission (this );
480415 return ;
@@ -918,12 +853,6 @@ private void updateNotificationPermissionMenuItem() {
918853 Timber .i ("Setting the permission item to visible, because NO NOTIFICATION PERMISSIONS" );
919854 checkAppPermissionItem .setVisible (true );
920855 }
921-
922- if (storagePermissionManager .getPermissionState () == STATE_MEDIA_ONLY ) {
923- // The item is visible because the user needs to update their storage permissions
924- Timber .i ("Setting the permission item to visible, because STORAGE PERMISSION UPDATE NEEDED" );
925- checkAppPermissionItem .setVisible (true );
926- }
927856 }
928857
929858 private void requestDisplayPermissionBalloon () {
@@ -1022,21 +951,41 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
1022951 Uri uri = data .getData ();
1023952 Timber .i ("File Uri: " + uri .toString ());
1024953 String path = null ;
954+ boolean fileSelectionSuccess = true ;
1025955 try {
1026956 path = new FileManager (this ).getPath (uri );
1027957 Timber .i ("File Path: " + path );
1028958 } catch (URISyntaxException e ) {
1029959 e .printStackTrace ();
1030960 Timber .e (e );
1031- return ;
961+ fileSelectionSuccess = false ;
1032962 }
1033- try {
1034- requestBackupImportMenuConfirmFile (new File (path ));
1035- } catch (Exception e ) {
1036- e .printStackTrace ();
1037- Timber .e (e );
1038- Toast .makeText (this , R .string .error_invalid_file_format , Toast .LENGTH_LONG ).show ();
963+
964+ if (!fileSelectionSuccess ) {
965+ // TOOD handle this
966+ }
967+
968+ if (path == null && fileSelectionSuccess ) {
969+ // Path is null. Looks like the file was not found?
970+ Timber .e ("Error! The file path is NULL!" );
971+ //TODO handle this!
972+ break ;
973+ }
974+
975+ if (fileSelectionSuccess ) {
976+ try {
977+ requestBackupImportMenuConfirmFile (new File (path ));
978+ } catch (Exception e ) {
979+ e .printStackTrace ();
980+ Timber .e (e );
981+ Toast .makeText (this , R .string .error_invalid_file_format , Toast .LENGTH_LONG ).show ();
982+ }
983+ } else {
984+ // File selector failed
985+ // TODO Handle this
1039986 }
987+ } else {
988+ // TODO what if not OK?
1040989 }
1041990 break ;
1042991 }
@@ -1139,7 +1088,7 @@ protected void onResume() {
11391088 databaseAdapter .open ();
11401089 loadNotesFromDB ();
11411090
1142- tutorialDontShowAgainCB .setChecked (PreferenceManager .getDefaultSharedPreferences (this ).getBoolean (PREFS_HIDE_TUTORIAL , false ));
1091+ tutorialDoNotShowAgainCB .setChecked (PreferenceManager .getDefaultSharedPreferences (this ).getBoolean (PREFS_HIDE_TUTORIAL , false ));
11431092 setupRelativeDateUpdater ();
11441093
11451094 Timber .i ("MainActivity: onResume()" );
0 commit comments