• 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.car.settings.location;
18 
19 import android.Manifest;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.ApplicationInfo;
23 import android.content.pm.PackageManager;
24 import android.content.pm.PackageManager.NameNotFoundException;
25 import android.net.Uri;
26 import android.os.Bundle;
27 import android.os.UserHandle;
28 import android.util.IconDrawableFactory;
29 
30 import com.android.car.settings.R;
31 import com.android.car.settings.common.Logger;
32 import com.android.car.ui.preference.CarUiTwoActionTextPreference;
33 
34 /** Utilities related to location-powered ADAS privacy policy. */
35 public final class AdasPrivacyPolicyUtil {
36     private static final Logger LOG = new Logger(AdasPrivacyPolicyUtil.class);
37     private static final String PRIVACY_POLICY_KEY = "privacy_policy";
38 
AdasPrivacyPolicyUtil()39     private AdasPrivacyPolicyUtil() {}
40 
41     /**
42      * Creates a {@link CarUiTwoActionTextPreference} for ADAS app with it's privacy policy and a
43      * link to its location permission settings.
44      */
createPrivacyPolicyPreference( Context context, String pkgName, UserHandle userHandle)45     public static CarUiTwoActionTextPreference createPrivacyPolicyPreference(
46             Context context, String pkgName, UserHandle userHandle) {
47         CarUiTwoActionTextPreference pref = new CarUiTwoActionTextPreference(context);
48 
49         PackageManager packageManager = context.getPackageManager();
50 
51         IconDrawableFactory drawableFactory = IconDrawableFactory.newInstance(context);
52         int userId = userHandle.getIdentifier();
53 
54         ApplicationInfo appInfo;
55         try {
56             appInfo =
57                     packageManager.getApplicationInfoAsUser(
58                             pkgName, PackageManager.GET_META_DATA, userId);
59         } catch (NameNotFoundException ex) {
60             LOG.e("Failed to get application info for " + pkgName);
61             return null;
62         }
63 
64         pref.setIcon(drawableFactory.getBadgedIcon(appInfo, userId));
65 
66         CharSequence appLabel = packageManager.getApplicationLabel(appInfo);
67         CharSequence badgedAppLabel = packageManager.getUserBadgedLabel(appLabel, userHandle);
68         pref.setTitle(badgedAppLabel);
69 
70         pref.setOnPreferenceClickListener(
71                 preference -> {
72                     Intent intent = new Intent(Intent.ACTION_MANAGE_APP_PERMISSION);
73                     intent.putExtra(
74                             Intent.EXTRA_PERMISSION_GROUP_NAME, Manifest.permission_group.LOCATION);
75                     intent.putExtra(Intent.EXTRA_PACKAGE_NAME, pkgName);
76                     intent.putExtra(Intent.EXTRA_USER, userHandle);
77                     context.startActivity(intent);
78                     return true;
79                 });
80 
81         pref.setSecondaryActionText(R.string.location_driver_assistance_privacy_policy_button_text);
82 
83         Bundle bundle = appInfo.metaData;
84 
85         if (bundle == null) {
86             LOG.e(pkgName + "doesn't provide meta data in manifest");
87             return pref;
88         }
89         CharSequence privacyPolicyLink = bundle.getCharSequence(PRIVACY_POLICY_KEY);
90         if (privacyPolicyLink == null) {
91             LOG.e(pkgName + " doesn't provide privacy policy");
92             return pref;
93         }
94         Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(privacyPolicyLink.toString()));
95 
96         pref.setOnSecondaryActionClickListener(
97                 () -> {
98                     context.startActivity(intent);
99                 });
100         return pref;
101     }
102 }
103