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.permission.safetylabel;
18 
19 import android.os.PersistableBundle;
20 
21 import androidx.annotation.NonNull;
22 import androidx.annotation.Nullable;
23 import androidx.annotation.VisibleForTesting;
24 
25 import com.android.permission.safetylabel.DataLabelConstants.DataUsage;
26 
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.Map;
30 
31 /**
32  * Data usage category representation containing one or more {@link DataType}. Valid category keys
33  * are defined in {@link DataCategoryConstants}, each category has a valid set of types {@link
34  * DataType}, which are mapped in {@link DataTypeConstants}
35  */
36 public class DataCategory {
37     private final Map<String, DataType> mDataTypes;
38 
DataCategory(@onNull Map<String, DataType> dataTypes)39     private DataCategory(@NonNull Map<String, DataType> dataTypes) {
40         this.mDataTypes = dataTypes;
41     }
42 
43     /**
44      * Returns a {@link java.util.Collections.UnmodifiableMap} of {@link String} category to {@link
45      * DataCategory} created by parsing a {@link PersistableBundle}
46      */
47     @NonNull
48     @VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
getDataCategoryMap( @ullable PersistableBundle dataLabelBundle, @DataUsage @NonNull String dataUsage)49     static Map<String, DataCategory> getDataCategoryMap(
50             @Nullable PersistableBundle dataLabelBundle, @DataUsage @NonNull String dataUsage) {
51         if (dataLabelBundle == null) {
52             return Collections.emptyMap();
53         }
54 
55         PersistableBundle dataCategoryMapBundle = dataLabelBundle.getPersistableBundle(dataUsage);
56         if (dataCategoryMapBundle == null) {
57             return Collections.emptyMap();
58         }
59 
60         Map<String, DataCategory> dataCategoryMap = new HashMap<>();
61         for (String category : DataCategoryConstants.VALID_CATEGORIES) {
62             DataCategory dataCategory = getDataCategory(dataCategoryMapBundle, dataUsage, category);
63             if (dataCategory != null) {
64                 dataCategoryMap.put(category, dataCategory);
65             }
66         }
67         return Collections.unmodifiableMap(dataCategoryMap);
68     }
69 
70     /**
71      * Returns a {@link DataCategory} created by parsing a {@link PersistableBundle}, or {@code
72      * null} if parsing results in an invalid or empty DataCategory
73      */
74     @Nullable
75     @VisibleForTesting
getDataCategory( @ullable PersistableBundle dataCategoryMapBundle, @NonNull String dataUsage, @NonNull String category)76     static DataCategory getDataCategory(
77             @Nullable PersistableBundle dataCategoryMapBundle,
78             @NonNull String dataUsage,
79             @NonNull String category) {
80         if (dataCategoryMapBundle == null) {
81             return null;
82         }
83 
84         PersistableBundle dataCategoryBundle = dataCategoryMapBundle.getPersistableBundle(category);
85 
86         Map<String, DataType> dataTypeMap =
87                 DataType.getDataTypeMap(dataCategoryBundle, dataUsage, category);
88         if (dataTypeMap.isEmpty()) {
89             return null;
90         }
91 
92         return new DataCategory(dataTypeMap);
93     }
94 
95     /** Return the type {@link Map} of String type key to {@link DataType} */
96     @NonNull
getDataTypes()97     public Map<String, DataType> getDataTypes() {
98         return mDataTypes;
99     }
100 }
101