Skip to content

Commit

Permalink
Avoid ScheduledActionService crashing due to new Android 8 restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
rivaldi8 committed Feb 9, 2018
1 parent d44b3f3 commit 1846921
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 39 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
</activity>
<service android:name=".service.ScheduledActionService"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE"
android:label="GnuCash Android Scheduler Execution Service"/>
<service android:name=".util.BackupJob"
android:permission="android.permission.BIND_JOB_SERVICE" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@
import org.gnucash.android.db.adapter.ScheduledActionDbAdapter;
import org.gnucash.android.db.adapter.SplitsDbAdapter;
import org.gnucash.android.db.adapter.TransactionsDbAdapter;
import org.gnucash.android.model.Book;
import org.gnucash.android.model.Commodity;
import org.gnucash.android.model.Money;
import org.gnucash.android.receivers.PeriodicJobReceiver;
import org.gnucash.android.service.ScheduledActionService;
import org.gnucash.android.ui.settings.PreferenceActivity;

Expand Down Expand Up @@ -333,8 +333,10 @@ public static Locale getDefaultLocale() {
* @param context Application context
*/
public static void startScheduledActionExecutionService(Context context){
Intent alarmIntent = new Intent(context, ScheduledActionService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, alarmIntent, PendingIntent.FLAG_NO_CREATE);
Intent alarmIntent = new Intent(context, PeriodicJobReceiver.class);
alarmIntent.setAction(PeriodicJobReceiver.ACTION_BACKUP);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0, alarmIntent,
PendingIntent.FLAG_NO_CREATE);

if (pendingIntent != null) //if service is already scheduled, just return
return;
Expand All @@ -346,7 +348,7 @@ public static void startScheduledActionExecutionService(Context context){
SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_FIFTEEN_MINUTES,
AlarmManager.INTERVAL_HOUR, pendingIntent);

context.startService(alarmIntent); //run the service the first time
ScheduledActionService.enqueueWork(context);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.content.Intent;
import android.util.Log;

import org.gnucash.android.service.ScheduledActionService;
import org.gnucash.android.util.BackupJob;

/**
Expand All @@ -31,6 +32,7 @@ public class PeriodicJobReceiver extends BroadcastReceiver {
private static final String LOG_TAG = "PeriodicJobReceiver";

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

@Override
public void onReceive(Context context, Intent intent) {
Expand All @@ -41,6 +43,8 @@ public void onReceive(Context context, Intent intent) {

if (intent.getAction().equals(ACTION_BACKUP)) {
BackupJob.enqueueWork(context);
} else if (intent.getAction().equals(ACTION_SCHEDULED_ACTIONS)) {
ScheduledActionService.enqueueWork(context);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

package org.gnucash.android.service;

import android.app.IntentService;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.PowerManager;
import android.support.annotation.NonNull;
import android.support.annotation.VisibleForTesting;
import android.support.v4.app.JobIntentService;
import android.util.Log;

import com.crashlytics.android.Crashlytics;
Expand All @@ -40,7 +41,6 @@
import org.gnucash.android.model.Book;
import org.gnucash.android.model.ScheduledAction;
import org.gnucash.android.model.Transaction;
import org.gnucash.android.util.BackupManager;

import java.sql.Timestamp;
import java.util.ArrayList;
Expand All @@ -55,46 +55,40 @@
* Scheduled runs of the service should be achieved using an {@link android.app.AlarmManager}</p>
* @author Ngewi Fet <ngewif@gmail.com>
*/
public class ScheduledActionService extends IntentService {
public class ScheduledActionService extends JobIntentService {

public static final String LOG_TAG = "ScheduledActionService";
private static final String LOG_TAG = "ScheduledActionService";
private static final int JOB_ID = 1001;

public ScheduledActionService() {
super(LOG_TAG);

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

@Override
protected void onHandleIntent(Intent intent) {
protected void onHandleWork(@NonNull Intent intent) {
Log.i(LOG_TAG, "Starting scheduled action service");

PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, LOG_TAG);
wakeLock.acquire();

try {
BooksDbAdapter booksDbAdapter = BooksDbAdapter.getInstance();
List<Book> books = booksDbAdapter.getAllRecords();
for (Book book : books) { //// TODO: 20.04.2017 Retrieve only the book UIDs with new method
DatabaseHelper dbHelper = new DatabaseHelper(GnuCashApplication.getAppContext(), book.getUID());
SQLiteDatabase db = dbHelper.getWritableDatabase();
RecurrenceDbAdapter recurrenceDbAdapter = new RecurrenceDbAdapter(db);
ScheduledActionDbAdapter scheduledActionDbAdapter = new ScheduledActionDbAdapter(db, recurrenceDbAdapter);

List<ScheduledAction> scheduledActions = scheduledActionDbAdapter.getAllEnabledScheduledActions();
Log.i(LOG_TAG, String.format("Processing %d total scheduled actions for Book: %s",
scheduledActions.size(), book.getDisplayName()));
processScheduledActions(scheduledActions, db);

//close all databases except the currently active database
if (!db.getPath().equals(GnuCashApplication.getActiveDb().getPath()))
db.close();
}

Log.i(LOG_TAG, "Completed service @ " + java.text.DateFormat.getDateTimeInstance().format(new Date()));

} finally { //release the lock either way
wakeLock.release();
BooksDbAdapter booksDbAdapter = BooksDbAdapter.getInstance();
List<Book> books = booksDbAdapter.getAllRecords();
for (Book book : books) { //// TODO: 20.04.2017 Retrieve only the book UIDs with new method
DatabaseHelper dbHelper = new DatabaseHelper(GnuCashApplication.getAppContext(), book.getUID());
SQLiteDatabase db = dbHelper.getWritableDatabase();
RecurrenceDbAdapter recurrenceDbAdapter = new RecurrenceDbAdapter(db);
ScheduledActionDbAdapter scheduledActionDbAdapter = new ScheduledActionDbAdapter(db, recurrenceDbAdapter);

List<ScheduledAction> scheduledActions = scheduledActionDbAdapter.getAllEnabledScheduledActions();
Log.i(LOG_TAG, String.format("Processing %d total scheduled actions for Book: %s",
scheduledActions.size(), book.getDisplayName()));
processScheduledActions(scheduledActions, db);

//close all databases except the currently active database
if (!db.getPath().equals(GnuCashApplication.getActiveDb().getPath()))
db.close();
}

Log.i(LOG_TAG, "Completed service @ " + java.text.DateFormat.getDateTimeInstance().format(new Date()));
}

/**
Expand Down

0 comments on commit 1846921

Please sign in to comment.