Skip to content

Commit

Permalink
Implement backup integration
Browse files Browse the repository at this point in the history
- Add SCHEDULE_EXACT_ALARM permission
- Add FLAG_IMMUTABLE to PendingIntents
  • Loading branch information
Tobias Länge committed Oct 27, 2022
1 parent 74e315b commit 8d562dd
Show file tree
Hide file tree
Showing 19 changed files with 382 additions and 42 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "libs/privacy-friendly-backup-api"]
path = libs/privacy-friendly-backup-api
url = https://github.com/SecUSo/privacy-friendly-backup-api.git
14 changes: 14 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

android {
compileSdkVersion 32
Expand Down Expand Up @@ -41,4 +42,17 @@ dependencies {
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
testImplementation 'junit:junit:4.13.2'

def room_version = "2.4.3"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
kapt "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-ktx:$room_version"

// Backup API
implementation project(':backup-api')
def work_version = '2.7.1'
implementation "androidx.work:work-runtime:$work_version"
implementation "androidx.work:work-runtime-ktx:$work_version"
androidTestImplementation "androidx.work:work-testing:$work_version"
}
53 changes: 35 additions & 18 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="org.secuso.privacyfriendlycircuittraining">

<supports-screens
Expand All @@ -13,7 +14,10 @@
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />


<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />

<application
android:name=".PFCircuitTrainingApplication"
android:allowBackup="true"
android:icon="@mipmap/icon"
android:label="@string/app_name"
Expand Down Expand Up @@ -80,22 +84,6 @@
android:name="android.support.PARENT_ACTIVITY"
android:value="org.secuso.privacyfriendlycircuittraining.activities.SettingsActivity" />
</activity>

<service
android:name="org.secuso.privacyfriendlycircuittraining.services.TimerService"
android:enabled="true"
android:exported="true" />

<receiver
android:name="org.secuso.privacyfriendlycircuittraining.receivers.OnBootCompletedBroadcastReceiver"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

<receiver android:name="org.secuso.privacyfriendlycircuittraining.receivers.MotivationAlertReceiver" />

<activity
android:name="org.secuso.privacyfriendlycircuittraining.activities.StatisticsActivity"
android:label="@string/title_activity_statistics"
Expand All @@ -105,7 +93,6 @@
android:name="android.support.PARENT_ACTIVITY"
android:value="org.secuso.privacyfriendlycircuittraining.activities.MainActivity" />
</activity>

<activity
android:name="org.secuso.privacyfriendlycircuittraining.activities.ExerciseSetActivity"
android:label="@string/title_activity_exercisesets"
Expand All @@ -115,7 +102,6 @@
android:name="android.support.PARENT_ACTIVITY"
android:value="org.secuso.privacyfriendlycircuittraining.activities.MainActivity" />
</activity>

<activity
android:name="org.secuso.privacyfriendlycircuittraining.activities.ExerciseActivity"
android:label="@string/title_activity_exercises"
Expand All @@ -125,6 +111,37 @@
android:name="android.support.PARENT_ACTIVITY"
android:value="org.secuso.privacyfriendlycircuittraining.activities.MainActivity" />
</activity>

<service
android:name=".backup.PFABackupService"
android:enabled="true"
android:exported="true"
android:process=":backup"
tools:ignore="ExportedService">
<intent-filter>
<action android:name="org.secuso.privacyfriendlybackup.api.pfa.PFAAuthService" />
</intent-filter>
</service>

<service
android:name="org.secuso.privacyfriendlycircuittraining.services.TimerService"
android:enabled="true"
android:exported="true" />

<receiver
android:name="org.secuso.privacyfriendlycircuittraining.receivers.OnBootCompletedBroadcastReceiver"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

<receiver android:name="org.secuso.privacyfriendlycircuittraining.receivers.MotivationAlertReceiver" />

<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
tools:node="remove" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.secuso.privacyfriendlycircuittraining

import android.app.Application
import android.util.Log
import androidx.work.Configuration
import org.secuso.privacyfriendlybackup.api.pfa.BackupManager
import org.secuso.privacyfriendlycircuittraining.backup.BackupCreator
import org.secuso.privacyfriendlycircuittraining.backup.BackupRestorer

class PFCircuitTrainingApplication : Application(), Configuration.Provider {

override fun onCreate() {
super.onCreate()
BackupManager.backupCreator = BackupCreator()
BackupManager.backupRestorer = BackupRestorer()
}

override fun getWorkManagerConfiguration(): Configuration {
return Configuration.Builder().setMinimumLoggingLevel(Log.INFO).build()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@

package org.secuso.privacyfriendlycircuittraining.activities;

import android.app.AlarmManager;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceActivity;
Expand All @@ -40,6 +42,7 @@

import org.secuso.privacyfriendlycircuittraining.R;
import org.secuso.privacyfriendlycircuittraining.database.PFASQLiteHelper;
import org.secuso.privacyfriendlycircuittraining.fragments.GrantExactAlarmPermissionDialogFragment;
import org.secuso.privacyfriendlycircuittraining.helpers.NotificationHelper;
import org.secuso.privacyfriendlycircuittraining.models.Exercise;
import org.secuso.privacyfriendlycircuittraining.models.ExerciseSet;
Expand Down Expand Up @@ -153,7 +156,16 @@ protected void onCreate(Bundle savedInstanceState) {

//Schedule the next motivation notification (necessary if permission was not granted)
if(NotificationHelper.isMotivationAlertEnabled(this)){
NotificationHelper.setMotivationAlert(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
if (!am.canScheduleExactAlarms()) { //Check permission to schedule exact alarm on versions >= Android S
new GrantExactAlarmPermissionDialogFragment().show(getFragmentManager(), GrantExactAlarmPermissionDialogFragment.TAG);
} else {
NotificationHelper.setMotivationAlert(this);
}
} else {
NotificationHelper.setMotivationAlert(this);
}
}

//Set the change listener for the switch button to turn block periodization on and off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.secuso.privacyfriendlycircuittraining.activities;

import android.annotation.TargetApi;
import android.app.AlarmManager;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
Expand All @@ -31,6 +32,7 @@
import android.view.MenuItem;

import org.secuso.privacyfriendlycircuittraining.R;
import org.secuso.privacyfriendlycircuittraining.fragments.GrantExactAlarmPermissionDialogFragment;
import org.secuso.privacyfriendlycircuittraining.helpers.NotificationHelper;

import java.util.List;
Expand Down Expand Up @@ -225,7 +227,17 @@ public void onCreate(Bundle savedInstanceState) {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if((Boolean) newValue){
NotificationHelper.setMotivationAlert(preference.getContext());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
AlarmManager am = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
if (!am.canScheduleExactAlarms()) { //Check permission to schedule exact alarm on versions >= Android S
new GrantExactAlarmPermissionDialogFragment().show(getChildFragmentManager(), GrantExactAlarmPermissionDialogFragment.TAG);
return false;
} else {
NotificationHelper.setMotivationAlert(preference.getContext());
}
} else {
NotificationHelper.setMotivationAlert(preference.getContext());
}
}
else{
NotificationHelper.cancelMotivationAlert(preference.getContext());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.secuso.privacyfriendlycircuittraining.backup

import android.content.Context
import android.preference.PreferenceManager
import android.util.JsonWriter
import android.util.Log
import org.secuso.privacyfriendlybackup.api.backup.DatabaseUtil.getSupportSQLiteOpenHelper
import org.secuso.privacyfriendlybackup.api.backup.DatabaseUtil.writeDatabase
import org.secuso.privacyfriendlybackup.api.backup.PreferenceUtil.writePreferences
import org.secuso.privacyfriendlybackup.api.pfa.IBackupCreator
import org.secuso.privacyfriendlycircuittraining.database.PFASQLiteHelper
import java.io.OutputStream
import java.io.OutputStreamWriter

class BackupCreator : IBackupCreator {
override fun writeBackup(context: Context, outputStream: OutputStream): Boolean {
Log.d(TAG, "createBackup() started")
val outputStreamWriter = OutputStreamWriter(outputStream, Charsets.UTF_8)
val writer = JsonWriter(outputStreamWriter)
writer.setIndent("")

try {
writer.beginObject()

Log.d(TAG, "Writing database")
writer.name("database")

val database = getSupportSQLiteOpenHelper(context, PFASQLiteHelper.DATABASE_NAME).readableDatabase

writeDatabase(writer, database)
database.close()

Log.d(TAG, "Writing preferences")
writer.name("preferences")

val pref = PreferenceManager.getDefaultSharedPreferences(context.applicationContext)
writePreferences(writer, pref)

writer.endObject()
writer.close()
} catch (e: Exception) {
Log.e(TAG, "Error occurred", e)
e.printStackTrace()
return false
}

Log.d(TAG, "Backup created successfully")
return true
}

companion object {
const val TAG = "PFABackupCreator"
}
}
Loading

0 comments on commit 8d562dd

Please sign in to comment.