1 /* 2 * Copyright (C) 2021 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 android.permission; 18 19 import static android.app.admin.DevicePolicyManager.PERMISSION_GRANT_STATE_DEFAULT; 20 import static android.app.admin.DevicePolicyManager.PERMISSION_GRANT_STATE_DENIED; 21 import static android.app.admin.DevicePolicyManager.PERMISSION_GRANT_STATE_GRANTED; 22 23 import static com.android.internal.util.Preconditions.checkArgument; 24 25 import android.annotation.NonNull; 26 import android.annotation.SystemApi; 27 import android.app.admin.DevicePolicyManager; 28 import android.os.Parcel; 29 import android.os.Parcelable; 30 31 import com.android.internal.util.Preconditions; 32 33 /** 34 * A data object representing an admin's request to control a certain permission 35 * for a certain app. 36 * This class is processed by the Permission Controller's 37 * setRuntimePermissionGrantStateByDeviceAdmin method. 38 * 39 * @hide 40 */ 41 @SystemApi 42 public final class AdminPermissionControlParams implements Parcelable { 43 // The package to grant/deny the permission to. 44 private final @NonNull String mGranteePackageName; 45 // The permission to grant/deny. 46 private final @NonNull String mPermission; 47 // The grant state (granted/denied/default). 48 private final @DevicePolicyManager.PermissionGrantState int mGrantState; 49 // Whether the admin can grant sensors-related permissions. 50 private final boolean mCanAdminGrantSensorsPermissions; 51 52 /** 53 * @hide 54 * A new instance is only created by the framework, so the constructor need not be visible 55 * as system API. 56 */ AdminPermissionControlParams(@onNull String granteePackageName, @NonNull String permission, @DevicePolicyManager.PermissionGrantState int grantState, boolean canAdminGrantSensorsPermissions)57 public AdminPermissionControlParams(@NonNull String granteePackageName, 58 @NonNull String permission, 59 @DevicePolicyManager.PermissionGrantState int grantState, 60 boolean canAdminGrantSensorsPermissions) { 61 Preconditions.checkStringNotEmpty(granteePackageName, "Package name must not be empty."); 62 Preconditions.checkStringNotEmpty(permission, "Permission must not be empty."); 63 checkArgument(grantState == PERMISSION_GRANT_STATE_GRANTED 64 || grantState == PERMISSION_GRANT_STATE_DENIED 65 || grantState == PERMISSION_GRANT_STATE_DEFAULT); 66 67 mGranteePackageName = granteePackageName; 68 mPermission = permission; 69 mGrantState = grantState; 70 mCanAdminGrantSensorsPermissions = canAdminGrantSensorsPermissions; 71 } 72 73 public static final @NonNull Creator<AdminPermissionControlParams> CREATOR = 74 new Creator<AdminPermissionControlParams>() { 75 @Override 76 public AdminPermissionControlParams createFromParcel(Parcel in) { 77 String granteePackageName = in.readString(); 78 String permission = in.readString(); 79 int grantState = in.readInt(); 80 boolean mayAdminGrantSensorPermissions = in.readBoolean(); 81 82 return new AdminPermissionControlParams(granteePackageName, permission, 83 grantState, mayAdminGrantSensorPermissions); 84 } 85 86 @Override 87 public AdminPermissionControlParams[] newArray(int size) { 88 return new AdminPermissionControlParams[size]; 89 } 90 }; 91 92 @Override describeContents()93 public int describeContents() { 94 return 0; 95 } 96 97 @Override writeToParcel(@onNull Parcel dest, int flags)98 public void writeToParcel(@NonNull Parcel dest, int flags) { 99 dest.writeString(mGranteePackageName); 100 dest.writeString(mPermission); 101 dest.writeInt(mGrantState); 102 dest.writeBoolean(mCanAdminGrantSensorsPermissions); 103 } 104 105 /** Returns the name of the package the permission applies to */ getGranteePackageName()106 public @NonNull String getGranteePackageName() { 107 return mGranteePackageName; 108 } 109 110 /** Returns the permission name */ getPermission()111 public @NonNull String getPermission() { 112 return mPermission; 113 } 114 115 /** Returns the grant state */ getGrantState()116 public @DevicePolicyManager.PermissionGrantState int getGrantState() { 117 return mGrantState; 118 } 119 120 /** 121 * return true if the admin may control grants of permissions related to sensors. 122 */ canAdminGrantSensorsPermissions()123 public boolean canAdminGrantSensorsPermissions() { 124 return mCanAdminGrantSensorsPermissions; 125 } 126 127 @Override toString()128 public String toString() { 129 return String.format( 130 "Grantee %s Permission %s state: %d admin grant of sensors permissions: %b", 131 mGranteePackageName, mPermission, mGrantState, mCanAdminGrantSensorsPermissions); 132 } 133 } 134