From 9211b049e04d0da4f8ec0c9b565b69bbbb8b60c8 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Mon, 8 Jun 2015 17:22:45 -0500 Subject: [PATCH] Add NotificationActivity Replaces NotificationFragment when opening notification --- src/main/AndroidManifest.xml | 10 +- .../reminders/NotificationFragment.java | 89 ----------------- .../astrid/reminders/Notifications.java | 24 ++--- .../reminders/ShowNotificationReceiver.java | 4 +- src/main/java/org/tasks/Broadcaster.java | 4 +- .../org/tasks/injection/ActivityModule.java | 4 +- .../tasks/injection/DialogFragmentModule.java | 4 +- .../org/tasks/injection/FragmentModule.java | 2 - .../java/org/tasks/intents/TaskIntents.java | 11 +-- .../tasks/reminders/NotificationActivity.java | 39 ++++++++ .../tasks/reminders/NotificationDialog.java | 95 +++++++++++++++++++ .../layout/astrid_reminder_view_portrait.xml | 85 ----------------- src/main/res/values-ar/strings.xml | 1 - src/main/res/values-bg-rBG/strings.xml | 1 - src/main/res/values-cs/strings.xml | 1 - src/main/res/values-de/strings.xml | 1 - src/main/res/values-el/strings.xml | 1 - src/main/res/values-es/strings.xml | 1 - src/main/res/values-fr/strings.xml | 1 - src/main/res/values-it/strings.xml | 1 - src/main/res/values-iw/strings.xml | 1 - src/main/res/values-ja/strings.xml | 1 - src/main/res/values-ko/strings.xml | 1 - src/main/res/values-nl/strings.xml | 1 - src/main/res/values-pl/strings.xml | 1 - src/main/res/values-pt-rBR/strings.xml | 1 - src/main/res/values-pt/strings.xml | 1 - src/main/res/values-ru/strings.xml | 1 - src/main/res/values-sk/strings.xml | 1 - src/main/res/values-sl-rSI/strings.xml | 1 - src/main/res/values-sv/strings.xml | 1 - src/main/res/values-tr/strings.xml | 1 - src/main/res/values-uk/strings.xml | 1 - src/main/res/values-zh-rCN/strings.xml | 1 - src/main/res/values-zh-rTW/strings.xml | 1 - src/main/res/values/strings-reminders.xml | 3 - 36 files changed, 165 insertions(+), 232 deletions(-) delete mode 100644 src/main/java/com/todoroo/astrid/reminders/NotificationFragment.java create mode 100644 src/main/java/org/tasks/reminders/NotificationActivity.java create mode 100644 src/main/java/org/tasks/reminders/NotificationDialog.java delete mode 100644 src/main/res/layout/astrid_reminder_view_portrait.xml diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index f0745d9e3..20490ad5d 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -310,7 +310,15 @@ android:theme="@style/Tasks.Dialog" android:taskAffinity="" android:excludeFromRecents="true" - android:launchMode="singleTask"/> + android:launchMode="singleTask" /> + + diff --git a/src/main/java/com/todoroo/astrid/reminders/NotificationFragment.java b/src/main/java/com/todoroo/astrid/reminders/NotificationFragment.java deleted file mode 100644 index ab254d41f..000000000 --- a/src/main/java/com/todoroo/astrid/reminders/NotificationFragment.java +++ /dev/null @@ -1,89 +0,0 @@ -/** - * Copyright (c) 2012 Todoroo Inc - * - * See the file "LICENSE" for the full license governing this code. - */ -package com.todoroo.astrid.reminders; - -import android.app.Dialog; -import android.content.Intent; -import android.view.View; -import android.widget.TextView; - -import com.todoroo.astrid.activity.AstridActivity; -import com.todoroo.astrid.activity.TaskListFragment; - -import org.tasks.Broadcaster; -import org.tasks.R; -import org.tasks.reminders.SnoozeActivity; - -import javax.inject.Inject; - -/** - * This activity is launched when a user opens up a notification from the - * tray. It launches the appropriate activity based on the passed in parameters. - * - * @author timsu - * - */ -public class NotificationFragment extends TaskListFragment { - - public static final String TOKEN_ID = "id"; //$NON-NLS-1$ - - @Inject Broadcaster broadcaster; - - @Override - protected void initializeData() { - displayNotificationPopup(); - super.initializeData(); - } - - private void displayNotificationPopup() { - final String title = extras.getString(Notifications.EXTRAS_TITLE); - final long taskId = extras.getLong(TOKEN_ID); - final AstridActivity activity = (AstridActivity) getActivity(); - - new Dialog(activity, R.style.ReminderDialog) {{ - setContentView(R.layout.astrid_reminder_view_portrait); - findViewById(R.id.speech_bubble_container).setVisibility(View.GONE); - - // set up listeners - findViewById(R.id.dismiss).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View arg0) { - dismiss(); - } - }); - - findViewById(R.id.reminder_snooze).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View arg0) { - dismiss(); - activity.startActivity(new Intent(activity, SnoozeActivity.class) {{ - putExtra(SnoozeActivity.EXTRA_TASK_ID, taskId); - }}); - } - }); - - findViewById(R.id.reminder_complete).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View arg0) { - broadcaster.completeTask(taskId); - dismiss(); - } - }); - - findViewById(R.id.reminder_edit).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - dismiss(); - activity.onTaskListItemClicked(taskId); - } - }); - - ((TextView) findViewById(R.id.reminder_title)).setText(activity.getString(R.string.rmd_NoA_dlg_title) + " " + title); - - setOwnerActivity(activity); - }}.show(); - } -} diff --git a/src/main/java/com/todoroo/astrid/reminders/Notifications.java b/src/main/java/com/todoroo/astrid/reminders/Notifications.java index 1dff12408..dcf685c75 100644 --- a/src/main/java/com/todoroo/astrid/reminders/Notifications.java +++ b/src/main/java/com/todoroo/astrid/reminders/Notifications.java @@ -30,6 +30,8 @@ import org.tasks.injection.InjectingBroadcastReceiver; import org.tasks.intents.TaskIntents; import org.tasks.notifications.NotificationManager; import org.tasks.preferences.Preferences; +import org.tasks.reminders.NotificationActivity; +import org.tasks.reminders.NotificationDialog; import javax.inject.Inject; @@ -130,30 +132,18 @@ public class Notifications extends InjectingBroadcastReceiver { String text = context.getString(R.string.app_name); - PendingIntent intent = preferences.useNotificationActions() - ? TaskIntents.getEditTaskPendingIntent(context, null, task.getId()) - : createNotificationIntent(id, taskTitle); + PendingIntent intent = createNotificationIntent(id, taskTitle); broadcaster.requestNotification((int) id, intent, type, taskTitle, text, ringTimes); return true; } private PendingIntent createNotificationIntent(final long id, final String taskTitle) { - final FilterWithCustomIntent itemFilter = new FilterWithCustomIntent(context.getString(R.string.rmd_NoA_filter), - context.getString(R.string.rmd_NoA_filter), - new QueryTemplate().where(TaskCriteria.byId(id)), - null); - Bundle customExtras = new Bundle(); - customExtras.putLong(NotificationFragment.TOKEN_ID, id); - customExtras.putString(EXTRAS_TITLE, taskTitle); - itemFilter.customExtras = customExtras; - itemFilter.customTaskList = new ComponentName(context, NotificationFragment.class); - Intent intent = new Intent(context, TaskListActivity.class) {{ - setAction("NOTIFY" + id); //$NON-NLS-1$ - putExtra(TaskListFragment.TOKEN_FILTER, itemFilter); - putExtra(NotificationFragment.TOKEN_ID, id); - putExtra(EXTRAS_TITLE, taskTitle); + Intent intent = new Intent(context, NotificationActivity.class) {{ setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK); + setAction("NOTIFY" + id); //$NON-NLS-1$ + putExtra(NotificationActivity.EXTRA_TASK_ID, id); + putExtra(NotificationActivity.EXTRA_TITLE, taskTitle); }}; return PendingIntent.getActivity(context, (int) id, intent, PendingIntent.FLAG_UPDATE_CURRENT); } diff --git a/src/main/java/com/todoroo/astrid/reminders/ShowNotificationReceiver.java b/src/main/java/com/todoroo/astrid/reminders/ShowNotificationReceiver.java index 6fb9de1f5..71361bbd5 100644 --- a/src/main/java/com/todoroo/astrid/reminders/ShowNotificationReceiver.java +++ b/src/main/java/com/todoroo/astrid/reminders/ShowNotificationReceiver.java @@ -23,6 +23,7 @@ import org.tasks.injection.InjectingBroadcastReceiver; import org.tasks.notifications.NotificationManager; import org.tasks.preferences.Preferences; import org.tasks.receivers.CompleteTaskReceiver; +import org.tasks.reminders.NotificationActivity; import org.tasks.reminders.SnoozeActivity; import java.util.concurrent.ExecutorService; @@ -56,7 +57,7 @@ public class ShowNotificationReceiver extends InjectingBroadcastReceiver { showNotification( context, intent.getIntExtra(Notifications.EXTRAS_NOTIF_ID, 0), - intent.getLongExtra(NotificationFragment.TOKEN_ID, 0L), + intent.getLongExtra(NotificationActivity.EXTRA_TASK_ID, 0L), intent.getParcelableExtra(Notifications.EXTRAS_CUSTOM_INTENT), intent.getIntExtra(Notifications.EXTRAS_TYPE, 0), intent.getStringExtra(Notifications.EXTRAS_TITLE), @@ -136,7 +137,6 @@ public class ShowNotificationReceiver extends InjectingBroadcastReceiver { } Notification notification = builder.build(); - notification.flags |= Notification.FLAG_AUTO_CANCEL; if (preferences.getBoolean(R.string.p_rmd_persistent, true)) { notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_SHOW_LIGHTS; notification.ledOffMS = 5000; diff --git a/src/main/java/org/tasks/Broadcaster.java b/src/main/java/org/tasks/Broadcaster.java index d990d3288..c1823ab5a 100644 --- a/src/main/java/org/tasks/Broadcaster.java +++ b/src/main/java/org/tasks/Broadcaster.java @@ -5,7 +5,6 @@ import android.content.Context; import android.content.Intent; import com.todoroo.astrid.api.AstridApiConstants; -import com.todoroo.astrid.reminders.NotificationFragment; import com.todoroo.astrid.reminders.Notifications; import com.todoroo.astrid.reminders.ReminderService; import com.todoroo.astrid.utility.Constants; @@ -13,6 +12,7 @@ import com.todoroo.astrid.utility.Constants; import org.tasks.injection.ForApplication; import org.tasks.receivers.CompleteTaskReceiver; import org.tasks.receivers.FirstLaunchReceiver; +import org.tasks.reminders.NotificationActivity; import javax.inject.Inject; import javax.inject.Singleton; @@ -48,7 +48,7 @@ public class Broadcaster { final String title, final String text, final int ringTimes) { sendOrderedBroadcast(new Intent(BROADCAST_IN_APP_NOTIFY) {{ putExtra(Notifications.EXTRAS_NOTIF_ID, (int) taskId); - putExtra(NotificationFragment.TOKEN_ID, taskId); + putExtra(NotificationActivity.EXTRA_TASK_ID, taskId); putExtra(Notifications.EXTRAS_CUSTOM_INTENT, intent); putExtra(Notifications.EXTRAS_TYPE, type); putExtra(Notifications.EXTRAS_TITLE, title); diff --git a/src/main/java/org/tasks/injection/ActivityModule.java b/src/main/java/org/tasks/injection/ActivityModule.java index 59f32c80e..bec28c5c3 100644 --- a/src/main/java/org/tasks/injection/ActivityModule.java +++ b/src/main/java/org/tasks/injection/ActivityModule.java @@ -37,6 +37,7 @@ import org.tasks.preferences.AppearancePreferences; import org.tasks.preferences.BackupPreferences; import org.tasks.preferences.BasicPreferences; import org.tasks.preferences.MiscellaneousPreferences; +import org.tasks.reminders.NotificationActivity; import org.tasks.reminders.SnoozeActivity; import org.tasks.voice.VoiceCommandActivity; @@ -82,7 +83,8 @@ import dagger.Provides; ReminderPreferences.class, AppearancePreferences.class, BackupPreferences.class, - LocationPickerActivity.class + LocationPickerActivity.class, + NotificationActivity.class }) public class ActivityModule { diff --git a/src/main/java/org/tasks/injection/DialogFragmentModule.java b/src/main/java/org/tasks/injection/DialogFragmentModule.java index a194e274b..e040f8ec0 100644 --- a/src/main/java/org/tasks/injection/DialogFragmentModule.java +++ b/src/main/java/org/tasks/injection/DialogFragmentModule.java @@ -4,13 +4,15 @@ import android.support.v4.app.DialogFragment; import android.support.v4.app.FragmentActivity; import org.tasks.dialogs.LocationPickerDialog; +import org.tasks.reminders.NotificationDialog; import dagger.Module; import dagger.Provides; @Module(addsTo = TasksModule.class, injects = { - LocationPickerDialog.class + LocationPickerDialog.class, + NotificationDialog.class }, library = true) public class DialogFragmentModule { diff --git a/src/main/java/org/tasks/injection/FragmentModule.java b/src/main/java/org/tasks/injection/FragmentModule.java index 972a01e5a..d7680a00e 100644 --- a/src/main/java/org/tasks/injection/FragmentModule.java +++ b/src/main/java/org/tasks/injection/FragmentModule.java @@ -8,7 +8,6 @@ import com.todoroo.astrid.actfm.TagViewFragment; import com.todoroo.astrid.activity.TaskEditFragment; import com.todoroo.astrid.activity.TaskListFragment; import com.todoroo.astrid.gtasks.GtasksListFragment; -import com.todoroo.astrid.reminders.NotificationFragment; import com.todoroo.astrid.subtasks.SubtasksListFragment; import com.todoroo.astrid.subtasks.SubtasksTagListFragment; import com.todoroo.astrid.ui.QuickAddBar; @@ -24,7 +23,6 @@ import dagger.Provides; injects = { TaskListFragment.class, GtasksListFragment.class, - NotificationFragment.class, SubtasksListFragment.class, SubtasksTagListFragment.class, TagViewFragment.class, diff --git a/src/main/java/org/tasks/intents/TaskIntents.java b/src/main/java/org/tasks/intents/TaskIntents.java index d87148555..e2a621273 100644 --- a/src/main/java/org/tasks/intents/TaskIntents.java +++ b/src/main/java/org/tasks/intents/TaskIntents.java @@ -48,24 +48,23 @@ public class TaskIntents { return intent; } - public static PendingIntent getEditTaskPendingIntent(Context context, final Filter filter, final long taskId) { + public static TaskStackBuilder getEditTaskStack(Context context, final Filter filter, final long taskId) { + TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context); boolean twoPaneLayout = context.getResources().getBoolean(R.bool.two_pane_layout); if (twoPaneLayout) { - Intent intent = new Intent(context, TaskListActivity.class) {{ + taskStackBuilder.addNextIntent(new Intent(context, TaskListActivity.class) {{ putExtra(TaskListActivity.OPEN_TASK, taskId); if (filter != null && filter instanceof FilterWithCustomIntent) { Bundle customExtras = ((FilterWithCustomIntent) filter).customExtras; putExtras(customExtras); } - }}; - return PendingIntent.getActivity(context, (int) taskId, intent, PendingIntent.FLAG_UPDATE_CURRENT); + }}); } else { - TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context); taskStackBuilder.addParentStack(TaskEditActivity.class); taskStackBuilder.addNextIntent(new Intent(context, TaskEditActivity.class) {{ putExtra(TaskEditFragment.TOKEN_ID, taskId); }}); - return taskStackBuilder.getPendingIntent((int) taskId, PendingIntent.FLAG_UPDATE_CURRENT); } + return taskStackBuilder; } } diff --git a/src/main/java/org/tasks/reminders/NotificationActivity.java b/src/main/java/org/tasks/reminders/NotificationActivity.java new file mode 100644 index 000000000..117060730 --- /dev/null +++ b/src/main/java/org/tasks/reminders/NotificationActivity.java @@ -0,0 +1,39 @@ +package org.tasks.reminders; + +import android.content.DialogInterface; +import android.content.Intent; +import android.os.Bundle; +import android.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentManager; +import android.support.v7.app.AppCompatActivity; + +import org.tasks.injection.InjectingFragmentActivity; + +public class NotificationActivity extends InjectingFragmentActivity implements DialogInterface.OnDismissListener { + + private static final String FRAG_TAG_NOTIFICATION_FRAGMENT = "frag_tag_notification_fragment"; + + public static final String EXTRA_TITLE = "extra_title"; + public static final String EXTRA_TASK_ID = "extra_task_id"; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + FragmentManager supportFragmentManager = getSupportFragmentManager(); + NotificationDialog fragment = (NotificationDialog) supportFragmentManager.findFragmentByTag(FRAG_TAG_NOTIFICATION_FRAGMENT); + if (fragment == null) { + Intent intent = getIntent(); + fragment = new NotificationDialog(); + fragment.setTitle(intent.getStringExtra(EXTRA_TITLE)); + fragment.setTaskId(intent.getLongExtra(EXTRA_TASK_ID, 0L)); + fragment.show(supportFragmentManager, FRAG_TAG_NOTIFICATION_FRAGMENT); + } + fragment.setOnDismissListener(this); + } + + @Override + public void onDismiss(DialogInterface dialog) { + finish(); + } +} diff --git a/src/main/java/org/tasks/reminders/NotificationDialog.java b/src/main/java/org/tasks/reminders/NotificationDialog.java new file mode 100644 index 000000000..bef101e42 --- /dev/null +++ b/src/main/java/org/tasks/reminders/NotificationDialog.java @@ -0,0 +1,95 @@ +package org.tasks.reminders; + +import android.app.Dialog; +import android.content.DialogInterface; +import android.content.Intent; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.v4.app.DialogFragment; +import android.support.v7.app.AlertDialog; +import android.widget.ArrayAdapter; + +import org.tasks.Broadcaster; +import org.tasks.R; +import org.tasks.injection.InjectingDialogFragment; +import org.tasks.intents.TaskIntents; +import org.tasks.notifications.NotificationManager; + +import javax.inject.Inject; + +import static java.util.Arrays.asList; + +public class NotificationDialog extends InjectingDialogFragment { + + @Inject NotificationManager notificationManager; + @Inject Broadcaster broadcaster; + + private long taskId; + private String title; + private DialogInterface.OnDismissListener onDismissListener; + + @NonNull + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + ArrayAdapter adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, asList( + getString(R.string.TAd_actionEditTask), + getString(R.string.rmd_NoA_snooze), + getString(R.string.rmd_NoA_done) + )); + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.Tasks_Dialog); + builder.setTitle(title); + builder.setAdapter(adapter, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + switch (which) { + case 0: + TaskIntents + .getEditTaskStack(getActivity(), null, taskId) + .startActivities(); + notificationManager.cancel(taskId); + dismiss(); + break; + case 1: + dismiss(); + startActivity(new Intent(getActivity(), SnoozeActivity.class) {{ + setFlags(FLAG_ACTIVITY_NEW_TASK); + putExtra(SnoozeActivity.EXTRA_TASK_ID, taskId); + }}); + break; + case 2: + broadcaster.completeTask(taskId); + dismiss(); + break; + } + } + }); + builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + if (onDismissListener != null) { + onDismissListener.onDismiss(dialog); + } + } + }); + return builder.show(); + } + + public void setOnDismissListener(DialogInterface.OnDismissListener onDismissListener) { + this.onDismissListener = onDismissListener; + } + + @Override + public void onDismiss(DialogInterface dialog) { + if (onDismissListener != null) { + onDismissListener.onDismiss(dialog); + } + } + + public void setTitle(String title) { + this.title = title; + } + + public void setTaskId(long taskId) { + this.taskId = taskId; + } +} diff --git a/src/main/res/layout/astrid_reminder_view_portrait.xml b/src/main/res/layout/astrid_reminder_view_portrait.xml deleted file mode 100644 index 05e8b8e08..000000000 --- a/src/main/res/layout/astrid_reminder_view_portrait.xml +++ /dev/null @@ -1,85 +0,0 @@ - - - - - - - - - - - - - - - -