Skip to content

Commit

Permalink
Show notifications even when app is in background (fixes ankidroid#4615)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutomic committed Sep 5, 2017
1 parent 1c80630 commit 273c93c
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 14 deletions.
6 changes: 4 additions & 2 deletions AnkiDroid/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,13 @@
</intent-filter>
</receiver>

<receiver android:name=".receiver.NotificationReceiver"
android:enabled="true"
android:exported="false" />
<receiver
android:name=".receiver.ReminderReceiver"
android:enabled="true"
android:exported="false">
</receiver>
android:exported="false" />
<receiver
android:name=".receiver.BootReceiver"
android:enabled="true"
Expand Down
3 changes: 3 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/anki/AnkiDroidApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
Expand All @@ -30,6 +31,7 @@

import com.ichi2.anki.dialogs.AnkiDroidCrashReportDialog;
import com.ichi2.anki.exception.StorageAccessException;
import com.ichi2.anki.services.BootService;
import com.ichi2.compat.CompatHelper;
import com.ichi2.utils.LanguageUtil;

Expand Down Expand Up @@ -198,6 +200,7 @@ public void onCreate() {
}
}
}
startService(new Intent(this, BootService.class));
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.ichi2.anki.receiver;

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

import com.ichi2.anki.services.NotificationService;

public class NotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final Intent serviceIntent = new Intent(context, NotificationService.class);
context.startService(serviceIntent);
}
}
35 changes: 33 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/anki/services/BootService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.app.IntentService;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.ichi2.anki.CollectionHelper;
import com.ichi2.anki.receiver.ReminderReceiver;
Expand All @@ -15,14 +17,21 @@
import java.util.Calendar;

public class BootService extends IntentService {

private AlarmManager mAlarmManager;

public BootService() {
super("BootService");
}

@Override
protected void onHandleIntent(Intent intent) {
final AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
scheduleDeckReminder();
scheduleNotification();
}

private void scheduleDeckReminder() {
try {
for (JSONObject deck : CollectionHelper.getInstance().getCol(this).getDecks().all()) {
Collection col = CollectionHelper.getInstance().getCol(this);
Expand All @@ -49,7 +58,7 @@ protected void onHandleIntent(Intent intent) {
calendar.set(Calendar.MINUTE, reminder.getJSONArray("time").getInt(1));
calendar.set(Calendar.SECOND, 0);

alarmManager.setInexactRepeating(
mAlarmManager.setInexactRepeating(
AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY,
Expand All @@ -62,4 +71,26 @@ protected void onHandleIntent(Intent intent) {
throw new RuntimeException(e);
}
}

private void scheduleNotification() {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
if (Integer.parseInt(sp.getString("minimumCardsDueForNotification", "1000001")) == 1000001)
return;

final Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, sp.getInt("dayOffset", 0));
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);

final PendingIntent notificationIntent =
PendingIntent.getBroadcast(this, 0, new Intent(this, ReminderReceiver.class), 0);
mAlarmManager.setInexactRepeating(
AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY,
notificationIntent
);

// TODO: need to cancel alarm when notifications are disabled
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import android.support.v4.content.ContextCompat;
import android.support.v4.content.IntentCompat;


import com.ichi2.anki.AnkiDroidApp;
import com.ichi2.anki.DeckPicker;
import com.ichi2.anki.R;
Expand Down
11 changes: 2 additions & 9 deletions AnkiDroid/src/main/java/com/ichi2/widget/WidgetStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
public final class WidgetStatus {

private static boolean sSmallWidgetEnabled = false;
private static boolean sNotificationEnabled = false;
private static AsyncTask<Context, Void, Context> sUpdateDeckStatusAsyncTask;


Expand All @@ -53,9 +52,8 @@ private WidgetStatus() {
public static void update(Context context) {
SharedPreferences preferences = AnkiDroidApp.getSharedPrefs(context);
sSmallWidgetEnabled = preferences.getBoolean("widgetSmallEnabled", false);
sNotificationEnabled = Integer.parseInt(preferences.getString("minimumCardsDueForNotification", "1000001")) < 1000000;
if ((sSmallWidgetEnabled || sNotificationEnabled)
&& ((sUpdateDeckStatusAsyncTask == null) || (sUpdateDeckStatusAsyncTask.getStatus() == AsyncTask.Status.FINISHED))) {
if (sSmallWidgetEnabled &&
((sUpdateDeckStatusAsyncTask == null) || (sUpdateDeckStatusAsyncTask.getStatus() == AsyncTask.Status.FINISHED))) {
Timber.d("WidgetStatus.update(): updating");
sUpdateDeckStatusAsyncTask = new UpdateDeckStatusAsyncTask();
sUpdateDeckStatusAsyncTask.execute(context);
Expand Down Expand Up @@ -108,11 +106,6 @@ protected void onPostExecute(Context context) {
intent = new Intent(context, AnkiDroidWidgetSmall.UpdateService.class);
context.startService(intent);
}
if (sNotificationEnabled) {
Intent intent;
intent = new Intent(context, NotificationService.class);
context.startService(intent);
}
}


Expand Down

0 comments on commit 273c93c

Please sign in to comment.