blob: 8c2e773f2e017fa61acc9ca662801a0d0562fb67 [file] [log] [blame]
Sander Alewijnseab18ea72014-09-11 15:15:23 +01001/*
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
17package com.android.managedprovisioning;
18
Rubin Xu292e9a32015-03-20 17:02:38 +000019import static android.app.admin.DeviceAdminReceiver.ACTION_PROFILE_PROVISIONING_COMPLETE;
20import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE;
21
Sander Alewijnseab18ea72014-09-11 15:15:23 +010022import android.app.Activity;
Craig Lafayettec25553f2015-03-28 17:00:52 -040023import android.app.admin.DeviceInitializerStatus;
Julia Reynoldsba253392015-03-23 15:03:12 -040024import android.app.admin.DevicePolicyManager;
Rubin Xu292e9a32015-03-20 17:02:38 +000025import android.content.ComponentName;
Sander Alewijnseab18ea72014-09-11 15:15:23 +010026import android.content.Intent;
Rubin Xu292e9a32015-03-20 17:02:38 +000027import android.content.pm.PackageManager;
Sander Alewijnseab18ea72014-09-11 15:15:23 +010028import android.os.Bundle;
Rubin Xu292e9a32015-03-20 17:02:38 +000029
30import com.android.managedprovisioning.Utils.IllegalProvisioningArgumentException;
Craig Lafayettec25553f2015-03-28 17:00:52 -040031import com.android.managedprovisioning.proxy.BluetoothConnectionService;
Sander Alewijnseab18ea72014-09-11 15:15:23 +010032
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
42public class HomeReceiverActivity extends Activity {
43 @Override
44 public void onCreate(Bundle savedInstanceState) {
45 super.onCreate(savedInstanceState);
Rubin Xu292e9a32015-03-20 17:02:38 +000046 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 Reynoldsba253392015-03-23 15:03:12 -040052 disableComponent(new ComponentName(this, HomeReceiverActivity.class));
Rubin Xu292e9a32015-03-20 17:02:38 +000053 }
Sander Alewijnseab18ea72014-09-11 15:15:23 +010054 finish();
55 }
Rubin Xu292e9a32015-03-20 17:02:38 +000056
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 Reynoldsba253392015-03-23 15:03:12 -040074 // Disable the Device Initializer component, if it exists, in case it did not do so itself.
Sander Alewijnse74d6c142015-04-13 11:51:19 +010075 if(mParams.deviceInitializerComponentName != null) {
Julia Reynoldsba253392015-03-23 15:03:12 -040076 DevicePolicyManager devicePolicyManager =
77 (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
Sander Alewijnse74d6c142015-04-13 11:51:19 +010078 devicePolicyManager.removeActiveAdmin(mParams.deviceInitializerComponentName);
79 disableComponent(mParams.deviceInitializerComponentName);
Julia Reynoldsba253392015-03-23 15:03:12 -040080 }
81
Rubin Xu292e9a32015-03-20 17:02:38 +000082 // 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 Alewijnse74d6c142015-04-13 11:51:19 +010092 if (mParams.adminExtrasBundle != null) {
Rubin Xu292e9a32015-03-20 17:02:38 +000093 result.putExtra(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE,
Sander Alewijnse74d6c142015-04-13 11:51:19 +010094 mParams.adminExtrasBundle);
Rubin Xu292e9a32015-03-20 17:02:38 +000095 }
96 sendBroadcast(result);
Craig Lafayettec25553f2015-03-28 17:00:52 -040097
98 // Shutdown Bluetooth connection if still active.
99 BluetoothConnectionService.sendStatusUpdate(this,
100 DeviceInitializerStatus.STATUS_STATE_DEVICE_PROVISIONED);
101 BluetoothConnectionService.sendBluetoothShutdownRequest(this);
Rubin Xu292e9a32015-03-20 17:02:38 +0000102 }
Julia Reynoldsba253392015-03-23 15:03:12 -0400103
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 Xu292e9a32015-03-20 17:02:38 +0000110}