diff --git a/.idea/misc.xml b/.idea/misc.xml index 0a7e42f..6fd4e63 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -51,7 +51,7 @@ - + diff --git a/app/src/main/java/in/basulabs/shakealarmclock/Activity_AlarmDetails.java b/app/src/main/java/in/basulabs/shakealarmclock/Activity_AlarmDetails.java index f184d6d..d1c9d2d 100644 --- a/app/src/main/java/in/basulabs/shakealarmclock/Activity_AlarmDetails.java +++ b/app/src/main/java/in/basulabs/shakealarmclock/Activity_AlarmDetails.java @@ -99,8 +99,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { } else if (getIntent().getAction().equals(ACTION_EXISTING_ALARM)) { - Bundle data = Objects.requireNonNull(getIntent().getExtras()) - .getBundle(BUNDLE_KEY_ALARM_DETAILS); + Bundle data = Objects.requireNonNull(getIntent().getExtras()).getBundle(BUNDLE_KEY_ALARM_DETAILS); assert data != null; @@ -346,8 +345,7 @@ public void onSaveButtonClick() { data.putInt(BUNDLE_KEY_OLD_ALARM_MINUTE, viewModel.getOldAlarmMinute()); } - Intent intent = new Intent(); - intent.putExtra(BUNDLE_KEY_ALARM_DETAILS, data); + Intent intent = new Intent().putExtra(BUNDLE_KEY_ALARM_DETAILS, data); setResult(RESULT_OK, intent); this.finish(); } diff --git a/app/src/main/java/in/basulabs/shakealarmclock/Activity_AlarmsList.java b/app/src/main/java/in/basulabs/shakealarmclock/Activity_AlarmsList.java index abd825f..72dc2eb 100644 --- a/app/src/main/java/in/basulabs/shakealarmclock/Activity_AlarmsList.java +++ b/app/src/main/java/in/basulabs/shakealarmclock/Activity_AlarmsList.java @@ -12,7 +12,6 @@ import android.os.Handler; import android.os.Looper; import android.os.Message; -import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -57,7 +56,7 @@ public class Activity_AlarmsList extends AppCompatActivity implements AlarmAdapt /** * Request code: Used when {@link Activity_AlarmDetails} is created for displaying the details of an existing alarm. */ - private static final int EXISTING_ALARM_REQUEST_CODE = 3198; + private static final int EXISTING_ALARM_REQUEST_CODE = 3178; private static final int MODE_ADD_NEW_ALARM = 103; private static final int MODE_ACTIVATE_EXISTING_ALARM = 604; @@ -86,8 +85,8 @@ protected void onCreate(Bundle savedInstanceState) { int defaultTheme = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q ? ConstantsAndStatics.THEME_SYSTEM : ConstantsAndStatics.THEME_AUTO_TIME; if (savedInstanceState == null) { - AppCompatDelegate.setDefaultNightMode(ConstantsAndStatics - .getTheme(sharedPreferences.getInt(ConstantsAndStatics.SHARED_PREF_KEY_THEME, defaultTheme))); + AppCompatDelegate + .setDefaultNightMode(ConstantsAndStatics.getTheme(sharedPreferences.getInt(ConstantsAndStatics.SHARED_PREF_KEY_THEME, defaultTheme))); } myInstance = this; @@ -116,15 +115,12 @@ protected void onCreate(Bundle savedInstanceState) { if (getIntent().getAction().equals(ConstantsAndStatics.ACTION_NEW_ALARM_FROM_INTENT)) { - Log.e(this.getClass().getSimpleName(), "Received intent."); - showAppUpdate = false; Intent intent = new Intent(this, Activity_AlarmDetails.class); intent.setAction(ConstantsAndStatics.ACTION_NEW_ALARM_FROM_INTENT); if (getIntent().getExtras() != null) { - Log.e(this.getClass().getSimpleName(), "Extras received too!"); intent.putExtras(getIntent().getExtras()); } @@ -185,9 +181,7 @@ private void manageViewStub(int count) { public static void onDateChanged() { if (myInstance != null) { myInstance.viewModel.forceInit(myInstance.alarmDatabase); - myInstance.alarmAdapter = new AlarmAdapter(myInstance.viewModel.getAlarmDataArrayList(), - myInstance, - myInstance); + myInstance.alarmAdapter = new AlarmAdapter(myInstance.viewModel.getAlarmDataArrayList(), myInstance, myInstance); myInstance.alarmsRecyclerView.swapAdapter(myInstance.alarmAdapter, false); } } @@ -233,6 +227,7 @@ private void addOrActivateAlarm(int mode, AlarmEntity alarmEntity, @Nullable Arr int alarmID; if (mode == MODE_ADD_NEW_ALARM) { + int[] result = viewModel.addAlarm(alarmDatabase, alarmEntity, repeatDays); alarmID = result[0]; @@ -262,7 +257,7 @@ private void addOrActivateAlarm(int mode, AlarmEntity alarmEntity, @Nullable Arr ZonedDateTime zonedDateTime = ZonedDateTime.of(alarmDateTime.withSecond(0), ZoneId.systemDefault()); - alarmManager.setAlarmClock(new AlarmManager.AlarmClockInfo(zonedDateTime.toEpochSecond() * 1000, pendingIntent), pendingIntent); + alarmManager.setAlarmClock(new AlarmManager.AlarmClockInfo(zonedDateTime.toEpochSecond() * 1000, pendingIntent), pendingIntent); ConstantsAndStatics.schedulePeriodicWork(this); } @@ -282,9 +277,9 @@ private void deleteOrDeactivateAlarm(int mode, int hour, int mins) { AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); - Intent intent = new Intent(Activity_AlarmsList.this, AlarmBroadcastReceiver.class); - intent.setAction(ConstantsAndStatics.ACTION_DELIVER_ALARM); - intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND); + Intent intent = new Intent(getApplicationContext(), AlarmBroadcastReceiver.class) + .setAction(ConstantsAndStatics.ACTION_DELIVER_ALARM) + .setFlags(Intent.FLAG_RECEIVER_FOREGROUND); int alarmID = viewModel.getAlarmId(alarmDatabase, hour, mins); @@ -294,6 +289,8 @@ private void deleteOrDeactivateAlarm(int mode, int hour, int mins) { alarmManager.cancel(pendingIntent); } + ConstantsAndStatics.killServices(this, alarmID); + if (mode == MODE_DELETE_ALARM) { viewModel.removeAlarm(alarmDatabase, hour, mins); alarmAdapter = new AlarmAdapter(viewModel.getAlarmDataArrayList(), this, this); @@ -339,10 +336,18 @@ public void onActivityResult(int requestCode, int resultCode, @Nullable Intent i if (intent != null) { - Bundle data = Objects.requireNonNull(intent.getExtras()) - .getBundle(ConstantsAndStatics.BUNDLE_KEY_ALARM_DETAILS); + Bundle data = Objects.requireNonNull(intent.getExtras()).getBundle(ConstantsAndStatics.BUNDLE_KEY_ALARM_DETAILS); assert data != null; + if (viewModel.getAlarmId(alarmDatabase, data.getInt(ConstantsAndStatics.BUNDLE_KEY_ALARM_HOUR), + data.getInt(ConstantsAndStatics.BUNDLE_KEY_ALARM_MINUTE)) != 0) { + + deleteOrDeactivateAlarm(MODE_DELETE_ALARM, + data.getInt(ConstantsAndStatics.BUNDLE_KEY_ALARM_HOUR), + data.getInt(ConstantsAndStatics.BUNDLE_KEY_ALARM_MINUTE)); + + } + AlarmEntity alarmEntity = new AlarmEntity(data.getInt(ConstantsAndStatics.BUNDLE_KEY_ALARM_HOUR), data.getInt(ConstantsAndStatics.BUNDLE_KEY_ALARM_MINUTE), true, @@ -365,7 +370,9 @@ public void onActivityResult(int requestCode, int resultCode, @Nullable Intent i } else if (requestCode == EXISTING_ALARM_REQUEST_CODE) { if (resultCode == RESULT_OK) { + if (intent != null) { + Bundle data = Objects.requireNonNull(intent.getExtras()) .getBundle(ConstantsAndStatics.BUNDLE_KEY_ALARM_DETAILS); assert data != null; @@ -416,8 +423,9 @@ public void onDeleteButtonClicked(int rowNumber, int hour, int mins) { @Override public void onItemClicked(int rowNumber, int hour, int mins) { - Intent intent = new Intent(this, Activity_AlarmDetails.class); - intent.setAction(ConstantsAndStatics.ACTION_EXISTING_ALARM); + + Context context = this; + AppCompatActivity activity = this; final String KEY_START_ACTIVITY = "startTheActivity"; @@ -432,8 +440,10 @@ public void handleMessage(@NonNull Message msg) { bundle.putIntegerArrayList(ConstantsAndStatics.BUNDLE_KEY_REPEAT_DAYS, data.getIntegerArrayList(ConstantsAndStatics.BUNDLE_KEY_REPEAT_DAYS)); - intent.putExtra(ConstantsAndStatics.BUNDLE_KEY_ALARM_DETAILS, bundle); - startActivityForResult(intent, EXISTING_ALARM_REQUEST_CODE); + Intent intent = new Intent(context, Activity_AlarmDetails.class) + .setAction(ConstantsAndStatics.ACTION_EXISTING_ALARM) + .putExtra(ConstantsAndStatics.BUNDLE_KEY_ALARM_DETAILS, bundle); + activity.startActivityForResult(intent, EXISTING_ALARM_REQUEST_CODE); } } }; @@ -475,7 +485,8 @@ private void checkForUpdatesFromGooglePlay() { try { context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=in.basulabs.shakealarmclock"))); } catch (android.content.ActivityNotFoundException exception) { - context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=in.basulabs.shakealarmclock"))); + context.startActivity( + new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=in.basulabs.shakealarmclock"))); } }) .start(); @@ -483,7 +494,7 @@ private void checkForUpdatesFromGooglePlay() { //------------------------------------------------------------------------------------------------------------- - private void checkForUpdatesFromGitHub(){ + private void checkForUpdatesFromGitHub() { Context context = this; new AppUpdater(this) diff --git a/app/src/main/java/in/basulabs/shakealarmclock/ViewModel_AlarmsList.java b/app/src/main/java/in/basulabs/shakealarmclock/ViewModel_AlarmsList.java index ecfaf5e..8037607 100644 --- a/app/src/main/java/in/basulabs/shakealarmclock/ViewModel_AlarmsList.java +++ b/app/src/main/java/in/basulabs/shakealarmclock/ViewModel_AlarmsList.java @@ -18,32 +18,10 @@ public class ViewModel_AlarmsList extends ViewModel { private MutableLiveData> alarmDataArrayList; - private MutableLiveData hasPermissionBeenRequested; - private MutableLiveData hasPermissionsDialogBeenShownBefore; private MutableLiveData alarmsCount; //-------------------------------------------------------------------------------------------------- - public boolean getHasPermissionsDialogBeenShownBefore() { - if (hasPermissionsDialogBeenShownBefore == null || hasPermissionsDialogBeenShownBefore - .getValue() == null) { - hasPermissionsDialogBeenShownBefore = new MutableLiveData<>(false); - return false; - } - return hasPermissionsDialogBeenShownBefore.getValue(); - } - - //-------------------------------------------------------------------------------------------------- - - public void setHasPermissionsDialogBeenShownBefore( - boolean hasPermissionsDialogBeenShownBefore) { - if (this.hasPermissionsDialogBeenShownBefore == null) { - this.hasPermissionsDialogBeenShownBefore = new MutableLiveData<>(); - } - this.hasPermissionsDialogBeenShownBefore.setValue(hasPermissionsDialogBeenShownBefore); - } - //-------------------------------------------------------------------------------------------------- - public LiveData getLiveAlarmsCount() { return alarmsCount; } @@ -89,12 +67,11 @@ public int getAlarmsCount(AlarmDatabase alarmDatabase) { //-------------------------------------------------------------------------------------------------- /** - * Updates the date of the alarm to the next feasible date, and then reads data into {@link - * #alarmDataArrayList}. + * Updates the date of the alarm to the next feasible date, and then reads data into {@link #alarmDataArrayList}. * * @param alarmDatabase The {@link AlarmDatabase} object to be used to read from/write to the database. - * @param force Whether the operation is to be forced. If this is {@code true}, the method will not - * return until the thread has completed execution. Otherwise the thread will run in the background. + * @param force Whether the operation is to be forced. If this is {@code true}, the method will not return until the thread has completed execution. + * Otherwise the thread will run in the background. */ private void init(AlarmDatabase alarmDatabase, boolean force) { @@ -196,35 +173,6 @@ public void init(AlarmDatabase alarmDatabase) { //-------------------------------------------------------------------------------------------------- - /** - * Checks whether permission has been requested during the current session. - * - * @return {@code true} if permission has been requested, otherwise {@code false}. - */ - public boolean getHasPermissionBeenRequested() { - if (hasPermissionBeenRequested == null || hasPermissionBeenRequested.getValue() == null) { - hasPermissionBeenRequested = new MutableLiveData<>(false); - return false; - } - return hasPermissionBeenRequested.getValue(); - } - - //-------------------------------------------------------------------------------------------------- - - /** - * Sets whether permission has been requested during the current session. - * - * @param hasPermissionBeenRequested As above. - */ - public void setHasPermissionBeenRequested(boolean hasPermissionBeenRequested) { - if (this.hasPermissionBeenRequested == null) { - this.hasPermissionBeenRequested = new MutableLiveData<>(); - } - this.hasPermissionBeenRequested.setValue(hasPermissionBeenRequested); - } - - //-------------------------------------------------------------------------------------------------- - /** * Get an {@link ArrayList} of {@link AlarmData} objects that can be used to instantiate the adapter. * @@ -245,12 +193,10 @@ public ArrayList getAlarmDataArrayList() { * * @param alarmDatabase The {@link AlarmDatabase} object. * @param alarmEntity The {@link AlarmEntity} object that contanins all the alarm details. - * @param repeatDays The days in which the alarm is to be repeated, if repeat is ON. Otherwise, this - * value can be null. + * @param repeatDays The days in which the alarm is to be repeated, if repeat is ON. Otherwise, this value can be null. * - * @return An array consiting of TWO elements: the alarm ID at index 0 and the position at which the alarm - * was inserted at index 1. The latter can be used to scroll the {@link - * androidx.recyclerview.widget.RecyclerView} using {@link androidx.recyclerview.widget.RecyclerView#scrollToPosition(int)}. + * @return An array consiting of TWO elements: the alarm ID at index 0 and the position at which the alarm was inserted at index 1. The latter can be used + * to scroll the {@link androidx.recyclerview.widget.RecyclerView} using {@link androidx.recyclerview.widget.RecyclerView#scrollToPosition(int)}. */ public int[] addAlarm(@NonNull AlarmDatabase alarmDatabase, @NonNull AlarmEntity alarmEntity, @Nullable ArrayList repeatDays) { @@ -459,12 +405,18 @@ public int toggleAlarmState(@NonNull AlarmDatabase alarmDatabase, int hour, int * @param hour The alarm hour. * @param mins The alarm minute. * - * @return The unique alarm ID. + * @return The unique alarm ID if the alarm is present in the database, otherwise 0 (zero). */ public int getAlarmId(@NonNull AlarmDatabase alarmDatabase, int hour, int mins) { - AtomicInteger alarmId = new AtomicInteger(5); + AtomicInteger alarmId = new AtomicInteger(0); - Thread thread = new Thread(() -> alarmId.set(alarmDatabase.alarmDAO().getAlarmId(hour, mins))); + Thread thread = new Thread(() -> { + try { + alarmId.set(alarmDatabase.alarmDAO().getAlarmId(hour, mins)); + } catch (Exception ex) { + alarmId.set(0); + } + }); thread.start(); try { @@ -484,8 +436,7 @@ public int getAlarmId(@NonNull AlarmDatabase alarmDatabase, int hour, int mins) * @param hour The alarm hour. * @param mins The alarm minute. * - * @return An {@link ArrayList} containing the days in which the alarm is set to repeat. Will return an - * empty {@link ArrayList} if repeat is OFF. + * @return An {@link ArrayList} containing the days in which the alarm is set to repeat. Will return an empty {@link ArrayList} if repeat is OFF. */ public ArrayList getRepeatDays(@NonNull AlarmDatabase alarmDatabase, int hour, int mins) { AtomicReference> repeatDays = new AtomicReference<>(new ArrayList<>()); @@ -537,8 +488,7 @@ public AlarmEntity getAlarmEntity(@NonNull AlarmDatabase alarmDatabase, int hour * @param hour The alarm hour. * @param mins The alarm minutes. * - * @return {@code -1} if the alarm is not present in the list, otherwise the index where the alarm is - * present. + * @return {@code -1} if the alarm is not present in the list, otherwise the index where the alarm is present. */ private int isAlarmInTheList(int hour, int mins) {