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 static com.android.permission.safetylabel.DataLabelConstants.DATA_USAGE_COLLECTED;
20 import static com.android.permission.safetylabel.DataLabelConstants.DATA_USAGE_SHARED;
21 
22 import android.os.PersistableBundle;
23 
24 import androidx.annotation.NonNull;
25 import androidx.annotation.Nullable;
26 import androidx.annotation.VisibleForTesting;
27 
28 import java.util.Map;
29 
30 /**
31  * Data label representation with data shared and data collected maps containing zero or more
32  * {@link DataCategory}
33  */
34 public class DataLabel {
35     @VisibleForTesting static final String KEY_DATA_LABEL = "data_labels";
36     private final Map<String, DataCategory> mDataCollected;
37     private final Map<String, DataCategory> mDataShared;
38 
DataLabel( @onNull Map<String, DataCategory> dataCollected, @NonNull Map<String, DataCategory> dataShared)39     public DataLabel(
40             @NonNull Map<String, DataCategory> dataCollected,
41             @NonNull Map<String, DataCategory> dataShared) {
42         mDataCollected = dataCollected;
43         mDataShared = dataShared;
44     }
45 
46     /** Returns a {@link DataLabel} created by parsing a SafetyLabel {@link PersistableBundle} */
47     @NonNull
48     @VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
getDataLabel(@ullable PersistableBundle safetyLabelBundle)49     public static DataLabel getDataLabel(@Nullable PersistableBundle safetyLabelBundle) {
50         if (safetyLabelBundle == null) {
51             return null;
52         }
53 
54         PersistableBundle dataLabelBundle = safetyLabelBundle.getPersistableBundle(KEY_DATA_LABEL);
55         if (dataLabelBundle == null) {
56             return null;
57         }
58 
59         Map<String, DataCategory> dataCollectedCategoryMap =
60                 DataCategory.getDataCategoryMap(dataLabelBundle, DATA_USAGE_COLLECTED);
61         Map<String, DataCategory> dataSharedCategoryMap =
62                 DataCategory.getDataCategoryMap(dataLabelBundle, DATA_USAGE_SHARED);
63         return new DataLabel(dataCollectedCategoryMap, dataSharedCategoryMap);
64     }
65 
66     /**
67      * Returns the data collected {@link Map} of {@link
68      * com.android.permission.safetylabel.DataCategoryConstants.Category} to {@link DataCategory}
69      */
70     @NonNull
getDataCollected()71     public Map<String, DataCategory> getDataCollected() {
72         return mDataCollected;
73     }
74 
75     /**
76      * Returns the data shared {@link Map} of {@link
77      * com.android.permission.safetylabel.DataCategoryConstants.Category} to {@link DataCategory}
78      */
79     @NonNull
getDataShared()80     public Map<String, DataCategory> getDataShared() {
81         return mDataShared;
82     }
83 }
84