Skip to content

Commit

Permalink
optimize alert enable/disable
Browse files Browse the repository at this point in the history
  • Loading branch information
rajulbabel committed Jun 24, 2021
1 parent 3f109f8 commit b81d3e5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId "com.vaccinenotifier"
minSdkVersion 26
targetSdkVersion 30
versionCode 1
versionName "1.0"
versionCode 2
versionName "1.1.1"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
11 changes: 8 additions & 3 deletions app/src/main/java/com/vaccinenotifier/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,17 @@ public void onNothingSelected(AdapterView<?> parent) {
}
});
refreshAlertDetails();
scheduleAlarm();
}

private void scheduleAlarm() {
private void scheduleAlarm(boolean enable) {
Intent intent = new Intent(getApplicationContext(), BroadcastReceiverImpl.class);
final PendingIntent pIntent = PendingIntent.getBroadcast(this, BroadcastReceiverImpl.REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), getResources().getInteger(R.integer.cronIntervalMs), pIntent);
if (enable) {
alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), getResources().getInteger(R.integer.cronIntervalMs), pIntent);
} else {
alarm.cancel(pIntent);
}
}

private void refreshAlertDetails() {
Expand Down Expand Up @@ -139,6 +142,7 @@ private void populateSharedPrefDataToUi(SlotConstraints slotConstraints) {
private void enableSwitch(boolean isEnable) {
SwitchCompat switchCompat = findViewById(R.id.alertSwitch);
switchCompat.setChecked(isEnable);
scheduleAlarm(isEnable);
}

private void checkCheckBox(List<Integer> checkBoxIds) {
Expand Down Expand Up @@ -173,6 +177,7 @@ private void disableAlert() {
getSharedPreferences(getString(R.string.alertsSharedPreferencesName), Context.MODE_PRIVATE).edit()
.putBoolean(SlotConstraints.Fields.isEnabled, false)
.apply();
Toast.makeText(MainActivity.this, getString(R.string.alertDisabled), Toast.LENGTH_SHORT).show();
}

public void switchListener(View view) {
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<string name="defaultFeeType">Free,Paid</string>
<string name="defaultDose">Dose1</string>

<integer name="cronIntervalMs">600000</integer>
<integer name="cronIntervalMs">60000</integer>
<string name="alertsSharedPreferencesName">Alerts</string>
<string name="availableCentersJson">availableCentersJson</string>
<string name="centerViewTypeNotification">centerViewTypeNotification</string>
Expand All @@ -34,6 +34,7 @@
<string name="createAlert">Create Alert</string>
<string name="alertDetails"><b>No alerts set</b></string>
<string name="alertCreated">Alert Created</string>
<string name="alertDisabled">Alert Disabled</string>
<string name="districtEmpty">Please select district</string>
<string name="centerNameDisplay">Center Name: %1$s</string>
<string name="centerPincodeDisplay">Center Pincode: %1$s</string>
Expand Down

0 comments on commit b81d3e5

Please sign in to comment.