Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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.server.pm.permission; |
| 18 | |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 19 | import static android.os.Process.FIRST_APPLICATION_UID; |
Todd Kennedy | 7c4c55d | 2017-11-02 10:01:39 -0700 | [diff] [blame] | 20 | |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 21 | import android.Manifest; |
| 22 | import android.annotation.NonNull; |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 23 | import android.annotation.Nullable; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 24 | import android.app.ActivityManager; |
| 25 | import android.app.DownloadManager; |
| 26 | import android.app.admin.DevicePolicyManager; |
| 27 | import android.companion.CompanionDeviceManager; |
Abodunrinwa Toki | 2c01b6c | 2018-02-13 17:47:48 +0000 | [diff] [blame] | 28 | import android.content.ComponentName; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 29 | import android.content.Context; |
| 30 | import android.content.Intent; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 31 | import android.content.pm.ApplicationInfo; |
Todd Kennedy | 42d6160 | 2017-12-12 14:44:19 -0800 | [diff] [blame] | 32 | import android.content.pm.PackageList; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 33 | import android.content.pm.PackageManager; |
| 34 | import android.content.pm.PackageManagerInternal; |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 35 | import android.content.pm.PackageManagerInternal.PackagesProvider; |
| 36 | import android.content.pm.PackageManagerInternal.SyncAdapterPackagesProvider; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 37 | import android.content.pm.PackageParser; |
| 38 | import android.content.pm.ProviderInfo; |
| 39 | import android.content.pm.ResolveInfo; |
| 40 | import android.media.RingtoneManager; |
| 41 | import android.net.Uri; |
| 42 | import android.os.Binder; |
| 43 | import android.os.Build; |
| 44 | import android.os.Environment; |
| 45 | import android.os.Handler; |
| 46 | import android.os.Looper; |
| 47 | import android.os.Message; |
| 48 | import android.os.UserHandle; |
| 49 | import android.os.storage.StorageManager; |
| 50 | import android.print.PrintManager; |
| 51 | import android.provider.CalendarContract; |
| 52 | import android.provider.ContactsContract; |
| 53 | import android.provider.MediaStore; |
| 54 | import android.provider.Telephony.Sms.Intents; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 55 | import android.security.Credentials; |
Abodunrinwa Toki | 2c01b6c | 2018-02-13 17:47:48 +0000 | [diff] [blame] | 56 | import android.service.textclassifier.TextClassifierService; |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 57 | import android.telephony.TelephonyManager; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 58 | import android.util.ArrayMap; |
| 59 | import android.util.ArraySet; |
| 60 | import android.util.Log; |
| 61 | import android.util.Slog; |
| 62 | import android.util.Xml; |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 63 | |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 64 | import com.android.internal.util.XmlUtils; |
| 65 | import com.android.server.LocalServices; |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 66 | import com.android.server.pm.PackageManagerService; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 67 | |
| 68 | import org.xmlpull.v1.XmlPullParser; |
| 69 | import org.xmlpull.v1.XmlPullParserException; |
| 70 | |
| 71 | import java.io.BufferedInputStream; |
| 72 | import java.io.File; |
| 73 | import java.io.FileInputStream; |
| 74 | import java.io.IOException; |
| 75 | import java.io.InputStream; |
| 76 | import java.util.ArrayList; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 77 | import java.util.Collections; |
| 78 | import java.util.List; |
| 79 | import java.util.Map; |
| 80 | import java.util.Set; |
| 81 | |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 82 | /** |
| 83 | * This class is the policy for granting runtime permissions to |
| 84 | * platform components and default handlers in the system such |
| 85 | * that the device is usable out-of-the-box. For example, the |
| 86 | * shell UID is a part of the system and the Phone app should |
| 87 | * have phone related permission by default. |
| 88 | * <p> |
| 89 | * NOTE: This class is at the wrong abstraction level. It is a part of the package manager |
| 90 | * service but knows about lots of higher level subsystems. The correct way to do this is |
| 91 | * to have an interface defined in the package manager but have the impl next to other |
| 92 | * policy stuff like PhoneWindowManager |
| 93 | */ |
| 94 | public final class DefaultPermissionGrantPolicy { |
| 95 | private static final String TAG = "DefaultPermGrantPolicy"; // must be <= 23 chars |
| 96 | private static final boolean DEBUG = false; |
| 97 | |
| 98 | private static final int DEFAULT_FLAGS = |
| 99 | PackageManager.MATCH_DIRECT_BOOT_AWARE | PackageManager.MATCH_DIRECT_BOOT_UNAWARE |
| 100 | | PackageManager.MATCH_UNINSTALLED_PACKAGES; |
| 101 | |
| 102 | private static final String AUDIO_MIME_TYPE = "audio/mpeg"; |
| 103 | |
| 104 | private static final String TAG_EXCEPTIONS = "exceptions"; |
| 105 | private static final String TAG_EXCEPTION = "exception"; |
| 106 | private static final String TAG_PERMISSION = "permission"; |
| 107 | private static final String ATTR_PACKAGE = "package"; |
| 108 | private static final String ATTR_NAME = "name"; |
| 109 | private static final String ATTR_FIXED = "fixed"; |
| 110 | |
| 111 | private static final Set<String> PHONE_PERMISSIONS = new ArraySet<>(); |
| 112 | static { |
| 113 | PHONE_PERMISSIONS.add(Manifest.permission.READ_PHONE_STATE); |
| 114 | PHONE_PERMISSIONS.add(Manifest.permission.CALL_PHONE); |
| 115 | PHONE_PERMISSIONS.add(Manifest.permission.READ_CALL_LOG); |
| 116 | PHONE_PERMISSIONS.add(Manifest.permission.WRITE_CALL_LOG); |
| 117 | PHONE_PERMISSIONS.add(Manifest.permission.ADD_VOICEMAIL); |
| 118 | PHONE_PERMISSIONS.add(Manifest.permission.USE_SIP); |
| 119 | PHONE_PERMISSIONS.add(Manifest.permission.PROCESS_OUTGOING_CALLS); |
| 120 | } |
| 121 | |
| 122 | private static final Set<String> CONTACTS_PERMISSIONS = new ArraySet<>(); |
| 123 | static { |
| 124 | CONTACTS_PERMISSIONS.add(Manifest.permission.READ_CONTACTS); |
| 125 | CONTACTS_PERMISSIONS.add(Manifest.permission.WRITE_CONTACTS); |
| 126 | CONTACTS_PERMISSIONS.add(Manifest.permission.GET_ACCOUNTS); |
| 127 | } |
| 128 | |
| 129 | private static final Set<String> LOCATION_PERMISSIONS = new ArraySet<>(); |
| 130 | static { |
| 131 | LOCATION_PERMISSIONS.add(Manifest.permission.ACCESS_FINE_LOCATION); |
| 132 | LOCATION_PERMISSIONS.add(Manifest.permission.ACCESS_COARSE_LOCATION); |
| 133 | } |
| 134 | |
Eric Enslen | 1e423b9 | 2017-12-18 11:30:21 -0800 | [diff] [blame] | 135 | private static final Set<String> COARSE_LOCATION_PERMISSIONS = new ArraySet<>(); |
| 136 | static { |
| 137 | COARSE_LOCATION_PERMISSIONS.add(Manifest.permission.ACCESS_COARSE_LOCATION); |
| 138 | } |
| 139 | |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 140 | private static final Set<String> CALENDAR_PERMISSIONS = new ArraySet<>(); |
| 141 | static { |
| 142 | CALENDAR_PERMISSIONS.add(Manifest.permission.READ_CALENDAR); |
| 143 | CALENDAR_PERMISSIONS.add(Manifest.permission.WRITE_CALENDAR); |
| 144 | } |
| 145 | |
| 146 | private static final Set<String> SMS_PERMISSIONS = new ArraySet<>(); |
| 147 | static { |
| 148 | SMS_PERMISSIONS.add(Manifest.permission.SEND_SMS); |
| 149 | SMS_PERMISSIONS.add(Manifest.permission.RECEIVE_SMS); |
| 150 | SMS_PERMISSIONS.add(Manifest.permission.READ_SMS); |
| 151 | SMS_PERMISSIONS.add(Manifest.permission.RECEIVE_WAP_PUSH); |
| 152 | SMS_PERMISSIONS.add(Manifest.permission.RECEIVE_MMS); |
| 153 | SMS_PERMISSIONS.add(Manifest.permission.READ_CELL_BROADCASTS); |
| 154 | } |
| 155 | |
| 156 | private static final Set<String> MICROPHONE_PERMISSIONS = new ArraySet<>(); |
| 157 | static { |
| 158 | MICROPHONE_PERMISSIONS.add(Manifest.permission.RECORD_AUDIO); |
| 159 | } |
| 160 | |
| 161 | private static final Set<String> CAMERA_PERMISSIONS = new ArraySet<>(); |
| 162 | static { |
| 163 | CAMERA_PERMISSIONS.add(Manifest.permission.CAMERA); |
| 164 | } |
| 165 | |
| 166 | private static final Set<String> SENSORS_PERMISSIONS = new ArraySet<>(); |
| 167 | static { |
| 168 | SENSORS_PERMISSIONS.add(Manifest.permission.BODY_SENSORS); |
| 169 | } |
| 170 | |
| 171 | private static final Set<String> STORAGE_PERMISSIONS = new ArraySet<>(); |
| 172 | static { |
| 173 | STORAGE_PERMISSIONS.add(Manifest.permission.READ_EXTERNAL_STORAGE); |
| 174 | STORAGE_PERMISSIONS.add(Manifest.permission.WRITE_EXTERNAL_STORAGE); |
| 175 | } |
| 176 | |
| 177 | private static final int MSG_READ_DEFAULT_PERMISSION_EXCEPTIONS = 1; |
| 178 | |
| 179 | private static final String ACTION_TRACK = "com.android.fitness.TRACK"; |
| 180 | |
| 181 | private final Handler mHandler; |
| 182 | |
| 183 | private PackagesProvider mLocationPackagesProvider; |
| 184 | private PackagesProvider mVoiceInteractionPackagesProvider; |
| 185 | private PackagesProvider mSmsAppPackagesProvider; |
| 186 | private PackagesProvider mDialerAppPackagesProvider; |
| 187 | private PackagesProvider mSimCallManagerPackagesProvider; |
Eric Enslen | 1e423b9 | 2017-12-18 11:30:21 -0800 | [diff] [blame] | 188 | private PackagesProvider mUseOpenWifiAppPackagesProvider; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 189 | private SyncAdapterPackagesProvider mSyncAdapterPackagesProvider; |
| 190 | |
| 191 | private ArrayMap<String, List<DefaultPermissionGrant>> mGrantExceptions; |
| 192 | private final Context mContext; |
| 193 | private final Object mLock = new Object(); |
| 194 | private final PackageManagerInternal mServiceInternal; |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 195 | private final PermissionManagerService mPermissionManager; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 196 | private final DefaultPermissionGrantedCallback mPermissionGrantedCallback; |
| 197 | public interface DefaultPermissionGrantedCallback { |
| 198 | /** Callback when permissions have been granted */ |
| 199 | public void onDefaultRuntimePermissionsGranted(int userId); |
| 200 | } |
| 201 | |
| 202 | public DefaultPermissionGrantPolicy(Context context, Looper looper, |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 203 | @Nullable DefaultPermissionGrantedCallback callback, |
| 204 | @NonNull PermissionManagerService permissionManager) { |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 205 | mContext = context; |
| 206 | mHandler = new Handler(looper) { |
| 207 | @Override |
| 208 | public void handleMessage(Message msg) { |
| 209 | if (msg.what == MSG_READ_DEFAULT_PERMISSION_EXCEPTIONS) { |
| 210 | synchronized (mLock) { |
| 211 | if (mGrantExceptions == null) { |
| 212 | mGrantExceptions = readDefaultPermissionExceptionsLocked(); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | }; |
| 218 | mPermissionGrantedCallback = callback; |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 219 | mPermissionManager = permissionManager; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 220 | mServiceInternal = LocalServices.getService(PackageManagerInternal.class); |
| 221 | } |
| 222 | |
| 223 | public void setLocationPackagesProvider(PackagesProvider provider) { |
| 224 | synchronized (mLock) { |
| 225 | mLocationPackagesProvider = provider; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | public void setVoiceInteractionPackagesProvider(PackagesProvider provider) { |
| 230 | synchronized (mLock) { |
| 231 | mVoiceInteractionPackagesProvider = provider; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | public void setSmsAppPackagesProvider(PackagesProvider provider) { |
| 236 | synchronized (mLock) { |
| 237 | mSmsAppPackagesProvider = provider; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | public void setDialerAppPackagesProvider(PackagesProvider provider) { |
| 242 | synchronized (mLock) { |
| 243 | mDialerAppPackagesProvider = provider; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | public void setSimCallManagerPackagesProvider(PackagesProvider provider) { |
| 248 | synchronized (mLock) { |
| 249 | mSimCallManagerPackagesProvider = provider; |
| 250 | } |
| 251 | } |
| 252 | |
Eric Enslen | 1e423b9 | 2017-12-18 11:30:21 -0800 | [diff] [blame] | 253 | public void setUseOpenWifiAppPackagesProvider(PackagesProvider provider) { |
| 254 | synchronized (mLock) { |
| 255 | mUseOpenWifiAppPackagesProvider = provider; |
| 256 | } |
| 257 | } |
| 258 | |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 259 | public void setSyncAdapterPackagesProvider(SyncAdapterPackagesProvider provider) { |
| 260 | synchronized (mLock) { |
| 261 | mSyncAdapterPackagesProvider = provider; |
| 262 | } |
| 263 | } |
| 264 | |
Todd Kennedy | 42d6160 | 2017-12-12 14:44:19 -0800 | [diff] [blame] | 265 | public void grantDefaultPermissions(int userId) { |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 266 | if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_EMBEDDED, 0)) { |
Todd Kennedy | 42d6160 | 2017-12-12 14:44:19 -0800 | [diff] [blame] | 267 | grantAllRuntimePermissions(userId); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 268 | } else { |
Todd Kennedy | 42d6160 | 2017-12-12 14:44:19 -0800 | [diff] [blame] | 269 | grantPermissionsToSysComponentsAndPrivApps(userId); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 270 | grantDefaultSystemHandlerPermissions(userId); |
| 271 | grantDefaultPermissionExceptions(userId); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | private void grantRuntimePermissionsForPackage(int userId, PackageParser.Package pkg) { |
| 276 | Set<String> permissions = new ArraySet<>(); |
| 277 | for (String permission : pkg.requestedPermissions) { |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 278 | final BasePermission bp = mPermissionManager.getPermission(permission); |
| 279 | if (bp == null) { |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 280 | continue; |
| 281 | } |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 282 | if (bp.isRuntime()) { |
| 283 | permissions.add(permission); |
| 284 | } |
| 285 | } |
| 286 | if (!permissions.isEmpty()) { |
| 287 | grantRuntimePermissions(pkg, permissions, true, userId); |
| 288 | } |
| 289 | } |
| 290 | |
Todd Kennedy | 42d6160 | 2017-12-12 14:44:19 -0800 | [diff] [blame] | 291 | private void grantAllRuntimePermissions(int userId) { |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 292 | Log.i(TAG, "Granting all runtime permissions for user " + userId); |
Todd Kennedy | 42d6160 | 2017-12-12 14:44:19 -0800 | [diff] [blame] | 293 | final PackageList packageList = mServiceInternal.getPackageList(); |
| 294 | for (String packageName : packageList.getPackageNames()) { |
| 295 | final PackageParser.Package pkg = mServiceInternal.getPackage(packageName); |
| 296 | if (pkg == null) { |
| 297 | continue; |
| 298 | } |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 299 | grantRuntimePermissionsForPackage(userId, pkg); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | public void scheduleReadDefaultPermissionExceptions() { |
| 304 | mHandler.sendEmptyMessage(MSG_READ_DEFAULT_PERMISSION_EXCEPTIONS); |
| 305 | } |
| 306 | |
Todd Kennedy | 42d6160 | 2017-12-12 14:44:19 -0800 | [diff] [blame] | 307 | private void grantPermissionsToSysComponentsAndPrivApps(int userId) { |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 308 | Log.i(TAG, "Granting permissions to platform components for user " + userId); |
Todd Kennedy | 42d6160 | 2017-12-12 14:44:19 -0800 | [diff] [blame] | 309 | final PackageList packageList = mServiceInternal.getPackageList(); |
| 310 | for (String packageName : packageList.getPackageNames()) { |
| 311 | final PackageParser.Package pkg = mServiceInternal.getPackage(packageName); |
| 312 | if (pkg == null) { |
| 313 | continue; |
| 314 | } |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 315 | if (!isSysComponentOrPersistentPlatformSignedPrivApp(pkg) |
| 316 | || !doesPackageSupportRuntimePermissions(pkg) |
| 317 | || pkg.requestedPermissions.isEmpty()) { |
| 318 | continue; |
| 319 | } |
| 320 | grantRuntimePermissionsForPackage(userId, pkg); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | private void grantDefaultSystemHandlerPermissions(int userId) { |
| 325 | Log.i(TAG, "Granting permissions to default platform handlers for user " + userId); |
| 326 | |
| 327 | final PackagesProvider locationPackagesProvider; |
| 328 | final PackagesProvider voiceInteractionPackagesProvider; |
| 329 | final PackagesProvider smsAppPackagesProvider; |
| 330 | final PackagesProvider dialerAppPackagesProvider; |
| 331 | final PackagesProvider simCallManagerPackagesProvider; |
Eric Enslen | 1e423b9 | 2017-12-18 11:30:21 -0800 | [diff] [blame] | 332 | final PackagesProvider useOpenWifiAppPackagesProvider; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 333 | final SyncAdapterPackagesProvider syncAdapterPackagesProvider; |
| 334 | |
| 335 | synchronized (mLock) { |
| 336 | locationPackagesProvider = mLocationPackagesProvider; |
| 337 | voiceInteractionPackagesProvider = mVoiceInteractionPackagesProvider; |
| 338 | smsAppPackagesProvider = mSmsAppPackagesProvider; |
| 339 | dialerAppPackagesProvider = mDialerAppPackagesProvider; |
| 340 | simCallManagerPackagesProvider = mSimCallManagerPackagesProvider; |
Eric Enslen | 1e423b9 | 2017-12-18 11:30:21 -0800 | [diff] [blame] | 341 | useOpenWifiAppPackagesProvider = mUseOpenWifiAppPackagesProvider; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 342 | syncAdapterPackagesProvider = mSyncAdapterPackagesProvider; |
| 343 | } |
| 344 | |
| 345 | String[] voiceInteractPackageNames = (voiceInteractionPackagesProvider != null) |
| 346 | ? voiceInteractionPackagesProvider.getPackages(userId) : null; |
| 347 | String[] locationPackageNames = (locationPackagesProvider != null) |
| 348 | ? locationPackagesProvider.getPackages(userId) : null; |
| 349 | String[] smsAppPackageNames = (smsAppPackagesProvider != null) |
| 350 | ? smsAppPackagesProvider.getPackages(userId) : null; |
| 351 | String[] dialerAppPackageNames = (dialerAppPackagesProvider != null) |
| 352 | ? dialerAppPackagesProvider.getPackages(userId) : null; |
| 353 | String[] simCallManagerPackageNames = (simCallManagerPackagesProvider != null) |
| 354 | ? simCallManagerPackagesProvider.getPackages(userId) : null; |
Eric Enslen | 1e423b9 | 2017-12-18 11:30:21 -0800 | [diff] [blame] | 355 | String[] useOpenWifiAppPackageNames = (useOpenWifiAppPackagesProvider != null) |
| 356 | ? useOpenWifiAppPackagesProvider.getPackages(userId) : null; |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 357 | String[] contactsSyncAdapterPackages = (syncAdapterPackagesProvider != null) ? |
| 358 | syncAdapterPackagesProvider.getPackages(ContactsContract.AUTHORITY, userId) : null; |
| 359 | String[] calendarSyncAdapterPackages = (syncAdapterPackagesProvider != null) ? |
| 360 | syncAdapterPackagesProvider.getPackages(CalendarContract.AUTHORITY, userId) : null; |
| 361 | |
| 362 | // Installer |
| 363 | final String installerPackageName = mServiceInternal.getKnownPackageName( |
| 364 | PackageManagerInternal.PACKAGE_INSTALLER, userId); |
| 365 | PackageParser.Package installerPackage = getSystemPackage(installerPackageName); |
| 366 | if (installerPackage != null |
| 367 | && doesPackageSupportRuntimePermissions(installerPackage)) { |
| 368 | grantRuntimePermissions(installerPackage, STORAGE_PERMISSIONS, true, userId); |
| 369 | } |
| 370 | |
| 371 | // Verifier |
| 372 | final String verifierPackageName = mServiceInternal.getKnownPackageName( |
| 373 | PackageManagerInternal.PACKAGE_VERIFIER, userId); |
| 374 | PackageParser.Package verifierPackage = getSystemPackage(verifierPackageName); |
| 375 | if (verifierPackage != null |
| 376 | && doesPackageSupportRuntimePermissions(verifierPackage)) { |
| 377 | grantRuntimePermissions(verifierPackage, STORAGE_PERMISSIONS, true, userId); |
| 378 | grantRuntimePermissions(verifierPackage, PHONE_PERMISSIONS, false, userId); |
| 379 | grantRuntimePermissions(verifierPackage, SMS_PERMISSIONS, false, userId); |
| 380 | } |
| 381 | |
| 382 | // SetupWizard |
| 383 | final String setupWizardPackageName = mServiceInternal.getKnownPackageName( |
| 384 | PackageManagerInternal.PACKAGE_SETUP_WIZARD, userId); |
| 385 | PackageParser.Package setupPackage = getSystemPackage(setupWizardPackageName); |
| 386 | if (setupPackage != null |
| 387 | && doesPackageSupportRuntimePermissions(setupPackage)) { |
| 388 | grantRuntimePermissions(setupPackage, PHONE_PERMISSIONS, userId); |
| 389 | grantRuntimePermissions(setupPackage, CONTACTS_PERMISSIONS, userId); |
| 390 | grantRuntimePermissions(setupPackage, LOCATION_PERMISSIONS, userId); |
| 391 | grantRuntimePermissions(setupPackage, CAMERA_PERMISSIONS, userId); |
| 392 | } |
| 393 | |
| 394 | // Camera |
| 395 | Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
| 396 | PackageParser.Package cameraPackage = getDefaultSystemHandlerActivityPackage( |
| 397 | cameraIntent, userId); |
| 398 | if (cameraPackage != null |
| 399 | && doesPackageSupportRuntimePermissions(cameraPackage)) { |
| 400 | grantRuntimePermissions(cameraPackage, CAMERA_PERMISSIONS, userId); |
| 401 | grantRuntimePermissions(cameraPackage, MICROPHONE_PERMISSIONS, userId); |
| 402 | grantRuntimePermissions(cameraPackage, STORAGE_PERMISSIONS, userId); |
| 403 | } |
| 404 | |
| 405 | // Media provider |
| 406 | PackageParser.Package mediaStorePackage = getDefaultProviderAuthorityPackage( |
| 407 | MediaStore.AUTHORITY, userId); |
| 408 | if (mediaStorePackage != null) { |
| 409 | grantRuntimePermissions(mediaStorePackage, STORAGE_PERMISSIONS, true, userId); |
Jerry Zhang | 27067df | 2017-10-18 11:51:54 -0700 | [diff] [blame] | 410 | grantRuntimePermissions(mediaStorePackage, PHONE_PERMISSIONS, true, userId); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | // Downloads provider |
| 414 | PackageParser.Package downloadsPackage = getDefaultProviderAuthorityPackage( |
| 415 | "downloads", userId); |
| 416 | if (downloadsPackage != null) { |
| 417 | grantRuntimePermissions(downloadsPackage, STORAGE_PERMISSIONS, true, userId); |
| 418 | } |
| 419 | |
| 420 | // Downloads UI |
| 421 | Intent downloadsUiIntent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS); |
| 422 | PackageParser.Package downloadsUiPackage = getDefaultSystemHandlerActivityPackage( |
| 423 | downloadsUiIntent, userId); |
| 424 | if (downloadsUiPackage != null |
| 425 | && doesPackageSupportRuntimePermissions(downloadsUiPackage)) { |
| 426 | grantRuntimePermissions(downloadsUiPackage, STORAGE_PERMISSIONS, true, userId); |
| 427 | } |
| 428 | |
| 429 | // Storage provider |
| 430 | PackageParser.Package storagePackage = getDefaultProviderAuthorityPackage( |
| 431 | "com.android.externalstorage.documents", userId); |
| 432 | if (storagePackage != null) { |
| 433 | grantRuntimePermissions(storagePackage, STORAGE_PERMISSIONS, true, userId); |
| 434 | } |
| 435 | |
Jeff Sharkey | 0095a82 | 2018-02-15 13:06:53 -0700 | [diff] [blame] | 436 | // Container service |
| 437 | PackageParser.Package containerPackage = getSystemPackage( |
| 438 | PackageManagerService.DEFAULT_CONTAINER_PACKAGE); |
| 439 | if (containerPackage != null) { |
| 440 | grantRuntimePermissions(containerPackage, STORAGE_PERMISSIONS, true, userId); |
| 441 | } |
| 442 | |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 443 | // CertInstaller |
| 444 | Intent certInstallerIntent = new Intent(Credentials.INSTALL_ACTION); |
| 445 | PackageParser.Package certInstallerPackage = getDefaultSystemHandlerActivityPackage( |
| 446 | certInstallerIntent, userId); |
| 447 | if (certInstallerPackage != null |
| 448 | && doesPackageSupportRuntimePermissions(certInstallerPackage)) { |
| 449 | grantRuntimePermissions(certInstallerPackage, STORAGE_PERMISSIONS, true, userId); |
| 450 | } |
| 451 | |
| 452 | // Dialer |
| 453 | if (dialerAppPackageNames == null) { |
| 454 | Intent dialerIntent = new Intent(Intent.ACTION_DIAL); |
| 455 | PackageParser.Package dialerPackage = getDefaultSystemHandlerActivityPackage( |
| 456 | dialerIntent, userId); |
| 457 | if (dialerPackage != null) { |
| 458 | grantDefaultPermissionsToDefaultSystemDialerApp(dialerPackage, userId); |
| 459 | } |
| 460 | } else { |
| 461 | for (String dialerAppPackageName : dialerAppPackageNames) { |
| 462 | PackageParser.Package dialerPackage = getSystemPackage(dialerAppPackageName); |
| 463 | if (dialerPackage != null) { |
| 464 | grantDefaultPermissionsToDefaultSystemDialerApp(dialerPackage, userId); |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | // Sim call manager |
| 470 | if (simCallManagerPackageNames != null) { |
| 471 | for (String simCallManagerPackageName : simCallManagerPackageNames) { |
| 472 | PackageParser.Package simCallManagerPackage = |
| 473 | getSystemPackage(simCallManagerPackageName); |
| 474 | if (simCallManagerPackage != null) { |
| 475 | grantDefaultPermissionsToDefaultSimCallManager(simCallManagerPackage, |
| 476 | userId); |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | |
Eric Enslen | 1e423b9 | 2017-12-18 11:30:21 -0800 | [diff] [blame] | 481 | // Use Open Wifi |
| 482 | if (useOpenWifiAppPackageNames != null) { |
| 483 | for (String useOpenWifiPackageName : useOpenWifiAppPackageNames) { |
| 484 | PackageParser.Package useOpenWifiPackage = |
| 485 | getSystemPackage(useOpenWifiPackageName); |
| 486 | if (useOpenWifiPackage != null) { |
| 487 | grantDefaultPermissionsToDefaultSystemUseOpenWifiApp(useOpenWifiPackage, |
| 488 | userId); |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 493 | // SMS |
| 494 | if (smsAppPackageNames == null) { |
| 495 | Intent smsIntent = new Intent(Intent.ACTION_MAIN); |
| 496 | smsIntent.addCategory(Intent.CATEGORY_APP_MESSAGING); |
| 497 | PackageParser.Package smsPackage = getDefaultSystemHandlerActivityPackage( |
| 498 | smsIntent, userId); |
| 499 | if (smsPackage != null) { |
| 500 | grantDefaultPermissionsToDefaultSystemSmsApp(smsPackage, userId); |
| 501 | } |
| 502 | } else { |
| 503 | for (String smsPackageName : smsAppPackageNames) { |
| 504 | PackageParser.Package smsPackage = getSystemPackage(smsPackageName); |
| 505 | if (smsPackage != null) { |
| 506 | grantDefaultPermissionsToDefaultSystemSmsApp(smsPackage, userId); |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | // Cell Broadcast Receiver |
| 512 | Intent cbrIntent = new Intent(Intents.SMS_CB_RECEIVED_ACTION); |
| 513 | PackageParser.Package cbrPackage = |
| 514 | getDefaultSystemHandlerActivityPackage(cbrIntent, userId); |
| 515 | if (cbrPackage != null && doesPackageSupportRuntimePermissions(cbrPackage)) { |
| 516 | grantRuntimePermissions(cbrPackage, SMS_PERMISSIONS, userId); |
| 517 | } |
| 518 | |
| 519 | // Carrier Provisioning Service |
| 520 | Intent carrierProvIntent = new Intent(Intents.SMS_CARRIER_PROVISION_ACTION); |
| 521 | PackageParser.Package carrierProvPackage = |
| 522 | getDefaultSystemHandlerServicePackage(carrierProvIntent, userId); |
| 523 | if (carrierProvPackage != null |
| 524 | && doesPackageSupportRuntimePermissions(carrierProvPackage)) { |
| 525 | grantRuntimePermissions(carrierProvPackage, SMS_PERMISSIONS, false, userId); |
| 526 | } |
| 527 | |
| 528 | // Calendar |
| 529 | Intent calendarIntent = new Intent(Intent.ACTION_MAIN); |
| 530 | calendarIntent.addCategory(Intent.CATEGORY_APP_CALENDAR); |
| 531 | PackageParser.Package calendarPackage = getDefaultSystemHandlerActivityPackage( |
| 532 | calendarIntent, userId); |
| 533 | if (calendarPackage != null |
| 534 | && doesPackageSupportRuntimePermissions(calendarPackage)) { |
| 535 | grantRuntimePermissions(calendarPackage, CALENDAR_PERMISSIONS, userId); |
| 536 | grantRuntimePermissions(calendarPackage, CONTACTS_PERMISSIONS, userId); |
| 537 | } |
| 538 | |
| 539 | // Calendar provider |
| 540 | PackageParser.Package calendarProviderPackage = getDefaultProviderAuthorityPackage( |
| 541 | CalendarContract.AUTHORITY, userId); |
| 542 | if (calendarProviderPackage != null) { |
| 543 | grantRuntimePermissions(calendarProviderPackage, CONTACTS_PERMISSIONS, userId); |
| 544 | grantRuntimePermissions(calendarProviderPackage, CALENDAR_PERMISSIONS, |
| 545 | true, userId); |
| 546 | grantRuntimePermissions(calendarProviderPackage, STORAGE_PERMISSIONS, userId); |
| 547 | } |
| 548 | |
| 549 | // Calendar provider sync adapters |
| 550 | List<PackageParser.Package> calendarSyncAdapters = getHeadlessSyncAdapterPackages( |
| 551 | calendarSyncAdapterPackages, userId); |
| 552 | final int calendarSyncAdapterCount = calendarSyncAdapters.size(); |
| 553 | for (int i = 0; i < calendarSyncAdapterCount; i++) { |
| 554 | PackageParser.Package calendarSyncAdapter = calendarSyncAdapters.get(i); |
| 555 | if (doesPackageSupportRuntimePermissions(calendarSyncAdapter)) { |
| 556 | grantRuntimePermissions(calendarSyncAdapter, CALENDAR_PERMISSIONS, userId); |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | // Contacts |
| 561 | Intent contactsIntent = new Intent(Intent.ACTION_MAIN); |
| 562 | contactsIntent.addCategory(Intent.CATEGORY_APP_CONTACTS); |
| 563 | PackageParser.Package contactsPackage = getDefaultSystemHandlerActivityPackage( |
| 564 | contactsIntent, userId); |
| 565 | if (contactsPackage != null |
| 566 | && doesPackageSupportRuntimePermissions(contactsPackage)) { |
| 567 | grantRuntimePermissions(contactsPackage, CONTACTS_PERMISSIONS, userId); |
| 568 | grantRuntimePermissions(contactsPackage, PHONE_PERMISSIONS, userId); |
| 569 | } |
| 570 | |
| 571 | // Contacts provider sync adapters |
| 572 | List<PackageParser.Package> contactsSyncAdapters = getHeadlessSyncAdapterPackages( |
| 573 | contactsSyncAdapterPackages, userId); |
| 574 | final int contactsSyncAdapterCount = contactsSyncAdapters.size(); |
| 575 | for (int i = 0; i < contactsSyncAdapterCount; i++) { |
| 576 | PackageParser.Package contactsSyncAdapter = contactsSyncAdapters.get(i); |
| 577 | if (doesPackageSupportRuntimePermissions(contactsSyncAdapter)) { |
| 578 | grantRuntimePermissions(contactsSyncAdapter, CONTACTS_PERMISSIONS, userId); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | // Contacts provider |
| 583 | PackageParser.Package contactsProviderPackage = getDefaultProviderAuthorityPackage( |
| 584 | ContactsContract.AUTHORITY, userId); |
| 585 | if (contactsProviderPackage != null) { |
| 586 | grantRuntimePermissions(contactsProviderPackage, CONTACTS_PERMISSIONS, |
| 587 | true, userId); |
| 588 | grantRuntimePermissions(contactsProviderPackage, PHONE_PERMISSIONS, |
| 589 | true, userId); |
| 590 | grantRuntimePermissions(contactsProviderPackage, STORAGE_PERMISSIONS, userId); |
| 591 | } |
| 592 | |
| 593 | // Device provisioning |
| 594 | Intent deviceProvisionIntent = new Intent( |
| 595 | DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE); |
| 596 | PackageParser.Package deviceProvisionPackage = |
| 597 | getDefaultSystemHandlerActivityPackage(deviceProvisionIntent, userId); |
| 598 | if (deviceProvisionPackage != null |
| 599 | && doesPackageSupportRuntimePermissions(deviceProvisionPackage)) { |
| 600 | grantRuntimePermissions(deviceProvisionPackage, CONTACTS_PERMISSIONS, userId); |
| 601 | } |
| 602 | |
| 603 | // Maps |
| 604 | Intent mapsIntent = new Intent(Intent.ACTION_MAIN); |
| 605 | mapsIntent.addCategory(Intent.CATEGORY_APP_MAPS); |
| 606 | PackageParser.Package mapsPackage = getDefaultSystemHandlerActivityPackage( |
| 607 | mapsIntent, userId); |
| 608 | if (mapsPackage != null |
| 609 | && doesPackageSupportRuntimePermissions(mapsPackage)) { |
| 610 | grantRuntimePermissions(mapsPackage, LOCATION_PERMISSIONS, userId); |
| 611 | } |
| 612 | |
| 613 | // Gallery |
| 614 | Intent galleryIntent = new Intent(Intent.ACTION_MAIN); |
| 615 | galleryIntent.addCategory(Intent.CATEGORY_APP_GALLERY); |
| 616 | PackageParser.Package galleryPackage = getDefaultSystemHandlerActivityPackage( |
| 617 | galleryIntent, userId); |
| 618 | if (galleryPackage != null |
| 619 | && doesPackageSupportRuntimePermissions(galleryPackage)) { |
| 620 | grantRuntimePermissions(galleryPackage, STORAGE_PERMISSIONS, userId); |
| 621 | } |
| 622 | |
| 623 | // Email |
| 624 | Intent emailIntent = new Intent(Intent.ACTION_MAIN); |
| 625 | emailIntent.addCategory(Intent.CATEGORY_APP_EMAIL); |
| 626 | PackageParser.Package emailPackage = getDefaultSystemHandlerActivityPackage( |
| 627 | emailIntent, userId); |
| 628 | if (emailPackage != null |
| 629 | && doesPackageSupportRuntimePermissions(emailPackage)) { |
| 630 | grantRuntimePermissions(emailPackage, CONTACTS_PERMISSIONS, userId); |
| 631 | grantRuntimePermissions(emailPackage, CALENDAR_PERMISSIONS, userId); |
| 632 | } |
| 633 | |
| 634 | // Browser |
| 635 | PackageParser.Package browserPackage = null; |
| 636 | String defaultBrowserPackage = mServiceInternal.getKnownPackageName( |
| 637 | PackageManagerInternal.PACKAGE_BROWSER, userId); |
| 638 | if (defaultBrowserPackage != null) { |
| 639 | browserPackage = getPackage(defaultBrowserPackage); |
| 640 | } |
| 641 | if (browserPackage == null) { |
| 642 | Intent browserIntent = new Intent(Intent.ACTION_MAIN); |
| 643 | browserIntent.addCategory(Intent.CATEGORY_APP_BROWSER); |
| 644 | browserPackage = getDefaultSystemHandlerActivityPackage( |
| 645 | browserIntent, userId); |
| 646 | } |
| 647 | if (browserPackage != null |
| 648 | && doesPackageSupportRuntimePermissions(browserPackage)) { |
| 649 | grantRuntimePermissions(browserPackage, LOCATION_PERMISSIONS, userId); |
| 650 | } |
| 651 | |
| 652 | // Voice interaction |
| 653 | if (voiceInteractPackageNames != null) { |
| 654 | for (String voiceInteractPackageName : voiceInteractPackageNames) { |
| 655 | PackageParser.Package voiceInteractPackage = getSystemPackage( |
| 656 | voiceInteractPackageName); |
| 657 | if (voiceInteractPackage != null |
| 658 | && doesPackageSupportRuntimePermissions(voiceInteractPackage)) { |
| 659 | grantRuntimePermissions(voiceInteractPackage, |
| 660 | CONTACTS_PERMISSIONS, userId); |
| 661 | grantRuntimePermissions(voiceInteractPackage, |
| 662 | CALENDAR_PERMISSIONS, userId); |
| 663 | grantRuntimePermissions(voiceInteractPackage, |
| 664 | MICROPHONE_PERMISSIONS, userId); |
| 665 | grantRuntimePermissions(voiceInteractPackage, |
| 666 | PHONE_PERMISSIONS, userId); |
| 667 | grantRuntimePermissions(voiceInteractPackage, |
| 668 | SMS_PERMISSIONS, userId); |
| 669 | grantRuntimePermissions(voiceInteractPackage, |
| 670 | LOCATION_PERMISSIONS, userId); |
| 671 | } |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | if (ActivityManager.isLowRamDeviceStatic()) { |
| 676 | // Allow voice search on low-ram devices |
| 677 | Intent globalSearchIntent = new Intent("android.search.action.GLOBAL_SEARCH"); |
| 678 | PackageParser.Package globalSearchPickerPackage = |
| 679 | getDefaultSystemHandlerActivityPackage(globalSearchIntent, userId); |
| 680 | |
| 681 | if (globalSearchPickerPackage != null |
| 682 | && doesPackageSupportRuntimePermissions(globalSearchPickerPackage)) { |
| 683 | grantRuntimePermissions(globalSearchPickerPackage, |
Ng Zhi An | 202372d | 2017-12-19 11:19:30 -0800 | [diff] [blame] | 684 | MICROPHONE_PERMISSIONS, false, userId); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 685 | grantRuntimePermissions(globalSearchPickerPackage, |
Ng Zhi An | 202372d | 2017-12-19 11:19:30 -0800 | [diff] [blame] | 686 | LOCATION_PERMISSIONS, false, userId); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 687 | } |
| 688 | } |
| 689 | |
| 690 | // Voice recognition |
| 691 | Intent voiceRecoIntent = new Intent("android.speech.RecognitionService"); |
| 692 | voiceRecoIntent.addCategory(Intent.CATEGORY_DEFAULT); |
| 693 | PackageParser.Package voiceRecoPackage = getDefaultSystemHandlerServicePackage( |
| 694 | voiceRecoIntent, userId); |
| 695 | if (voiceRecoPackage != null |
| 696 | && doesPackageSupportRuntimePermissions(voiceRecoPackage)) { |
| 697 | grantRuntimePermissions(voiceRecoPackage, MICROPHONE_PERMISSIONS, userId); |
| 698 | } |
| 699 | |
| 700 | // Location |
| 701 | if (locationPackageNames != null) { |
| 702 | for (String packageName : locationPackageNames) { |
| 703 | PackageParser.Package locationPackage = getSystemPackage(packageName); |
| 704 | if (locationPackage != null |
| 705 | && doesPackageSupportRuntimePermissions(locationPackage)) { |
| 706 | grantRuntimePermissions(locationPackage, CONTACTS_PERMISSIONS, userId); |
| 707 | grantRuntimePermissions(locationPackage, CALENDAR_PERMISSIONS, userId); |
| 708 | grantRuntimePermissions(locationPackage, MICROPHONE_PERMISSIONS, userId); |
| 709 | grantRuntimePermissions(locationPackage, PHONE_PERMISSIONS, userId); |
| 710 | grantRuntimePermissions(locationPackage, SMS_PERMISSIONS, userId); |
| 711 | grantRuntimePermissions(locationPackage, LOCATION_PERMISSIONS, |
| 712 | true, userId); |
| 713 | grantRuntimePermissions(locationPackage, CAMERA_PERMISSIONS, userId); |
| 714 | grantRuntimePermissions(locationPackage, SENSORS_PERMISSIONS, userId); |
| 715 | grantRuntimePermissions(locationPackage, STORAGE_PERMISSIONS, userId); |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | // Music |
| 721 | Intent musicIntent = new Intent(Intent.ACTION_VIEW); |
| 722 | musicIntent.addCategory(Intent.CATEGORY_DEFAULT); |
| 723 | musicIntent.setDataAndType(Uri.fromFile(new File("foo.mp3")), |
| 724 | AUDIO_MIME_TYPE); |
| 725 | PackageParser.Package musicPackage = getDefaultSystemHandlerActivityPackage( |
| 726 | musicIntent, userId); |
| 727 | if (musicPackage != null |
| 728 | && doesPackageSupportRuntimePermissions(musicPackage)) { |
| 729 | grantRuntimePermissions(musicPackage, STORAGE_PERMISSIONS, userId); |
| 730 | } |
| 731 | |
| 732 | // Home |
| 733 | Intent homeIntent = new Intent(Intent.ACTION_MAIN); |
| 734 | homeIntent.addCategory(Intent.CATEGORY_HOME); |
| 735 | homeIntent.addCategory(Intent.CATEGORY_LAUNCHER_APP); |
| 736 | PackageParser.Package homePackage = getDefaultSystemHandlerActivityPackage( |
| 737 | homeIntent, userId); |
| 738 | if (homePackage != null |
| 739 | && doesPackageSupportRuntimePermissions(homePackage)) { |
| 740 | grantRuntimePermissions(homePackage, LOCATION_PERMISSIONS, false, userId); |
| 741 | } |
| 742 | |
| 743 | // Watches |
| 744 | if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH, 0)) { |
| 745 | // Home application on watches |
| 746 | Intent wearHomeIntent = new Intent(Intent.ACTION_MAIN); |
| 747 | wearHomeIntent.addCategory(Intent.CATEGORY_HOME_MAIN); |
| 748 | |
| 749 | PackageParser.Package wearHomePackage = getDefaultSystemHandlerActivityPackage( |
| 750 | wearHomeIntent, userId); |
| 751 | |
| 752 | if (wearHomePackage != null |
| 753 | && doesPackageSupportRuntimePermissions(wearHomePackage)) { |
| 754 | grantRuntimePermissions(wearHomePackage, CONTACTS_PERMISSIONS, false, |
| 755 | userId); |
| 756 | grantRuntimePermissions(wearHomePackage, PHONE_PERMISSIONS, true, userId); |
| 757 | grantRuntimePermissions(wearHomePackage, MICROPHONE_PERMISSIONS, false, |
| 758 | userId); |
| 759 | grantRuntimePermissions(wearHomePackage, LOCATION_PERMISSIONS, false, |
| 760 | userId); |
| 761 | } |
| 762 | |
| 763 | // Fitness tracking on watches |
| 764 | Intent trackIntent = new Intent(ACTION_TRACK); |
| 765 | PackageParser.Package trackPackage = getDefaultSystemHandlerActivityPackage( |
| 766 | trackIntent, userId); |
| 767 | if (trackPackage != null |
| 768 | && doesPackageSupportRuntimePermissions(trackPackage)) { |
| 769 | grantRuntimePermissions(trackPackage, SENSORS_PERMISSIONS, false, userId); |
| 770 | grantRuntimePermissions(trackPackage, LOCATION_PERMISSIONS, false, userId); |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | // Print Spooler |
| 775 | PackageParser.Package printSpoolerPackage = getSystemPackage( |
| 776 | PrintManager.PRINT_SPOOLER_PACKAGE_NAME); |
| 777 | if (printSpoolerPackage != null |
| 778 | && doesPackageSupportRuntimePermissions(printSpoolerPackage)) { |
| 779 | grantRuntimePermissions(printSpoolerPackage, LOCATION_PERMISSIONS, true, userId); |
| 780 | } |
| 781 | |
| 782 | // EmergencyInfo |
| 783 | Intent emergencyInfoIntent = new Intent(TelephonyManager.ACTION_EMERGENCY_ASSISTANCE); |
| 784 | PackageParser.Package emergencyInfoPckg = getDefaultSystemHandlerActivityPackage( |
| 785 | emergencyInfoIntent, userId); |
| 786 | if (emergencyInfoPckg != null |
| 787 | && doesPackageSupportRuntimePermissions(emergencyInfoPckg)) { |
| 788 | grantRuntimePermissions(emergencyInfoPckg, CONTACTS_PERMISSIONS, true, userId); |
| 789 | grantRuntimePermissions(emergencyInfoPckg, PHONE_PERMISSIONS, true, userId); |
| 790 | } |
| 791 | |
| 792 | // NFC Tag viewer |
| 793 | Intent nfcTagIntent = new Intent(Intent.ACTION_VIEW); |
| 794 | nfcTagIntent.setType("vnd.android.cursor.item/ndef_msg"); |
| 795 | PackageParser.Package nfcTagPkg = getDefaultSystemHandlerActivityPackage( |
| 796 | nfcTagIntent, userId); |
| 797 | if (nfcTagPkg != null |
| 798 | && doesPackageSupportRuntimePermissions(nfcTagPkg)) { |
| 799 | grantRuntimePermissions(nfcTagPkg, CONTACTS_PERMISSIONS, false, userId); |
| 800 | grantRuntimePermissions(nfcTagPkg, PHONE_PERMISSIONS, false, userId); |
| 801 | } |
| 802 | |
| 803 | // Storage Manager |
| 804 | Intent storageManagerIntent = new Intent(StorageManager.ACTION_MANAGE_STORAGE); |
| 805 | PackageParser.Package storageManagerPckg = getDefaultSystemHandlerActivityPackage( |
| 806 | storageManagerIntent, userId); |
| 807 | if (storageManagerPckg != null |
| 808 | && doesPackageSupportRuntimePermissions(storageManagerPckg)) { |
| 809 | grantRuntimePermissions(storageManagerPckg, STORAGE_PERMISSIONS, true, userId); |
| 810 | } |
| 811 | |
| 812 | // Companion devices |
| 813 | PackageParser.Package companionDeviceDiscoveryPackage = getSystemPackage( |
| 814 | CompanionDeviceManager.COMPANION_DEVICE_DISCOVERY_PACKAGE_NAME); |
| 815 | if (companionDeviceDiscoveryPackage != null |
| 816 | && doesPackageSupportRuntimePermissions(companionDeviceDiscoveryPackage)) { |
| 817 | grantRuntimePermissions(companionDeviceDiscoveryPackage, |
| 818 | LOCATION_PERMISSIONS, true, userId); |
| 819 | } |
| 820 | |
| 821 | // Ringtone Picker |
| 822 | Intent ringtonePickerIntent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); |
| 823 | PackageParser.Package ringtonePickerPackage = |
| 824 | getDefaultSystemHandlerActivityPackage(ringtonePickerIntent, userId); |
| 825 | if (ringtonePickerPackage != null |
| 826 | && doesPackageSupportRuntimePermissions(ringtonePickerPackage)) { |
| 827 | grantRuntimePermissions(ringtonePickerPackage, |
| 828 | STORAGE_PERMISSIONS, true, userId); |
| 829 | } |
| 830 | |
Abodunrinwa Toki | 2c01b6c | 2018-02-13 17:47:48 +0000 | [diff] [blame] | 831 | // TextClassifier Service |
| 832 | ComponentName textClassifierComponent = |
| 833 | TextClassifierService.getServiceComponentName(mContext); |
| 834 | if (textClassifierComponent != null) { |
| 835 | Intent textClassifierServiceIntent = new Intent(TextClassifierService.SERVICE_INTERFACE) |
| 836 | .setComponent(textClassifierComponent); |
| 837 | PackageParser.Package textClassifierPackage = |
| 838 | getDefaultSystemHandlerServicePackage(textClassifierServiceIntent, userId); |
| 839 | if (textClassifierPackage != null |
| 840 | && doesPackageSupportRuntimePermissions(textClassifierPackage)) { |
| 841 | grantRuntimePermissions(textClassifierPackage, PHONE_PERMISSIONS, true, userId); |
| 842 | grantRuntimePermissions(textClassifierPackage, SMS_PERMISSIONS, true, userId); |
| 843 | grantRuntimePermissions(textClassifierPackage, CALENDAR_PERMISSIONS, true, userId); |
| 844 | grantRuntimePermissions(textClassifierPackage, LOCATION_PERMISSIONS, true, userId); |
| 845 | grantRuntimePermissions(textClassifierPackage, CONTACTS_PERMISSIONS, true, userId); |
| 846 | } |
| 847 | } |
| 848 | |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 849 | if (mPermissionGrantedCallback != null) { |
| 850 | mPermissionGrantedCallback.onDefaultRuntimePermissionsGranted(userId); |
| 851 | } |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | private void grantDefaultPermissionsToDefaultSystemDialerApp( |
| 855 | PackageParser.Package dialerPackage, int userId) { |
| 856 | if (doesPackageSupportRuntimePermissions(dialerPackage)) { |
| 857 | boolean isPhonePermFixed = |
| 858 | mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH, 0); |
| 859 | grantRuntimePermissions( |
| 860 | dialerPackage, PHONE_PERMISSIONS, isPhonePermFixed, userId); |
| 861 | grantRuntimePermissions(dialerPackage, CONTACTS_PERMISSIONS, userId); |
| 862 | grantRuntimePermissions(dialerPackage, SMS_PERMISSIONS, userId); |
| 863 | grantRuntimePermissions(dialerPackage, MICROPHONE_PERMISSIONS, userId); |
| 864 | grantRuntimePermissions(dialerPackage, CAMERA_PERMISSIONS, userId); |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | private void grantDefaultPermissionsToDefaultSystemSmsApp( |
| 869 | PackageParser.Package smsPackage, int userId) { |
| 870 | if (doesPackageSupportRuntimePermissions(smsPackage)) { |
| 871 | grantRuntimePermissions(smsPackage, PHONE_PERMISSIONS, userId); |
| 872 | grantRuntimePermissions(smsPackage, CONTACTS_PERMISSIONS, userId); |
| 873 | grantRuntimePermissions(smsPackage, SMS_PERMISSIONS, userId); |
| 874 | grantRuntimePermissions(smsPackage, STORAGE_PERMISSIONS, userId); |
| 875 | grantRuntimePermissions(smsPackage, MICROPHONE_PERMISSIONS, userId); |
| 876 | grantRuntimePermissions(smsPackage, CAMERA_PERMISSIONS, userId); |
| 877 | } |
| 878 | } |
| 879 | |
Eric Enslen | 1e423b9 | 2017-12-18 11:30:21 -0800 | [diff] [blame] | 880 | private void grantDefaultPermissionsToDefaultSystemUseOpenWifiApp( |
| 881 | PackageParser.Package useOpenWifiPackage, int userId) { |
| 882 | if (doesPackageSupportRuntimePermissions(useOpenWifiPackage)) { |
| 883 | grantRuntimePermissions(useOpenWifiPackage, COARSE_LOCATION_PERMISSIONS, userId); |
| 884 | } |
| 885 | } |
| 886 | |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 887 | public void grantDefaultPermissionsToDefaultSmsApp(String packageName, int userId) { |
| 888 | Log.i(TAG, "Granting permissions to default sms app for user:" + userId); |
| 889 | if (packageName == null) { |
| 890 | return; |
| 891 | } |
| 892 | PackageParser.Package smsPackage = getPackage(packageName); |
| 893 | if (smsPackage != null && doesPackageSupportRuntimePermissions(smsPackage)) { |
| 894 | grantRuntimePermissions(smsPackage, PHONE_PERMISSIONS, false, true, userId); |
| 895 | grantRuntimePermissions(smsPackage, CONTACTS_PERMISSIONS, false, true, userId); |
| 896 | grantRuntimePermissions(smsPackage, SMS_PERMISSIONS, false, true, userId); |
| 897 | grantRuntimePermissions(smsPackage, STORAGE_PERMISSIONS, false, true, userId); |
| 898 | grantRuntimePermissions(smsPackage, MICROPHONE_PERMISSIONS, false, true, userId); |
| 899 | grantRuntimePermissions(smsPackage, CAMERA_PERMISSIONS, false, true, userId); |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | public void grantDefaultPermissionsToDefaultDialerApp(String packageName, int userId) { |
| 904 | Log.i(TAG, "Granting permissions to default dialer app for user:" + userId); |
| 905 | if (packageName == null) { |
| 906 | return; |
| 907 | } |
| 908 | PackageParser.Package dialerPackage = getPackage(packageName); |
| 909 | if (dialerPackage != null |
| 910 | && doesPackageSupportRuntimePermissions(dialerPackage)) { |
| 911 | grantRuntimePermissions(dialerPackage, PHONE_PERMISSIONS, false, true, userId); |
| 912 | grantRuntimePermissions(dialerPackage, CONTACTS_PERMISSIONS, false, true, userId); |
| 913 | grantRuntimePermissions(dialerPackage, SMS_PERMISSIONS, false, true, userId); |
| 914 | grantRuntimePermissions(dialerPackage, MICROPHONE_PERMISSIONS, false, true, userId); |
| 915 | grantRuntimePermissions(dialerPackage, CAMERA_PERMISSIONS, false, true, userId); |
| 916 | } |
| 917 | } |
| 918 | |
Eric Enslen | 1e423b9 | 2017-12-18 11:30:21 -0800 | [diff] [blame] | 919 | public void grantDefaultPermissionsToDefaultUseOpenWifiApp(String packageName, int userId) { |
| 920 | Log.i(TAG, "Granting permissions to default Use Open WiFi app for user:" + userId); |
| 921 | if (packageName == null) { |
| 922 | return; |
| 923 | } |
| 924 | PackageParser.Package useOpenWifiPackage = getPackage(packageName); |
| 925 | if (useOpenWifiPackage != null |
| 926 | && doesPackageSupportRuntimePermissions(useOpenWifiPackage)) { |
| 927 | grantRuntimePermissions( |
| 928 | useOpenWifiPackage, COARSE_LOCATION_PERMISSIONS, false, true, userId); |
| 929 | } |
| 930 | } |
| 931 | |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 932 | private void grantDefaultPermissionsToDefaultSimCallManager( |
| 933 | PackageParser.Package simCallManagerPackage, int userId) { |
| 934 | Log.i(TAG, "Granting permissions to sim call manager for user:" + userId); |
| 935 | if (doesPackageSupportRuntimePermissions(simCallManagerPackage)) { |
| 936 | grantRuntimePermissions(simCallManagerPackage, PHONE_PERMISSIONS, userId); |
| 937 | grantRuntimePermissions(simCallManagerPackage, MICROPHONE_PERMISSIONS, userId); |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | public void grantDefaultPermissionsToDefaultSimCallManager(String packageName, int userId) { |
| 942 | if (packageName == null) { |
| 943 | return; |
| 944 | } |
| 945 | PackageParser.Package simCallManagerPackage = getPackage(packageName); |
| 946 | if (simCallManagerPackage != null) { |
| 947 | grantDefaultPermissionsToDefaultSimCallManager(simCallManagerPackage, userId); |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | public void grantDefaultPermissionsToEnabledCarrierApps(String[] packageNames, int userId) { |
| 952 | Log.i(TAG, "Granting permissions to enabled carrier apps for user:" + userId); |
| 953 | if (packageNames == null) { |
| 954 | return; |
| 955 | } |
| 956 | for (String packageName : packageNames) { |
| 957 | PackageParser.Package carrierPackage = getSystemPackage(packageName); |
| 958 | if (carrierPackage != null |
| 959 | && doesPackageSupportRuntimePermissions(carrierPackage)) { |
| 960 | grantRuntimePermissions(carrierPackage, PHONE_PERMISSIONS, userId); |
| 961 | grantRuntimePermissions(carrierPackage, LOCATION_PERMISSIONS, userId); |
| 962 | grantRuntimePermissions(carrierPackage, SMS_PERMISSIONS, userId); |
| 963 | } |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | public void grantDefaultPermissionsToEnabledImsServices(String[] packageNames, int userId) { |
| 968 | Log.i(TAG, "Granting permissions to enabled ImsServices for user:" + userId); |
| 969 | if (packageNames == null) { |
| 970 | return; |
| 971 | } |
| 972 | for (String packageName : packageNames) { |
| 973 | PackageParser.Package imsServicePackage = getSystemPackage(packageName); |
| 974 | if (imsServicePackage != null |
| 975 | && doesPackageSupportRuntimePermissions(imsServicePackage)) { |
| 976 | grantRuntimePermissions(imsServicePackage, PHONE_PERMISSIONS, userId); |
| 977 | grantRuntimePermissions(imsServicePackage, MICROPHONE_PERMISSIONS, userId); |
| 978 | grantRuntimePermissions(imsServicePackage, LOCATION_PERMISSIONS, userId); |
| 979 | grantRuntimePermissions(imsServicePackage, CAMERA_PERMISSIONS, userId); |
| 980 | } |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | public void grantDefaultPermissionsToDefaultBrowser(String packageName, int userId) { |
| 985 | Log.i(TAG, "Granting permissions to default browser for user:" + userId); |
| 986 | if (packageName == null) { |
| 987 | return; |
| 988 | } |
| 989 | PackageParser.Package browserPackage = getSystemPackage(packageName); |
| 990 | if (browserPackage != null |
| 991 | && doesPackageSupportRuntimePermissions(browserPackage)) { |
| 992 | grantRuntimePermissions(browserPackage, LOCATION_PERMISSIONS, false, false, userId); |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | private PackageParser.Package getDefaultSystemHandlerActivityPackage( |
| 997 | Intent intent, int userId) { |
| 998 | ResolveInfo handler = mServiceInternal.resolveIntent(intent, |
| 999 | intent.resolveType(mContext.getContentResolver()), DEFAULT_FLAGS, userId, false); |
| 1000 | if (handler == null || handler.activityInfo == null) { |
| 1001 | return null; |
| 1002 | } |
| 1003 | if (mServiceInternal.isResolveActivityComponent(handler.activityInfo)) { |
| 1004 | return null; |
| 1005 | } |
| 1006 | return getSystemPackage(handler.activityInfo.packageName); |
| 1007 | } |
| 1008 | |
| 1009 | private PackageParser.Package getDefaultSystemHandlerServicePackage( |
| 1010 | Intent intent, int userId) { |
| 1011 | List<ResolveInfo> handlers = mServiceInternal.queryIntentServices( |
| 1012 | intent, DEFAULT_FLAGS, Binder.getCallingUid(), userId); |
| 1013 | if (handlers == null) { |
| 1014 | return null; |
| 1015 | } |
| 1016 | final int handlerCount = handlers.size(); |
| 1017 | for (int i = 0; i < handlerCount; i++) { |
| 1018 | ResolveInfo handler = handlers.get(i); |
| 1019 | PackageParser.Package handlerPackage = getSystemPackage( |
| 1020 | handler.serviceInfo.packageName); |
| 1021 | if (handlerPackage != null) { |
| 1022 | return handlerPackage; |
| 1023 | } |
| 1024 | } |
| 1025 | return null; |
| 1026 | } |
| 1027 | |
| 1028 | private List<PackageParser.Package> getHeadlessSyncAdapterPackages( |
| 1029 | String[] syncAdapterPackageNames, int userId) { |
| 1030 | List<PackageParser.Package> syncAdapterPackages = new ArrayList<>(); |
| 1031 | |
| 1032 | Intent homeIntent = new Intent(Intent.ACTION_MAIN); |
| 1033 | homeIntent.addCategory(Intent.CATEGORY_LAUNCHER); |
| 1034 | |
| 1035 | for (String syncAdapterPackageName : syncAdapterPackageNames) { |
| 1036 | homeIntent.setPackage(syncAdapterPackageName); |
| 1037 | |
| 1038 | ResolveInfo homeActivity = mServiceInternal.resolveIntent(homeIntent, |
| 1039 | homeIntent.resolveType(mContext.getContentResolver()), DEFAULT_FLAGS, |
| 1040 | userId, false); |
| 1041 | if (homeActivity != null) { |
| 1042 | continue; |
| 1043 | } |
| 1044 | |
| 1045 | PackageParser.Package syncAdapterPackage = getSystemPackage(syncAdapterPackageName); |
| 1046 | if (syncAdapterPackage != null) { |
| 1047 | syncAdapterPackages.add(syncAdapterPackage); |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | return syncAdapterPackages; |
| 1052 | } |
| 1053 | |
| 1054 | private PackageParser.Package getDefaultProviderAuthorityPackage( |
| 1055 | String authority, int userId) { |
| 1056 | ProviderInfo provider = |
| 1057 | mServiceInternal.resolveContentProvider(authority, DEFAULT_FLAGS, userId); |
| 1058 | if (provider != null) { |
| 1059 | return getSystemPackage(provider.packageName); |
| 1060 | } |
| 1061 | return null; |
| 1062 | } |
| 1063 | |
| 1064 | private PackageParser.Package getPackage(String packageName) { |
| 1065 | return mServiceInternal.getPackage(packageName); |
| 1066 | } |
| 1067 | |
| 1068 | private PackageParser.Package getSystemPackage(String packageName) { |
| 1069 | PackageParser.Package pkg = getPackage(packageName); |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 1070 | if (pkg != null && pkg.isSystem()) { |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1071 | return !isSysComponentOrPersistentPlatformSignedPrivApp(pkg) ? pkg : null; |
| 1072 | } |
| 1073 | return null; |
| 1074 | } |
| 1075 | |
| 1076 | private void grantRuntimePermissions(PackageParser.Package pkg, Set<String> permissions, |
| 1077 | int userId) { |
| 1078 | grantRuntimePermissions(pkg, permissions, false, false, userId); |
| 1079 | } |
| 1080 | |
| 1081 | private void grantRuntimePermissions(PackageParser.Package pkg, Set<String> permissions, |
| 1082 | boolean systemFixed, int userId) { |
| 1083 | grantRuntimePermissions(pkg, permissions, systemFixed, false, userId); |
| 1084 | } |
| 1085 | |
| 1086 | private void grantRuntimePermissions(PackageParser.Package pkg, Set<String> permissions, |
Eric Enslen | 1e423b9 | 2017-12-18 11:30:21 -0800 | [diff] [blame] | 1087 | boolean systemFixed, boolean ignoreSystemPackage, int userId) { |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1088 | if (pkg.requestedPermissions.isEmpty()) { |
| 1089 | return; |
| 1090 | } |
| 1091 | |
| 1092 | List<String> requestedPermissions = pkg.requestedPermissions; |
| 1093 | Set<String> grantablePermissions = null; |
| 1094 | |
Eric Enslen | 1e423b9 | 2017-12-18 11:30:21 -0800 | [diff] [blame] | 1095 | // In some cases, like for the Phone or SMS app, we grant permissions regardless |
| 1096 | // of if the version on the system image declares the permission as used since |
| 1097 | // selecting the app as the default for that function the user makes a deliberate |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1098 | // choice to grant this app the permissions needed to function. For all other |
| 1099 | // apps, (default grants on first boot and user creation) we don't grant default |
| 1100 | // permissions if the version on the system image does not declare them. |
Eric Enslen | 1e423b9 | 2017-12-18 11:30:21 -0800 | [diff] [blame] | 1101 | if (!ignoreSystemPackage && pkg.isUpdatedSystemApp()) { |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1102 | final PackageParser.Package disabledPkg = |
| 1103 | mServiceInternal.getDisabledPackage(pkg.packageName); |
| 1104 | if (disabledPkg != null) { |
| 1105 | if (disabledPkg.requestedPermissions.isEmpty()) { |
| 1106 | return; |
| 1107 | } |
| 1108 | if (!requestedPermissions.equals(disabledPkg.requestedPermissions)) { |
| 1109 | grantablePermissions = new ArraySet<>(requestedPermissions); |
| 1110 | requestedPermissions = disabledPkg.requestedPermissions; |
| 1111 | } |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | final int grantablePermissionCount = requestedPermissions.size(); |
| 1116 | for (int i = 0; i < grantablePermissionCount; i++) { |
| 1117 | String permission = requestedPermissions.get(i); |
| 1118 | |
| 1119 | // If there is a disabled system app it may request a permission the updated |
| 1120 | // version ot the data partition doesn't, In this case skip the permission. |
| 1121 | if (grantablePermissions != null && !grantablePermissions.contains(permission)) { |
| 1122 | continue; |
| 1123 | } |
| 1124 | |
| 1125 | if (permissions.contains(permission)) { |
| 1126 | final int flags = mServiceInternal.getPermissionFlagsTEMP( |
| 1127 | permission, pkg.packageName, userId); |
| 1128 | |
| 1129 | // If any flags are set to the permission, then it is either set in |
| 1130 | // its current state by the system or device/profile owner or the user. |
| 1131 | // In all these cases we do not want to clobber the current state. |
| 1132 | // Unless the caller wants to override user choices. The override is |
| 1133 | // to make sure we can grant the needed permission to the default |
| 1134 | // sms and phone apps after the user chooses this in the UI. |
Eric Enslen | 1e423b9 | 2017-12-18 11:30:21 -0800 | [diff] [blame] | 1135 | if (flags == 0 || ignoreSystemPackage) { |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1136 | // Never clobber policy or system. |
| 1137 | final int fixedFlags = PackageManager.FLAG_PERMISSION_SYSTEM_FIXED |
| 1138 | | PackageManager.FLAG_PERMISSION_POLICY_FIXED; |
| 1139 | if ((flags & fixedFlags) != 0) { |
| 1140 | continue; |
| 1141 | } |
| 1142 | |
| 1143 | mServiceInternal.grantRuntimePermission( |
| 1144 | pkg.packageName, permission, userId, false); |
| 1145 | if (DEBUG) { |
| 1146 | Log.i(TAG, "Granted " + (systemFixed ? "fixed " : "not fixed ") |
| 1147 | + permission + " to default handler " + pkg.packageName); |
| 1148 | } |
| 1149 | |
| 1150 | int newFlags = PackageManager.FLAG_PERMISSION_GRANTED_BY_DEFAULT; |
| 1151 | if (systemFixed) { |
| 1152 | newFlags |= PackageManager.FLAG_PERMISSION_SYSTEM_FIXED; |
| 1153 | } |
| 1154 | |
| 1155 | mServiceInternal.updatePermissionFlagsTEMP(permission, pkg.packageName, |
| 1156 | newFlags, newFlags, userId); |
| 1157 | } |
| 1158 | |
| 1159 | // If a component gets a permission for being the default handler A |
| 1160 | // and also default handler B, we grant the weaker grant form. |
| 1161 | if ((flags & PackageManager.FLAG_PERMISSION_GRANTED_BY_DEFAULT) != 0 |
| 1162 | && (flags & PackageManager.FLAG_PERMISSION_SYSTEM_FIXED) != 0 |
| 1163 | && !systemFixed) { |
| 1164 | if (DEBUG) { |
| 1165 | Log.i(TAG, "Granted not fixed " + permission + " to default handler " |
| 1166 | + pkg.packageName); |
| 1167 | } |
| 1168 | mServiceInternal.updatePermissionFlagsTEMP(permission, pkg.packageName, |
| 1169 | PackageManager.FLAG_PERMISSION_SYSTEM_FIXED, 0, userId); |
| 1170 | } |
| 1171 | } |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | private boolean isSysComponentOrPersistentPlatformSignedPrivApp(PackageParser.Package pkg) { |
| 1176 | if (UserHandle.getAppId(pkg.applicationInfo.uid) < FIRST_APPLICATION_UID) { |
| 1177 | return true; |
| 1178 | } |
Todd Kennedy | c29b11a | 2017-10-23 15:55:59 -0700 | [diff] [blame] | 1179 | if (!pkg.isPrivileged()) { |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1180 | return false; |
| 1181 | } |
| 1182 | final PackageParser.Package disabledPkg = |
| 1183 | mServiceInternal.getDisabledPackage(pkg.packageName); |
| 1184 | if (disabledPkg != null) { |
| 1185 | if ((disabledPkg.applicationInfo.flags & ApplicationInfo.FLAG_PERSISTENT) == 0) { |
| 1186 | return false; |
| 1187 | } |
| 1188 | } else if ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_PERSISTENT) == 0) { |
| 1189 | return false; |
| 1190 | } |
| 1191 | final String systemPackageName = mServiceInternal.getKnownPackageName( |
| 1192 | PackageManagerInternal.PACKAGE_SYSTEM, UserHandle.USER_SYSTEM); |
| 1193 | final PackageParser.Package systemPackage = getPackage(systemPackageName); |
Dan Cashman | 1dbe6d0 | 2018-01-23 11:18:28 -0800 | [diff] [blame] | 1194 | return pkg.mSigningDetails.hasAncestorOrSelf(systemPackage.mSigningDetails) |
| 1195 | || systemPackage.mSigningDetails.checkCapability(pkg.mSigningDetails, |
| 1196 | PackageParser.SigningDetails.CertCapabilities.PERMISSION); |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | private void grantDefaultPermissionExceptions(int userId) { |
| 1200 | mHandler.removeMessages(MSG_READ_DEFAULT_PERMISSION_EXCEPTIONS); |
| 1201 | |
| 1202 | synchronized (mLock) { |
| 1203 | // mGrantExceptions is null only before the first read and then |
| 1204 | // it serves as a cache of the default grants that should be |
| 1205 | // performed for every user. If there is an entry then the app |
| 1206 | // is on the system image and supports runtime permissions. |
| 1207 | if (mGrantExceptions == null) { |
| 1208 | mGrantExceptions = readDefaultPermissionExceptionsLocked(); |
| 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | Set<String> permissions = null; |
| 1213 | final int exceptionCount = mGrantExceptions.size(); |
| 1214 | for (int i = 0; i < exceptionCount; i++) { |
| 1215 | String packageName = mGrantExceptions.keyAt(i); |
| 1216 | PackageParser.Package pkg = getSystemPackage(packageName); |
| 1217 | List<DefaultPermissionGrant> permissionGrants = mGrantExceptions.valueAt(i); |
| 1218 | final int permissionGrantCount = permissionGrants.size(); |
| 1219 | for (int j = 0; j < permissionGrantCount; j++) { |
| 1220 | DefaultPermissionGrant permissionGrant = permissionGrants.get(j); |
| 1221 | if (permissions == null) { |
| 1222 | permissions = new ArraySet<>(); |
| 1223 | } else { |
| 1224 | permissions.clear(); |
| 1225 | } |
| 1226 | permissions.add(permissionGrant.name); |
| 1227 | grantRuntimePermissions(pkg, permissions, |
| 1228 | permissionGrant.fixed, userId); |
| 1229 | } |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | private File[] getDefaultPermissionFiles() { |
| 1234 | ArrayList<File> ret = new ArrayList<File>(); |
| 1235 | File dir = new File(Environment.getRootDirectory(), "etc/default-permissions"); |
| 1236 | if (dir.isDirectory() && dir.canRead()) { |
| 1237 | Collections.addAll(ret, dir.listFiles()); |
| 1238 | } |
| 1239 | dir = new File(Environment.getVendorDirectory(), "etc/default-permissions"); |
| 1240 | if (dir.isDirectory() && dir.canRead()) { |
| 1241 | Collections.addAll(ret, dir.listFiles()); |
| 1242 | } |
Jiyong Park | 0989e38 | 2018-03-13 10:26:47 +0900 | [diff] [blame^] | 1243 | dir = new File(Environment.getOdmDirectory(), "etc/default-permissions"); |
| 1244 | if (dir.isDirectory() && dir.canRead()) { |
| 1245 | Collections.addAll(ret, dir.listFiles()); |
| 1246 | } |
Jaekyun Seok | 1713d9e | 2018-01-12 21:47:26 +0900 | [diff] [blame] | 1247 | dir = new File(Environment.getProductDirectory(), "etc/default-permissions"); |
| 1248 | if (dir.isDirectory() && dir.canRead()) { |
| 1249 | Collections.addAll(ret, dir.listFiles()); |
| 1250 | } |
Todd Kennedy | 82b0842 | 2017-09-28 13:32:05 -0700 | [diff] [blame] | 1251 | return ret.isEmpty() ? null : ret.toArray(new File[0]); |
| 1252 | } |
| 1253 | |
| 1254 | private @NonNull ArrayMap<String, List<DefaultPermissionGrant>> |
| 1255 | readDefaultPermissionExceptionsLocked() { |
| 1256 | File[] files = getDefaultPermissionFiles(); |
| 1257 | if (files == null) { |
| 1258 | return new ArrayMap<>(0); |
| 1259 | } |
| 1260 | |
| 1261 | ArrayMap<String, List<DefaultPermissionGrant>> grantExceptions = new ArrayMap<>(); |
| 1262 | |
| 1263 | // Iterate over the files in the directory and scan .xml files |
| 1264 | for (File file : files) { |
| 1265 | if (!file.getPath().endsWith(".xml")) { |
| 1266 | Slog.i(TAG, "Non-xml file " + file |
| 1267 | + " in " + file.getParent() + " directory, ignoring"); |
| 1268 | continue; |
| 1269 | } |
| 1270 | if (!file.canRead()) { |
| 1271 | Slog.w(TAG, "Default permissions file " + file + " cannot be read"); |
| 1272 | continue; |
| 1273 | } |
| 1274 | try ( |
| 1275 | InputStream str = new BufferedInputStream(new FileInputStream(file)) |
| 1276 | ) { |
| 1277 | XmlPullParser parser = Xml.newPullParser(); |
| 1278 | parser.setInput(str, null); |
| 1279 | parse(parser, grantExceptions); |
| 1280 | } catch (XmlPullParserException | IOException e) { |
| 1281 | Slog.w(TAG, "Error reading default permissions file " + file, e); |
| 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | return grantExceptions; |
| 1286 | } |
| 1287 | |
| 1288 | private void parse(XmlPullParser parser, Map<String, List<DefaultPermissionGrant>> |
| 1289 | outGrantExceptions) throws IOException, XmlPullParserException { |
| 1290 | final int outerDepth = parser.getDepth(); |
| 1291 | int type; |
| 1292 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 1293 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 1294 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 1295 | continue; |
| 1296 | } |
| 1297 | if (TAG_EXCEPTIONS.equals(parser.getName())) { |
| 1298 | parseExceptions(parser, outGrantExceptions); |
| 1299 | } else { |
| 1300 | Log.e(TAG, "Unknown tag " + parser.getName()); |
| 1301 | } |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | private void parseExceptions(XmlPullParser parser, Map<String, List<DefaultPermissionGrant>> |
| 1306 | outGrantExceptions) throws IOException, XmlPullParserException { |
| 1307 | final int outerDepth = parser.getDepth(); |
| 1308 | int type; |
| 1309 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 1310 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 1311 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 1312 | continue; |
| 1313 | } |
| 1314 | if (TAG_EXCEPTION.equals(parser.getName())) { |
| 1315 | String packageName = parser.getAttributeValue(null, ATTR_PACKAGE); |
| 1316 | |
| 1317 | List<DefaultPermissionGrant> packageExceptions = |
| 1318 | outGrantExceptions.get(packageName); |
| 1319 | if (packageExceptions == null) { |
| 1320 | // The package must be on the system image |
| 1321 | PackageParser.Package pkg = getSystemPackage(packageName); |
| 1322 | if (pkg == null) { |
| 1323 | Log.w(TAG, "Unknown package:" + packageName); |
| 1324 | XmlUtils.skipCurrentTag(parser); |
| 1325 | continue; |
| 1326 | } |
| 1327 | |
| 1328 | // The package must support runtime permissions |
| 1329 | if (!doesPackageSupportRuntimePermissions(pkg)) { |
| 1330 | Log.w(TAG, "Skipping non supporting runtime permissions package:" |
| 1331 | + packageName); |
| 1332 | XmlUtils.skipCurrentTag(parser); |
| 1333 | continue; |
| 1334 | } |
| 1335 | packageExceptions = new ArrayList<>(); |
| 1336 | outGrantExceptions.put(packageName, packageExceptions); |
| 1337 | } |
| 1338 | |
| 1339 | parsePermission(parser, packageExceptions); |
| 1340 | } else { |
| 1341 | Log.e(TAG, "Unknown tag " + parser.getName() + "under <exceptions>"); |
| 1342 | } |
| 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | private void parsePermission(XmlPullParser parser, List<DefaultPermissionGrant> |
| 1347 | outPackageExceptions) throws IOException, XmlPullParserException { |
| 1348 | final int outerDepth = parser.getDepth(); |
| 1349 | int type; |
| 1350 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 1351 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 1352 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 1353 | continue; |
| 1354 | } |
| 1355 | |
| 1356 | if (TAG_PERMISSION.contains(parser.getName())) { |
| 1357 | String name = parser.getAttributeValue(null, ATTR_NAME); |
| 1358 | if (name == null) { |
| 1359 | Log.w(TAG, "Mandatory name attribute missing for permission tag"); |
| 1360 | XmlUtils.skipCurrentTag(parser); |
| 1361 | continue; |
| 1362 | } |
| 1363 | |
| 1364 | final boolean fixed = XmlUtils.readBooleanAttribute(parser, ATTR_FIXED); |
| 1365 | |
| 1366 | DefaultPermissionGrant exception = new DefaultPermissionGrant(name, fixed); |
| 1367 | outPackageExceptions.add(exception); |
| 1368 | } else { |
| 1369 | Log.e(TAG, "Unknown tag " + parser.getName() + "under <exception>"); |
| 1370 | } |
| 1371 | } |
| 1372 | } |
| 1373 | |
| 1374 | private static boolean doesPackageSupportRuntimePermissions(PackageParser.Package pkg) { |
| 1375 | return pkg.applicationInfo.targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1; |
| 1376 | } |
| 1377 | |
| 1378 | private static final class DefaultPermissionGrant { |
| 1379 | final String name; |
| 1380 | final boolean fixed; |
| 1381 | |
| 1382 | public DefaultPermissionGrant(String name, boolean fixed) { |
| 1383 | this.name = name; |
| 1384 | this.fixed = fixed; |
| 1385 | } |
| 1386 | } |
| 1387 | } |