Skip to content

Commit

Permalink
Backup books daily instead of every time the ScheduledActionService runs
Browse files Browse the repository at this point in the history
  • Loading branch information
rivaldi8 committed Feb 8, 2018
1 parent 96a5bab commit d44b3f3
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 9 deletions.
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ static def gitSha() {


android {
compileSdkVersion 26
buildToolsVersion '27.0.0'
compileSdkVersion 27
buildToolsVersion '27.0.3'
defaultConfig {
applicationId "org.gnucash.android"
testApplicationId 'org.gnucash.android.test'
minSdkVersion 19
targetSdkVersion 26
targetSdkVersion 27
versionCode versionMajor * 10000 + versionMinor * 1000 + versionPatch * 100 + versionBuild
versionName "${versionMajor}.${versionMinor}.${versionPatch}"
resValue "string", "app_version_name", "${versionName}"
Expand Down Expand Up @@ -179,7 +179,7 @@ afterEvaluate {
}


def androidSupportVersion = "26.0.1"
def androidSupportVersion = "27.0.2"
def androidEspressoVersion = "3.0.0"
def androidSupportTestVersion = "1.0.0"

Expand Down
8 changes: 8 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@
<service android:name=".service.ScheduledActionService"
android:exported="false"
android:label="GnuCash Android Scheduler Execution Service"/>
<service android:name=".util.BackupJob"
android:permission="android.permission.BIND_JOB_SERVICE" />
<receiver android:name=".receivers.TransactionRecorder"
android:label="Records transactions received through intents"
android:permission="org.gnucash.android.permission.RECORD_TRANSACTION">
Expand Down Expand Up @@ -154,6 +156,12 @@
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<receiver
android:name=".receivers.PeriodicJobReceiver" android:exported="false">
<intent-filter>
<action android:name="org.gnucash.android.action_backup" />
</intent-filter>
</receiver>
<provider
android:authorities="${applicationId}.fileprovider"
android:name="android.support.v4.content.FileProvider"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@
import android.content.Intent;

import org.gnucash.android.app.GnuCashApplication;
import org.gnucash.android.util.BackupManager;

/**
* Receiver which is called when the device finishes booting.
* It starts the service for running scheduled events
* It schedules periodic jobs.
* @author Ngewi Fet <ngewif@gmail.com>
*/
public class BootReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
GnuCashApplication.startScheduledActionExecutionService(context);
BackupManager.schedulePeriodicBackups(context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* Copyright (c) 2018 Àlex Magaz Graça <alexandre.magaz@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.gnucash.android.receivers;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import org.gnucash.android.util.BackupJob;

/**
* Receiver to run periodic jobs.
*
* @author Àlex Magaz Graça <alexandre.magaz@gmail.com>
*/
public class PeriodicJobReceiver extends BroadcastReceiver {
private static final String LOG_TAG = "PeriodicJobReceiver";

public static final String ACTION_BACKUP = "org.gnucash.android.action_backup";

@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() == null) {
Log.w(LOG_TAG, "No action was set in the intent. Ignoring...");
return;
}

if (intent.getAction().equals(ACTION_BACKUP)) {
BackupJob.enqueueWork(context);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ protected void onHandleIntent(Intent intent) {
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, LOG_TAG);
wakeLock.acquire();

BackupManager.backupAllBooks(); //First run automatic backup of all books before doing anything else
try {
BooksDbAdapter booksDbAdapter = BooksDbAdapter.getInstance();
List<Book> books = booksDbAdapter.getAllRecords();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ private void init() {
showWhatsNewDialog(this);
}
GnuCashApplication.startScheduledActionExecutionService(this);
BackupManager.schedulePeriodicBackups(this);
}

@Override
Expand Down
46 changes: 46 additions & 0 deletions app/src/main/java/org/gnucash/android/util/BackupJob.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* Copyright (c) 2018 Àlex Magaz Graça <alexandre.magaz@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.gnucash.android.util;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.JobIntentService;
import android.util.Log;


/**
* Job to back up books periodically.
*
* <p>The backups are triggered by an alarm set in
* {@link BackupManager#schedulePeriodicBackups(Context)}
* (through {@link org.gnucash.android.receivers.PeriodicJobReceiver}).</p>
*/
public class BackupJob extends JobIntentService {
private static final String LOG_TAG = "BackupJob";
private static final int JOB_ID = 1000;

public static void enqueueWork(Context context) {
Intent intent = new Intent(context, BackupJob.class);
enqueueWork(context, BackupJob.class, JOB_ID, intent);
}

@Override
protected void onHandleWork(@NonNull Intent intent) {
Log.i(LOG_TAG, "Doing backup of all books.");
BackupManager.backupAllBooks();
}
}
41 changes: 38 additions & 3 deletions app/src/main/java/org/gnucash/android/util/BackupManager.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
/* Copyright (c) 2018 Àlex Magaz Graça <alexandre.magaz@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.gnucash.android.util;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.ArrayAdapter;

import com.crashlytics.android.Crashlytics;

Expand All @@ -16,6 +34,7 @@
import org.gnucash.android.export.Exporter;
import org.gnucash.android.export.xml.GncXmlExporter;
import org.gnucash.android.model.Book;
import org.gnucash.android.receivers.PeriodicJobReceiver;
import org.gnucash.android.ui.settings.PreferenceActivity;

import java.io.BufferedOutputStream;
Expand All @@ -29,15 +48,19 @@
import java.util.List;
import java.util.zip.GZIPOutputStream;


/**
* Deals with all backup-related tasks.
*/
public class BackupManager {
private static final String LOG_TAG = "BackupManager";
public static final String KEY_BACKUP_FILE = "book_backup_file_key";

/**
* Perform an automatic backup of all books in the database.
* This method is run everytime the service is executed
* This method is run every time the service is executed
*/
public static void backupAllBooks() {
static void backupAllBooks() {
BooksDbAdapter booksDbAdapter = BooksDbAdapter.getInstance();
List<String> bookUIDs = booksDbAdapter.getAllBookUIDs();
Context context = GnuCashApplication.getAppContext();
Expand Down Expand Up @@ -155,4 +178,16 @@ public static List<File> getBackupList(String bookUID) {
Collections.reverse(backupFilesList);
return backupFilesList;
}

public static void schedulePeriodicBackups(Context context) {
Log.i(LOG_TAG, "Scheduling backup job");
Intent intent = new Intent(context, PeriodicJobReceiver.class);
intent.setAction(PeriodicJobReceiver.ACTION_BACKUP);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context,0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_FIFTEEN_MINUTES,
AlarmManager.INTERVAL_DAY, alarmIntent);
}
}

0 comments on commit d44b3f3

Please sign in to comment.