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