Sander Alewijnse | ab18ea7 | 2014-09-11 15:15:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014, 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; |
| 18 | |
Rubin Xu | 292e9a3 | 2015-03-20 17:02:38 +0000 | [diff] [blame] | 19 | import static android.app.admin.DeviceAdminReceiver.ACTION_PROFILE_PROVISIONING_COMPLETE; |
| 20 | import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE; |
| 21 | |
Sander Alewijnse | ab18ea7 | 2014-09-11 15:15:23 +0100 | [diff] [blame] | 22 | import android.app.Activity; |
Craig Lafayette | c25553f | 2015-03-28 17:00:52 -0400 | [diff] [blame] | 23 | import android.app.admin.DeviceInitializerStatus; |
Julia Reynolds | ba25339 | 2015-03-23 15:03:12 -0400 | [diff] [blame] | 24 | import android.app.admin.DevicePolicyManager; |
Rubin Xu | 292e9a3 | 2015-03-20 17:02:38 +0000 | [diff] [blame] | 25 | import android.content.ComponentName; |
Sander Alewijnse | ab18ea7 | 2014-09-11 15:15:23 +0100 | [diff] [blame] | 26 | import android.content.Intent; |
Rubin Xu | 292e9a3 | 2015-03-20 17:02:38 +0000 | [diff] [blame] | 27 | import android.content.pm.PackageManager; |
Sander Alewijnse | ab18ea7 | 2014-09-11 15:15:23 +0100 | [diff] [blame] | 28 | import android.os.Bundle; |
Rubin Xu | 292e9a3 | 2015-03-20 17:02:38 +0000 | [diff] [blame] | 29 | |
| 30 | import com.android.managedprovisioning.Utils.IllegalProvisioningArgumentException; |
Craig Lafayette | c25553f | 2015-03-28 17:00:52 -0400 | [diff] [blame] | 31 | import com.android.managedprovisioning.proxy.BluetoothConnectionService; |
Sander Alewijnse | ab18ea7 | 2014-09-11 15:15:23 +0100 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | * This class is used to make sure that we start the mdm after we shut the Setup wizard down. |
| 35 | * The shut down of the Setup wizard is initiated in the DeviceOwnerProvisioningActivity by setting |
| 36 | * Global.DEVICE_PROVISIONED. This will cause the Setup wizard to shut down and send a HOME intent. |
| 37 | * Instead of opening the home screen we want to open the mdm, so the HOME intent is caught by this |
| 38 | * activity instead, which notifies the DeviceOwnerProvisioningService to send the |
| 39 | * ACTION_PROFILE_PROVISIONING_COMPLETE to the mdm, which will then open up. |
| 40 | */ |
| 41 | |
| 42 | public class HomeReceiverActivity extends Activity { |
| 43 | @Override |
| 44 | public void onCreate(Bundle savedInstanceState) { |
| 45 | super.onCreate(savedInstanceState); |
Rubin Xu | 292e9a3 | 2015-03-20 17:02:38 +0000 | [diff] [blame] | 46 | try { |
| 47 | finalizeProvisioning(); |
| 48 | stopService(new Intent(this, DeviceOwnerProvisioningService.class)); |
| 49 | } finally { |
| 50 | // Disable the HomeReceiverActivity. Make sure this is always called to prevent an |
| 51 | // infinite loop of HomeReceiverActivity capturing HOME intent in case something fails. |
Julia Reynolds | ba25339 | 2015-03-23 15:03:12 -0400 | [diff] [blame] | 52 | disableComponent(new ComponentName(this, HomeReceiverActivity.class)); |
Rubin Xu | 292e9a3 | 2015-03-20 17:02:38 +0000 | [diff] [blame] | 53 | } |
Sander Alewijnse | ab18ea7 | 2014-09-11 15:15:23 +0100 | [diff] [blame] | 54 | finish(); |
| 55 | } |
Rubin Xu | 292e9a3 | 2015-03-20 17:02:38 +0000 | [diff] [blame] | 56 | |
| 57 | private void finalizeProvisioning() { |
| 58 | IntentStore store = BootReminder.getDeviceOwnerFinalizingIntentStore(this); |
| 59 | Intent intent = store.load(); |
| 60 | if (intent == null) { |
| 61 | ProvisionLogger.loge("Fail to retrieve ProvisioningParams from intent store."); |
| 62 | return; |
| 63 | } |
| 64 | store.clear(); |
| 65 | ProvisioningParams mParams; |
| 66 | try { |
| 67 | MessageParser parser = new MessageParser(); |
| 68 | mParams = parser.parseIntent(intent); |
| 69 | } catch (IllegalProvisioningArgumentException e) { |
| 70 | ProvisionLogger.loge("Failed to parse provisioning intent", e); |
| 71 | return; |
| 72 | } |
| 73 | |
Julia Reynolds | ba25339 | 2015-03-23 15:03:12 -0400 | [diff] [blame] | 74 | // Disable the Device Initializer component, if it exists, in case it did not do so itself. |
Sander Alewijnse | 74d6c14 | 2015-04-13 11:51:19 +0100 | [diff] [blame] | 75 | if(mParams.deviceInitializerComponentName != null) { |
Julia Reynolds | ba25339 | 2015-03-23 15:03:12 -0400 | [diff] [blame] | 76 | DevicePolicyManager devicePolicyManager = |
| 77 | (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE); |
Sander Alewijnse | 74d6c14 | 2015-04-13 11:51:19 +0100 | [diff] [blame] | 78 | devicePolicyManager.removeActiveAdmin(mParams.deviceInitializerComponentName); |
| 79 | disableComponent(mParams.deviceInitializerComponentName); |
Julia Reynolds | ba25339 | 2015-03-23 15:03:12 -0400 | [diff] [blame] | 80 | } |
| 81 | |
Rubin Xu | 292e9a3 | 2015-03-20 17:02:38 +0000 | [diff] [blame] | 82 | // Finalizing provisioning: send complete intent to mdm. |
| 83 | Intent result = new Intent(ACTION_PROFILE_PROVISIONING_COMPLETE); |
| 84 | try { |
| 85 | result.setComponent(mParams.inferDeviceAdminComponentName(this)); |
| 86 | } catch (Utils.IllegalProvisioningArgumentException e) { |
| 87 | ProvisionLogger.loge("Failed to infer the device admin component name", e); |
| 88 | return; |
| 89 | } |
| 90 | result.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES | |
| 91 | Intent.FLAG_RECEIVER_FOREGROUND); |
Sander Alewijnse | 74d6c14 | 2015-04-13 11:51:19 +0100 | [diff] [blame] | 92 | if (mParams.adminExtrasBundle != null) { |
Rubin Xu | 292e9a3 | 2015-03-20 17:02:38 +0000 | [diff] [blame] | 93 | result.putExtra(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE, |
Sander Alewijnse | 74d6c14 | 2015-04-13 11:51:19 +0100 | [diff] [blame] | 94 | mParams.adminExtrasBundle); |
Rubin Xu | 292e9a3 | 2015-03-20 17:02:38 +0000 | [diff] [blame] | 95 | } |
| 96 | sendBroadcast(result); |
Craig Lafayette | c25553f | 2015-03-28 17:00:52 -0400 | [diff] [blame] | 97 | |
| 98 | // Shutdown Bluetooth connection if still active. |
| 99 | BluetoothConnectionService.sendStatusUpdate(this, |
| 100 | DeviceInitializerStatus.STATUS_STATE_DEVICE_PROVISIONED); |
| 101 | BluetoothConnectionService.sendBluetoothShutdownRequest(this); |
Rubin Xu | 292e9a3 | 2015-03-20 17:02:38 +0000 | [diff] [blame] | 102 | } |
Julia Reynolds | ba25339 | 2015-03-23 15:03:12 -0400 | [diff] [blame] | 103 | |
| 104 | private void disableComponent(ComponentName component) { |
| 105 | PackageManager pm = getPackageManager(); |
| 106 | pm.setComponentEnabledSetting(component, |
| 107 | PackageManager.COMPONENT_ENABLED_STATE_DISABLED, |
| 108 | PackageManager.DONT_KILL_APP); |
| 109 | } |
Rubin Xu | 292e9a3 | 2015-03-20 17:02:38 +0000 | [diff] [blame] | 110 | } |