From 108c1f284b00b14aa8f62b1d27aafcf5e9f702fb Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Sat, 11 Jul 2020 07:53:25 -0500 Subject: [PATCH] Update task and back stack behavior --- app/src/main/AndroidManifest.xml | 3 +- .../todoroo/astrid/activity/MainActivity.kt | 119 ++++++++++++++---- .../astrid/activity/ShareLinkActivity.java | 34 ++--- .../astrid/gcal/CalendarAlarmReceiver.java | 2 +- .../todoroo/astrid/service/TaskCreator.java | 2 +- .../todoroo/astrid/timers/TimerPlugin.java | 1 - app/src/main/java/org/tasks/Notifier.kt | 7 +- .../java/org/tasks/activities/UriHandler.kt | 6 +- .../tasks/dashclock/DashClockExtension.java | 5 +- .../java/org/tasks/intents/TaskIntents.java | 19 +-- .../tasks/reminders/NotificationActivity.java | 2 +- .../main/java/org/tasks/widget/TasksWidget.kt | 5 +- .../main/java/org/tasks/widget/TileService.kt | 6 +- 13 files changed, 140 insertions(+), 71 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 1b2850430..2bf21a46e 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -191,10 +191,11 @@ diff --git a/app/src/main/java/com/todoroo/astrid/activity/MainActivity.kt b/app/src/main/java/com/todoroo/astrid/activity/MainActivity.kt index 6081df379..3468b3e2c 100644 --- a/app/src/main/java/com/todoroo/astrid/activity/MainActivity.kt +++ b/app/src/main/java/com/todoroo/astrid/activity/MainActivity.kt @@ -180,9 +180,12 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl val intent = intent val openFilter = intent.getFilter val loadFilter = intent.getFilterString + val openTask = !intent.isFromHistory + && (intent.hasExtra(OPEN_TASK) || intent.hasExtra(CREATE_TASK)) Timber.d(""" ********** + broughtToFront: ${intent.broughtToFront} isFromHistory: ${intent.isFromHistory} flags: ${intent.flagsToString} OPEN_FILTER: ${openFilter?.let { "${it.listingTitle}: $it" }} @@ -192,7 +195,7 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl taskListFragment: ${taskListFragment?.getFilter()?.let { "${it.listingTitle}: $it" }} taskEditFragment: ${taskEditFragment?.editViewModel?.task} **********""") - if (openFilter != null || loadFilter != null) { + if (!openTask && (openFilter != null || !loadFilter.isNullOrBlank())) { taskEditFragment?.let { lifecycleScope.launch { it.save() @@ -207,19 +210,49 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl defaultFilterProvider.getFilterFromPreference(loadFilter) } clearUi() - openTaskListFragment(filter) - openTask(filter) + if (isSinglePaneLayout) { + if (openTask) { + setFilter(filter) + openTask(filter) + } else { + openTaskListFragment(filter, true) + } + } else { + openTaskListFragment(filter, true) + openTask(filter) + } } } else if (openFilter != null) { clearUi() - openTaskListFragment(openFilter) - openTask(openFilter) + if (isSinglePaneLayout) { + if (openTask) { + setFilter(openFilter) + openTask(openFilter) + } else { + openTaskListFragment(openFilter, true) + } + } else { + openTaskListFragment(openFilter, true) + openTask(openFilter) + } } else { val existing = taskListFragment - openTaskListFragment( - if (existing == null || existing.getFilter() !== filter) TaskListFragment.newTaskListFragment(applicationContext, filter) else existing, - false) - openTask(filter) + val target = if (existing == null || existing.getFilter() !== filter) { + TaskListFragment.newTaskListFragment(applicationContext, filter) + } else { + existing + } + if (isSinglePaneLayout) { + if (openTask) { + setFilter(null) + openTask(null) + } else { + openTaskListFragment(filter, false) + } + } else { + openTaskListFragment(target, false) + openTask(filter) + } } if (intent.hasExtra(TOKEN_CREATE_NEW_LIST_NAME)) { val listName = intent.getStringExtra(TOKEN_CREATE_NEW_LIST_NAME) @@ -238,16 +271,23 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl } private fun hideDetailFragment() { - supportFragmentManager - .beginTransaction() - .replace(R.id.detail, newEmptyTaskEditFragment(filter!!)) - .commit() + filter?.let { + supportFragmentManager + .beginTransaction() + .replace(R.id.detail, newEmptyTaskEditFragment(it)) + .commit() + } if (isSinglePaneLayout) { binding.master.visibility = View.VISIBLE binding.detail.visibility = View.GONE } } + private fun setFilter(newFilter: Filter?) { + filter = newFilter + applyTheme() + } + private fun openTaskListFragment(filter: Filter?, force: Boolean = false) { openTaskListFragment(TaskListFragment.newTaskListFragment(applicationContext, filter), force) } @@ -268,7 +308,7 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl supportFragmentManager .beginTransaction() .replace(R.id.master, taskListFragment, FRAG_TAG_TASK_LIST) - .commitNow() + .commit() } @@ -318,7 +358,7 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl if (task == null) { return } - lifecycleScope.launch { + lifecycleScope.launchWhenResumed { taskEditFragment?.let { it.editViewModel.cleared.removeObservers(this@MainActivity) it.save() @@ -383,12 +423,24 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl get() = !resources.getBoolean(R.bool.two_pane_layout) fun removeTaskEditFragment() { - supportFragmentManager - .popBackStackImmediate( - TaskEditFragment.TAG_TASKEDIT_FRAGMENT, FragmentManager.POP_BACK_STACK_INCLUSIVE) - hideDetailFragment() - hideKeyboard() - taskListFragment?.loadTaskListContent() + supportFragmentManager.popBackStack( + TaskEditFragment.TAG_TASKEDIT_FRAGMENT, + FragmentManager.POP_BACK_STACK_INCLUSIVE) + val removeTask = intent.removeTask + val finishAffinity = intent.finishAffinity + if (finishAffinity || taskListFragment == null) { + finishAffinity() + } else { + if (removeTask && intent.broughtToFront) { + moveTaskToBack(true) + } + hideKeyboard() + hideDetailFragment() + taskListFragment?.let { + setFilter(it.getFilter()) + it.loadTaskListContent() + } + } } private fun hideKeyboard() { @@ -452,6 +504,8 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl const val LOAD_FILTER = "load_filter" const val CREATE_TASK = "open_task" // $NON-NLS-1$ const val OPEN_TASK = "open_new_task" // $NON-NLS-1$ + const val REMOVE_TASK = "remove_task" + const val FINISH_AFFINITY = "finish_affinity" private const val FRAG_TAG_TASK_LIST = "frag_tag_task_list" private const val FRAG_TAG_WHATS_NEW = "frag_tag_whats_new" private const val EXTRA_FILTER = "extra_filter" @@ -478,9 +532,32 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl } } + val Intent.removeTask: Boolean + get() = if (isFromHistory) { + false + } else { + getBooleanExtra(REMOVE_TASK, false).let { + removeExtra(REMOVE_TASK) + it + } + } + + val Intent.finishAffinity: Boolean + get() = if (isFromHistory) { + false + } else { + getBooleanExtra(FINISH_AFFINITY, false).let { + removeExtra(FINISH_AFFINITY) + it + } + } + val Intent.isFromHistory: Boolean get() = flags and FLAG_FROM_HISTORY == FLAG_FROM_HISTORY + val Intent.broughtToFront: Boolean + get() = flags and Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT > 0 + val Intent.flagsToString get() = if (BuildConfig.DEBUG) "" else Intent::class.java.declaredFields diff --git a/app/src/main/java/com/todoroo/astrid/activity/ShareLinkActivity.java b/app/src/main/java/com/todoroo/astrid/activity/ShareLinkActivity.java index eaec0cccd..99e52b474 100644 --- a/app/src/main/java/com/todoroo/astrid/activity/ShareLinkActivity.java +++ b/app/src/main/java/com/todoroo/astrid/activity/ShareLinkActivity.java @@ -6,13 +6,12 @@ import static com.google.common.collect.Lists.newArrayList; import static org.tasks.Strings.isNullOrEmpty; import static org.tasks.files.FileHelper.copyToUri; import static org.tasks.files.FileHelper.getFilename; -import static org.tasks.intents.TaskIntents.getTaskListIntent; +import static org.tasks.intents.TaskIntents.getEditTaskIntent; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; -import androidx.core.app.TaskStackBuilder; import com.google.common.io.Files; import com.todoroo.astrid.data.Task; import com.todoroo.astrid.service.TaskCreator; @@ -37,29 +36,10 @@ public final class ShareLinkActivity extends InjectingAppCompatActivity { @Inject TaskCreator taskCreator; @Inject Preferences preferences; - private static TaskStackBuilder getEditTaskStack(Context context, Task task) { - Intent intent = getTaskListIntent(context, null); - intent.putExtra(MainActivity.OPEN_TASK, task); - return TaskStackBuilder.create(context).addNextIntent(intent); - } - @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - readIntent(); - } - - @Override - protected void onNewIntent(Intent intent) { - super.onNewIntent(intent); - - setIntent(intent); - - readIntent(); - } - - private void readIntent() { Intent intent = getIntent(); String action = intent.getAction(); @@ -67,7 +47,7 @@ public final class ShareLinkActivity extends InjectingAppCompatActivity { CharSequence text = intent.getCharSequenceExtra(Intent.EXTRA_PROCESS_TEXT); if (text != null) { Task task = taskCreator.createWithValues(text.toString()); - getEditTaskStack(this, task).startActivities(); + editTask(task); } } else if (ACTION_SEND.equals(action)) { String subject = intent.getStringExtra(Intent.EXTRA_SUBJECT); @@ -76,20 +56,26 @@ public final class ShareLinkActivity extends InjectingAppCompatActivity { if (hasAttachments(intent)) { task.putTransitory(TaskAttachment.KEY, copyAttachment(intent)); } - getEditTaskStack(this, task).startActivities(); + editTask(task); } else if (ACTION_SEND_MULTIPLE.equals(action)) { Task task = taskCreator.createWithValues(intent.getStringExtra(Intent.EXTRA_SUBJECT)); task.setNotes(intent.getStringExtra(Intent.EXTRA_TEXT)); if (hasAttachments(intent)) { task.putTransitory(TaskAttachment.KEY, copyMultipleAttachments(intent)); } - getEditTaskStack(this, task).startActivities(); + editTask(task); } else { Timber.e("Unhandled intent: %s", intent); } finish(); } + private void editTask(Task task) { + Intent intent = getEditTaskIntent(this, null, task); + intent.putExtra(MainActivity.FINISH_AFFINITY, true); + startActivity(intent); + } + private ArrayList copyAttachment(Intent intent) { Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM); String filename = getFilename(context, uri); diff --git a/app/src/main/java/com/todoroo/astrid/gcal/CalendarAlarmReceiver.java b/app/src/main/java/com/todoroo/astrid/gcal/CalendarAlarmReceiver.java index 9d85e8fcf..28364e798 100644 --- a/app/src/main/java/com/todoroo/astrid/gcal/CalendarAlarmReceiver.java +++ b/app/src/main/java/com/todoroo/astrid/gcal/CalendarAlarmReceiver.java @@ -85,7 +85,7 @@ public class CalendarAlarmReceiver extends InjectingBroadcastReceiver { intent.putExtra(CalendarReminderActivity.TOKEN_EVENT_NAME, event.getTitle()); intent.putExtra(CalendarReminderActivity.TOKEN_EVENT_END_TIME, event.getEnd()); intent.putExtra(CalendarReminderActivity.TOKEN_FROM_POSTPONE, fromPostpone); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); context.startActivity(intent); } } diff --git a/app/src/main/java/com/todoroo/astrid/service/TaskCreator.java b/app/src/main/java/com/todoroo/astrid/service/TaskCreator.java index 80015e9b4..986549a27 100644 --- a/app/src/main/java/com/todoroo/astrid/service/TaskCreator.java +++ b/app/src/main/java/com/todoroo/astrid/service/TaskCreator.java @@ -126,7 +126,7 @@ public class TaskCreator { } public Task createWithValues(Filter filter, String title) { - return create(filter.valuesForNewTasks, title); + return create(filter == null ? null : filter.valuesForNewTasks, title); } /** * Create task from the given content values, saving it. This version doesn't need to start with a diff --git a/app/src/main/java/com/todoroo/astrid/timers/TimerPlugin.java b/app/src/main/java/com/todoroo/astrid/timers/TimerPlugin.java index 040cf9947..8e4474554 100644 --- a/app/src/main/java/com/todoroo/astrid/timers/TimerPlugin.java +++ b/app/src/main/java/com/todoroo/astrid/timers/TimerPlugin.java @@ -90,7 +90,6 @@ public class TimerPlugin { } else { Filter filter = createFilter(context); Intent notifyIntent = TaskIntents.getTaskListIntent(context, filter); - notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(context, Constants.NOTIFICATION_TIMER, notifyIntent, 0); diff --git a/app/src/main/java/org/tasks/Notifier.kt b/app/src/main/java/org/tasks/Notifier.kt index 007fd7901..bd2176f51 100644 --- a/app/src/main/java/org/tasks/Notifier.kt +++ b/app/src/main/java/org/tasks/Notifier.kt @@ -2,15 +2,14 @@ package org.tasks import android.app.PendingIntent import android.content.Context -import android.content.Intent import androidx.core.app.NotificationCompat import com.todoroo.andlib.utility.AndroidUtilities -import com.todoroo.astrid.activity.MainActivity import com.todoroo.astrid.api.Filter import com.todoroo.astrid.dao.TaskDaoBlocking import com.todoroo.astrid.reminders.ReminderService import com.todoroo.astrid.voice.VoiceOutputAssistant import dagger.hilt.android.qualifiers.ApplicationContext +import org.tasks.intents.TaskIntents import org.tasks.notifications.AudioManager import org.tasks.notifications.Notification import org.tasks.notifications.NotificationManager @@ -40,9 +39,7 @@ class Notifier @Inject constructor( if (count == 0) { return } - val intent = Intent(context, MainActivity::class.java) - intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_MULTIPLE_TASK - intent.putExtra(MainActivity.OPEN_FILTER, filter) + val intent = TaskIntents.getTaskListIntent(context, filter) val pendingIntent = PendingIntent.getActivity( context, filter.listingTitle.hashCode(), intent, PendingIntent.FLAG_UPDATE_CURRENT) val summaryTitle = context.resources.getQuantityString(R.plurals.task_count, count, count) diff --git a/app/src/main/java/org/tasks/activities/UriHandler.kt b/app/src/main/java/org/tasks/activities/UriHandler.kt index 98f4dc0df..77026f290 100644 --- a/app/src/main/java/org/tasks/activities/UriHandler.kt +++ b/app/src/main/java/org/tasks/activities/UriHandler.kt @@ -28,7 +28,7 @@ class UriHandler : AppCompatActivity() { lifecycleScope.launch { val task = taskDao.fetch(id) task?.let { - startActivity(TaskIntents.getEditTaskIntent(this@UriHandler, it)) + startActivity(TaskIntents.getEditTaskIntent(this@UriHandler, null, it)) } finish() } @@ -49,7 +49,9 @@ class UriHandler : AppCompatActivity() { } private fun newTask() { - startActivity(TaskIntents.getNewTaskIntent(this, null)) + val intent = TaskIntents.getNewTaskIntent(this@UriHandler, null) + intent.flags = TaskIntents.FLAGS + startActivity(intent) finish() } } \ No newline at end of file diff --git a/app/src/main/java/org/tasks/dashclock/DashClockExtension.java b/app/src/main/java/org/tasks/dashclock/DashClockExtension.java index 2bc71bcaf..7139ecb51 100644 --- a/app/src/main/java/org/tasks/dashclock/DashClockExtension.java +++ b/app/src/main/java/org/tasks/dashclock/DashClockExtension.java @@ -4,7 +4,6 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import com.google.android.apps.dashclock.api.ExtensionData; -import com.todoroo.astrid.activity.MainActivity; import com.todoroo.astrid.api.Filter; import com.todoroo.astrid.dao.TaskDaoBlocking; import com.todoroo.astrid.data.Task; @@ -13,6 +12,7 @@ import java.util.List; import javax.inject.Inject; import org.tasks.LocalBroadcastManager; import org.tasks.R; +import org.tasks.intents.TaskIntents; import org.tasks.preferences.DefaultFilterProvider; import org.tasks.preferences.Preferences; import timber.log.Timber; @@ -61,8 +61,7 @@ public class DashClockExtension extends com.google.android.apps.dashclock.api.Da if (count == 0) { publish(null); } else { - Intent clickIntent = new Intent(this, MainActivity.class); - clickIntent.putExtra(MainActivity.OPEN_FILTER, filter); + Intent clickIntent = TaskIntents.getTaskListIntent(this, filter); ExtensionData extensionData = new ExtensionData() .visible(true) diff --git a/app/src/main/java/org/tasks/intents/TaskIntents.java b/app/src/main/java/org/tasks/intents/TaskIntents.java index 9ec24f39a..27312b67d 100644 --- a/app/src/main/java/org/tasks/intents/TaskIntents.java +++ b/app/src/main/java/org/tasks/intents/TaskIntents.java @@ -10,26 +10,31 @@ import com.todoroo.astrid.data.Task; public class TaskIntents { - public static Intent getEditTaskIntent(Context context, Task task) { - return getEditTaskIntent(context, null, task); - } + public static final int FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP; public static Intent getNewTaskIntent(Context context, @Nullable Filter filter) { - Intent intent = TaskIntents.getTaskListIntent(context, filter); + Intent intent = new Intent(context, MainActivity.class); + if (filter != null) { + intent.putExtra(MainActivity.OPEN_FILTER, filter); + } intent.putExtra(MainActivity.CREATE_TASK, 0L); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); + intent.putExtra(MainActivity.REMOVE_TASK, true); return intent; } public static Intent getEditTaskIntent(Context context, @Nullable Filter filter, Task task) { - Intent intent = getTaskListIntent(context, filter); + Intent intent = new Intent(context, MainActivity.class); + if (filter != null) { + intent.putExtra(MainActivity.OPEN_FILTER, filter); + } intent.putExtra(MainActivity.OPEN_TASK, task); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); + intent.putExtra(MainActivity.REMOVE_TASK, true); return intent; } public static Intent getTaskListIntent(Context context, @Nullable Filter filter) { Intent intent = new Intent(context, MainActivity.class); + intent.setFlags(FLAGS); if (filter != null) { intent.putExtra(MainActivity.OPEN_FILTER, filter); } diff --git a/app/src/main/java/org/tasks/reminders/NotificationActivity.java b/app/src/main/java/org/tasks/reminders/NotificationActivity.java index cd6dcfc3e..c05573361 100644 --- a/app/src/main/java/org/tasks/reminders/NotificationActivity.java +++ b/app/src/main/java/org/tasks/reminders/NotificationActivity.java @@ -88,7 +88,7 @@ public class NotificationActivity extends InjectingAppCompatActivity .observeOn(AndroidSchedulers.mainThread()) .subscribe( task -> { - startActivity(TaskIntents.getEditTaskIntent(this, task)); + startActivity(TaskIntents.getEditTaskIntent(this, null, task)); finish(); }, e -> Timber.e("Task not found: %s", taskId))); diff --git a/app/src/main/java/org/tasks/widget/TasksWidget.kt b/app/src/main/java/org/tasks/widget/TasksWidget.kt index 940709237..36c924564 100644 --- a/app/src/main/java/org/tasks/widget/TasksWidget.kt +++ b/app/src/main/java/org/tasks/widget/TasksWidget.kt @@ -123,7 +123,6 @@ class TasksWidget : AppWidgetProvider() { private fun getOpenListIntent(context: Context, filter: Filter, widgetId: Int): PendingIntent { val intent = TaskIntents.getTaskListIntent(context, filter) - intent.flags = flags intent.action = "open_list" return PendingIntent.getActivity(context, widgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT) } @@ -136,7 +135,7 @@ class TasksWidget : AppWidgetProvider() { private fun getWidgetConfigIntent(context: Context, widgetId: Int): PendingIntent { val intent = Intent(context, WidgetConfigActivity::class.java) - intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK + intent.flags = flags intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId) intent.action = "widget_settings" return PendingIntent.getActivity(context, widgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT) @@ -144,7 +143,7 @@ class TasksWidget : AppWidgetProvider() { private fun getChooseListIntent(context: Context, filter: Filter, widgetId: Int): PendingIntent { val intent = Intent(context, FilterSelectionActivity::class.java) - intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK + intent.flags = flags intent.putExtra(FilterSelectionActivity.EXTRA_FILTER, filter) intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId) intent.action = "choose_list" diff --git a/app/src/main/java/org/tasks/widget/TileService.kt b/app/src/main/java/org/tasks/widget/TileService.kt index ea582ccab..62790a825 100644 --- a/app/src/main/java/org/tasks/widget/TileService.kt +++ b/app/src/main/java/org/tasks/widget/TileService.kt @@ -7,5 +7,9 @@ import org.tasks.intents.TaskIntents @RequiresApi(api = VERSION_CODES.N) class TileService : TileService() { - override fun onClick() = startActivityAndCollapse(TaskIntents.getNewTaskIntent(this, null)) + override fun onClick() { + val newTaskIntent = TaskIntents.getNewTaskIntent(this, null) + .addFlags(TaskIntents.FLAGS) + startActivityAndCollapse(newTaskIntent) + } } \ No newline at end of file