Skip to content

Commit

Permalink
Support for Direct Boot
Browse files Browse the repository at this point in the history
  • Loading branch information
WrichikBasu committed Feb 18, 2024
1 parent fd9c9d2 commit 90a7e81
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 25 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ android {
applicationId "in.basulabs.shakealarmclock"
minSdkVersion 21
targetSdkVersion 34
versionCode 32
versionName "4.0.0"
versionCode 33
versionName "4.1.0-beta"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
android:name=".backend.Service_SnoozeAlarm"
android:enabled="true"
android:exported="false"
android:directBootAware="true"
android:foregroundServiceType="specialUse">
<property
android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
Expand All @@ -94,6 +95,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
android:name=".backend.Service_RingAlarm"
android:enabled="true"
android:exported="false"
android:directBootAware="true"
android:foregroundServiceType="specialUse">
<property
android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
Expand All @@ -104,13 +106,17 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
<receiver
android:name=".backend.AlarmBroadcastReceiver"
android:enabled="true"
android:exported="true">
android:exported="true"
android:directBootAware="true">
<intent-filter>
<action android:name="in.basulabs.shakealarmclock.DELIVER_ALARM" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
</intent-filter>
</receiver>

<activity
Expand Down Expand Up @@ -166,6 +172,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
android:name=".backend.Service_SetAlarmsPostBoot"
android:enabled="true"
android:exported="true"
android:directBootAware="true"
android:foregroundServiceType="specialUse">
<property
android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public void onReceive(Context context, Intent intent) {
.getBundle(ConstantsAndStatics.BUNDLE_KEY_ALARM_DETAILS));
ContextCompat.startForegroundService(context, intent1);

} else if (Objects.equals(intent.getAction(), Intent.ACTION_BOOT_COMPLETED)) {
} else if (Objects.equals(intent.getAction(), Intent.ACTION_BOOT_COMPLETED) ||
Objects.equals(intent.getAction(), Intent.ACTION_LOCKED_BOOT_COMPLETED)) {

PowerManager powerManager = (PowerManager) context.getSystemService(
POWER_SERVICE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package in.basulabs.shakealarmclock.backend;

import android.content.Context;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.room.Database;
Expand All @@ -31,9 +32,6 @@
@TypeConverters({Convertors.class})
public abstract class AlarmDatabase extends RoomDatabase {

private static final String DATABASE_NAME
= "in_basulabs_shakeAlarmClock_AlarmDatabase";

private static AlarmDatabase instance;

static final Migration MIGRATION_1_2 = new Migration(1, 2) {
Expand All @@ -44,10 +42,15 @@ public void migrate(@NonNull SupportSQLiteDatabase database) {
}
};

public static synchronized AlarmDatabase getInstance(Context context) {
public static synchronized AlarmDatabase getInstance(@NonNull Context context) {

if (instance == null) {
instance = Room.databaseBuilder(context.getApplicationContext(),
AlarmDatabase.class, DATABASE_NAME)

instance = Room.databaseBuilder(
Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
? context.createDeviceProtectedStorageContext()
: context.getApplicationContext(),
AlarmDatabase.class, ConstantsAndStatics.DATABASE_NAME)
.addMigrations(MIGRATION_1_2)
.fallbackToDestructiveMigrationOnDowngrade()
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,12 @@ public final class ConstantsAndStatics {
public static final String WORK_TAG_ACTIVATE_ALARMS =
"in.basulabs.WORK_ACTIVATE_ALARMS";

public static final String DATABASE_NAME =
"in_basulabs_shakeAlarmClock_AlarmDatabase";

public static final String SHARED_PREF_KEY_DATABASE_MOVED =
"in.basulabs.shakealarmclock.DATABASE_MOVED";

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package in.basulabs.shakealarmclock.frontend;

import static in.basulabs.shakealarmclock.backend.ConstantsAndStatics.DEBUG_TAG;
import static in.basulabs.shakealarmclock.backend.ConstantsAndStatics.DATABASE_NAME;

import android.app.AlarmManager;
import android.app.NotificationChannel;
Expand All @@ -31,7 +31,6 @@
import android.os.Looper;
import android.os.Message;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
Expand Down Expand Up @@ -107,16 +106,18 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_alarmslist);
setSupportActionBar(findViewById(R.id.toolbar));

sharedPref = getSharedPreferences(ConstantsAndStatics.SHARED_PREF_FILE_NAME,
MODE_PRIVATE);
sharedPrefEditor = sharedPref.edit();

moveDatabase();

alarmDatabase = AlarmDatabase.getInstance(this);
viewModel = new ViewModelProvider(this).get(ViewModel_AlarmsList.class);

// Initialise the view model:
viewModel.init(alarmDatabase);

sharedPref = getSharedPreferences(ConstantsAndStatics.SHARED_PREF_FILE_NAME,
MODE_PRIVATE);
sharedPrefEditor = sharedPref.edit();

// Find and set the app theme:
int defaultTheme = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q ?
ConstantsAndStatics.THEME_SYSTEM : ConstantsAndStatics.THEME_AUTO_TIME;
Expand Down Expand Up @@ -204,12 +205,7 @@ protected void onResume() {
// Note that if essential perms have been requested in the current
// session, non-essential perms will not be asked in this session.
if (viewModel.getCanRequestNonEssentialPerms()) {
Log.d(DEBUG_TAG, "Can request non-essential " +
"perms now.");
requestNonEssentialPermsOnly();
} else {
Log.d(DEBUG_TAG, "Cannot request non-essential " +
"perms now.");
}
}
}
Expand Down Expand Up @@ -851,8 +847,6 @@ private void deleteNotifChannels() {
*/
private void setCanAskForNonEssentialPerms() {

Log.d(DEBUG_TAG, "Started.");

long numberOfTimesAppOpened =
sharedPref.getLong(
ConstantsAndStatics.SHARED_PREF_KEY_NO_OF_TIMES_APP_OPENED,
Expand Down Expand Up @@ -890,9 +884,31 @@ && getIntent().getAction()
ConstantsAndStatics.SHARED_PREF_KEY_NO_OF_TIMES_APP_OPENED,
numberOfTimesAppOpened).commit();

Log.d(DEBUG_TAG, "Ended.");
}

private void moveDatabase(){

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

if (getDatabasePath(DATABASE_NAME).exists()) {

if (!sharedPref.getBoolean(
ConstantsAndStatics.SHARED_PREF_KEY_DATABASE_MOVED,
false)) {

createDeviceProtectedStorageContext().moveDatabaseFrom(
getApplicationContext(), DATABASE_NAME);

Log.d(DEBUG_TAG, ""+ viewModel.getCanRequestNonEssentialPerms());
sharedPrefEditor.putBoolean(
ConstantsAndStatics.SHARED_PREF_KEY_DATABASE_MOVED, true)
.commit();

//todo remove:
Toast.makeText(this, "Moved database.", Toast.LENGTH_LONG).show();
}

}
}

}

Expand Down

0 comments on commit 90a7e81

Please sign in to comment.