From 50a8aece6f0f797932ec9359047f3c599b8fabb5 Mon Sep 17 00:00:00 2001 From: Alinson Xavier Date: Mon, 4 Apr 2016 08:18:59 -0400 Subject: [PATCH] Move expensive call to background thread --- .../uhabits/HabitBroadcastReceiver.java | 115 ++++++++++-------- 1 file changed, 65 insertions(+), 50 deletions(-) diff --git a/app/src/main/java/org/isoron/uhabits/HabitBroadcastReceiver.java b/app/src/main/java/org/isoron/uhabits/HabitBroadcastReceiver.java index 501b00454..057aedfba 100644 --- a/app/src/main/java/org/isoron/uhabits/HabitBroadcastReceiver.java +++ b/app/src/main/java/org/isoron/uhabits/HabitBroadcastReceiver.java @@ -40,6 +40,7 @@ import org.isoron.uhabits.helpers.DateHelper; import org.isoron.uhabits.helpers.ReminderHelper; import org.isoron.uhabits.models.Checkmark; import org.isoron.uhabits.models.Habit; +import org.isoron.uhabits.tasks.BaseTask; import java.util.Date; @@ -138,62 +139,76 @@ public class HabitBroadcastReceiver extends BroadcastReceiver } - private void createNotification(Context context, Intent intent) + private void createNotification(final Context context, final Intent intent) { - Uri data = intent.getData(); - Habit habit = Habit.get(ContentUris.parseId(data)); - Long timestamp = intent.getLongExtra("timestamp", DateHelper.getStartOfToday()); - Long reminderTime = intent.getLongExtra("reminderTime", DateHelper.getStartOfToday()); + final Uri data = intent.getData(); + final Habit habit = Habit.get(ContentUris.parseId(data)); + final Long timestamp = intent.getLongExtra("timestamp", DateHelper.getStartOfToday()); + final Long reminderTime = intent.getLongExtra("reminderTime", DateHelper.getStartOfToday()); if (habit == null) return; - if (habit.checkmarks.getTodayValue() != Checkmark.UNCHECKED) return; - - habit.highlight = 1; - habit.save(); - - if (!checkWeekday(intent, habit)) return; - - // Check if reminder has been turned off after alarm was scheduled - if (habit.reminderHour == null) return; - - Intent contentIntent = new Intent(context, MainActivity.class); - contentIntent.setData(data); - PendingIntent contentPendingIntent = - PendingIntent.getActivity(context, 0, contentIntent, 0); - - PendingIntent dismissPendingIntent = buildDismissIntent(context); - PendingIntent checkIntentPending = buildCheckIntent(context, habit, timestamp); - PendingIntent snoozeIntentPending = buildSnoozeIntent(context, habit); - - Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); - - NotificationCompat.WearableExtender wearableExtender = - new NotificationCompat.WearableExtender().setBackground( - BitmapFactory.decodeResource(context.getResources(), R.drawable.stripe)); - - Notification notification = - new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_notification) - .setContentTitle(habit.name) - .setContentText(habit.description) - .setContentIntent(contentPendingIntent) - .setDeleteIntent(dismissPendingIntent) - .addAction(R.drawable.ic_action_check, - context.getString(R.string.check), checkIntentPending) - .addAction(R.drawable.ic_action_snooze, - context.getString(R.string.snooze), snoozeIntentPending) - .setSound(soundUri) - .extend(wearableExtender) - .setWhen(reminderTime) - .setShowWhen(true) - .build(); - notification.flags |= Notification.FLAG_AUTO_CANCEL; + new BaseTask() + { + int todayValue; - NotificationManager notificationManager = - (NotificationManager) context.getSystemService(Activity.NOTIFICATION_SERVICE); + @Override + protected void doInBackground() + { + todayValue = habit.checkmarks.getTodayValue(); + } - int notificationId = (int) (habit.getId() % Integer.MAX_VALUE); - notificationManager.notify(notificationId, notification); + @Override + protected void onPostExecute(Void aVoid) + { + if (todayValue != Checkmark.UNCHECKED) return; + if (!checkWeekday(intent, habit)) return; + + // Check if reminder has been turned off after alarm was scheduled + if (habit.reminderHour == null) return; + + Intent contentIntent = new Intent(context, MainActivity.class); + contentIntent.setData(data); + PendingIntent contentPendingIntent = + PendingIntent.getActivity(context, 0, contentIntent, 0); + + PendingIntent dismissPendingIntent = buildDismissIntent(context); + PendingIntent checkIntentPending = buildCheckIntent(context, habit, timestamp); + PendingIntent snoozeIntentPending = buildSnoozeIntent(context, habit); + + Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); + + NotificationCompat.WearableExtender wearableExtender = + new NotificationCompat.WearableExtender().setBackground( + BitmapFactory.decodeResource(context.getResources(), R.drawable.stripe)); + + Notification notification = + new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_notification) + .setContentTitle(habit.name) + .setContentText(habit.description) + .setContentIntent(contentPendingIntent) + .setDeleteIntent(dismissPendingIntent) + .addAction(R.drawable.ic_action_check, + context.getString(R.string.check), checkIntentPending) + .addAction(R.drawable.ic_action_snooze, + context.getString(R.string.snooze), snoozeIntentPending) + .setSound(soundUri) + .extend(wearableExtender) + .setWhen(reminderTime) + .setShowWhen(true) + .build(); + + notification.flags |= Notification.FLAG_AUTO_CANCEL; + + NotificationManager notificationManager = + (NotificationManager) context.getSystemService(Activity.NOTIFICATION_SERVICE); + + int notificationId = (int) (habit.getId() % Integer.MAX_VALUE); + notificationManager.notify(notificationId, notification); + + super.onPostExecute(aVoid); + } + }.execute(); } public static PendingIntent buildSnoozeIntent(Context context, Habit habit)