Skip to content

Commit

Permalink
Merge pull request #7 from WrichikBasu/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
WrichikBasu authored Oct 31, 2020
2 parents 5df689d + 5979e76 commit 86977f5
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 64 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,19 @@ Tired of pressing the power button/swiping the screen every time an alarm rings?


## Salient features:
1. Works offline.
1. The alarm is handled by a service which has almost no dependency with the UI. So, even if your UI freezes, the alarm will ring and can be dismissed.
1. Uses latest Android Room database for storing alarms.
1. Inbuilt dark theme, even in phones that do not support it.
1. Snooze your alarm as many times as you want with custom snooze options.

## How to use?
Simply download the latest apk file, install it and you are all set!

## Google Play:

You can also download the app from [Google Play](https://play.google.com/store/apps/details?id=in.basulabs.shakealarmclock).

## How to contribute?
Please read the [guidelines for contributing](https://github.com/WrichikBasu/ShakeAlarmClock/blob/master/CONTRIBUTING.md).

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 7
versionName "1.2.4"
versionCode 8
versionName "1.2.5"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ private void deleteOrDeactivateAlarm(int mode, int hour, int mins) {

int alarmID = viewModel.getAlarmId(alarmDatabase, hour, mins);

// Kill any foreground service based on this alarm:
ConstantsAndStatics.killServices(this, alarmID);

PendingIntent pendingIntent = PendingIntent.getBroadcast(Activity_AlarmsList.this, alarmID, intent,
PendingIntent.FLAG_NO_CREATE);

Expand All @@ -306,8 +309,6 @@ private void deleteOrDeactivateAlarm(int mode, int hour, int mins) {
viewModel.toggleAlarmState(alarmDatabase, hour, mins, 0);
}

// Kill any foreground service based on this alarm:
ConstantsAndStatics.killServices(this, alarmID);

ConstantsAndStatics.schedulePeriodicWork(this);
}
Expand All @@ -324,8 +325,12 @@ private void deleteOrDeactivateAlarm(int mode, int hour, int mins) {
private void toggleAlarmState(int hour, int mins, final int newAlarmState) {

if (newAlarmState == 0) {
ConstantsAndStatics.killServices(this, viewModel.getAlarmId(alarmDatabase, hour, mins));

deleteOrDeactivateAlarm(MODE_DEACTIVATE_ONLY, hour, mins);
} else {
ConstantsAndStatics.killServices(this, viewModel.getAlarmId(alarmDatabase, hour, mins));

addOrActivateAlarm(MODE_ACTIVATE_EXISTING_ALARM, viewModel.getAlarmEntity(alarmDatabase, hour, mins),
viewModel.getRepeatDays(alarmDatabase, hour, mins));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,10 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {

case AlarmClock.ACTION_DISMISS_ALARM:

if (Service_RingAlarm.isThisServiceRunning || Service_SnoozeAlarm.isThisServiceRunning) {
Intent intent1 = new Intent();
intent1.setAction(ConstantsAndStatics.ACTION_CANCEL_ALARM);
sendBroadcast(intent1);
} else {
Intent intent1 = new Intent(this, Activity_AlarmsList.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent1);
}
Intent intent2 = new Intent(this, Activity_AlarmsList.class);
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent2);

break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
}

if (intent.hasExtra(ConstantsAndStatics.EXTRA_PLAY_RINGTONE)) {
playTone = Objects.requireNonNull(intent.getExtras())
.getBoolean(ConstantsAndStatics.EXTRA_PLAY_RINGTONE);
playTone = Objects.requireNonNull(intent.getExtras()).getBoolean(ConstantsAndStatics.EXTRA_PLAY_RINGTONE);
} else {
playTone = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.content.Intent;
import android.os.Process;
import android.util.Log;

import androidx.annotation.Nullable;
Expand Down Expand Up @@ -157,6 +158,8 @@ final class ConstantsAndStatics {
static final String ACTION_NEW_ALARM_FROM_INTENT =
"in.basulabs.shakealarmclock.ACTION_NEW_ALARM_FROM_INTENT";

static final String ACTION_STOP_IMMEDIATELY = "in.basulabs.shakealarmclock.STOP_IMMEDIATELY";

/**
* Indicates whether {@link Activity_RingtonePicker} should play the ringtone when the user clicks on a {@link
* android.widget.RadioButton}. Default: {@code true}.
Expand Down Expand Up @@ -367,14 +370,10 @@ static int getTheme(int theme) {
//---------------------------------------------------------------------------------------------------------

static void killServices(Context context, int alarmID) {
if (Service_RingAlarm.isThisServiceRunning && Service_RingAlarm.alarmID == alarmID) {
Intent intent = new Intent(context, Service_RingAlarm.class);
context.stopService(intent);
}
if (Service_SnoozeAlarm.isThisServiceRunning && Service_SnoozeAlarm.alarmID == alarmID) {
Intent intent = new Intent(context, Service_SnoozeAlarm.class);
context.stopService(intent);
}
Intent intent = new Intent()
.putExtra(BUNDLE_KEY_ALARM_ID, alarmID)
.setAction(ACTION_STOP_IMMEDIATELY);
context.sendBroadcast(intent);
}

//---------------------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ private void displayAlarmTone() {
.query(viewModel.getAlarmToneUri(), null, null, null, null)) {

try {
if (cursor != null && cursor.moveToFirst()) {
if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) {

int index = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
if (index != - 1) {
Expand Down Expand Up @@ -363,35 +363,28 @@ private void setDate() {

@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.saveButton:
saveButtonClicked();
break;
case R.id.cancelButton:
listener.onCancelButtonClick();
break;
case R.id.repeatConstraintLayout:
listener.onRequestRepeatFragCreation();
break;
case R.id.snoozeConstraintLayout:
listener.onRequestSnoozeFragCreation();
break;
case R.id.alarmDateConstraintLayout:
listener.onRequestDatePickerFragCreation();
break;
case R.id.alarmToneConstraintLayout:
Intent intent = new Intent(requireContext(), Activity_RingtonePicker.class)
.setAction(RingtoneManager.ACTION_RINGTONE_PICKER)
.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_ALARM)
.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select alarm tone:")
.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false)
.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true)
.putExtra(ConstantsAndStatics.EXTRA_PLAY_RINGTONE, false)
.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, Settings.System.DEFAULT_ALARM_ALERT_URI)
.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, viewModel.getAlarmToneUri());
startActivityForResult(intent, RINGTONE_REQUEST_CODE);
break;

if (view.getId() == R.id.saveButton){
saveButtonClicked();
} else if (view.getId() == R.id.cancelButton){
listener.onCancelButtonClick();
} else if (view.getId() == R.id.repeatConstraintLayout){
listener.onRequestRepeatFragCreation();
} else if (view.getId() == R.id.snoozeConstraintLayout){
listener.onRequestSnoozeFragCreation();
} else if (view.getId() == R.id.alarmDateConstraintLayout){
listener.onRequestDatePickerFragCreation();
} else if (view.getId() == R.id.alarmToneConstraintLayout){
Intent intent = new Intent(requireContext(), Activity_RingtonePicker.class)
.setAction(RingtoneManager.ACTION_RINGTONE_PICKER)
.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_ALARM)
.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select alarm tone:")
.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false)
.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true)
.putExtra(ConstantsAndStatics.EXTRA_PLAY_RINGTONE, false)
.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, Settings.System.DEFAULT_ALARM_ALERT_URI)
.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, viewModel.getAlarmToneUri());
startActivityForResult(intent, RINGTONE_REQUEST_CODE);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.IBinder;
import android.os.Process;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.util.Log;

import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;
Expand Down Expand Up @@ -70,8 +72,7 @@ public class Service_RingAlarm extends Service implements SensorEventListener {

private Uri alarmToneUri;

public static boolean isThisServiceRunning = false;
public static int alarmID;
private int alarmID;

private SharedPreferences sharedPreferences;

Expand All @@ -81,13 +82,19 @@ public class Service_RingAlarm extends Service implements SensorEventListener {

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

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_SNOOZE_ALARM)) {
snoozeAlarm();
} else if (Objects.equals(intent.getAction(), ConstantsAndStatics.ACTION_CANCEL_ALARM)) {
dismissAlarm();
} else if (intent.getAction().equals(ConstantsAndStatics.ACTION_STOP_IMMEDIATELY)){
if (Objects.requireNonNull(intent.getExtras()).getInt(ConstantsAndStatics.BUNDLE_KEY_ALARM_ID) == alarmID){
stopForeground(true);
stopSelf();
Process.killProcess(Process.myPid());
}
}
}
};
Expand All @@ -102,7 +109,6 @@ public int onStartCommand(Intent intent, int flags, int startId) {
} else {
startForeground(NOTIFICATION_ID, buildRingNotification());
}
isThisServiceRunning = true;

ConstantsAndStatics.cancelScheduledPeriodicWork(this);

Expand All @@ -121,7 +127,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
alarmToneUri = alarmDetails.getParcelable(ConstantsAndStatics.BUNDLE_KEY_ALARM_TONE_URI);

alarmID = alarmDetails.getInt(ConstantsAndStatics.BUNDLE_KEY_ALARM_ID);

Log.e(this.getClass().getSimpleName(), "alarmID = " + alarmID);

ringTimer = new CountDownTimer(60000, 1000) {

Expand Down Expand Up @@ -152,6 +158,7 @@ public void onFinish() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ConstantsAndStatics.ACTION_SNOOZE_ALARM);
intentFilter.addAction(ConstantsAndStatics.ACTION_CANCEL_ALARM);
intentFilter.addAction(ConstantsAndStatics.ACTION_STOP_IMMEDIATELY);
registerReceiver(broadcastReceiver, intentFilter);

ringAlarm();
Expand All @@ -164,7 +171,6 @@ public void onFinish() {
@Override
public void onDestroy() {
super.onDestroy();
isThisServiceRunning = false;
try {
ringTimer.cancel();
vibrator.cancel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.os.Process;

import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;
Expand All @@ -33,8 +34,7 @@

public class Service_SnoozeAlarm extends Service {

public static boolean isThisServiceRunning = false;
public static int alarmID;
private int alarmID;
private Bundle alarmDetails;
private static final int NOTIFICATION_ID = 651;
private int numberOfTimesTheAlarmhasBeenSnoozed;
Expand All @@ -43,11 +43,17 @@ public class Service_SnoozeAlarm extends Service {

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

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_CANCEL_ALARM)) {
dismissAlarm();
} else if (Objects.equals(intent.getAction(), ConstantsAndStatics.ACTION_STOP_IMMEDIATELY)){
if (Objects.requireNonNull(intent.getExtras()).getInt(ConstantsAndStatics.BUNDLE_KEY_ALARM_ID) == alarmID){
stopForeground(true);
stopSelf();
Process.killProcess(Process.myPid());
}
}
}
};
Expand All @@ -63,7 +69,6 @@ public int onStartCommand(Intent intent, int flags, int startId) {
} else {
startForeground(NOTIFICATION_ID, buildSnoozeNotification());
}
isThisServiceRunning = true;

ConstantsAndStatics.cancelScheduledPeriodicWork(this);

Expand All @@ -77,6 +82,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ConstantsAndStatics.ACTION_CANCEL_ALARM);
intentFilter.addAction(ConstantsAndStatics.ACTION_STOP_IMMEDIATELY);
registerReceiver(broadcastReceiver, intentFilter);

Service_SnoozeAlarm myInstance = this;
Expand Down Expand Up @@ -270,7 +276,6 @@ public IBinder onBind(Intent intent) {
public void onDestroy() {
super.onDestroy();
unregisterReceiver(broadcastReceiver);
isThisServiceRunning = false;
}


Expand Down

0 comments on commit 86977f5

Please sign in to comment.