1 /* 2 * Copyright (C) 2016 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.annotation.UserIdInt; 20 import android.os.UserHandle; 21 import android.util.SparseArray; 22 23 /** 24 * Manages package names that need special protection. 25 * 26 * TODO: This class should persist the information by itself, and also keeps track of device admin 27 * packages for all users. Then PMS.isPackageDeviceAdmin() should use it instead of talking 28 * to DPMS. 29 */ 30 public class ProtectedPackages { 31 @UserIdInt 32 private int mDeviceOwnerUserId; 33 34 private String mDeviceOwnerPackage; 35 36 private SparseArray<String> mProfileOwnerPackages; 37 38 private final Object mLock = new Object(); 39 40 /** 41 * Sets the device/profile owner information. 42 */ setDeviceAndProfileOwnerPackages( int deviceOwnerUserId, String deviceOwnerPackage, SparseArray<String> profileOwnerPackages)43 public void setDeviceAndProfileOwnerPackages( 44 int deviceOwnerUserId, String deviceOwnerPackage, 45 SparseArray<String> profileOwnerPackages) { 46 synchronized (mLock) { 47 mDeviceOwnerUserId = deviceOwnerUserId; 48 mDeviceOwnerPackage = 49 (deviceOwnerUserId == UserHandle.USER_NULL) ? null : deviceOwnerPackage; 50 mProfileOwnerPackages = (profileOwnerPackages == null) ? null 51 : profileOwnerPackages.clone(); 52 } 53 } 54 hasDeviceOwnerOrProfileOwner(int userId, String packageName)55 private boolean hasDeviceOwnerOrProfileOwner(int userId, String packageName) { 56 if (packageName == null) { 57 return false; 58 } 59 synchronized (mLock) { 60 if (mDeviceOwnerPackage != null) { 61 if ((mDeviceOwnerUserId == userId) 62 && (packageName.equals(mDeviceOwnerPackage))) { 63 return true; 64 } 65 } 66 if (mProfileOwnerPackages != null) { 67 if (packageName.equals(mProfileOwnerPackages.get(userId))) { 68 return true; 69 } 70 } 71 } 72 return false; 73 } 74 75 /** 76 * Whether a package or the components in a package's enabled state can be changed 77 * by other callers than itself. 78 */ canPackageStateBeChanged(@serIdInt int userId, String packageName)79 public boolean canPackageStateBeChanged(@UserIdInt int userId, String packageName) { 80 return hasDeviceOwnerOrProfileOwner(userId, packageName); 81 } 82 83 /** 84 * Whether a package's data be cleared. 85 */ canPackageBeWiped(@serIdInt int userId, String packageName)86 public boolean canPackageBeWiped(@UserIdInt int userId, String packageName) { 87 return hasDeviceOwnerOrProfileOwner(userId, packageName); 88 } 89 } 90