Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a3965885 authored by Garfield Tan's avatar Garfield Tan
Browse files

Clear tasks and start a new one when we failed to restore a task.

This is to cover a corner case where the targeted activity has been
moved across system updates so Android can't find the targeted activity.

Test: It stops crashing and tasks can still be restored.
Bug: 32178848
Change-Id: I2288b3e8758d2e4f86b61f101aaeb834e1f9b719
parent a9e4522b
Loading
Loading
Loading
Loading
+34 −7
Original line number Diff line number Diff line
@@ -59,16 +59,27 @@ public class LauncherActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        launch();

        finish();
    }

    private void launch() {
        ActivityManager activities = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

        Intent intent = findTask(activities);
        if (intent != null) {
            restoreTask(intent);
            if (restoreTask(intent)) {
                return;
            } else {
            startTask();
                // We failed to restore the task. It may happen when system was just updated and we
                // moved the location of the targeted activity. Chances is that the rest of tasks
                // can't be restored either, so clean those tasks and start a new one.
                clearTask(activities);
            }
        }

        finish();
        startTask();
    }

    private @Nullable Intent findTask(ActivityManager activities) {
@@ -92,10 +103,26 @@ public class LauncherActivity extends Activity {
        startActivity(intent);
    }

    private void restoreTask(Intent intent) {
    private boolean restoreTask(Intent intent) {
        if (DEBUG) Log.d(TAG, "Restoring existing task > " + intent.getData());
        try {
            // TODO: This doesn't appear to restore a task once it has stopped running.
            startActivity(intent);

            return true;
        } catch (Exception e) {
            Log.w(TAG, "Failed to restore task > " + intent.getData() +
                    ". Clear all existing tasks and start a new one.", e);
        }

        return false;
    }

    private void clearTask(ActivityManager activities) {
        List<AppTask> tasks = activities.getAppTasks();
        for (AppTask task : tasks) {
            task.finishAndRemoveTask();
        }
    }

    public static final Intent createLaunchIntent(Activity activity) {