1 /* 2 * Copyright 2018, 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.managedprovisioning.finalization; 18 19 import static android.app.admin.DeviceAdminReceiver.ACTION_PROFILE_PROVISIONING_COMPLETE; 20 import static android.app.admin.DevicePolicyManager.ACTION_PROVISIONING_SUCCESSFUL; 21 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE; 22 23 import android.annotation.NonNull; 24 import android.app.Activity; 25 import android.content.Context; 26 import android.content.Intent; 27 import android.os.UserHandle; 28 29 import com.android.managedprovisioning.common.IllegalProvisioningArgumentException; 30 import com.android.managedprovisioning.common.ProvisionLogger; 31 import com.android.managedprovisioning.common.Utils; 32 import com.android.managedprovisioning.model.ProvisioningParams; 33 import com.google.android.setupcompat.util.WizardManagerHelper; 34 35 /** 36 * Helper class for creating intents in finalization controller. 37 */ 38 class ProvisioningIntentProvider { maybeLaunchDpc(ProvisioningParams params, int userId, Utils utils, Context context)39 void maybeLaunchDpc(ProvisioningParams params, int userId, Utils utils, Context context) { 40 final Intent dpcLaunchIntent = createDpcLaunchIntent(params); 41 if (utils.canResolveIntentAsUser(context, dpcLaunchIntent, userId)) { 42 context.startActivityAsUser(createDpcLaunchIntent(params), UserHandle.of(userId)); 43 ProvisionLogger.logd("Dpc was launched for user: " + userId); 44 } 45 } 46 canLaunchDpc(ProvisioningParams params, int userId, Utils utils, Context context)47 boolean canLaunchDpc(ProvisioningParams params, int userId, Utils utils, Context context) { 48 final Intent dpcLaunchIntent = createDpcLaunchIntent(params); 49 return utils.canResolveIntentAsUser(context, dpcLaunchIntent, userId); 50 } 51 createProvisioningCompleteIntent( @onNull ProvisioningParams params, int userId, Utils utils, Context context)52 Intent createProvisioningCompleteIntent( 53 @NonNull ProvisioningParams params, int userId, Utils utils, Context context) { 54 Intent intent = new Intent(ACTION_PROFILE_PROVISIONING_COMPLETE); 55 try { 56 intent.setComponent(params.inferDeviceAdminComponentName(utils, context, userId)); 57 } catch (IllegalProvisioningArgumentException e) { 58 ProvisionLogger.loge("Failed to infer the device admin component name", e); 59 return null; 60 } 61 intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES | Intent.FLAG_RECEIVER_FOREGROUND); 62 addExtrasToIntent(intent, params); 63 return intent; 64 } 65 createDpcLaunchIntent(@onNull ProvisioningParams params)66 private Intent createDpcLaunchIntent(@NonNull ProvisioningParams params) { 67 Intent intent = new Intent(ACTION_PROVISIONING_SUCCESSFUL); 68 final String packageName = params.inferDeviceAdminPackageName(); 69 if (packageName == null) { 70 ProvisionLogger.loge("Device admin package name is null"); 71 return null; 72 } 73 intent.setPackage(packageName); 74 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 75 addExtrasToIntent(intent, params); 76 return intent; 77 } 78 addExtrasToIntent(Intent intent, ProvisioningParams params)79 private void addExtrasToIntent(Intent intent, ProvisioningParams params) { 80 intent.putExtra(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE, params.adminExtrasBundle); 81 } 82 launchFinalizationScreen(Context context, ProvisioningParams params)83 void launchFinalizationScreen(Context context, ProvisioningParams params) { 84 final Intent finalizationScreen = new Intent(context, FinalScreenActivity.class); 85 if (context instanceof Activity) { 86 final Intent intent = ((Activity) context).getIntent(); 87 if (intent != null) { 88 WizardManagerHelper.copyWizardManagerExtras(intent, finalizationScreen); 89 } 90 } 91 finalizationScreen.putExtra(FinalScreenActivity.EXTRA_PROVISIONING_PARAMS, params); 92 context.startActivity(finalizationScreen); 93 } 94 } 95