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

Commit f08cfd45 authored by Hai Zhang's avatar Hai Zhang
Browse files

Add role behavior for home.

So that we can disable home for work profile, exclude fallback home
activities from Settings in UI, and indicate work profile support.

Bug: 124260975
Test: manual
Change-Id: I27d114f610de3730f576f7ea3deb497e33522ce5
parent 3b5c9b24
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -533,6 +533,9 @@
    <!-- Label for the car projection role. [CHAR LIMIT=30] -->
    <string name="role_label_car_projection">Car Projection app</string>

    <!-- Summary indicating that a home app is missing work profile support [CHAR LIMIT=60] -->
    <string name="home_missing_work_profile_support">Doesn\u2019t support work profile</string>

    <!-- Dialog body explaining that the app just selected by the user will not work after a reboot until the user enters their credentials, such as a PIN or password. [CHAR LIMIT=NONE] -->
    <string name="encryption_unaware_confirmation_message">Note: If you restart your device and have a screen lock set, this app can\u2019t start until you unlock your device.</string>

+5 −1
Original line number Diff line number Diff line
@@ -318,7 +318,11 @@
      ~ @see com.android.settings.applications.defaultapps.DefaultHomePicker
      ~ @see com.android.server.pm.PackageManagerService#setHomeActivity(ComponentName, int)
      -->
    <role name="android.app.role.HOME" exclusive="true" label="@string/role_label_home">
    <role
        name="android.app.role.HOME"
        behavior="HomeRoleBehavior"
        exclusive="true"
        label="@string/role_label_home">
        <required-components>
            <activity>
                <intent-filter>
+3 −4
Original line number Diff line number Diff line
@@ -28,7 +28,6 @@ import android.content.pm.ServiceInfo;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
import android.service.voice.VoiceInteractionService;
import android.util.ArraySet;
@@ -38,6 +37,8 @@ import android.util.Xml;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

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

import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
@@ -56,9 +57,7 @@ public class AssistantRoleBehavior implements RoleBehavior {
    @Override
    public boolean isAvailableAsUser(@NonNull Role role, @NonNull UserHandle user,
            @NonNull Context context) {
        UserManager userManager = context.getSystemService(UserManager.class);

        return !userManager.isManagedProfile(user.getIdentifier())
        return !UserUtils.isWorkProfile(user, context)
                && !context.getSystemService(ActivityManager.class).isLowRamDevice();
    }

+87 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 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 com.android.packageinstaller.role.model;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Build;
import android.os.UserHandle;
import android.provider.Settings;

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

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

import java.util.Objects;

/**
 * Class for behavior of the home role.
 *
 * @see com.android.settings.applications.DefaultAppSettings
 * @see com.android.settings.applications.defaultapps.DefaultHomePreferenceController
 * @see com.android.settings.applications.defaultapps.DefaultHomePicker
 */
public class HomeRoleBehavior implements RoleBehavior {

    @Override
    public boolean isAvailableAsUser(@NonNull Role role, @NonNull UserHandle user,
            @NonNull Context context) {
        return !UserUtils.isWorkProfile(user, context);
    }

    @Override
    public void prepareApplicationPreferenceAsUser(@NonNull Role role,
            @NonNull Preference preference, @NonNull ApplicationInfo applicationInfo,
            @NonNull UserHandle user, @NonNull Context context) {
        // Home is not available for work profile, so we can just use the current user.
        boolean isSettingsApplication = isSettingsApplication(applicationInfo, context);
        preference.setVisible(!isSettingsApplication);
        boolean missingWorkProfileSupport = isMissingWorkProfileSupport(applicationInfo, context);
        preference.setEnabled(!missingWorkProfileSupport);
        preference.setSummary(missingWorkProfileSupport ? context.getString(
                R.string.home_missing_work_profile_support) : null);
    }

    private boolean isMissingWorkProfileSupport(@NonNull ApplicationInfo applicationInfo,
            @NonNull Context context) {
        boolean hasWorkProfile = UserUtils.getWorkProfile(context) != null;
        if (!hasWorkProfile) {
            return false;
        }
        boolean isWorkProfileSupported = applicationInfo.targetSdkVersion
                >= Build.VERSION_CODES.LOLLIPOP;
        return !isWorkProfileSupported;
    }

    private boolean isSettingsApplication(@NonNull ApplicationInfo applicationInfo,
            @NonNull Context context) {
        PackageManager packageManager = context.getPackageManager();
        ResolveInfo resolveInfo = packageManager.resolveActivity(new Intent(
                Settings.ACTION_SETTINGS), PackageManager.MATCH_DEFAULT_ONLY
                | PackageManager.MATCH_DIRECT_BOOT_AWARE
                | PackageManager.MATCH_DIRECT_BOOT_UNAWARE);
        if (resolveInfo == null || resolveInfo.activityInfo == null) {
            return false;
        }
        return Objects.equals(applicationInfo.packageName, resolveInfo.activityInfo.packageName);
    }
}
+18 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.preference.Preference;

import com.android.packageinstaller.Constants;
import com.android.packageinstaller.role.utils.PackageUtils;
@@ -261,6 +262,23 @@ public class Role {
        return null;
    }

    /**
     * Prepare a {@link Preference} for an application.
     *
     * @param preference the {@link Preference} for the application
     * @param applicationInfo the {@link ApplicationInfo} for the application
     * @param user the user for the application
     * @param context the {@code Context} to retrieve system services
     */
    public void prepareApplicationPreferenceAsUser(@NonNull Preference preference,
            @NonNull ApplicationInfo applicationInfo, @NonNull UserHandle user,
            @NonNull Context context) {
        if (mBehavior != null) {
            mBehavior.prepareApplicationPreferenceAsUser(this, preference, applicationInfo, user,
                    context);
        }
    }

    /**
     * Get the confirmation message for adding an application as a holder of this role.
     *
Loading