Update task and back stack behavior

pull/1051/head
Alex Baker 4 years ago
parent d795f1f6d6
commit 108c1f284b

@ -191,10 +191,11 @@
<!-- Activity launched from ShareLink menu item -->
<!-- TODO: clearTaskOnLaunch probable cause of #275 -->
<activity
android:clearTaskOnLaunch="true"
android:icon="@mipmap/ic_launcher_blue"
android:label="@string/action_create_new_task"
android:name="com.todoroo.astrid.activity.ShareLinkActivity"
android:taskAffinity=":share"
android:excludeFromRecents="true"
android:roundIcon="@mipmap/ic_launcher_blue">
<intent-filter>
<action android:name="android.intent.action.PROCESS_TEXT"/>

@ -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,20 +210,50 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl
defaultFilterProvider.getFilterFromPreference(loadFilter)
}
clearUi()
openTaskListFragment(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)
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)
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)
intent.removeExtra(TOKEN_CREATE_NEW_LIST_NAME)
@ -238,16 +271,23 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl
}
private fun hideDetailFragment() {
filter?.let {
supportFragmentManager
.beginTransaction()
.replace(R.id.detail, newEmptyTaskEditFragment(filter!!))
.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()
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()
taskListFragment?.loadTaskListContent()
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

@ -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<Uri> copyAttachment(Intent intent) {
Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
String filename = getFilename(context, uri);

@ -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);
}
}

@ -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

@ -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);

@ -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)

@ -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()
}
}

@ -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)

@ -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);
}

@ -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)));

@ -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"

@ -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)
}
}
Loading…
Cancel
Save