Skip to content

Commit

Permalink
Merge pull request #13 from WrichikBasu/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
WrichikBasu authored Nov 3, 2020
2 parents 303ffc4 + c351e61 commit 41fcb20
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 94 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);

Expand All @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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";

Expand All @@ -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);
}
}
};
Expand Down Expand Up @@ -475,15 +485,16 @@ 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();
}

//-------------------------------------------------------------------------------------------------------------

private void checkForUpdatesFromGitHub(){
private void checkForUpdatesFromGitHub() {
Context context = this;

new AppUpdater(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,10 @@
public class ViewModel_AlarmsList extends ViewModel {

private MutableLiveData<ArrayList<AlarmData>> alarmDataArrayList;
private MutableLiveData<Boolean> hasPermissionBeenRequested;
private MutableLiveData<Boolean> hasPermissionsDialogBeenShownBefore;
private MutableLiveData<Integer> 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<Integer> getLiveAlarmsCount() {
return alarmsCount;
}
Expand Down Expand Up @@ -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) {

Expand Down Expand Up @@ -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.
*
Expand All @@ -245,12 +193,10 @@ public ArrayList<AlarmData> 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<Integer> repeatDays) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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<Integer> getRepeatDays(@NonNull AlarmDatabase alarmDatabase, int hour, int mins) {
AtomicReference<ArrayList<Integer>> repeatDays = new AtomicReference<>(new ArrayList<>());
Expand Down Expand Up @@ -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) {

Expand Down

0 comments on commit 41fcb20

Please sign in to comment.