• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.permission.utils;
18 
19 import android.annotation.SuppressLint;
20 import android.content.Context;
21 import android.content.pm.ApplicationInfo;
22 import android.content.pm.Attribution;
23 import android.content.pm.PackageInfo;
24 import android.content.pm.PackageManager;
25 import android.content.res.Resources;
26 import android.os.Build;
27 
28 import androidx.annotation.Nullable;
29 import androidx.annotation.RequiresApi;
30 
31 import com.android.modules.utils.build.SdkLevel;
32 
33 import java.util.HashMap;
34 import java.util.Map;
35 
36 /**
37  * Utils related to subattribution.
38  */
39 public class SubattributionUtils {
40 
41     /**
42      * Returns true if the app supports subattribution.
43      */
isSubattributionSupported(Context context, ApplicationInfo appInfo)44     public static boolean isSubattributionSupported(Context context, ApplicationInfo appInfo) {
45         if (!SdkLevel.isAtLeastS()) {
46             return false;
47         }
48         return appInfo.areAttributionsUserVisible();
49     }
50 
51     /**
52      * Returns the attribution label map for the package if the app supports subattribtion; Returns
53      * {@code null} otherwise.
54      */
55     @Nullable
56     @SuppressLint("NewApi") // isSubattributionSupported checks api level
getAttributionLabels(Context context, PackageInfo pkgInfo)57     public static Map<Integer, String> getAttributionLabels(Context context,
58             PackageInfo pkgInfo) {
59         if (!isSubattributionSupported(context, pkgInfo.applicationInfo)) {
60             return null;
61         }
62         return getAttributionLabelsInternal(context, pkgInfo);
63     }
64 
65     /**
66      * Returns the attribution label map for the package if the app supports subattribtion; Returns
67      * {@code null} otherwise.
68      */
69     @Nullable
70     @SuppressLint("NewApi") // isSubattributionSupported checks api level
getAttributionLabels(Context context, ApplicationInfo appInfo)71     public static Map<Integer, String> getAttributionLabels(Context context,
72             ApplicationInfo appInfo) {
73         if (!isSubattributionSupported(context, appInfo)) {
74             return null;
75         }
76 
77         PackageInfo packageInfo;
78         try {
79             packageInfo = context.getPackageManager().getPackageInfo(appInfo.packageName,
80                     PackageManager.GET_PERMISSIONS | PackageManager.GET_ATTRIBUTIONS);
81         } catch (PackageManager.NameNotFoundException e) {
82             return null;
83         }
84         return getAttributionLabelsInternal(context, packageInfo);
85     }
86 
87     @Nullable
88     @RequiresApi(Build.VERSION_CODES.S)
getAttributionLabelsInternal(Context context, PackageInfo pkgInfo)89     private static Map<Integer, String> getAttributionLabelsInternal(Context context,
90             PackageInfo pkgInfo) {
91         Context pkgContext;
92         try {
93             pkgContext = context.createPackageContext(pkgInfo.packageName, 0);
94         } catch (PackageManager.NameNotFoundException e) {
95             return null;
96         }
97         Map<Integer, String> attributionLabels = new HashMap<>();
98         for (Attribution attribution : pkgInfo.attributions) {
99             int label = attribution.getLabel();
100             try {
101                 String resourceForLabel = pkgContext.getString(attribution.getLabel());
102                 attributionLabels.put(label, resourceForLabel);
103             } catch (Resources.NotFoundException e) {
104                 // should never happen
105             }
106         }
107         return attributionLabels;
108     }
109 }
110