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 18, 2018
1 parent b5b666a commit dea1741
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 43 deletions.
2 changes: 2 additions & 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 Expand Up @@ -160,6 +161,7 @@
android:name=".receivers.PeriodicJobReceiver" android:exported="false">
<intent-filter>
<action android:name="org.gnucash.android.action_backup" />
<action android:name="org.gnucash.android.action_scheduled_actions" />
</intent-filter>
</receiver>
<provider
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,20 +333,22 @@ 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_SCHEDULED_ACTIONS);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0, alarmIntent,
PendingIntent.FLAG_NO_CREATE);

if (pendingIntent != null) //if service is already scheduled, just return
return;
else
pendingIntent = PendingIntent.getService(context, 0, alarmIntent, 0);
pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
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,17 +20,21 @@
import android.content.Intent;
import android.util.Log;

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

/**
* Receiver to run periodic jobs.
*
* <p>For now, backups and scheduled actions.</p>
*
* @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";
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 +45,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 @@ -50,51 +50,50 @@

/**
* Service for running scheduled events.
* <p>The service is started and goes through all scheduled event entries in the the database and executes them.
* Then it is stopped until the next time it is run. <br>
* Scheduled runs of the service should be achieved using an {@link android.app.AlarmManager}</p>
*
* <p>It's run every time the <code>enqueueWork</code> is called. It goes
* through all scheduled event entries in the the database and executes them.</p>
*
* <p>Scheduled runs of the service should be achieved using an
* {@link android.app.AlarmManager}, with
* {@link org.gnucash.android.receivers.PeriodicJobReceiver} as an intermediary.</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 dea1741

Please sign in to comment.