0
0
mirror of https://github.com/ankidroid/Anki-Android.git synced 2024-09-20 20:03:05 +02:00

Fix the notification and widget counts

This commit is contained in:
timrae 2015-09-08 23:37:53 +09:00
parent 0b385f6a40
commit f2eb8d87ae
3 changed files with 26 additions and 36 deletions

View File

@ -440,13 +440,6 @@ public class AnkiActivity extends AppCompatActivity implements LoaderManager.Loa
Intent resultIntent = new Intent(this, DeckPicker.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
if (resultPendingIntent == null) {
// PendingIntent could not be created... probably something wrong with the extras
// try again without the extras, though the original dialog will not be shown when app started
Timber.e("AnkiActivity.showSimpleNotification() failed due to null PendingIntent");
resultIntent = new Intent(this, DeckPicker.class);
resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}
builder.setContentIntent(resultPendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.

View File

@ -14,14 +14,16 @@
package com.ichi2.anki.services;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.IntentCompat;
import com.ichi2.anki.AnkiDroidApp;
@ -48,45 +50,40 @@ public class NotificationService extends Service {
@Override
public void onStart(Intent intent, int startId) {
Timber.i("NotificationService: OnStart");
public int onStartCommand(Intent intent, int flags, int startId) {
Timber.i("NotificationService: OnStartCommand");
Context context = AnkiDroidApp.getInstance().getBaseContext();
Context context = getApplicationContext();
SharedPreferences preferences = AnkiDroidApp.getSharedPrefs(context);
int minimumCardsDueForNotification = Integer.parseInt(preferences.getString("minimumCardsDueForNotification",
"25"));
int minCardsDue = Integer.parseInt(preferences.getString("minimumCardsDueForNotification", "25"));
int dueCardsCount = WidgetStatus.fetchDue(context);
if (dueCardsCount >= minimumCardsDueForNotification) {
// Show a notification
int icon = R.drawable.ic_stat_notify;
CharSequence tickerText = String.format(
getString(R.string.widget_minimum_cards_due_notification_ticker_text), dueCardsCount);
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
if (dueCardsCount >= minCardsDue) {
// Build basic notification
String cardsDueText = getString(R.string.widget_minimum_cards_due_notification_ticker_text, dueCardsCount);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_notify)
.setContentTitle(cardsDueText)
.setTicker(cardsDueText);
// Enable vibrate and blink if set in preferences
if (preferences.getBoolean("widgetVibrate", false)) {
notification.defaults |= Notification.DEFAULT_VIBRATE;
builder.setVibrate(new long[] { 1000, 1000, 1000});
}
if (preferences.getBoolean("widgetBlink", false)) {
notification.defaults |= Notification.DEFAULT_LIGHTS;
builder.setLights(Color.BLUE, 1000, 1000);
}
Context appContext = getApplicationContext();
CharSequence contentTitle = getText(R.string.widget_minimum_cards_due_notification_ticker_title);
Intent ankiDroidIntent = new Intent(context, DeckPicker.class);
ankiDroidIntent.setAction(Intent.ACTION_MAIN);
ankiDroidIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingAnkiDroidIntent = PendingIntent.getActivity(context, 0, ankiDroidIntent,
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(context, DeckPicker.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(appContext, contentTitle, tickerText, pendingAnkiDroidIntent);
mNotificationManager.notify(WIDGET_NOTIFY_ID, notification);
builder.setContentIntent(resultPendingIntent);
// mId allows you to update the notification later on.
mNotificationManager.notify(WIDGET_NOTIFY_ID, builder.build());
} else {
// Cancel the existing notification, if any.
mNotificationManager.cancel(WIDGET_NOTIFY_ID);
}
return START_STICKY;
}

View File

@ -2425,7 +2425,7 @@ public class Sched {
for (String s : cs) {
done += deck.getJSONArray(s + "Today").getInt(1);
}
mCachedDeckCounts.put(d.did, new Pair<String[], long[]> (d.names, new long[]{done, d.newCount, d.lrnCount, d.newCount}));
mCachedDeckCounts.put(d.did, new Pair<String[], long[]> (d.names, new long[]{done, d.newCount, d.lrnCount, d.revCount}));
}
}