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 com.android.managedprovisioning.preprovisioning; 18 19 20 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_SUPPORTED_MODES; 21 import static android.app.admin.DevicePolicyManager.FLAG_SUPPORTED_MODES_DEVICE_OWNER; 22 23 import android.app.Activity; 24 import android.app.AlertDialog; 25 import android.app.admin.DevicePolicyManager; 26 import android.content.DialogInterface; 27 import android.content.Intent; 28 import android.os.Bundle; 29 30 import com.android.managedprovisioning.R; 31 import com.android.managedprovisioning.common.ProvisionLogger; 32 import com.android.managedprovisioning.common.SettingsFacade; 33 34 import com.google.android.setupdesign.util.DeviceHelper; 35 36 //TODO(b/181323689) Create tests for activity 37 //TODO(b/213177227) Extract business logic to a controller for testability 38 public class PreProvisioningActivityViaNfc extends Activity { 39 @Override onCreate(Bundle savedInstanceState)40 protected void onCreate(Bundle savedInstanceState) { 41 super.onCreate(savedInstanceState); 42 43 if (new SettingsFacade().isDeviceProvisioned(this)) { 44 ProvisionLogger.loge("Device already provisioned, aborting NFC provisioning."); 45 createDeviceAlreadySetupDialog().show(); 46 } else { 47 Intent provisioningIntent = transformIntentToProvisioningIntent(); 48 if (provisioningIntent != null) { 49 Intent provisioningIntentWithAdditionalExtras = 50 addAdditionalExtras(provisioningIntent); 51 startActivity(provisioningIntentWithAdditionalExtras); 52 finish(); 53 } else { 54 ProvisionLogger.loge("NFC tag data is invalid."); 55 createCantSetupDeviceDialog().show(); 56 } 57 } 58 } 59 addAdditionalExtras(Intent provisioningIntent)60 private Intent addAdditionalExtras(Intent provisioningIntent) { 61 return new Intent(provisioningIntent).putExtra( 62 EXTRA_PROVISIONING_SUPPORTED_MODES, FLAG_SUPPORTED_MODES_DEVICE_OWNER); 63 } 64 transformIntentToProvisioningIntent()65 private Intent transformIntentToProvisioningIntent() { 66 DevicePolicyManager devicePolicyManager = 67 getApplicationContext().getSystemService(DevicePolicyManager.class); 68 return devicePolicyManager.createProvisioningIntentFromNfcIntent(getIntent()); 69 } 70 createCantSetupDeviceDialog()71 private AlertDialog createCantSetupDeviceDialog() { 72 return new AlertDialog.Builder(this) 73 .setMessage(R.string.contact_your_admin_for_help) 74 .setTitle(R.string.cant_set_up_device) 75 .setPositiveButton(android.R.string.ok, createDialogOnClickListener()) 76 .setCancelable(false) 77 .create(); 78 } 79 createDeviceAlreadySetupDialog()80 private AlertDialog createDeviceAlreadySetupDialog() { 81 var context = getApplicationContext(); 82 return new AlertDialog.Builder(this) 83 .setMessage(R.string.if_questions_contact_admin) 84 .setTitle(context.getString( 85 R.string.device_already_set_up, DeviceHelper.getDeviceName(context))) 86 .setPositiveButton(android.R.string.ok, createDialogOnClickListener()) 87 .setCancelable(false) 88 .create(); 89 } 90 createDialogOnClickListener()91 private DialogInterface.OnClickListener createDialogOnClickListener() { 92 return (dialog, id) -> finish(); 93 } 94 } 95