summaryrefslogtreecommitdiff
path: root/PermissionController/role-controller/java/com/android/role/controller/behavior/HomeRoleBehavior.java
blob: 4bf5a6294e24ba9af1c912385328f12b1c7f5dae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * 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.role.controller.behavior;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.android.modules.utils.build.SdkLevel;
import com.android.role.controller.model.AppOpPermissions;
import com.android.role.controller.model.Permissions;
import com.android.role.controller.model.Role;
import com.android.role.controller.model.RoleBehavior;
import com.android.role.controller.model.VisibilityMixin;
import com.android.role.controller.util.UserUtils;

import java.util.Arrays;
import java.util.List;
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 {

    private static final List<String> AUTOMOTIVE_PERMISSIONS = Arrays.asList(
            android.Manifest.permission.READ_CALL_LOG,
            android.Manifest.permission.WRITE_CALL_LOG,
            android.Manifest.permission.READ_CONTACTS);

    @RequiresApi(Build.VERSION_CODES.TIRAMISU)
    private static final List<String> WEAR_PERMISSIONS_T = Arrays.asList(
            android.Manifest.permission.POST_NOTIFICATIONS,
            android.Manifest.permission.SYSTEM_APPLICATION_OVERLAY);

    @RequiresApi(Build.VERSION_CODES.VANILLA_ICE_CREAM)
    private static final List<String> WEAR_PERMISSIONS_V = Arrays.asList(
            android.Manifest.permission.ALWAYS_UPDATE_WALLPAPER);

    private static final List<String> WEAR_APP_OP_PERMISSIONS = Arrays.asList(
            android.Manifest.permission.SYSTEM_ALERT_WINDOW);

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

    /**
     * @see com.android.server.pm.PackageManagerService#getDefaultHomeActivity(int)
     */
    @Nullable
    @Override
    public String getFallbackHolderAsUser(@NonNull Role role, @NonNull UserHandle user,
            @NonNull Context context) {
        Context userContext = UserUtils.getUserContext(context, user);
        PackageManager userPackageManager = userContext.getPackageManager();
        Intent intent = role.getRequiredComponents().get(0).getIntentFilterData().createIntent();
        List<ResolveInfo> resolveInfos = userPackageManager.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 (isSettingsApplicationAsUser(resolveInfo.activityInfo.applicationInfo, user,
                    context)) {
                continue;
            }
            if (resolveInfo.priority > priority) {
                packageName = resolveInfo.activityInfo.packageName;
                priority = resolveInfo.priority;
            } else if (resolveInfo.priority == priority) {
                packageName = null;
            }
        }
        return packageName;
    }

    /**
     * Check if the application is a settings application
     */
    private static boolean isSettingsApplicationAsUser(@NonNull ApplicationInfo applicationInfo,
            @NonNull UserHandle user, @NonNull Context context) {
        Context userContext = UserUtils.getUserContext(context, user);
        PackageManager userPackageManager = userContext.getPackageManager();
        ResolveInfo resolveInfo = userPackageManager.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
                || !resolveInfo.activityInfo.exported) {
            return false;
        }
        return Objects.equals(applicationInfo.packageName, resolveInfo.activityInfo.packageName);
    }

    @Override
    public void onHolderSelectedAsUser(@NonNull Role role, @NonNull String packageName,
            @NonNull UserHandle user, @NonNull Context context) {
        // Launch the new home app so the change is immediately visible even if the home button is
        // not pressed.
        Intent intent = new Intent(Intent.ACTION_MAIN)
                .addCategory(Intent.CATEGORY_HOME)
                .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    @Override
    public void grantAsUser(@NonNull Role role, @NonNull String packageName,
            @NonNull UserHandle user, @NonNull Context context) {
        if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) {
            Permissions.grantAsUser(packageName, AUTOMOTIVE_PERMISSIONS,
                    true, false, true, false, false, user, context);
        }

        // Before T, ALLOW_SLIPPERY_TOUCHES may either not exist, or may not be a role permission
        if (isRolePermission(android.Manifest.permission.ALLOW_SLIPPERY_TOUCHES, context)) {
            Permissions.grantAsUser(packageName,
                    Arrays.asList(android.Manifest.permission.ALLOW_SLIPPERY_TOUCHES),
                    true, false, true, false, false, user, context);
        }

        if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH)) {
            if (SdkLevel.isAtLeastT()) {
                Permissions.grantAsUser(packageName, WEAR_PERMISSIONS_T,
                        true, false, true, false, false, user, context);
                for (String permission : WEAR_APP_OP_PERMISSIONS) {
                    AppOpPermissions.grantAsUser(packageName, permission, true, user, context);
                }
            }
            if (SdkLevel.isAtLeastV()) {
                Permissions.grantAsUser(packageName, WEAR_PERMISSIONS_V,
                        true, false, true, false, false, user, context);
            }
        }
    }

    @Override
    public void revokeAsUser(@NonNull Role role, @NonNull String packageName,
            @NonNull UserHandle user, @NonNull Context context) {
        if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) {
            Permissions.revokeAsUser(packageName, AUTOMOTIVE_PERMISSIONS, true, false, false,
                    user, context);
        }

        // Before T, ALLOW_SLIPPERY_TOUCHES may either not exist, or may not be a role permission
        if (isRolePermission(android.Manifest.permission.ALLOW_SLIPPERY_TOUCHES, context)) {
            Permissions.revokeAsUser(packageName,
                    Arrays.asList(android.Manifest.permission.ALLOW_SLIPPERY_TOUCHES),
                    true, false, false, user, context);
        }

        if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH)) {
            if (SdkLevel.isAtLeastT()) {
                Permissions.revokeAsUser(packageName, WEAR_PERMISSIONS_T, true, false, false,
                        user, context);
                for (String permission : WEAR_APP_OP_PERMISSIONS) {
                    AppOpPermissions.revokeAsUser(packageName, permission, user, context);
                }
            }
            if (SdkLevel.isAtLeastV()) {
                Permissions.revokeAsUser(packageName, WEAR_PERMISSIONS_V, true, false, false,
                        user, context);
            }
        }
    }

    /**
     * Return true if the permission exists, and has 'role' protection level.
     * Return false otherwise.
     */
    private boolean isRolePermission(@NonNull String permissionName, @NonNull Context context) {
        PermissionInfo permissionInfo;
        try {
            permissionInfo = context.getPackageManager().getPermissionInfo(permissionName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
        final int flags = permissionInfo.getProtectionFlags();
        return (flags & PermissionInfo.PROTECTION_FLAG_ROLE) == PermissionInfo.PROTECTION_FLAG_ROLE;
    }

    @Override
    public boolean isVisibleAsUser(@NonNull Role role, @NonNull UserHandle user,
            @NonNull Context context) {
        return VisibilityMixin.isVisible("config_showDefaultHome", false, user, context);
    }

    @Override
    public boolean isApplicationVisibleAsUser(@NonNull Role role,
            @NonNull ApplicationInfo applicationInfo, @NonNull UserHandle user,
            @NonNull Context context) {
        return !isSettingsApplicationAsUser(applicationInfo, user, context);
    }
}