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.app.admin; 18 19 import static java.util.Objects.requireNonNull; 20 21 import android.accounts.Account; 22 import android.annotation.NonNull; 23 import android.annotation.Nullable; 24 import android.annotation.TestApi; 25 import android.content.ComponentName; 26 import android.os.Parcel; 27 import android.os.Parcelable; 28 import android.stats.devicepolicy.DevicePolicyEnums; 29 30 /** 31 * Params required to provision a managed profile, see 32 * {@link DevicePolicyManager#createAndProvisionManagedProfile}. 33 * 34 * @hide 35 */ 36 @TestApi 37 public final class ManagedProfileProvisioningParams implements Parcelable { 38 private static final String LEAVE_ALL_SYSTEM_APPS_ENABLED_PARAM = 39 "LEAVE_ALL_SYSTEM_APPS_ENABLED"; 40 private static final String ORGANIZATION_OWNED_PROVISIONING_PARAM = 41 "ORGANIZATION_OWNED_PROVISIONING"; 42 private static final String ACCOUNT_TO_MIGRATE_PROVIDED_PARAM = "ACCOUNT_TO_MIGRATE_PROVIDED"; 43 private static final String KEEP_MIGRATED_ACCOUNT_PARAM = "KEEP_MIGRATED_ACCOUNT"; 44 45 @NonNull private final ComponentName mProfileAdminComponentName; 46 @NonNull private final String mOwnerName; 47 @Nullable private final String mProfileName; 48 @Nullable private final Account mAccountToMigrate; 49 private final boolean mLeaveAllSystemAppsEnabled; 50 private final boolean mOrganizationOwnedProvisioning; 51 private final boolean mKeepAccountMigrated; 52 53 ManagedProfileProvisioningParams( @onNull ComponentName profileAdminComponentName, @NonNull String ownerName, @Nullable String profileName, @Nullable Account accountToMigrate, boolean leaveAllSystemAppsEnabled, boolean organizationOwnedProvisioning, boolean keepAccountMigrated)54 private ManagedProfileProvisioningParams( 55 @NonNull ComponentName profileAdminComponentName, 56 @NonNull String ownerName, 57 @Nullable String profileName, 58 @Nullable Account accountToMigrate, 59 boolean leaveAllSystemAppsEnabled, 60 boolean organizationOwnedProvisioning, 61 boolean keepAccountMigrated) { 62 this.mProfileAdminComponentName = requireNonNull(profileAdminComponentName); 63 this.mOwnerName = requireNonNull(ownerName); 64 this.mProfileName = profileName; 65 this.mAccountToMigrate = accountToMigrate; 66 this.mLeaveAllSystemAppsEnabled = leaveAllSystemAppsEnabled; 67 this.mOrganizationOwnedProvisioning = organizationOwnedProvisioning; 68 this.mKeepAccountMigrated = keepAccountMigrated; 69 } 70 71 @NonNull getProfileAdminComponentName()72 public ComponentName getProfileAdminComponentName() { 73 return mProfileAdminComponentName; 74 } 75 76 @NonNull getOwnerName()77 public String getOwnerName() { 78 return mOwnerName; 79 } 80 81 @Nullable getProfileName()82 public String getProfileName() { 83 return mProfileName; 84 } 85 86 @Nullable getAccountToMigrate()87 public Account getAccountToMigrate() { 88 return mAccountToMigrate; 89 } 90 isLeaveAllSystemAppsEnabled()91 public boolean isLeaveAllSystemAppsEnabled() { 92 return mLeaveAllSystemAppsEnabled; 93 } 94 isOrganizationOwnedProvisioning()95 public boolean isOrganizationOwnedProvisioning() { 96 return mOrganizationOwnedProvisioning; 97 } 98 isKeepAccountMigrated()99 public boolean isKeepAccountMigrated() { 100 return mKeepAccountMigrated; 101 } 102 103 /** 104 * Logs the provisioning params using {@link DevicePolicyEventLogger}. 105 */ logParams(@onNull String callerPackage)106 public void logParams(@NonNull String callerPackage) { 107 requireNonNull(callerPackage); 108 109 logParam(callerPackage, LEAVE_ALL_SYSTEM_APPS_ENABLED_PARAM, mLeaveAllSystemAppsEnabled); 110 logParam(callerPackage, ORGANIZATION_OWNED_PROVISIONING_PARAM, 111 mOrganizationOwnedProvisioning); 112 logParam(callerPackage, KEEP_MIGRATED_ACCOUNT_PARAM, mKeepAccountMigrated); 113 logParam(callerPackage, ACCOUNT_TO_MIGRATE_PROVIDED_PARAM, 114 /* value= */ mAccountToMigrate != null); 115 } 116 logParam(String callerPackage, String param, boolean value)117 private void logParam(String callerPackage, String param, boolean value) { 118 DevicePolicyEventLogger 119 .createEvent(DevicePolicyEnums.PLATFORM_PROVISIONING_PARAM) 120 .setStrings(callerPackage) 121 .setAdmin(mProfileAdminComponentName) 122 .setStrings(param) 123 .setBoolean(value) 124 .write(); 125 } 126 127 /** 128 * Builder class for {@link ManagedProfileProvisioningParams} objects. 129 */ 130 public static final class Builder { 131 @NonNull private final ComponentName mProfileAdminComponentName; 132 @NonNull private final String mOwnerName; 133 @Nullable private String mProfileName; 134 @Nullable private Account mAccountToMigrate; 135 private boolean mLeaveAllSystemAppsEnabled; 136 private boolean mOrganizationOwnedProvisioning; 137 private boolean mKeepAccountMigrated; 138 139 /** 140 * Initialize a new {@link Builder) to construct a {@link ManagedProfileProvisioningParams}. 141 * <p> 142 * See {@link DevicePolicyManager#createAndProvisionManagedProfile} 143 * 144 * @param profileAdminComponentName The admin {@link ComponentName} to be set as the profile 145 * owner. 146 * @param ownerName The name of the profile owner. 147 * 148 * @throws NullPointerException if {@code profileAdminComponentName} or 149 * {@code ownerName} are null. 150 */ Builder( @onNull ComponentName profileAdminComponentName, @NonNull String ownerName)151 public Builder( 152 @NonNull ComponentName profileAdminComponentName, @NonNull String ownerName) { 153 requireNonNull(profileAdminComponentName); 154 requireNonNull(ownerName); 155 this.mProfileAdminComponentName = profileAdminComponentName; 156 this.mOwnerName = ownerName; 157 } 158 159 /** 160 * Sets the profile name of the created profile when 161 * {@link DevicePolicyManager#createAndProvisionManagedProfile} is called. Defaults to 162 * {@code null} if not set. 163 */ 164 @NonNull setProfileName(@ullable String profileName)165 public Builder setProfileName(@Nullable String profileName) { 166 this.mProfileName = profileName; 167 return this; 168 } 169 170 /** 171 * Sets the {@link Account} to migrate from the parent profile to the created profile when 172 * {@link DevicePolicyManager#createAndProvisionManagedProfile} is called. If not set, or 173 * set to {@code null}, no accounts will be migrated. 174 */ 175 @NonNull setAccountToMigrate(@ullable Account accountToMigrate)176 public Builder setAccountToMigrate(@Nullable Account accountToMigrate) { 177 this.mAccountToMigrate = accountToMigrate; 178 return this; 179 } 180 181 /** 182 * Sets whether non-required system apps should be installed on 183 * the created profile when {@link DevicePolicyManager#createAndProvisionManagedProfile} 184 * is called. Defaults to {@code false} if not set. 185 */ 186 @NonNull setLeaveAllSystemAppsEnabled(boolean leaveAllSystemAppsEnabled)187 public Builder setLeaveAllSystemAppsEnabled(boolean leaveAllSystemAppsEnabled) { 188 this.mLeaveAllSystemAppsEnabled = leaveAllSystemAppsEnabled; 189 return this; 190 } 191 192 /** 193 * Sets if this device is owned by an organization. Defaults to {@code false} 194 * if not set. 195 */ 196 @NonNull setOrganizationOwnedProvisioning(boolean organizationOwnedProvisioning)197 public Builder setOrganizationOwnedProvisioning(boolean organizationOwnedProvisioning) { 198 this.mOrganizationOwnedProvisioning = organizationOwnedProvisioning; 199 return this; 200 } 201 202 /** 203 * Sets whether to keep the account on the parent profile during account migration. 204 * Defaults to {@code false}. 205 */ 206 @NonNull setKeepAccountMigrated(boolean keepAccountMigrated)207 public Builder setKeepAccountMigrated(boolean keepAccountMigrated) { 208 this.mKeepAccountMigrated = keepAccountMigrated; 209 return this; 210 } 211 212 /** 213 * Combines all of the attributes that have been set on this {@code Builder}. 214 * 215 * @return a new {@link ManagedProfileProvisioningParams} object. 216 */ 217 @NonNull build()218 public ManagedProfileProvisioningParams build() { 219 return new ManagedProfileProvisioningParams( 220 mProfileAdminComponentName, 221 mOwnerName, 222 mProfileName, 223 mAccountToMigrate, 224 mLeaveAllSystemAppsEnabled, 225 mOrganizationOwnedProvisioning, 226 mKeepAccountMigrated); 227 } 228 } 229 230 @Override describeContents()231 public int describeContents() { 232 return 0; 233 } 234 235 @Override toString()236 public String toString() { 237 return "ManagedProfileProvisioningParams{" 238 + "mProfileAdminComponentName=" + mProfileAdminComponentName 239 + ", mOwnerName=" + mOwnerName 240 + ", mProfileName=" + (mProfileName == null ? "null" : mProfileName) 241 + ", mAccountToMigrate=" + (mAccountToMigrate == null ? "null" : mAccountToMigrate) 242 + ", mLeaveAllSystemAppsEnabled=" + mLeaveAllSystemAppsEnabled 243 + ", mOrganizationOwnedProvisioning=" + mOrganizationOwnedProvisioning 244 + ", mKeepAccountMigrated=" + mKeepAccountMigrated 245 + '}'; 246 } 247 248 @Override writeToParcel(@onNull Parcel dest, @Nullable int flags)249 public void writeToParcel(@NonNull Parcel dest, @Nullable int flags) { 250 dest.writeTypedObject(mProfileAdminComponentName, flags); 251 dest.writeString(mOwnerName); 252 dest.writeString(mProfileName); 253 dest.writeTypedObject(mAccountToMigrate, flags); 254 dest.writeBoolean(mLeaveAllSystemAppsEnabled); 255 dest.writeBoolean(mOrganizationOwnedProvisioning); 256 dest.writeBoolean(mKeepAccountMigrated); 257 } 258 259 public static final @NonNull Creator<ManagedProfileProvisioningParams> CREATOR = 260 new Creator<ManagedProfileProvisioningParams>() { 261 @Override 262 public ManagedProfileProvisioningParams createFromParcel(Parcel in) { 263 ComponentName componentName = in.readTypedObject(ComponentName.CREATOR); 264 String ownerName = in.readString(); 265 String profileName = in.readString(); 266 Account account = in.readTypedObject(Account.CREATOR); 267 boolean leaveAllSystemAppsEnabled = in.readBoolean(); 268 boolean organizationOwnedProvisioning = in.readBoolean(); 269 boolean keepAccountMigrated = in.readBoolean(); 270 271 return new ManagedProfileProvisioningParams( 272 componentName, 273 ownerName, 274 profileName, 275 account, 276 leaveAllSystemAppsEnabled, 277 organizationOwnedProvisioning, 278 keepAccountMigrated); 279 } 280 281 @Override 282 public ManagedProfileProvisioningParams[] newArray(int size) { 283 return new ManagedProfileProvisioningParams[size]; 284 } 285 }; 286 } 287