• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.v34;
18 
19 import android.content.Context;
20 import android.content.pm.PackageManager;
21 import android.content.res.Resources;
22 import android.os.Build;
23 import android.os.UserHandle;
24 
25 import androidx.annotation.NonNull;
26 import androidx.annotation.RequiresApi;
27 
28 import com.android.role.controller.model.Role;
29 import com.android.role.controller.model.RoleBehavior;
30 import com.android.role.controller.util.UserUtils;
31 
32 import java.util.Objects;
33 
34 /**
35  * Class for behavior of the Notes role.
36  */
37 @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
38 public class NotesRoleBehavior implements RoleBehavior {
39 
40     @Override
isAvailableAsUser(@onNull Role role, @NonNull UserHandle user, @NonNull Context context)41     public boolean isAvailableAsUser(@NonNull Role role, @NonNull UserHandle user,
42             @NonNull Context context) {
43         // Role should be enabled by OEMs.
44         Resources resources = getResources(context);
45         if (!resources.getBoolean(android.R.bool.config_enableDefaultNotes)) {
46             return false;
47         }
48 
49         if (UserUtils.isManagedProfile(user, context)) {
50             // The role holder for work profile is separately controlled via config.
51             return resources.getBoolean(android.R.bool.config_enableDefaultNotesForWorkProfile);
52         }
53 
54         return !UserUtils.isProfile(user, context);
55     }
56 
57     /**
58      * Gets {@link Resources} for fetching notes role related resources.
59      * <p>
60      * When running within the system server process, this method retrieves system resource that
61      * include Runtime Resource Overlay (RRO) values. These RRO values are not accessible through
62      * the {@code context} object provided to this class during construction.
63      *
64      * @throws RuntimeException when no system package is found when this class runs in the system
65      * server process.
66      */
67     @NonNull
getResources(@onNull Context context)68     private static Resources getResources(@NonNull Context context) {
69         if (Objects.equals(context.getPackageName(), "android")) {
70             try {
71                 return context.getPackageManager().getResourcesForApplication("system");
72             } catch (PackageManager.NameNotFoundException e) {
73                 throw new RuntimeException("System package not found", e);
74             }
75         } else {
76             return context.getResources();
77         }
78     }
79 }
80