Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -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; |
| 18 | |
| 19 | import android.Manifest; |
Dianne Hackborn | ca8e6da | 2015-06-24 15:19:17 -0700 | [diff] [blame] | 20 | import android.app.DownloadManager; |
Dianne Hackborn | 388cd2c | 2015-06-26 17:32:36 -0700 | [diff] [blame] | 21 | import android.app.admin.DevicePolicyManager; |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 22 | import android.content.Intent; |
| 23 | import android.content.pm.ApplicationInfo; |
| 24 | import android.content.pm.PackageManager; |
| 25 | import android.content.pm.PackageManagerInternal.PackagesProvider; |
Svetoslav | 0010b70 | 2015-06-30 18:05:26 -0700 | [diff] [blame] | 26 | import android.content.pm.PackageManagerInternal.SyncAdapterPackagesProvider; |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 27 | import android.content.pm.PackageParser; |
Dianne Hackborn | ca8e6da | 2015-06-24 15:19:17 -0700 | [diff] [blame] | 28 | import android.content.pm.ProviderInfo; |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 29 | import android.content.pm.ResolveInfo; |
| 30 | import android.net.Uri; |
| 31 | import android.os.Build; |
| 32 | import android.os.UserHandle; |
Dianne Hackborn | 388cd2c | 2015-06-26 17:32:36 -0700 | [diff] [blame] | 33 | import android.provider.CalendarContract; |
| 34 | import android.provider.ContactsContract; |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 35 | import android.provider.MediaStore; |
| 36 | import android.util.ArraySet; |
| 37 | import android.util.Log; |
| 38 | |
| 39 | import java.io.File; |
| 40 | import java.util.ArrayList; |
| 41 | import java.util.List; |
| 42 | import java.util.Set; |
| 43 | |
| 44 | import static android.os.Process.FIRST_APPLICATION_UID; |
| 45 | |
| 46 | /** |
| 47 | * This class is the policy for granting runtime permissions to |
| 48 | * platform components and default handlers in the system such |
| 49 | * that the device is usable out-of-the-box. For example, the |
| 50 | * shell UID is a part of the system and the Phone app should |
| 51 | * have phone related permission by default. |
| 52 | */ |
| 53 | final class DefaultPermissionGrantPolicy { |
Jeff Davidson | 2a88031 | 2015-06-22 16:54:34 -0700 | [diff] [blame] | 54 | private static final String TAG = "DefaultPermGrantPolicy"; // must be <= 23 chars |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 55 | private static final boolean DEBUG = false; |
| 56 | |
| 57 | private static final String PACKAGE_MIME_TYPE = "application/vnd.android.package-archive"; |
Jeff Sharkey | 7186dd3 | 2015-06-30 17:32:45 -0700 | [diff] [blame] | 58 | private static final String AUDIO_MIME_TYPE = "audio/mpeg"; |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 59 | |
| 60 | private static final Set<String> PHONE_PERMISSIONS = new ArraySet<>(); |
| 61 | static { |
| 62 | PHONE_PERMISSIONS.add(Manifest.permission.READ_PHONE_STATE); |
| 63 | PHONE_PERMISSIONS.add(Manifest.permission.CALL_PHONE); |
Dianne Hackborn | 388cd2c | 2015-06-26 17:32:36 -0700 | [diff] [blame] | 64 | PHONE_PERMISSIONS.add(Manifest.permission.READ_CALL_LOG); |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 65 | PHONE_PERMISSIONS.add(Manifest.permission.WRITE_CALL_LOG); |
| 66 | PHONE_PERMISSIONS.add(Manifest.permission.ADD_VOICEMAIL); |
| 67 | PHONE_PERMISSIONS.add(Manifest.permission.USE_SIP); |
| 68 | PHONE_PERMISSIONS.add(Manifest.permission.PROCESS_OUTGOING_CALLS); |
| 69 | } |
| 70 | |
| 71 | private static final Set<String> CONTACTS_PERMISSIONS = new ArraySet<>(); |
| 72 | static { |
| 73 | CONTACTS_PERMISSIONS.add(Manifest.permission.READ_CONTACTS); |
| 74 | CONTACTS_PERMISSIONS.add(Manifest.permission.WRITE_CONTACTS); |
| 75 | } |
| 76 | |
| 77 | private static final Set<String> LOCATION_PERMISSIONS = new ArraySet<>(); |
| 78 | static { |
| 79 | LOCATION_PERMISSIONS.add(Manifest.permission.ACCESS_FINE_LOCATION); |
| 80 | LOCATION_PERMISSIONS.add(Manifest.permission.ACCESS_COARSE_LOCATION); |
| 81 | } |
| 82 | |
| 83 | private static final Set<String> CALENDAR_PERMISSIONS = new ArraySet<>(); |
| 84 | static { |
| 85 | CALENDAR_PERMISSIONS.add(Manifest.permission.READ_CALENDAR); |
| 86 | CALENDAR_PERMISSIONS.add(Manifest.permission.WRITE_CALENDAR); |
| 87 | } |
| 88 | |
| 89 | private static final Set<String> SMS_PERMISSIONS = new ArraySet<>(); |
| 90 | static { |
| 91 | SMS_PERMISSIONS.add(Manifest.permission.SEND_SMS); |
| 92 | SMS_PERMISSIONS.add(Manifest.permission.RECEIVE_SMS); |
| 93 | SMS_PERMISSIONS.add(Manifest.permission.READ_SMS); |
| 94 | SMS_PERMISSIONS.add(Manifest.permission.RECEIVE_WAP_PUSH); |
| 95 | SMS_PERMISSIONS.add(Manifest.permission.RECEIVE_MMS); |
| 96 | SMS_PERMISSIONS.add(Manifest.permission.READ_CELL_BROADCASTS); |
| 97 | } |
| 98 | |
| 99 | private static final Set<String> MICROPHONE_PERMISSIONS = new ArraySet<>(); |
| 100 | static { |
| 101 | MICROPHONE_PERMISSIONS.add(Manifest.permission.RECORD_AUDIO); |
| 102 | } |
| 103 | |
| 104 | private static final Set<String> CAMERA_PERMISSIONS = new ArraySet<>(); |
| 105 | static { |
| 106 | CAMERA_PERMISSIONS.add(Manifest.permission.CAMERA); |
| 107 | } |
| 108 | |
| 109 | private static final Set<String> SENSORS_PERMISSIONS = new ArraySet<>(); |
| 110 | static { |
| 111 | SENSORS_PERMISSIONS.add(Manifest.permission.BODY_SENSORS); |
| 112 | } |
| 113 | |
| 114 | private static final Set<String> STORAGE_PERMISSIONS = new ArraySet<>(); |
| 115 | static { |
Svet Ganov | 975fa47 | 2015-06-22 20:45:31 -0700 | [diff] [blame] | 116 | STORAGE_PERMISSIONS.add(Manifest.permission.READ_EXTERNAL_STORAGE); |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 117 | STORAGE_PERMISSIONS.add(Manifest.permission.WRITE_EXTERNAL_STORAGE); |
| 118 | } |
| 119 | |
Dianne Hackborn | 388cd2c | 2015-06-26 17:32:36 -0700 | [diff] [blame] | 120 | private static final Set<String> ACCOUNTS_PERMISSIONS = new ArraySet<>(); |
| 121 | static { |
Dianne Hackborn | 8214333 | 2015-06-29 23:45:45 -0700 | [diff] [blame] | 122 | //ACCOUNTS_PERMISSIONS.add(Manifest.permission.GET_ACCOUNTS); |
Dianne Hackborn | 388cd2c | 2015-06-26 17:32:36 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 125 | private static final Set<String> SETTINGS_PERMISSIONS = new ArraySet<>(); |
| 126 | static { |
| 127 | SETTINGS_PERMISSIONS.add(Manifest.permission.WRITE_SETTINGS); |
| 128 | } |
| 129 | |
| 130 | private static final Set<String> INSTALLER_PERMISSIONS = new ArraySet<>(); |
| 131 | static { |
| 132 | INSTALLER_PERMISSIONS.add(Manifest.permission.GRANT_REVOKE_PERMISSIONS); |
| 133 | INSTALLER_PERMISSIONS.add(Manifest.permission.INTERACT_ACROSS_USERS_FULL); |
| 134 | INSTALLER_PERMISSIONS.add(Manifest.permission.CLEAR_APP_USER_DATA); |
| 135 | INSTALLER_PERMISSIONS.add(Manifest.permission.KILL_UID); |
| 136 | } |
| 137 | |
| 138 | private static final Set<String> VERIFIER_PERMISSIONS = new ArraySet<>(); |
| 139 | static { |
| 140 | INSTALLER_PERMISSIONS.add(Manifest.permission.GRANT_REVOKE_PERMISSIONS); |
| 141 | } |
| 142 | |
| 143 | private final PackageManagerService mService; |
| 144 | |
| 145 | private PackagesProvider mImePackagesProvider; |
| 146 | private PackagesProvider mLocationPackagesProvider; |
| 147 | private PackagesProvider mVoiceInteractionPackagesProvider; |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 148 | private PackagesProvider mSmsAppPackagesProvider; |
| 149 | private PackagesProvider mDialerAppPackagesProvider; |
Svetoslav | 0010b70 | 2015-06-30 18:05:26 -0700 | [diff] [blame] | 150 | private SyncAdapterPackagesProvider mSyncAdapterPackagesProvider; |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 151 | |
| 152 | public DefaultPermissionGrantPolicy(PackageManagerService service) { |
| 153 | mService = service; |
| 154 | } |
| 155 | |
| 156 | public void setImePackagesProviderLPr(PackagesProvider provider) { |
| 157 | mImePackagesProvider = provider; |
| 158 | } |
| 159 | |
| 160 | public void setLocationPackagesProviderLPw(PackagesProvider provider) { |
| 161 | mLocationPackagesProvider = provider; |
| 162 | } |
| 163 | |
| 164 | public void setVoiceInteractionPackagesProviderLPw(PackagesProvider provider) { |
| 165 | mVoiceInteractionPackagesProvider = provider; |
| 166 | } |
| 167 | |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 168 | public void setSmsAppPackagesProviderLPw(PackagesProvider provider) { |
| 169 | mSmsAppPackagesProvider = provider; |
| 170 | } |
| 171 | |
| 172 | public void setDialerAppPackagesProviderLPw(PackagesProvider provider) { |
| 173 | mDialerAppPackagesProvider = provider; |
Jeff Davidson | 2a88031 | 2015-06-22 16:54:34 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Svetoslav | 0010b70 | 2015-06-30 18:05:26 -0700 | [diff] [blame] | 176 | public void setSyncAdapterPackagesProviderrLPw(SyncAdapterPackagesProvider provider) { |
| 177 | mSyncAdapterPackagesProvider = provider; |
| 178 | } |
| 179 | |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 180 | public void grantDefaultPermissions(int userId) { |
| 181 | grantPermissionsToSysComponentsAndPrivApps(userId); |
| 182 | grantDefaultSystemHandlerPermissions(userId); |
| 183 | } |
| 184 | |
| 185 | private void grantPermissionsToSysComponentsAndPrivApps(int userId) { |
Jeff Sharkey | 7186dd3 | 2015-06-30 17:32:45 -0700 | [diff] [blame] | 186 | Log.i(TAG, "Granting permissions to platform components for user " + userId); |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 187 | |
| 188 | synchronized (mService.mPackages) { |
| 189 | for (PackageParser.Package pkg : mService.mPackages.values()) { |
| 190 | if (!isSysComponentOrPersistentPrivApp(pkg) |
| 191 | || !doesPackageSupportRuntimePermissions(pkg)) { |
| 192 | continue; |
| 193 | } |
| 194 | final int permissionCount = pkg.requestedPermissions.size(); |
| 195 | for (int i = 0; i < permissionCount; i++) { |
| 196 | String permission = pkg.requestedPermissions.get(i); |
| 197 | BasePermission bp = mService.mSettings.mPermissions.get(permission); |
| 198 | if (bp != null && bp.isRuntime()) { |
| 199 | final int flags = mService.getPermissionFlags(permission, |
| 200 | pkg.packageName, userId); |
| 201 | if ((flags & PackageManager.FLAG_PERMISSION_SYSTEM_FIXED) == 0) { |
| 202 | mService.grantRuntimePermission(pkg.packageName, permission, userId); |
| 203 | mService.updatePermissionFlags(permission, pkg.packageName, |
| 204 | PackageManager.MASK_PERMISSION_FLAGS, |
Svet Ganov | 77ab6a8 | 2015-07-03 12:03:02 -0700 | [diff] [blame] | 205 | PackageManager.FLAG_PERMISSION_SYSTEM_FIXED |
| 206 | | PackageManager.FLAG_PERMISSION_GRANTED_BY_DEFAULT, userId); |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 207 | if (DEBUG) { |
| 208 | Log.i(TAG, "Granted " + permission + " to system component " |
| 209 | + pkg.packageName); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | private void grantDefaultSystemHandlerPermissions(int userId) { |
Jeff Sharkey | 7186dd3 | 2015-06-30 17:32:45 -0700 | [diff] [blame] | 219 | Log.i(TAG, "Granting permissions to default platform handlers for user " + userId); |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 220 | |
| 221 | final PackagesProvider imePackagesProvider; |
| 222 | final PackagesProvider locationPackagesProvider; |
| 223 | final PackagesProvider voiceInteractionPackagesProvider; |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 224 | final PackagesProvider smsAppPackagesProvider; |
| 225 | final PackagesProvider dialerAppPackagesProvider; |
Svetoslav | 0010b70 | 2015-06-30 18:05:26 -0700 | [diff] [blame] | 226 | final SyncAdapterPackagesProvider syncAdapterPackagesProvider; |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 227 | |
| 228 | synchronized (mService.mPackages) { |
| 229 | imePackagesProvider = mImePackagesProvider; |
| 230 | locationPackagesProvider = mLocationPackagesProvider; |
| 231 | voiceInteractionPackagesProvider = mVoiceInteractionPackagesProvider; |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 232 | smsAppPackagesProvider = mSmsAppPackagesProvider; |
| 233 | dialerAppPackagesProvider = mDialerAppPackagesProvider; |
Svetoslav | 0010b70 | 2015-06-30 18:05:26 -0700 | [diff] [blame] | 234 | syncAdapterPackagesProvider = mSyncAdapterPackagesProvider; |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | String[] imePackageNames = (imePackagesProvider != null) |
| 238 | ? imePackagesProvider.getPackages(userId) : null; |
| 239 | String[] voiceInteractPackageNames = (voiceInteractionPackagesProvider != null) |
| 240 | ? voiceInteractionPackagesProvider.getPackages(userId) : null; |
| 241 | String[] locationPackageNames = (locationPackagesProvider != null) |
| 242 | ? locationPackagesProvider.getPackages(userId) : null; |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 243 | String[] smsAppPackageNames = (smsAppPackagesProvider != null) |
| 244 | ? smsAppPackagesProvider.getPackages(userId) : null; |
| 245 | String[] dialerAppPackageNames = (dialerAppPackagesProvider != null) |
| 246 | ? dialerAppPackagesProvider.getPackages(userId) : null; |
Svetoslav | 0010b70 | 2015-06-30 18:05:26 -0700 | [diff] [blame] | 247 | String[] contactsSyncAdapterPackages = (syncAdapterPackagesProvider != null) ? |
| 248 | syncAdapterPackagesProvider.getPackages(ContactsContract.AUTHORITY, userId) : null; |
| 249 | String[] calendarSyncAdapterPackages = (syncAdapterPackagesProvider != null) ? |
| 250 | syncAdapterPackagesProvider.getPackages(CalendarContract.AUTHORITY, userId) : null; |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 251 | |
| 252 | synchronized (mService.mPackages) { |
| 253 | // Installers |
| 254 | Intent installerIntent = new Intent(Intent.ACTION_INSTALL_PACKAGE); |
| 255 | installerIntent.addCategory(Intent.CATEGORY_DEFAULT); |
| 256 | installerIntent.setDataAndType(Uri.fromFile(new File("foo.apk")), |
| 257 | PACKAGE_MIME_TYPE); |
| 258 | List<PackageParser.Package> installerPackages = |
| 259 | getPrivilegedHandlerActivityPackagesLPr(installerIntent, userId); |
| 260 | final int installerCount = installerPackages.size(); |
| 261 | for (int i = 0; i < installerCount; i++) { |
| 262 | PackageParser.Package installPackage = installerPackages.get(i); |
| 263 | grantInstallPermissionsLPw(installPackage, INSTALLER_PERMISSIONS, userId); |
Svet Ganov | 6a166af | 2015-06-30 10:15:44 -0700 | [diff] [blame] | 264 | grantRuntimePermissionsLPw(installPackage, STORAGE_PERMISSIONS, true, userId); |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | // Verifiers |
| 268 | Intent verifierIntent = new Intent(Intent.ACTION_PACKAGE_NEEDS_VERIFICATION); |
| 269 | verifierIntent.setType(PACKAGE_MIME_TYPE); |
| 270 | List<PackageParser.Package> verifierPackages = |
| 271 | getPrivilegedHandlerReceiverPackagesLPr(verifierIntent, userId); |
| 272 | final int verifierCount = verifierPackages.size(); |
| 273 | for (int i = 0; i < verifierCount; i++) { |
| 274 | PackageParser.Package verifierPackage = verifierPackages.get(i); |
| 275 | grantInstallPermissionsLPw(verifierPackage, VERIFIER_PERMISSIONS, userId); |
Dianne Hackborn | ca8e6da | 2015-06-24 15:19:17 -0700 | [diff] [blame] | 276 | grantRuntimePermissionsLPw(verifierPackage, STORAGE_PERMISSIONS, userId); |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | // SetupWizard |
| 280 | Intent setupIntent = new Intent(Intent.ACTION_MAIN); |
| 281 | setupIntent.addCategory(Intent.CATEGORY_HOME); |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 282 | PackageParser.Package setupPackage = getDefaultSystemHandlerActivityPackageLPr( |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 283 | setupIntent, userId); |
| 284 | if (setupPackage != null |
| 285 | && doesPackageSupportRuntimePermissions(setupPackage)) { |
| 286 | grantRuntimePermissionsLPw(setupPackage, PHONE_PERMISSIONS, userId); |
| 287 | grantRuntimePermissionsLPw(setupPackage, CONTACTS_PERMISSIONS, userId); |
| 288 | grantRuntimePermissionsLPw(setupPackage, SETTINGS_PERMISSIONS, userId); |
| 289 | } |
| 290 | |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 291 | // Camera |
| 292 | Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 293 | PackageParser.Package cameraPackage = getDefaultSystemHandlerActivityPackageLPr( |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 294 | cameraIntent, userId); |
| 295 | if (cameraPackage != null |
| 296 | && doesPackageSupportRuntimePermissions(cameraPackage)) { |
| 297 | grantRuntimePermissionsLPw(cameraPackage, CAMERA_PERMISSIONS, userId); |
| 298 | grantRuntimePermissionsLPw(cameraPackage, MICROPHONE_PERMISSIONS, userId); |
Dianne Hackborn | ca8e6da | 2015-06-24 15:19:17 -0700 | [diff] [blame] | 299 | grantRuntimePermissionsLPw(cameraPackage, STORAGE_PERMISSIONS, userId); |
| 300 | } |
| 301 | |
| 302 | // Media provider |
| 303 | PackageParser.Package mediaStorePackage = getDefaultProviderAuthorityPackageLPr( |
| 304 | MediaStore.AUTHORITY, userId); |
| 305 | if (mediaStorePackage != null) { |
Svetoslav | 0010b70 | 2015-06-30 18:05:26 -0700 | [diff] [blame] | 306 | grantRuntimePermissionsLPw(mediaStorePackage, STORAGE_PERMISSIONS, true, userId); |
Dianne Hackborn | ca8e6da | 2015-06-24 15:19:17 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | // Downloads provider |
| 310 | PackageParser.Package downloadsPackage = getDefaultProviderAuthorityPackageLPr( |
| 311 | "downloads", userId); |
| 312 | if (downloadsPackage != null) { |
Svetoslav | 0010b70 | 2015-06-30 18:05:26 -0700 | [diff] [blame] | 313 | grantRuntimePermissionsLPw(downloadsPackage, STORAGE_PERMISSIONS, true, userId); |
Dianne Hackborn | ca8e6da | 2015-06-24 15:19:17 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | // Downloads UI |
| 317 | Intent downloadsUiIntent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS); |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 318 | PackageParser.Package downloadsUiPackage = getDefaultSystemHandlerActivityPackageLPr( |
Dianne Hackborn | ca8e6da | 2015-06-24 15:19:17 -0700 | [diff] [blame] | 319 | downloadsUiIntent, userId); |
| 320 | if (downloadsUiPackage != null |
| 321 | && doesPackageSupportRuntimePermissions(downloadsUiPackage)) { |
Svetoslav | 0010b70 | 2015-06-30 18:05:26 -0700 | [diff] [blame] | 322 | grantRuntimePermissionsLPw(downloadsUiPackage, STORAGE_PERMISSIONS, true, userId); |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Jeff Sharkey | 7186dd3 | 2015-06-30 17:32:45 -0700 | [diff] [blame] | 325 | // Storage provider |
| 326 | PackageParser.Package storagePackage = getDefaultProviderAuthorityPackageLPr( |
| 327 | "com.android.externalstorage.documents", userId); |
| 328 | if (storagePackage != null) { |
| 329 | grantRuntimePermissionsLPw(storagePackage, STORAGE_PERMISSIONS, userId); |
| 330 | } |
| 331 | |
Svetoslav | a5a0d94 | 2015-07-01 19:49:58 -0700 | [diff] [blame] | 332 | // Dialer |
| 333 | if (dialerAppPackageNames == null) { |
| 334 | Intent dialerIntent = new Intent(Intent.ACTION_DIAL); |
| 335 | PackageParser.Package dialerPackage = getDefaultSystemHandlerActivityPackageLPr( |
| 336 | dialerIntent, userId); |
| 337 | if (dialerPackage != null) { |
| 338 | grantDefaultPermissionsToDefaultSystemDialerAppLPr(dialerPackage, userId); |
| 339 | } |
| 340 | } else { |
| 341 | for (String dialerAppPackageName : dialerAppPackageNames) { |
| 342 | PackageParser.Package dialerPackage = getSystemPackageLPr(dialerAppPackageName); |
| 343 | if (dialerPackage != null) { |
| 344 | grantDefaultPermissionsToDefaultSystemDialerAppLPr(dialerPackage, userId); |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 349 | // SMS |
Svetoslav | a5a0d94 | 2015-07-01 19:49:58 -0700 | [diff] [blame] | 350 | if (smsAppPackageNames == null) { |
| 351 | Intent smsIntent = new Intent(Intent.ACTION_MAIN); |
| 352 | smsIntent.addCategory(Intent.CATEGORY_APP_MESSAGING); |
| 353 | PackageParser.Package smsPackage = getDefaultSystemHandlerActivityPackageLPr( |
| 354 | smsIntent, userId); |
| 355 | if (smsPackage != null) { |
| 356 | grantDefaultPermissionsToDefaultSystemSmsAppLPr(smsPackage, userId); |
| 357 | } |
| 358 | } else { |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 359 | for (String smsPackageName : smsAppPackageNames) { |
Svetoslav | a5a0d94 | 2015-07-01 19:49:58 -0700 | [diff] [blame] | 360 | PackageParser.Package smsPackage = getSystemPackageLPr(smsPackageName); |
| 361 | if (smsPackage != null) { |
| 362 | grantDefaultPermissionsToDefaultSystemSmsAppLPr(smsPackage, userId); |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 363 | } |
| 364 | } |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | // Calendar |
| 368 | Intent calendarIntent = new Intent(Intent.ACTION_MAIN); |
| 369 | calendarIntent.addCategory(Intent.CATEGORY_APP_CALENDAR); |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 370 | PackageParser.Package calendarPackage = getDefaultSystemHandlerActivityPackageLPr( |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 371 | calendarIntent, userId); |
| 372 | if (calendarPackage != null |
| 373 | && doesPackageSupportRuntimePermissions(calendarPackage)) { |
| 374 | grantRuntimePermissionsLPw(calendarPackage, CALENDAR_PERMISSIONS, userId); |
| 375 | grantRuntimePermissionsLPw(calendarPackage, CONTACTS_PERMISSIONS, userId); |
Dianne Hackborn | 388cd2c | 2015-06-26 17:32:36 -0700 | [diff] [blame] | 376 | grantRuntimePermissionsLPw(calendarPackage, ACCOUNTS_PERMISSIONS, userId); |
| 377 | } |
| 378 | |
| 379 | // Calendar provider |
| 380 | PackageParser.Package calendarProviderPackage = getDefaultProviderAuthorityPackageLPr( |
| 381 | CalendarContract.AUTHORITY, userId); |
| 382 | if (calendarProviderPackage != null) { |
| 383 | grantRuntimePermissionsLPw(calendarProviderPackage, CONTACTS_PERMISSIONS, userId); |
Svetoslav | 0010b70 | 2015-06-30 18:05:26 -0700 | [diff] [blame] | 384 | grantRuntimePermissionsLPw(calendarProviderPackage, CALENDAR_PERMISSIONS, |
| 385 | true, userId); |
Dianne Hackborn | 388cd2c | 2015-06-26 17:32:36 -0700 | [diff] [blame] | 386 | grantRuntimePermissionsLPw(calendarProviderPackage, ACCOUNTS_PERMISSIONS, userId); |
| 387 | grantRuntimePermissionsLPw(calendarProviderPackage, STORAGE_PERMISSIONS, userId); |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Svetoslav | 0010b70 | 2015-06-30 18:05:26 -0700 | [diff] [blame] | 390 | // Calendar provider sync adapters |
Svetoslav | a5a0d94 | 2015-07-01 19:49:58 -0700 | [diff] [blame] | 391 | List<PackageParser.Package> calendarSyncAdapters = getHeadlessSyncAdapterPackagesLPr( |
| 392 | calendarSyncAdapterPackages, |
Svetoslav | 0010b70 | 2015-06-30 18:05:26 -0700 | [diff] [blame] | 393 | userId); |
| 394 | final int calendarSyncAdapterCount = calendarSyncAdapters.size(); |
| 395 | for (int i = 0; i < calendarSyncAdapterCount; i++) { |
| 396 | PackageParser.Package calendarSyncAdapter = calendarSyncAdapters.get(i); |
| 397 | if (doesPackageSupportRuntimePermissions(calendarSyncAdapter)) { |
| 398 | grantRuntimePermissionsLPw(calendarSyncAdapter, CALENDAR_PERMISSIONS, userId); |
| 399 | } |
| 400 | } |
| 401 | |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 402 | // Contacts |
| 403 | Intent contactsIntent = new Intent(Intent.ACTION_MAIN); |
| 404 | contactsIntent.addCategory(Intent.CATEGORY_APP_CONTACTS); |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 405 | PackageParser.Package contactsPackage = getDefaultSystemHandlerActivityPackageLPr( |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 406 | contactsIntent, userId); |
| 407 | if (contactsPackage != null |
| 408 | && doesPackageSupportRuntimePermissions(contactsPackage)) { |
| 409 | grantRuntimePermissionsLPw(contactsPackage, CONTACTS_PERMISSIONS, userId); |
| 410 | grantRuntimePermissionsLPw(contactsPackage, PHONE_PERMISSIONS, userId); |
Dianne Hackborn | 388cd2c | 2015-06-26 17:32:36 -0700 | [diff] [blame] | 411 | grantRuntimePermissionsLPw(contactsPackage, ACCOUNTS_PERMISSIONS, userId); |
| 412 | } |
| 413 | |
Svetoslav | 0010b70 | 2015-06-30 18:05:26 -0700 | [diff] [blame] | 414 | // Contacts provider sync adapters |
Svetoslav | a5a0d94 | 2015-07-01 19:49:58 -0700 | [diff] [blame] | 415 | List<PackageParser.Package> contactsSyncAdapters = getHeadlessSyncAdapterPackagesLPr( |
| 416 | contactsSyncAdapterPackages, |
Svetoslav | 0010b70 | 2015-06-30 18:05:26 -0700 | [diff] [blame] | 417 | userId); |
| 418 | final int contactsSyncAdapterCount = contactsSyncAdapters.size(); |
| 419 | for (int i = 0; i < contactsSyncAdapterCount; i++) { |
| 420 | PackageParser.Package contactsSyncAdapter = contactsSyncAdapters.get(i); |
| 421 | if (doesPackageSupportRuntimePermissions(contactsSyncAdapter)) { |
| 422 | grantRuntimePermissionsLPw(contactsSyncAdapter, CONTACTS_PERMISSIONS, userId); |
| 423 | } |
| 424 | } |
| 425 | |
Dianne Hackborn | 388cd2c | 2015-06-26 17:32:36 -0700 | [diff] [blame] | 426 | // Contacts provider |
| 427 | PackageParser.Package contactsProviderPackage = getDefaultProviderAuthorityPackageLPr( |
| 428 | ContactsContract.AUTHORITY, userId); |
| 429 | if (contactsProviderPackage != null) { |
Svetoslav | 0010b70 | 2015-06-30 18:05:26 -0700 | [diff] [blame] | 430 | grantRuntimePermissionsLPw(contactsProviderPackage, CONTACTS_PERMISSIONS, |
| 431 | true, userId); |
Makoto Onuki | 7a4082e | 2015-07-06 16:59:36 -0700 | [diff] [blame^] | 432 | grantRuntimePermissionsLPw(contactsProviderPackage, PHONE_PERMISSIONS, |
| 433 | true, userId); |
Dianne Hackborn | 388cd2c | 2015-06-26 17:32:36 -0700 | [diff] [blame] | 434 | grantRuntimePermissionsLPw(contactsProviderPackage, ACCOUNTS_PERMISSIONS, userId); |
| 435 | grantRuntimePermissionsLPw(contactsProviderPackage, STORAGE_PERMISSIONS, userId); |
| 436 | } |
| 437 | |
| 438 | // Device provisioning |
| 439 | Intent deviceProvisionIntent = new Intent( |
| 440 | DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE); |
Svet Ganov | b6e0013 | 2015-06-29 20:19:25 -0700 | [diff] [blame] | 441 | PackageParser.Package deviceProvisionPackage = |
| 442 | getDefaultSystemHandlerActivityPackageLPr(deviceProvisionIntent, userId); |
Dianne Hackborn | 388cd2c | 2015-06-26 17:32:36 -0700 | [diff] [blame] | 443 | if (deviceProvisionPackage != null |
| 444 | && doesPackageSupportRuntimePermissions(deviceProvisionPackage)) { |
Amith Yamasani | a72eac6 | 2015-07-06 09:24:08 -0700 | [diff] [blame] | 445 | grantRuntimePermissionsLPw(deviceProvisionPackage, ACCOUNTS_PERMISSIONS, userId); |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | // Maps |
| 449 | Intent mapsIntent = new Intent(Intent.ACTION_MAIN); |
| 450 | mapsIntent.addCategory(Intent.CATEGORY_APP_MAPS); |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 451 | PackageParser.Package mapsPackage = getDefaultSystemHandlerActivityPackageLPr( |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 452 | mapsIntent, userId); |
| 453 | if (mapsPackage != null |
| 454 | && doesPackageSupportRuntimePermissions(mapsPackage)) { |
| 455 | grantRuntimePermissionsLPw(mapsPackage, LOCATION_PERMISSIONS, userId); |
| 456 | } |
| 457 | |
Svet Ganov | 200d494 | 2015-07-01 20:46:02 -0700 | [diff] [blame] | 458 | // Gallery |
| 459 | Intent galleryIntent = new Intent(Intent.ACTION_MAIN); |
| 460 | galleryIntent.addCategory(Intent.CATEGORY_APP_GALLERY); |
| 461 | PackageParser.Package galleryPackage = getDefaultSystemHandlerActivityPackageLPr( |
| 462 | galleryIntent, userId); |
| 463 | if (galleryPackage != null |
| 464 | && doesPackageSupportRuntimePermissions(galleryPackage)) { |
| 465 | grantRuntimePermissionsLPw(galleryPackage, STORAGE_PERMISSIONS, userId); |
| 466 | } |
| 467 | |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 468 | // Email |
| 469 | Intent emailIntent = new Intent(Intent.ACTION_MAIN); |
| 470 | emailIntent.addCategory(Intent.CATEGORY_APP_EMAIL); |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 471 | PackageParser.Package emailPackage = getDefaultSystemHandlerActivityPackageLPr( |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 472 | emailIntent, userId); |
| 473 | if (emailPackage != null |
| 474 | && doesPackageSupportRuntimePermissions(emailPackage)) { |
| 475 | grantRuntimePermissionsLPw(emailPackage, CONTACTS_PERMISSIONS, userId); |
| 476 | } |
| 477 | |
| 478 | // Browser |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 479 | PackageParser.Package browserPackage = null; |
| 480 | String defaultBrowserPackage = mService.getDefaultBrowserPackageName(userId); |
| 481 | if (defaultBrowserPackage != null) { |
| 482 | browserPackage = getPackageLPr(defaultBrowserPackage); |
| 483 | } |
| 484 | if (browserPackage == null) { |
| 485 | Intent browserIntent = new Intent(Intent.ACTION_MAIN); |
| 486 | browserIntent.addCategory(Intent.CATEGORY_APP_BROWSER); |
| 487 | browserPackage = getDefaultSystemHandlerActivityPackageLPr( |
| 488 | browserIntent, userId); |
| 489 | } |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 490 | if (browserPackage != null |
| 491 | && doesPackageSupportRuntimePermissions(browserPackage)) { |
| 492 | grantRuntimePermissionsLPw(browserPackage, LOCATION_PERMISSIONS, userId); |
| 493 | } |
| 494 | |
| 495 | // IME |
| 496 | if (imePackageNames != null) { |
| 497 | for (String imePackageName : imePackageNames) { |
| 498 | PackageParser.Package imePackage = getSystemPackageLPr(imePackageName); |
| 499 | if (imePackage != null |
| 500 | && doesPackageSupportRuntimePermissions(imePackage)) { |
| 501 | grantRuntimePermissionsLPw(imePackage, CONTACTS_PERMISSIONS, userId); |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | // Voice interaction |
| 507 | if (voiceInteractPackageNames != null) { |
| 508 | for (String voiceInteractPackageName : voiceInteractPackageNames) { |
| 509 | PackageParser.Package voiceInteractPackage = getSystemPackageLPr( |
| 510 | voiceInteractPackageName); |
| 511 | if (voiceInteractPackage != null |
| 512 | && doesPackageSupportRuntimePermissions(voiceInteractPackage)) { |
| 513 | grantRuntimePermissionsLPw(voiceInteractPackage, |
| 514 | CONTACTS_PERMISSIONS, userId); |
| 515 | grantRuntimePermissionsLPw(voiceInteractPackage, |
| 516 | CALENDAR_PERMISSIONS, userId); |
| 517 | grantRuntimePermissionsLPw(voiceInteractPackage, |
| 518 | MICROPHONE_PERMISSIONS, userId); |
| 519 | grantRuntimePermissionsLPw(voiceInteractPackage, |
| 520 | PHONE_PERMISSIONS, userId); |
| 521 | grantRuntimePermissionsLPw(voiceInteractPackage, |
| 522 | SMS_PERMISSIONS, userId); |
| 523 | grantRuntimePermissionsLPw(voiceInteractPackage, |
| 524 | LOCATION_PERMISSIONS, userId); |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | // Location |
| 530 | if (locationPackageNames != null) { |
| 531 | for (String packageName : locationPackageNames) { |
| 532 | PackageParser.Package locationPackage = getSystemPackageLPr(packageName); |
| 533 | if (locationPackage != null |
| 534 | && doesPackageSupportRuntimePermissions(locationPackage)) { |
| 535 | grantRuntimePermissionsLPw(locationPackage, CONTACTS_PERMISSIONS, userId); |
| 536 | grantRuntimePermissionsLPw(locationPackage, CALENDAR_PERMISSIONS, userId); |
| 537 | grantRuntimePermissionsLPw(locationPackage, MICROPHONE_PERMISSIONS, userId); |
| 538 | grantRuntimePermissionsLPw(locationPackage, PHONE_PERMISSIONS, userId); |
| 539 | grantRuntimePermissionsLPw(locationPackage, SMS_PERMISSIONS, userId); |
Svetoslav | 0010b70 | 2015-06-30 18:05:26 -0700 | [diff] [blame] | 540 | grantRuntimePermissionsLPw(locationPackage, LOCATION_PERMISSIONS, |
| 541 | true, userId); |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 542 | grantRuntimePermissionsLPw(locationPackage, CAMERA_PERMISSIONS, userId); |
| 543 | grantRuntimePermissionsLPw(locationPackage, SENSORS_PERMISSIONS, userId); |
| 544 | grantRuntimePermissionsLPw(locationPackage, STORAGE_PERMISSIONS, userId); |
| 545 | } |
| 546 | } |
| 547 | } |
Jeff Davidson | 2a88031 | 2015-06-22 16:54:34 -0700 | [diff] [blame] | 548 | |
Jeff Sharkey | 7186dd3 | 2015-06-30 17:32:45 -0700 | [diff] [blame] | 549 | // Music |
| 550 | Intent musicIntent = new Intent(Intent.ACTION_VIEW); |
| 551 | musicIntent.addCategory(Intent.CATEGORY_DEFAULT); |
| 552 | musicIntent.setDataAndType(Uri.fromFile(new File("foo.mp3")), |
| 553 | AUDIO_MIME_TYPE); |
| 554 | PackageParser.Package musicPackage = getDefaultSystemHandlerActivityPackageLPr( |
| 555 | musicIntent, userId); |
| 556 | if (musicPackage != null |
| 557 | && doesPackageSupportRuntimePermissions(musicPackage)) { |
| 558 | grantRuntimePermissionsLPw(musicPackage, STORAGE_PERMISSIONS, userId); |
| 559 | } |
| 560 | |
Svet Ganov | ba3ba81 | 2015-06-26 10:54:06 -0700 | [diff] [blame] | 561 | mService.mSettings.onDefaultRuntimePermissionsGrantedLPr(userId); |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | |
Svetoslav | a5a0d94 | 2015-07-01 19:49:58 -0700 | [diff] [blame] | 565 | private void grantDefaultPermissionsToDefaultSystemDialerAppLPr( |
| 566 | PackageParser.Package dialerPackage, int userId) { |
| 567 | if (doesPackageSupportRuntimePermissions(dialerPackage)) { |
| 568 | grantRuntimePermissionsLPw(dialerPackage, PHONE_PERMISSIONS, userId); |
| 569 | grantRuntimePermissionsLPw(dialerPackage, CONTACTS_PERMISSIONS, userId); |
| 570 | grantRuntimePermissionsLPw(dialerPackage, SMS_PERMISSIONS, userId); |
| 571 | grantRuntimePermissionsLPw(dialerPackage, MICROPHONE_PERMISSIONS, userId); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | |
| 576 | private void grantDefaultPermissionsToDefaultSystemSmsAppLPr( |
| 577 | PackageParser.Package smsPackage, int userId) { |
| 578 | if (doesPackageSupportRuntimePermissions(smsPackage)) { |
| 579 | grantRuntimePermissionsLPw(smsPackage, PHONE_PERMISSIONS, userId); |
| 580 | grantRuntimePermissionsLPw(smsPackage, CONTACTS_PERMISSIONS, userId); |
| 581 | grantRuntimePermissionsLPw(smsPackage, SMS_PERMISSIONS, userId); |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 586 | public void grantDefaultPermissionsToDefaultSmsAppLPr(String packageName, int userId) { |
| 587 | Log.i(TAG, "Granting permissions to default sms app for user:" + userId); |
| 588 | if (packageName == null) { |
| 589 | return; |
| 590 | } |
| 591 | PackageParser.Package smsPackage = getPackageLPr(packageName); |
| 592 | if (smsPackage != null && doesPackageSupportRuntimePermissions(smsPackage)) { |
| 593 | grantRuntimePermissionsLPw(smsPackage, PHONE_PERMISSIONS, userId); |
| 594 | grantRuntimePermissionsLPw(smsPackage, CONTACTS_PERMISSIONS, userId); |
| 595 | grantRuntimePermissionsLPw(smsPackage, SMS_PERMISSIONS, userId); |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | public void grantDefaultPermissionsToDefaultDialerAppLPr(String packageName, int userId) { |
| 600 | Log.i(TAG, "Granting permissions to default dialer app for user:" + userId); |
| 601 | if (packageName == null) { |
| 602 | return; |
| 603 | } |
| 604 | PackageParser.Package dialerPackage = getPackageLPr(packageName); |
| 605 | if (dialerPackage != null |
| 606 | && doesPackageSupportRuntimePermissions(dialerPackage)) { |
| 607 | grantRuntimePermissionsLPw(dialerPackage, PHONE_PERMISSIONS, userId); |
| 608 | grantRuntimePermissionsLPw(dialerPackage, CONTACTS_PERMISSIONS, userId); |
| 609 | grantRuntimePermissionsLPw(dialerPackage, SMS_PERMISSIONS, userId); |
| 610 | grantRuntimePermissionsLPw(dialerPackage, MICROPHONE_PERMISSIONS, userId); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | public void grantDefaultPermissionsToEnabledCarrierAppsLPr(String[] packageNames, int userId) { |
| 615 | Log.i(TAG, "Granting permissions to enabled carrier apps for user:" + userId); |
| 616 | if (packageNames == null) { |
| 617 | return; |
| 618 | } |
| 619 | for (String packageName : packageNames) { |
| 620 | PackageParser.Package carrierPackage = getSystemPackageLPr(packageName); |
| 621 | if (carrierPackage != null |
| 622 | && doesPackageSupportRuntimePermissions(carrierPackage)) { |
| 623 | grantRuntimePermissionsLPw(carrierPackage, PHONE_PERMISSIONS, userId); |
| 624 | grantRuntimePermissionsLPw(carrierPackage, LOCATION_PERMISSIONS, userId); |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | public void grantDefaultPermissionsToDefaultBrowserLPr(String packageName, int userId) { |
| 630 | Log.i(TAG, "Granting permissions to default browser for user:" + userId); |
| 631 | if (packageName == null) { |
| 632 | return; |
| 633 | } |
| 634 | PackageParser.Package browserPackage = getSystemPackageLPr(packageName); |
| 635 | if (browserPackage != null |
| 636 | && doesPackageSupportRuntimePermissions(browserPackage)) { |
| 637 | grantRuntimePermissionsLPw(browserPackage, LOCATION_PERMISSIONS, userId); |
| 638 | } |
| 639 | } |
| 640 | |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 641 | private List<PackageParser.Package> getPrivilegedHandlerReceiverPackagesLPr( |
| 642 | Intent intent, int userId) { |
| 643 | List<ResolveInfo> handlers = mService.queryIntentReceivers( |
| 644 | intent, intent.resolveTypeIfNeeded(mService.mContext.getContentResolver()), |
| 645 | 0, userId); |
| 646 | return getPrivilegedPackages(handlers); |
| 647 | } |
| 648 | |
| 649 | private List<PackageParser.Package> getPrivilegedHandlerActivityPackagesLPr( |
| 650 | Intent intent, int userId) { |
| 651 | List<ResolveInfo> handlers = mService.queryIntentActivities( |
| 652 | intent, intent.resolveTypeIfNeeded(mService.mContext.getContentResolver()), |
| 653 | 0, userId); |
| 654 | return getPrivilegedPackages(handlers); |
| 655 | } |
| 656 | |
| 657 | private List<PackageParser.Package> getPrivilegedPackages(List<ResolveInfo> resolveInfos) { |
| 658 | List<PackageParser.Package> handlerPackages = new ArrayList<>(); |
| 659 | final int handlerCount = resolveInfos.size(); |
| 660 | for (int i = 0; i < handlerCount; i++) { |
| 661 | ResolveInfo handler = resolveInfos.get(i); |
| 662 | PackageParser.Package handlerPackage = getPrivilegedPackageLPr( |
| 663 | handler.activityInfo.packageName); |
| 664 | if (handlerPackage != null) { |
| 665 | handlerPackages.add(handlerPackage); |
| 666 | } |
| 667 | } |
| 668 | return handlerPackages; |
| 669 | } |
| 670 | |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 671 | private PackageParser.Package getDefaultSystemHandlerActivityPackageLPr( |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 672 | Intent intent, int userId) { |
| 673 | List<ResolveInfo> handlers = mService.queryIntentActivities(intent, null, 0, userId); |
| 674 | final int handlerCount = handlers.size(); |
| 675 | for (int i = 0; i < handlerCount; i++) { |
| 676 | ResolveInfo handler = handlers.get(i); |
| 677 | // TODO: This is a temporary hack to figure out the setup app. |
| 678 | PackageParser.Package handlerPackage = getSystemPackageLPr( |
| 679 | handler.activityInfo.packageName); |
| 680 | if (handlerPackage != null) { |
| 681 | return handlerPackage; |
| 682 | } |
| 683 | } |
| 684 | return null; |
| 685 | } |
| 686 | |
Svetoslav | 0010b70 | 2015-06-30 18:05:26 -0700 | [diff] [blame] | 687 | private List<PackageParser.Package> getHeadlessSyncAdapterPackagesLPr( |
| 688 | String[] syncAdapterPackageNames, int userId) { |
| 689 | List<PackageParser.Package> syncAdapterPackages = new ArrayList<>(); |
| 690 | |
| 691 | Intent homeIntent = new Intent(Intent.ACTION_MAIN); |
| 692 | homeIntent.addCategory(Intent.CATEGORY_HOME); |
| 693 | |
| 694 | for (String syncAdapterPackageName : syncAdapterPackageNames) { |
| 695 | homeIntent.setPackage(syncAdapterPackageName); |
| 696 | |
| 697 | List<ResolveInfo> homeActivities = mService.queryIntentActivities(homeIntent, |
| 698 | homeIntent.resolveType(mService.mContext.getContentResolver()), 0, userId); |
| 699 | if (!homeActivities.isEmpty()) { |
| 700 | continue; |
| 701 | } |
| 702 | |
| 703 | PackageParser.Package syncAdapterPackage = getSystemPackageLPr(syncAdapterPackageName); |
| 704 | if (syncAdapterPackage != null) { |
| 705 | syncAdapterPackages.add(syncAdapterPackage); |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | return syncAdapterPackages; |
| 710 | } |
| 711 | |
Dianne Hackborn | ca8e6da | 2015-06-24 15:19:17 -0700 | [diff] [blame] | 712 | private PackageParser.Package getDefaultProviderAuthorityPackageLPr( |
| 713 | String authority, int userId) { |
| 714 | ProviderInfo provider = mService.resolveContentProvider(authority, 0, userId); |
| 715 | if (provider != null) { |
| 716 | return getSystemPackageLPr(provider.packageName); |
| 717 | } |
| 718 | return null; |
| 719 | } |
| 720 | |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 721 | private PackageParser.Package getPackageLPr(String packageName) { |
| 722 | return mService.mPackages.get(packageName); |
| 723 | } |
| 724 | |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 725 | private PackageParser.Package getSystemPackageLPr(String packageName) { |
Svetoslav | cdfd230 | 2015-06-25 19:07:31 -0700 | [diff] [blame] | 726 | PackageParser.Package pkg = getPackageLPr(packageName); |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 727 | if (pkg != null && pkg.isSystemApp()) { |
| 728 | return !isSysComponentOrPersistentPrivApp(pkg) ? pkg : null; |
| 729 | } |
| 730 | return null; |
| 731 | } |
| 732 | |
| 733 | private PackageParser.Package getPrivilegedPackageLPr(String packageName) { |
| 734 | PackageParser.Package pkg = mService.mPackages.get(packageName); |
| 735 | if (pkg != null && pkg.applicationInfo.isPrivilegedApp()) { |
| 736 | return !isSysComponentOrPersistentPrivApp(pkg) ? pkg : null; |
| 737 | } |
| 738 | return null; |
| 739 | } |
| 740 | |
| 741 | private void grantRuntimePermissionsLPw(PackageParser.Package pkg, Set<String> permissions, |
| 742 | int userId) { |
Svet Ganov | 6a166af | 2015-06-30 10:15:44 -0700 | [diff] [blame] | 743 | grantRuntimePermissionsLPw(pkg, permissions, false, userId); |
| 744 | |
| 745 | } |
| 746 | |
| 747 | private void grantRuntimePermissionsLPw(PackageParser.Package pkg, Set<String> permissions, |
| 748 | boolean systemFixed, int userId) { |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 749 | List<String> requestedPermissions = pkg.requestedPermissions; |
| 750 | |
| 751 | if (pkg.isUpdatedSystemApp()) { |
| 752 | PackageSetting sysPs = mService.mSettings.getDisabledSystemPkgLPr(pkg.packageName); |
| 753 | if (sysPs != null) { |
| 754 | requestedPermissions = sysPs.pkg.requestedPermissions; |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | final int permissionCount = requestedPermissions.size(); |
| 759 | for (int i = 0; i < permissionCount; i++) { |
| 760 | String permission = requestedPermissions.get(i); |
| 761 | if (permissions.contains(permission)) { |
| 762 | final int flags = mService.getPermissionFlags(permission, pkg.packageName, userId); |
| 763 | |
| 764 | // If any flags are set to the permission, then it is either set in |
| 765 | // its current state by the system or device/profile owner or the user. |
| 766 | // In all these cases we do not want to clobber the current state. |
| 767 | if (flags == 0) { |
| 768 | mService.grantRuntimePermission(pkg.packageName, permission, userId); |
| 769 | if (DEBUG) { |
| 770 | Log.i(TAG, "Granted " + permission + " to default handler " |
| 771 | + pkg.packageName); |
| 772 | } |
Svet Ganov | 6a166af | 2015-06-30 10:15:44 -0700 | [diff] [blame] | 773 | |
Svet Ganov | 77ab6a8 | 2015-07-03 12:03:02 -0700 | [diff] [blame] | 774 | int newFlags = PackageManager.FLAG_PERMISSION_GRANTED_BY_DEFAULT; |
Svet Ganov | 6a166af | 2015-06-30 10:15:44 -0700 | [diff] [blame] | 775 | if (systemFixed) { |
Svet Ganov | 77ab6a8 | 2015-07-03 12:03:02 -0700 | [diff] [blame] | 776 | newFlags |= PackageManager.FLAG_PERMISSION_SYSTEM_FIXED; |
Svet Ganov | 6a166af | 2015-06-30 10:15:44 -0700 | [diff] [blame] | 777 | } |
Svet Ganov | 77ab6a8 | 2015-07-03 12:03:02 -0700 | [diff] [blame] | 778 | |
| 779 | mService.updatePermissionFlags(permission, pkg.packageName, |
| 780 | newFlags, newFlags, userId); |
Svet Ganov | adc1cf4 | 2015-06-15 16:36:24 -0700 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | private void grantInstallPermissionsLPw(PackageParser.Package pkg, Set<String> permissions, |
| 787 | int userId) { |
| 788 | List<String> requestedPermissions = pkg.requestedPermissions; |
| 789 | |
| 790 | if (pkg.isUpdatedSystemApp()) { |
| 791 | PackageSetting sysPs = mService.mSettings.getDisabledSystemPkgLPr(pkg.packageName); |
| 792 | if (sysPs != null) { |
| 793 | requestedPermissions = sysPs.pkg.requestedPermissions; |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | final int permissionCount = requestedPermissions.size(); |
| 798 | for (int i = 0; i < permissionCount; i++) { |
| 799 | String permission = requestedPermissions.get(i); |
| 800 | if (permissions.contains(permission)) { |
| 801 | final int flags = mService.getPermissionFlags(permission, pkg.packageName, userId); |
| 802 | |
| 803 | // If any flags are set to the permission, then it is either set in |
| 804 | // its current state by the system or device/profile owner or the user. |
| 805 | // In all these cases we do not want to clobber the current state. |
| 806 | if (flags == 0) { |
| 807 | mService.grantInstallPermissionLPw(permission, pkg); |
| 808 | if (DEBUG) { |
| 809 | Log.i(TAG, "Granted install " + permission + " to " + pkg.packageName); |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | private static boolean isSysComponentOrPersistentPrivApp(PackageParser.Package pkg) { |
| 817 | return UserHandle.getAppId(pkg.applicationInfo.uid) < FIRST_APPLICATION_UID |
| 818 | || ((pkg.applicationInfo.privateFlags |
| 819 | & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0 |
| 820 | && (pkg.applicationInfo.flags & ApplicationInfo.FLAG_PERSISTENT) != 0); |
| 821 | } |
| 822 | |
| 823 | private static boolean doesPackageSupportRuntimePermissions(PackageParser.Package pkg) { |
| 824 | return pkg.applicationInfo.targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1; |
| 825 | } |
| 826 | } |