Skip to content

Commit

Permalink
Merge pull request #1 from WrichikBasu/dev
Browse files Browse the repository at this point in the history
Replaced background service with WorkManager
  • Loading branch information
WrichikBasu authored Oct 22, 2020
2 parents df4789c + 7d8e9c8 commit c93be39
Show file tree
Hide file tree
Showing 17 changed files with 460 additions and 641 deletions.
19 changes: 6 additions & 13 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,27 @@ android {
applicationId "in.basulabs.shakealarmclock"
minSdkVersion 21
targetSdkVersion 30
versionCode 2
versionName "1.1"
versionCode 3
versionName "1.2"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

compileOptions {
// Flag to enable support for the new language APIs
coreLibraryDesugaringEnabled true
// Sets Java compatibility to Java 8

sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

buildTypes {
release {
minifyEnabled true
shrinkResources true
minifyEnabled false
shrinkResources false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

/*sourceSets {
main {
java {
}
}
}*/

lintOptions {
disable 'ApplySharedPref'
}
Expand All @@ -46,6 +38,7 @@ dependencies {
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.10'
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "androidx.work:work-runtime:2.4.0"
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
implementation "androidx.room:room-runtime:2.2.5"
Expand Down
20 changes: 12 additions & 8 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,24 @@
android:theme="@style/AppTheme"
tools:ignore="AllowBackup">

<provider
android:name="androidx.work.impl.WorkManagerInitializer"
android:authorities="in.basulabs.shakealarmclock.workmanager-init"
android:enabled="false"
android:exported="false"
tools:node="remove"/>

<provider
android:name=".MyWorkManagerInitializer"
android:authorities="in.basulabs.shakealarmclock.MyWorkManagerInitializer"
android:exported="false" />

<service
android:name=".Service_SnoozeAlarm"
android:enabled="true"
android:exported="false"
android:process=":RingAlarm"/>

<service
android:name=".Service_AlarmActivater"
android:enabled="true"
android:exported="true"
android:process=":AlarmActivater"
android:stopWithTask="false"
tools:ignore="ExportedService" />

<activity
android:name=".Activity_Plugin"
android:configChanges="orientation|keyboard|uiMode" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
defaultTheme = ConstantsAndStatics.THEME_SYSTEM;
} else {
defaultTheme = ConstantsAndStatics.THEME_TIME;
defaultTheme = ConstantsAndStatics.THEME_AUTO_TIME;
}
AppCompatDelegate.setDefaultNightMode(
ConstantsAndStatics.getTheme(sharedPreferences.getInt(ConstantsAndStatics.SHARED_PREF_KEY_THEME, defaultTheme)));
Expand Down
123 changes: 29 additions & 94 deletions app/src/main/java/in/basulabs/shakealarmclock/Activity_AlarmsList.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,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 @@ -86,21 +85,10 @@ protected void onCreate(Bundle savedInstanceState) {

viewModel.init(alarmDatabase);

int defaultTheme;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
defaultTheme = ConstantsAndStatics.THEME_SYSTEM;
} else {
defaultTheme = ConstantsAndStatics.THEME_TIME;
}

checkForPlugin();

/*Log.e(this.getClass().toString(), "default theme: " + defaultTheme +
" received theme: " + ConstantsAndStatics
.getTheme(sharedPreferences.getInt(ConstantsAndStatics.SHARED_PREF_KEY_THEME, defaultTheme)));*/
int defaultTheme = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q ? ConstantsAndStatics.THEME_SYSTEM : ConstantsAndStatics.THEME_AUTO_TIME;

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 All @@ -116,14 +104,11 @@ protected void onCreate(Bundle savedInstanceState) {

viewStub = findViewById(R.id.viewStub);

Intent intent = new Intent(getApplicationContext(), Service_AlarmActivater.class);
getApplicationContext().startService(intent);

SharedPreferences.Editor prefEditor = getSharedPreferences(ConstantsAndStatics.SHARED_PREF_FILE_NAME,
MODE_PRIVATE)
.edit()
.remove(ConstantsAndStatics.SHARED_PREF_KEY_WAS_APP_RECENTLY_ACTIVE)
.putBoolean(ConstantsAndStatics.SHARED_PREF_KEY_WAS_APP_RECENTLY_ACTIVE, true);
SharedPreferences.Editor prefEditor =
getSharedPreferences(ConstantsAndStatics.SHARED_PREF_FILE_NAME, MODE_PRIVATE)
.edit()
.remove(ConstantsAndStatics.SHARED_PREF_KEY_WAS_APP_RECENTLY_ACTIVE)
.putBoolean(ConstantsAndStatics.SHARED_PREF_KEY_WAS_APP_RECENTLY_ACTIVE, true);
prefEditor.commit();

initAdapter();
Expand Down Expand Up @@ -156,12 +141,14 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
return super.onOptionsItemSelected(item);
}


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

@Override
protected void onDestroy() {
super.onDestroy();
myInstance = null;
ConstantsAndStatics.schedulePeriodicWork(this);
}


Expand All @@ -179,7 +166,6 @@ private void manageViewStub(int count) {

public static void onDateChanged() {
if (myInstance != null) {
//Log.e(myInstance.getClass().getSimpleName(), "Inside onDateChanged()");
myInstance.viewModel.forceInit(myInstance.alarmDatabase);
myInstance.alarmAdapter = new AlarmAdapter(myInstance.viewModel.getAlarmDataArrayList(),
myInstance,
Expand All @@ -193,7 +179,6 @@ public static void onDateChanged() {
@Override
protected void onResume() {
super.onResume();
//Log.e(this.getClass().getSimpleName(), "Inside onResume().");
if (showPermissionDialog) {
showPermissionDialog = false;
showPermissionDialog();
Expand All @@ -219,8 +204,8 @@ private void checkAndRequestPermission() {
if (! viewModel.getHasPermissionBeenRequested()) {
viewModel.setHasPermissionBeenRequested(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
} else if (shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)) {
showPermissionDialog();
} else {
Expand Down Expand Up @@ -261,17 +246,16 @@ private void initAdapter() {
//--------------------------------------------------------------------------------------------------

/**
* Adds an alarm to the database, updates {@link #alarmAdapter}, and activates it via {@link
* AlarmManager}.
* Adds an alarm to the database, updates {@link #alarmAdapter}, and activates it via {@link AlarmManager}.
*
* @param mode The mode. Can be either {@link #MODE_ADD_NEW_ALARM} or {@link
* #MODE_ACTIVATE_EXISTING_ALARM}.
* @param mode The mode. Can be either {@link #MODE_ADD_NEW_ALARM} or {@link #MODE_ACTIVATE_EXISTING_ALARM}.
* @param alarmEntity The {@link AlarmEntity} object representing the alarm.
* @param repeatDays The days in which the alarm is to repeat. Can be {@code null} is repeat is OFF.
*/
private void addOrActivateAlarm(int mode, AlarmEntity alarmEntity,
@Nullable ArrayList<Integer> repeatDays) {
//Log.e(this.getClass().getSimpleName(), "addOrActivateAlarm() method called.");

ConstantsAndStatics.cancelScheduledPeriodicWork(this);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

Expand Down Expand Up @@ -331,14 +315,12 @@ private void addOrActivateAlarm(int mode, AlarmEntity alarmEntity,

alarmID = result[0];

//alarmAdapter.add(alarmDateTime, alarmEntity.isRepeatOn, repeatDays, alarmEntity.alarmType);
alarmAdapter = new AlarmAdapter(viewModel.getAlarmDataArrayList(), this, this);
alarmsRecyclerView.swapAdapter(alarmAdapter, false);
alarmsRecyclerView.scrollToPosition(result[1]);

} else {
viewModel.toggleAlarmState(alarmDatabase, alarmEntity.alarmHour, alarmEntity.alarmMinutes, 1);
//alarmAdapter.toggleAlarmState(alarmEntity.alarmHour, alarmEntity.alarmMinutes, 1);
alarmAdapter = new AlarmAdapter(viewModel.getAlarmDataArrayList(), this, this);
alarmsRecyclerView.swapAdapter(alarmAdapter, false);
alarmID = alarmEntity.alarmID;
Expand All @@ -359,34 +341,10 @@ private void addOrActivateAlarm(int mode, AlarmEntity alarmEntity,

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

/*Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, alarmDateTime.getYear());
calendar.set(Calendar.MONTH, alarmDateTime.getMonthValue() - 1);
calendar.set(Calendar.DAY_OF_MONTH, alarmDateTime.getDayOfMonth());
calendar.set(Calendar.HOUR_OF_DAY, alarmDateTime.getHour());
calendar.set(Calendar.MINUTE, alarmDateTime.getMinute());
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);*/

alarmManager.setAlarmClock(new AlarmManager.AlarmClockInfo(zonedDateTime.toEpochSecond() * 1000,
pendingIntent), pendingIntent);

/*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
pendingIntent);
} else {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}*/

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

/*PendingIntent pi = PendingIntent.getBroadcast(Activity_AlarmsList.this, alarmID, intent,
PendingIntent.FLAG_NO_CREATE);
if (pi == null) {
Log.e(this.getClass().getSimpleName(), "addOrActivateAlarm() pending intent not found.");
} else {
Log.e(this.getClass().getSimpleName(), "addOrActivateAlarm() pending intent found.");
}*/
ConstantsAndStatics.schedulePeriodicWork(this);
}

//------------------------------------------------------------------------------------------------------
Expand All @@ -399,7 +357,8 @@ private void addOrActivateAlarm(int mode, AlarmEntity alarmEntity,
* @param mins The alarm minutes.
*/
private void deleteOrDeactivateAlarm(int mode, int hour, int mins) {
//Log.e(this.getClass().getSimpleName(), "deleteOrDeactivateAlarm() called");

ConstantsAndStatics.cancelScheduledPeriodicWork(this);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

Expand All @@ -409,41 +368,25 @@ private void deleteOrDeactivateAlarm(int mode, int hour, int mins) {

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

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

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

if (pendingIntent != null) {
alarmManager.cancel(pendingIntent);
//Log.e(this.getClass().getSimpleName(), "Alarm cancelled.");
} /*else {
Log.e(this.getClass().getSimpleName(), "Alarm not found.");
}*/
}

if (mode == MODE_DELETE_ALARM) {
viewModel.removeAlarm(alarmDatabase, hour, mins);
alarmAdapter = new AlarmAdapter(viewModel.getAlarmDataArrayList(), this, this);
alarmsRecyclerView.swapAdapter(alarmAdapter, false);
//alarmAdapter.remove(hour, mins);
} else {
viewModel.toggleAlarmState(alarmDatabase, hour, mins, 0);
//alarmAdapter.toggleAlarmState(hour, mins, 0);
//No need to swap adapter again.
}


//////////////////////////////////////////////////////////
// Kill any foreground service based on this alarm
/////////////////////////////////////////////////////////
/*if (Service_RingAlarm.isThisServiceRunning) {
if (Service_RingAlarm.alarmHour == hour && Service_RingAlarm.alarmMinute == mins) {
Intent intent1 = new Intent(this, Service_RingAlarm.class);
stopService(intent1);
}
}*/
// Kill any foreground service based on this alarm:
ConstantsAndStatics.killServices(this, alarmID);

ConstantsAndStatics.schedulePeriodicWork(this);
}

//--------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -471,17 +414,17 @@ private void toggleAlarmState(int hour, int mins, final int newAlarmState) {
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);

if (requestCode == NEW_ALARM_REQUEST_CODE) {
/*if (resultCode == RESULT_CANCELED) {
Log.e(this.getClass().getSimpleName(), "No new alarm.");
}*/

if (resultCode == RESULT_OK) {

if (intent != null) {
//Log.e(this.getClass().getSimpleName(), "Received new alarm.");

Bundle data = Objects.requireNonNull(intent.getExtras())
.getBundle(ConstantsAndStatics.BUNDLE_KEY_ALARM_DETAILS);
assert data != null;

AlarmEntity alarmEntity = new AlarmEntity(data.getInt(ConstantsAndStatics.BUNDLE_KEY_ALARM_HOUR),
data.getInt(ConstantsAndStatics.BUNDLE_KEY_ALARM_MINUTE),
true,
Expand All @@ -502,13 +445,9 @@ public void onActivityResult(int requestCode, int resultCode, @Nullable Intent i
}
}
} else if (requestCode == EXISTING_ALARM_REQUEST_CODE) {
/*if (resultCode == RESULT_CANCELED) {
Log.e(this.getClass().getSimpleName(), "No changes made in existing alarm.");
}*/

if (resultCode == RESULT_OK) {
if (intent != null) {
//Log.e(this.getClass().getSimpleName(), "Changes were made in existing alarm.");
Bundle data = Objects.requireNonNull(intent.getExtras())
.getBundle(ConstantsAndStatics.BUNDLE_KEY_ALARM_DETAILS);
assert data != null;
Expand Down Expand Up @@ -545,23 +484,20 @@ public void onActivityResult(int requestCode, int resultCode, @Nullable Intent i

@Override
public void onOnOffButtonClick(int rowNumber, int hour, int mins, int currentAlarmState) {
//Log.e(this.getClass().getSimpleName(), "OnOff Btn clicked.");
toggleAlarmState(hour, mins, currentAlarmState);
}

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

@Override
public void onDeleteButtonClicked(int rowNumber, int hour, int mins) {
//Log.e(this.getClass().getSimpleName(), "Delete button clicked.");
deleteOrDeactivateAlarm(MODE_DELETE_ALARM, hour, mins);
}

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

@Override
public void onItemClicked(int rowNumber, int hour, int mins) {
//Log.e(this.getClass().getSimpleName(), "Row clicked. Row number: " + rowNumber);
Intent intent = new Intent(this, Activity_AlarmDetails.class);
intent.setAction(ConstantsAndStatics.ACTION_EXISTING_ALARM);

Expand Down Expand Up @@ -636,7 +572,6 @@ private boolean isInstalledOnInternalStorage() {
//--------------------------------------------------------------------------------------------------

private void checkForPlugin() {
//Log.e(this.getClass().getSimpleName(), "Checking for plugin");
if (! isInstalledOnInternalStorage()) {
try {
getPackageManager().getApplicationInfo("in.basulabs.shakealarmclockplugin", 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
defaultTheme = ConstantsAndStatics.THEME_SYSTEM;
} else {
defaultTheme = ConstantsAndStatics.THEME_TIME;
defaultTheme = ConstantsAndStatics.THEME_AUTO_TIME;
}

applyTheme();
Expand Down
Loading

0 comments on commit c93be39

Please sign in to comment.