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

Commit cfefeb72 authored by Yuncheol Heo's avatar Yuncheol Heo
Browse files

Configure the number of tasks to scan to determine Home visibility.

- Auto uses the multi display and its Home uses TaskView, so it's not enough to peek the top Tasks to get the visibility of Home.
- Set the default number of tasks to retrieve as 10 to scan all visible tasks.
- Don't query PM to check if the task is Home, check the ActivityType instead.

Bug: 236099098
Test: atest android.app.cts.ActivityManagerTest#testHomeVisibilityListener

Change-Id: I10b6d84292d3180f095dd9f8cf76b4c9db641091
parent 5c8fd0dd
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -5124,7 +5124,7 @@ public class ActivityManager {
        Preconditions.checkNotNull(listener);
        Preconditions.checkNotNull(executor);
        try {
            listener.init(mContext, executor, this);
            listener.init(mContext, executor);
            getService().registerProcessObserver(listener.mObserver);
            // Notify upon first registration.
            executor.execute(() ->
+23 −22
Original line number Diff line number Diff line
@@ -16,16 +16,17 @@

package android.app;

import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
import static android.content.Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS;
import static android.view.Display.DEFAULT_DISPLAY;

import android.annotation.SuppressLint;
import android.annotation.SystemApi;
import android.annotation.TestApi;
import android.content.ComponentName;
import android.content.Context;
import android.os.Binder;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Executor;

/**
@@ -39,20 +40,21 @@ import java.util.concurrent.Executor;
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
@TestApi
public abstract class HomeVisibilityListener {
    private Context mContext;
    private ActivityManager mActivityManager;
    private ActivityTaskManager mActivityTaskManager;
    private Executor mExecutor;
    private int mMaxScanTasksForHomeVisibility;
    /** @hide */
    android.app.IProcessObserver.Stub mObserver;
    /** @hide */
    boolean mIsHomeActivityVisible;

    /** @hide */
    void init(Context context, Executor executor, ActivityManager activityManager) {
        mContext = context;
        mActivityManager = activityManager;
        mIsHomeActivityVisible = isHomeActivityVisible();
    void init(Context context, Executor executor) {
        mActivityTaskManager = ActivityTaskManager.getInstance();
        mExecutor = executor;
        mMaxScanTasksForHomeVisibility = context.getResources().getInteger(
                com.android.internal.R.integer.config_maxScanTasksForHomeVisibility);
        mIsHomeActivityVisible = isHomeActivityVisible();
    }

    /**
@@ -91,22 +93,21 @@ public abstract class HomeVisibilityListener {
    }

    private boolean isHomeActivityVisible() {
        List<ActivityManager.RunningTaskInfo> tasks = mActivityManager.getRunningTasks(1);
        if (tasks == null || tasks.isEmpty()) {
        List<ActivityManager.RunningTaskInfo> tasksTopToBottom = mActivityTaskManager.getTasks(
                mMaxScanTasksForHomeVisibility, /* filterOnlyVisibleRecents= */ true,
                /* keepIntentExtra= */ false, DEFAULT_DISPLAY);
        if (tasksTopToBottom == null || tasksTopToBottom.isEmpty()) {
            return false;
        }

        String top = tasks.get(0).topActivity.getPackageName();
        if (top == null) {
            return false;
        for (int i = 0, taskSize = tasksTopToBottom.size(); i < taskSize; ++i) {
            ActivityManager.RunningTaskInfo task = tasksTopToBottom.get(i);
            if (!task.isVisible()
                    || (task.baseIntent.getFlags() & FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) != 0) {
                continue;
            }

        // We can assume that the screen is idle if the home application is in the foreground.
        ComponentName defaultHomeComponent = mContext.getPackageManager()
                .getHomeActivities(new ArrayList<>());
        if (defaultHomeComponent == null) return false;

        String defaultHomePackage = defaultHomeComponent.getPackageName();
        return Objects.equals(top, defaultHomePackage);
            return task.getActivityType() == ACTIVITY_TYPE_HOME;
        }
        return false;
    }
}
+3 −0
Original line number Diff line number Diff line
@@ -5872,4 +5872,7 @@

    <!-- Whether the wake screen on notifications feature is available. -->
    <bool name="config_pulseOnNotificationsAvailable">true</bool>

    <!-- The number of tasks to scan to get the visibility of Home -->
    <integer name="config_maxScanTasksForHomeVisibility">10</integer>
</resources>
+2 −0
Original line number Diff line number Diff line
@@ -3976,6 +3976,8 @@

  <java-symbol type="string" name="config_customCountryDetector" />

  <java-symbol type="integer" name="config_maxScanTasksForHomeVisibility" />

  <!-- For Foldables -->
  <java-symbol type="array" name="config_foldedDeviceStates" />
  <java-symbol type="array" name="config_deviceStatesOnWhichToWakeUp" />