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

Commit 50b3cc03 authored by Prabir Pradhan's avatar Prabir Pradhan
Browse files

Move launcher activities to the back when back pressed on root task

This change partially reverts the back button behavior change introduced
in: I48ec35f841ab3b306fe80845150000c390908f5e

Instead of always moving the task to the back when the back button is
pressed on the root task, we only do it when the activity that is open
is a "launcher activity". These are activities that act as the main
entry points into an application, indicated with an intent filter with
ACTION_MAIN and CATEGORY_LAUNCHER.

Bug: 176235889
Test: atest WmTests
Test: atest CtsWindowManagerDeviceTestCases
Test: manual: flash crosshatch, press back button on a launcher activity,
observe task is moved to back; press back on non-launcher activity,
observe task is finished.

Change-Id: I1cbf85f1a007aeedcb66b9343d182188bacc78e7
parent c3d7c80f
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -400,6 +400,9 @@ android.app.INotificationManager
android.app.IProcessObserver$Stub$Proxy
android.app.IProcessObserver$Stub
android.app.IProcessObserver
android.app.IRequestFinishCallback$Stub$Proxy
android.app.IRequestFinishCallback$Stub
android.app.IRequestFinishCallback
android.app.ISearchManager$Stub$Proxy
android.app.ISearchManager$Stub
android.app.ISearchManager
+19 −1
Original line number Diff line number Diff line
@@ -156,6 +156,7 @@ import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -3811,6 +3812,22 @@ public class Activity extends ContextThemeWrapper
        return false;
    }

    private static final class RequestFinishCallback extends IRequestFinishCallback.Stub {
        private final WeakReference<Activity> mActivityRef;

        RequestFinishCallback(WeakReference<Activity> activityRef) {
            mActivityRef = activityRef;
        }

        @Override
        public void requestFinish() {
            Activity activity = mActivityRef.get();
            if (activity != null) {
                activity.mHandler.post(activity::finishAfterTransition);
            }
        }
    }

    /**
     * Called when the activity has detected the user's press of the back
     * key.  The default implementation simply finishes the current activity,
@@ -3834,7 +3851,8 @@ public class Activity extends ContextThemeWrapper
        // Inform activity task manager that the activity received a back press while at the
        // root of the task. This call allows ActivityTaskManager to intercept or move the task
        // to the back.
        ActivityClient.getInstance().onBackPressedOnTaskRoot(mToken);
        ActivityClient.getInstance().onBackPressedOnTaskRoot(mToken,
                new RequestFinishCallback(new WeakReference<>(this)));

        // Activity was launched when user tapped a link in the Autofill Save UI - Save UI must
        // be restored now.
+2 −2
Original line number Diff line number Diff line
@@ -480,9 +480,9 @@ public class ActivityClient {
        }
    }

    void onBackPressedOnTaskRoot(IBinder token) {
    void onBackPressedOnTaskRoot(IBinder token, IRequestFinishCallback callback) {
        try {
            getActivityClientController().onBackPressedOnTaskRoot(token);
            getActivityClientController().onBackPressedOnTaskRoot(token, callback);
        } catch (RemoteException e) {
            e.rethrowFromSystemServer();
        }
+3 −1
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package android.app;

import android.app.ActivityManager;
import android.app.IRequestFinishCallback;
import android.app.PictureInPictureParams;
import android.content.ComponentName;
import android.content.Intent;
@@ -141,7 +142,8 @@ interface IActivityClientController {
     * Reports that an Activity received a back key press when there were no additional activities
     * on the back stack.
     */
    oneway void onBackPressedOnTaskRoot(in IBinder token);
    oneway void onBackPressedOnTaskRoot(in IBinder activityToken,
            in IRequestFinishCallback callback);

    /** Reports that the splash screen view has attached to activity.  */
    oneway void splashScreenAttached(in IBinder token);
+27 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.app;

/**
 * This callback allows ActivityTaskManager to ask the calling Activity
 * to finish in response to a call to onBackPressedOnTaskRoot.
 *
 * {@hide}
 */
oneway interface IRequestFinishCallback {
    void requestFinish();
}
Loading