1 /*
2  * Copyright (C) 2019 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 package com.android.car.developeroptions.applications.specialaccess.financialapps;
17 
18 import static android.Manifest.permission.READ_SMS;
19 import static android.Manifest.permission.SMS_FINANCIAL_TRANSACTIONS;
20 
21 import android.app.AppOpsManager;
22 import android.content.Context;
23 import android.content.pm.PackageInfo;
24 import android.content.pm.PackageManager;
25 import android.content.pm.PackageManager.NameNotFoundException;
26 import android.os.Build;
27 import android.util.Log;
28 
29 import androidx.preference.Preference;
30 import androidx.preference.PreferenceScreen;
31 import androidx.preference.SwitchPreference;
32 
33 import com.android.internal.annotations.VisibleForTesting;
34 import com.android.internal.util.ArrayUtils;
35 import com.android.car.developeroptions.core.BasePreferenceController;
36 import com.android.settingslib.applications.ApplicationsState;
37 import com.android.settingslib.applications.ApplicationsState.AppEntry;
38 
39 import java.util.ArrayList;
40 import java.util.List;
41 
42 public class FinancialAppsController extends BasePreferenceController
43         implements ApplicationsState.Callbacks {
44     private final static String TAG = FinancialAppsController.class.getSimpleName();
45 
46     @VisibleForTesting
47     PreferenceScreen mRoot;
48 
FinancialAppsController(Context context, String key)49     public FinancialAppsController(Context context, String key) {
50         super(context, key);
51     }
52 
53     @AvailabilityStatus
getAvailabilityStatus()54     public int getAvailabilityStatus() {
55         return AVAILABLE;
56     }
57 
58     @Override
displayPreference(PreferenceScreen screen)59     public void displayPreference(PreferenceScreen screen) {
60         super.displayPreference(screen);
61         mRoot = screen;
62     }
63 
64     @Override
updateState(Preference preference)65     public void updateState(Preference preference) {
66         updateList();
67     }
68 
updateList()69     private void updateList() {
70         mRoot.removeAll();
71 
72         final PackageManager packageManager = mContext.getPackageManager();
73         final AppOpsManager appOpsManager = mContext.getSystemService(AppOpsManager.class);
74 
75         final List<PackageInfo> installedPackages =
76                 packageManager.getInstalledPackages(PackageManager.GET_PERMISSIONS);
77         final int numPackages = installedPackages.size();
78         for (int i = 0; i < numPackages; i++) {
79             final PackageInfo installedPackage = installedPackages.get(i);
80 
81             if (installedPackage.requestedPermissions == null) {
82                 continue;
83             }
84             final int targetSdk = installedPackage.applicationInfo.targetSdkVersion;
85             final String pkgName = installedPackage.packageName;
86 
87             if ((targetSdk >= Build.VERSION_CODES.Q
88                     && ArrayUtils.contains(installedPackage.requestedPermissions,
89                             SMS_FINANCIAL_TRANSACTIONS))
90                     || (targetSdk < Build.VERSION_CODES.Q
91                     && ArrayUtils.contains(installedPackage.requestedPermissions,
92                             READ_SMS))) {
93                 final SwitchPreference pref = new SwitchPreference(mRoot.getContext());
94                 pref.setTitle(installedPackage.applicationInfo.loadLabel(packageManager));
95                 pref.setKey(pkgName);
96 
97                 pref.setChecked(
98                         appOpsManager.checkOp(
99                                 targetSdk >= Build.VERSION_CODES.Q
100                                         ? AppOpsManager.OP_SMS_FINANCIAL_TRANSACTIONS
101                                         : AppOpsManager.OP_READ_SMS,
102                                 installedPackage.applicationInfo.uid,
103                                 pkgName) == AppOpsManager.MODE_ALLOWED);
104 
105                 pref.setOnPreferenceChangeListener((preference, newValue) -> {
106                     final int uid;
107                     try {
108                         uid = packageManager.getPackageInfo(preference.getKey(), 0)
109                                 .applicationInfo.uid;
110                     } catch (NameNotFoundException e) {
111                         Log.e(TAG, "onPreferenceChange: Failed to get uid for "
112                                 + preference.getKey());
113                         return false;
114                     }
115 
116                     appOpsManager.setMode(
117                             targetSdk >= Build.VERSION_CODES.Q
118                                     ? AppOpsManager.OP_SMS_FINANCIAL_TRANSACTIONS
119                                     : AppOpsManager.OP_READ_SMS,
120                             uid,
121                             pkgName,
122                             (Boolean) newValue ? AppOpsManager.MODE_ALLOWED
123                                     : AppOpsManager.MODE_IGNORED);
124                     return true;
125                 });
126                 mRoot.addPreference(pref);
127             }
128         }
129     }
130 
131     @Override
onRunningStateChanged(boolean running)132     public void onRunningStateChanged(boolean running) {}
133 
134     @Override
onPackageListChanged()135     public void onPackageListChanged() {
136         updateList();
137     }
138 
139     @Override
onRebuildComplete(ArrayList<AppEntry> apps)140     public void onRebuildComplete(ArrayList<AppEntry> apps) {}
141 
142     @Override
onPackageIconChanged()143     public void onPackageIconChanged() {}
144 
145     @Override
onPackageSizeChanged(String packageName)146     public void onPackageSizeChanged(String packageName) {}
147 
148     @Override
onAllSizesComputed()149     public void onAllSizesComputed() {}
150 
151     @Override
onLauncherInfoChanged()152     public void onLauncherInfoChanged() {}
153 
154     @Override
onLoadEntriesCompleted()155     public void onLoadEntriesCompleted() {}
156 }
157