1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.role.controller.behavior.v35;
18 
19 import android.app.admin.DevicePolicyManager;
20 import android.content.Context;
21 import android.os.Build;
22 import android.os.UserHandle;
23 import android.os.UserManager;
24 import android.provider.Settings;
25 
26 import androidx.annotation.NonNull;
27 import androidx.annotation.RequiresApi;
28 
29 import com.android.role.controller.model.Role;
30 import com.android.role.controller.model.RoleBehavior;
31 import com.android.role.controller.util.UserUtils;
32 
33 /**
34  * Class for behavior of the Retail Demo role.
35  */
36 @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
37 public class RetailDemoRoleBehavior implements RoleBehavior {
38 
39     @Override
isPackageQualifiedAsUser(@onNull Role role, @NonNull String packageName, @NonNull UserHandle user, @NonNull Context context)40     public Boolean isPackageQualifiedAsUser(@NonNull Role role, @NonNull String packageName,
41             @NonNull UserHandle user, @NonNull Context context) {
42         if (Settings.Global.getInt(context.getContentResolver(),
43                 Settings.Global.DEVICE_DEMO_MODE, 0) == 0) {
44             return false;
45         }
46         Context userContext = UserUtils.getUserContext(context, user);
47         DevicePolicyManager userDevicePolicyManager =
48                 userContext.getSystemService(DevicePolicyManager.class);
49         if (!(userDevicePolicyManager.isDeviceOwnerApp(packageName)
50                 || userDevicePolicyManager.isProfileOwnerApp(packageName))) {
51             return false;
52         }
53         // Fallback to do additional default check.
54         return null;
55     }
56 
57     @Override
isAvailableAsUser(@onNull Role role, @NonNull UserHandle user, @NonNull Context context)58     public boolean isAvailableAsUser(@NonNull Role role, @NonNull UserHandle user,
59             @NonNull Context context) {
60         Context userContext = UserUtils.getUserContext(context, user);
61         UserManager userUserManager = userContext.getSystemService(UserManager.class);
62         return userUserManager.isSystemUser() || userUserManager.isMainUser()
63                 || userUserManager.isDemoUser();
64     }
65 }
66