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

Commit 8b1b37d5 authored by Hai Zhang's avatar Hai Zhang
Browse files

Add fallback holder for home role behavior.

There is logic inside PackageManagerService and other parts of the
system that allows using a home app without setting it as the default
home (i.e. being configured as the preferred activity for home
intent). This won't work in the new role mechanism because if it's
used as the default home app, it should be set as the default home
app, and setting an app as default home app means configuring it as
the preferred activity.

The only behavior change is that when a user installed another home
app, and they were using the only home app and it wasn't manually
selected so not configured as the preferred activity, they will not be
presented with an intent disambiguation dialog when pressing home. But
that's not a very big behavior change, because when they install a
third home app, they will have to go to settings to change it anyway.

Bug: 124260975
Test: manual
Change-Id: Ide32c5fc218ba6e19dd45593d58a61c81fe03e9e
parent f5b3ea16
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -323,6 +323,7 @@
        behavior="HomeRoleBehavior"
        exclusive="true"
        label="@string/role_label_home">
        <!-- Also used by HomeRoleBehavior.getFallbackHolder(). -->
        <required-components>
            <activity>
                <intent-filter>
+36 −0
Original line number Diff line number Diff line
@@ -26,11 +26,13 @@ import android.os.UserHandle;
import android.provider.Settings;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.Preference;

import com.android.packageinstaller.role.utils.UserUtils;
import com.android.permissioncontroller.R;

import java.util.List;
import java.util.Objects;

/**
@@ -48,6 +50,40 @@ public class HomeRoleBehavior implements RoleBehavior {
        return !UserUtils.isWorkProfile(user, context);
    }

    /**
     * @see com.android.server.pm.PackageManagerService#getDefaultHomeActivity(int)
     */
    @Nullable
    @Override
    public String getFallbackHolder(@NonNull Role role, @NonNull Context context) {
        PackageManager packageManager = context.getPackageManager();
        Intent intent = role.getRequiredComponents().get(0).getIntentFilterData().createIntent();
        List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(intent,
                PackageManager.MATCH_DEFAULT_ONLY | PackageManager.MATCH_DIRECT_BOOT_AWARE
                | PackageManager.MATCH_DIRECT_BOOT_UNAWARE);

        String packageName = null;
        int priority = Integer.MIN_VALUE;
        int resolveInfosSize = resolveInfos.size();
        for (int i = 0; i < resolveInfosSize; i++) {
            ResolveInfo resolveInfo = resolveInfos.get(i);

            // Leave the fallback to PackageManagerService if there is only the fallback home in
            // Settings, because if we fallback to it here, we cannot fallback to a normal home
            // later, and user cannot see the fallback home in the UI anyway.
            if (isSettingsApplication(resolveInfo.activityInfo.applicationInfo, context)) {
                continue;
            }
            if (resolveInfo.priority > priority) {
                packageName = resolveInfo.activityInfo.packageName;
                priority = resolveInfo.priority;
            } else if (resolveInfo.priority == priority) {
                packageName = null;
            }
        }
        return packageName;
    }

    @Override
    public void prepareApplicationPreferenceAsUser(@NonNull Role role,
            @NonNull Preference preference, @NonNull ApplicationInfo applicationInfo,