Skip to content

Commit

Permalink
Merge pull request #15 from WrichikBasu/bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
WrichikBasu authored Nov 7, 2020
2 parents 5c3c434 + d79f783 commit 7acbcc1
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 48 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.

4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "in.basulabs.shakealarmclock"
minSdkVersion 21
targetSdkVersion 30
versionCode 11
versionName "1.2.8"
versionCode 12
versionName "1.2.9"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import android.os.Build;
import android.os.Bundle;
import android.provider.AlarmClock;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
Expand Down Expand Up @@ -68,22 +67,17 @@ public class Activity_IntentManager extends AppCompatActivity {
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Log.e(this.getClass().getSimpleName(), Objects.requireNonNull(getIntent().getAction()));

Intent intent = getIntent();

switch (Objects.requireNonNull(intent.getAction())) {

case AlarmClock.ACTION_SET_ALARM:

Log.e(this.getClass().getSimpleName(), "received action SET_ALARM.");

if (! intent.hasExtra(AlarmClock.EXTRA_HOUR) || ! intent.hasExtra(AlarmClock.EXTRA_MINUTES)) {
///////////////////////////////////////////////////////////////////////
// These two extras are necessary for an alarm to be set. Without
// these, the user will be redirected to Activity_AlarmsList.
///////////////////////////////////////////////////////////////////////
Log.e(this.getClass().getSimpleName(), "Intent does not have any extras.");

Intent intent1 = new Intent(this, Activity_AlarmsList.class);
intent1.setAction(ACTION_NEW_ALARM_FROM_INTENT)
Expand All @@ -94,11 +88,8 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (isVoiceInteraction()) {

Log.e(this.getClass().getSimpleName(), "Voice interaction.");

Bundle status = new Bundle();
VoiceInteractor.Prompt prompt = new VoiceInteractor.Prompt(
new String[]{"You can do that in the app."},
VoiceInteractor.Prompt prompt = new VoiceInteractor.Prompt(new String[]{"You can do that in the app."},
"You can do that in the app.");

VoiceInteractor.Request request = new VoiceInteractor.CompleteVoiceRequest(prompt, status);
Expand All @@ -111,11 +102,8 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (isVoiceInteraction()) {

Log.e(this.getClass().getSimpleName(), "Voice interaction.");

Bundle status = new Bundle();
VoiceInteractor.Prompt prompt = new VoiceInteractor.Prompt(
new String[]{"Your alarm has been set by Shake Alarm Clock."},
VoiceInteractor.Prompt prompt = new VoiceInteractor.Prompt(new String[]{"Your alarm has been set by Shake Alarm Clock."},
"Your alarm has been set by Shake Alarm Clock.");

VoiceInteractor.Request request = new VoiceInteractor.CompleteVoiceRequest(prompt, status);
Expand All @@ -128,12 +116,8 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {

case AlarmClock.ACTION_DISMISS_ALARM:

if (Service_RingAlarm.isThisServiceRunning && Service_RingAlarm.alarmID != - 1) {
Intent intent1 = new Intent(this, Service_RingAlarm.class);
stopService(intent1);
} else if (Service_SnoozeAlarm.isThisServiceRunning && Service_SnoozeAlarm.alarmID != - 1) {
Intent intent1 = new Intent(this, Service_SnoozeAlarm.class);
stopService(intent1);
if (Service_RingAlarm.isThisServiceRunning || Service_SnoozeAlarm.isThisServiceRunning) {
sendBroadcast(new Intent(ConstantsAndStatics.ACTION_CANCEL_ALARM));
} else {
Intent intent2 = new Intent(this, Activity_AlarmsList.class)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
Expand Down Expand Up @@ -206,21 +190,18 @@ private void setAlarm() {

if (! doesFileExist(alarmToneUri)) {
// Uri invalid or file doesn't exist; fall back to default tone
alarmToneUri = Uri.parse(sharedPreferences.getString(SHARED_PREF_KEY_DEFAULT_ALARM_TONE_URI,
"content://settings/system/alarm_alert"));
alarmToneUri = Uri.parse(sharedPreferences.getString(SHARED_PREF_KEY_DEFAULT_ALARM_TONE_URI, "content://settings/system/alarm_alert"));
}

}
} else {
alarmToneUri = Uri.parse(sharedPreferences.getString(SHARED_PREF_KEY_DEFAULT_ALARM_TONE_URI,
"content://settings/system/alarm_alert"));
alarmToneUri = Uri.parse(sharedPreferences.getString(SHARED_PREF_KEY_DEFAULT_ALARM_TONE_URI, "content://settings/system/alarm_alert"));
}

int volume;
if (alarmToneUri != null) {
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
volume = sharedPreferences.getInt(SHARED_PREF_KEY_DEFAULT_ALARM_VOLUME,
audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM) - 1);
volume = sharedPreferences.getInt(SHARED_PREF_KEY_DEFAULT_ALARM_VOLUME, audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM) - 1);
} else {
volume = 0;
}
Expand All @@ -241,8 +222,7 @@ private void setAlarm() {
////////////////////////////////
AlarmDatabase alarmDatabase = AlarmDatabase.getInstance(this);

LocalDateTime alarmDateTime = ConstantsAndStatics.getAlarmDateTime(LocalDate.now(), alarmTime,
isRepeatOn, repeatDays);
LocalDateTime alarmDateTime = ConstantsAndStatics.getAlarmDateTime(LocalDate.now(), alarmTime, isRepeatOn, repeatDays);

if (intent.getExtras().getBoolean(AlarmClock.EXTRA_SKIP_UI, false)) {
// We have been asked to skip the UI. Alarm will be set by this activity itself.
Expand All @@ -269,8 +249,8 @@ private void setAlarm() {

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

Intent intent1 = new Intent(this, AlarmBroadcastReceiver.class);
intent1.setAction(ConstantsAndStatics.ACTION_DELIVER_ALARM)
Intent intent1 = new Intent(this, AlarmBroadcastReceiver.class)
.setAction(ConstantsAndStatics.ACTION_DELIVER_ALARM)
.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);

alarmEntity.alarmID = alarmID.get();
Expand All @@ -280,13 +260,11 @@ private void setAlarm() {
data.putInt(BUNDLE_KEY_ALARM_ID, alarmID.get());
intent1.putExtra(BUNDLE_KEY_ALARM_DETAILS, data);

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, alarmID.get(),
intent1, PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), alarmID.get(), intent1, 0);

ZonedDateTime zonedDateTime = ZonedDateTime.of(alarmDateTime.withSecond(0), ZoneId.systemDefault());
ZonedDateTime zonedDateTime = ZonedDateTime.of(alarmDateTime.withSecond(0).withNano(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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class Activity_RingAlarm extends AppCompatActivity implements View.OnClic

private SharedPreferences sharedPreferences;

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (Objects.equals(intent.getAction(), ConstantsAndStatics.ACTION_DESTROY_RING_ALARM_ACTIVITY)) {
Expand Down Expand Up @@ -73,7 +73,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
alarmTimeTextView.setText(getResources().getString(R.string.time_24hour,
localTime.getHour(), localTime.getMinute()));
} else {
String amPm = localTime.getHour() <= 12 ? "AM" : "PM";
String amPm = localTime.getHour() < 12 ? "AM" : "PM";

if ((localTime.getHour() <= 12) && (localTime.getHour() > 0)) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class Service_SnoozeAlarm extends Service {
private static final int NOTIFICATION_ID = 651;
private int numberOfTimesTheAlarmhasBeenSnoozed;

private NotificationManager notificationManager;

private CountDownTimer snoozeTimer;

public static boolean isThisServiceRunning = false;
Expand Down Expand Up @@ -78,6 +80,8 @@ public int onStartCommand(Intent intent, int flags, int startId) {
intentFilter.addAction(ConstantsAndStatics.ACTION_CANCEL_ALARM);
registerReceiver(broadcastReceiver, intentFilter);

notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

Service_SnoozeAlarm myInstance = this;

ZonedDateTime alarmDateTime = ZonedDateTime.of(LocalDateTime.of(
Expand All @@ -91,10 +95,11 @@ public int onStartCommand(Intent intent, int flags, int startId) {
ZonedDateTime newAlarmDateTime = alarmDateTime.plusMinutes(numberOfTimesTheAlarmhasBeenSnoozed *
alarmDetails.getInt(ConstantsAndStatics.BUNDLE_KEY_SNOOZE_TIME_IN_MINS));

snoozeTimer = new CountDownTimer(Math.abs(Duration.between(ZonedDateTime.now(), newAlarmDateTime).toMillis()), 500) {
snoozeTimer = new CountDownTimer(Math.abs(Duration.between(ZonedDateTime.now(), newAlarmDateTime).toMillis()), 2000) {

@Override
public void onTick(long millisUntilFinished) {
updateNotification();
}

@Override
Expand All @@ -120,8 +125,8 @@ public void onFinish() {
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(Integer.toString(NOTIFICATION_ID),"in.basulabs.shakealarmclock Notifications", importance);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel(Integer.toString(NOTIFICATION_ID), "in.basulabs.shakealarmclock Notifications", importance);
//NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
channel.setSound(null, null);
assert notificationManager != null;
notificationManager.createNotificationChannel(channel);
Expand All @@ -133,8 +138,7 @@ private void createNotificationChannel() {
private Notification buildSnoozeNotification() {
createNotificationChannel();

Intent intent = new Intent();
intent.setAction(ConstantsAndStatics.ACTION_CANCEL_ALARM);
Intent intent = new Intent().setAction(ConstantsAndStatics.ACTION_CANCEL_ALARM);
PendingIntent contentPendingIntent = PendingIntent.getBroadcast(this, 5017, intent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, Integer.toString(NOTIFICATION_ID))
Expand All @@ -150,6 +154,24 @@ private Notification buildSnoozeNotification() {

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

private void updateNotification() {
Intent intent = new Intent()
.setAction(ConstantsAndStatics.ACTION_CANCEL_ALARM);
PendingIntent contentPendingIntent = PendingIntent.getBroadcast(this, 5017, intent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, Integer.toString(NOTIFICATION_ID))
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(getResources().getString(R.string.notifContent_snooze))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_STATUS)
.setSmallIcon(R.drawable.ic_notif)
.setContentIntent(contentPendingIntent);

notificationManager.notify(NOTIFICATION_ID, builder.build());
}

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

/**
* Dismisses the current alarm, and sets the next alarm if repeat is enabled.
*/
Expand Down Expand Up @@ -220,11 +242,11 @@ private void dismissAlarm() {

intent.putExtra(ConstantsAndStatics.BUNDLE_KEY_ALARM_DETAILS, alarmDetails);

PendingIntent pendingIntent2 = PendingIntent.getBroadcast(this, alarmDetails.getInt(ConstantsAndStatics.BUNDLE_KEY_ALARM_ID), intent, 0);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(this, alarmDetails.getInt(ConstantsAndStatics.BUNDLE_KEY_ALARM_ID), intent, 0);

ZonedDateTime zonedDateTime = ZonedDateTime.of(alarmDateTime.withSecond(0), ZoneId.systemDefault());

alarmManager.setAlarmClock(new AlarmManager.AlarmClockInfo(zonedDateTime.toEpochSecond() * 1000, pendingIntent2), pendingIntent2);
alarmManager.setAlarmClock(new AlarmManager.AlarmClockInfo(zonedDateTime.toEpochSecond() * 1000, pendingIntent2), pendingIntent2);
}
ConstantsAndStatics.schedulePeriodicWork(this);
stopForeground(true);
Expand Down Expand Up @@ -261,9 +283,14 @@ public IBinder onBind(Intent intent) {
@Override
public void onDestroy() {
super.onDestroy();
try{
snoozeTimer.cancel();
} catch (Exception ignored){
}
unregisterReceiver(broadcastReceiver);
isThisServiceRunning = false;
alarmID = -1;
alarmID = - 1;
notificationManager.cancel(NOTIFICATION_ID);
}


Expand Down

0 comments on commit 7acbcc1

Please sign in to comment.