1 /*
2  * Copyright (C) 2018 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.permissioncontroller.role.model;
18 
19 import android.content.Context;
20 import android.util.ArrayMap;
21 
22 import androidx.annotation.NonNull;
23 import androidx.annotation.Nullable;
24 
25 /**
26  * Provides access to all the {@link Role} definitions.
27  */
28 public class Roles {
29 
30     @NonNull
31     private static final Object sLock = new Object();
32 
33     @Nullable
34     private static ArrayMap<String, Role> sRoles;
35 
Roles()36     private Roles() {}
37 
38     /**
39      * Get the roles defined in {@code roles.xml}.
40      *
41      * @param context the {@code Context} used to read the XML resource
42      *
43      * @return a map from role name to {@link Role} instances
44      */
45     @NonNull
get(@onNull Context context)46     public static ArrayMap<String, Role> get(@NonNull Context context) {
47         synchronized (sLock) {
48             if (sRoles == null) {
49                 sRoles = new RoleParser(context).parse();
50             }
51             return sRoles;
52         }
53     }
54 }
55