Alexandra Gherghina | e9965bb | 2014-02-25 17:09:40 +0000 | [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 | |
Jessica Hummel | 8119e12 | 2014-01-10 12:13:49 +0000 | [diff] [blame] | 17 | package com.android.managedprovisioning; |
| 18 | |
Alexandra Gherghina | 902c46f | 2014-11-20 16:29:29 +0000 | [diff] [blame] | 19 | import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE; |
| 20 | |
| 21 | import android.accounts.Account; |
| 22 | import android.accounts.AccountManager; |
| 23 | import android.accounts.AccountManagerFuture; |
| 24 | import android.accounts.AuthenticatorException; |
| 25 | import android.accounts.OperationCanceledException; |
Jessica Hummel | 8119e12 | 2014-01-10 12:13:49 +0000 | [diff] [blame] | 26 | import android.app.Activity; |
Sander Alewijnse | 606780d | 2014-10-29 10:51:39 +0000 | [diff] [blame] | 27 | import android.app.ActivityManagerNative; |
Jessica Hummel | 2226514 | 2014-07-15 16:10:12 +0100 | [diff] [blame] | 28 | import android.app.AlertDialog; |
Sander Alewijnse | 606780d | 2014-10-29 10:51:39 +0000 | [diff] [blame] | 29 | import android.app.IActivityManager; |
Rubin Xu | 8857747 | 2014-10-06 11:00:17 +0100 | [diff] [blame] | 30 | import android.app.ProgressDialog; |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 31 | import android.content.BroadcastReceiver; |
Robin Lee | 4af4ed0 | 2014-08-04 12:29:31 +0100 | [diff] [blame] | 32 | import android.content.ComponentName; |
Jessica Hummel | b5f001d | 2014-01-10 16:39:07 +0000 | [diff] [blame] | 33 | import android.content.Context; |
Jessica Hummel | 2226514 | 2014-07-15 16:10:12 +0100 | [diff] [blame] | 34 | import android.content.DialogInterface; |
Jessica Hummel | b5f001d | 2014-01-10 16:39:07 +0000 | [diff] [blame] | 35 | import android.content.Intent; |
Nicolas Prevot | 3581ff9 | 2014-05-01 17:21:43 +0100 | [diff] [blame] | 36 | import android.content.IntentFilter; |
Alexandra Gherghina | 902c46f | 2014-11-20 16:29:29 +0000 | [diff] [blame] | 37 | import android.os.AsyncTask; |
Jessica Hummel | 8119e12 | 2014-01-10 12:13:49 +0000 | [diff] [blame] | 38 | import android.os.Bundle; |
Sander Alewijnse | 3dc234a | 2014-11-27 18:30:11 +0000 | [diff] [blame] | 39 | import android.os.Handler; |
Sander Alewijnse | 606780d | 2014-10-29 10:51:39 +0000 | [diff] [blame] | 40 | import android.os.RemoteException; |
Esteban Talavera | 928febf | 2014-08-01 14:37:08 +0100 | [diff] [blame] | 41 | import android.os.UserHandle; |
Jessica Hummel | c4d5d2b | 2014-02-27 15:37:01 +0000 | [diff] [blame] | 42 | import android.os.UserManager; |
Sander Alewijnse | 606780d | 2014-10-29 10:51:39 +0000 | [diff] [blame] | 43 | import android.provider.Settings; |
Jessica Hummel | 81fe104 | 2014-06-23 17:10:38 +0100 | [diff] [blame] | 44 | import android.support.v4.content.LocalBroadcastManager; |
Jessica Hummel | a08b23e | 2014-02-03 18:12:58 +0000 | [diff] [blame] | 45 | import android.view.LayoutInflater; |
| 46 | import android.view.View; |
Jessica Hummel | 0ea830d | 2014-06-11 12:29:40 +0100 | [diff] [blame] | 47 | import android.widget.Button; |
Sander Alewijnse | 606780d | 2014-10-29 10:51:39 +0000 | [diff] [blame] | 48 | import android.widget.TextView; |
Jessica Hummel | 8119e12 | 2014-01-10 12:13:49 +0000 | [diff] [blame] | 49 | |
Alexandra Gherghina | 902c46f | 2014-11-20 16:29:29 +0000 | [diff] [blame] | 50 | import java.io.IOException; |
| 51 | |
Jason Monk | c6d71b8 | 2014-01-17 14:52:00 -0500 | [diff] [blame] | 52 | /** |
Sander Alewijnse | 30bc13c | 2014-11-03 12:21:30 +0000 | [diff] [blame] | 53 | * Profile owner provisioning sets up a separate profile on a device whose primary user is already |
| 54 | * set up. |
| 55 | * |
| 56 | * <p> |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 57 | * The typical example is setting up a corporate profile that is controlled by their employer on a |
| 58 | * users personal device to keep personal and work data separate. |
| 59 | * |
Sander Alewijnse | 30bc13c | 2014-11-03 12:21:30 +0000 | [diff] [blame] | 60 | * <p> |
Sander Alewijnse | 606780d | 2014-10-29 10:51:39 +0000 | [diff] [blame] | 61 | * The activity handles the UI for managed profile provisioning and starts the |
Sander Alewijnse | 30bc13c | 2014-11-03 12:21:30 +0000 | [diff] [blame] | 62 | * {@link ProfileOwnerProvisioningService}, which runs through the setup steps in an |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 63 | * async task. |
Jason Monk | c6d71b8 | 2014-01-17 14:52:00 -0500 | [diff] [blame] | 64 | */ |
Sander Alewijnse | 30bc13c | 2014-11-03 12:21:30 +0000 | [diff] [blame] | 65 | public class ProfileOwnerProvisioningActivity extends Activity { |
Rubin Xu | 8857747 | 2014-10-06 11:00:17 +0100 | [diff] [blame] | 66 | protected static final String ACTION_CANCEL_PROVISIONING = |
| 67 | "com.android.managedprovisioning.CANCEL_PROVISIONING"; |
| 68 | |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 69 | private BroadcastReceiver mServiceMessageReceiver; |
Jessica Hummel | b5f001d | 2014-01-10 16:39:07 +0000 | [diff] [blame] | 70 | |
Rubin Xu | 8857747 | 2014-10-06 11:00:17 +0100 | [diff] [blame] | 71 | // Provisioning service started |
Sander Alewijnse | 606780d | 2014-10-29 10:51:39 +0000 | [diff] [blame] | 72 | private static final int CANCELSTATUS_PROVISIONING = 1; |
Rubin Xu | 8857747 | 2014-10-06 11:00:17 +0100 | [diff] [blame] | 73 | // Back button pressed during provisioning, confirm dialog showing. |
Sander Alewijnse | 606780d | 2014-10-29 10:51:39 +0000 | [diff] [blame] | 74 | private static final int CANCELSTATUS_CONFIRMING = 2; |
Rubin Xu | 8857747 | 2014-10-06 11:00:17 +0100 | [diff] [blame] | 75 | // Cancel confirmed, waiting for the provisioning service to complete. |
Sander Alewijnse | 606780d | 2014-10-29 10:51:39 +0000 | [diff] [blame] | 76 | private static final int CANCELSTATUS_CANCELLING = 3; |
| 77 | // Cancelling not possible anymore, provisioning already finished succesfully. |
| 78 | private static final int CANCELSTATUS_FINALIZING = 4; |
| 79 | |
| 80 | private static final String KEY_CANCELSTATUS= "cancelstatus"; |
| 81 | private static final String KEY_PENDING_INTENT = "pending_intent"; |
| 82 | |
| 83 | private int mCancelStatus = CANCELSTATUS_PROVISIONING; |
Rubin Xu | 8857747 | 2014-10-06 11:00:17 +0100 | [diff] [blame] | 84 | private Intent mPendingProvisioningResult = null; |
| 85 | private ProgressDialog mCancelProgressDialog = null; |
Alexandra Gherghina | 902c46f | 2014-11-20 16:29:29 +0000 | [diff] [blame] | 86 | private AccountManager mAccountManager; |
Rubin Xu | 8857747 | 2014-10-06 11:00:17 +0100 | [diff] [blame] | 87 | |
Jessica Hummel | 8119e12 | 2014-01-10 12:13:49 +0000 | [diff] [blame] | 88 | @Override |
Jessica Hummel | c4d5d2b | 2014-02-27 15:37:01 +0000 | [diff] [blame] | 89 | protected void onCreate(Bundle savedInstanceState) { |
Jessica Hummel | 8119e12 | 2014-01-10 12:13:49 +0000 | [diff] [blame] | 90 | super.onCreate(savedInstanceState); |
Sander Alewijnse | 30bc13c | 2014-11-03 12:21:30 +0000 | [diff] [blame] | 91 | ProvisionLogger.logd("Profile owner provisioning activity ONCREATE"); |
Alexandra Gherghina | 902c46f | 2014-11-20 16:29:29 +0000 | [diff] [blame] | 92 | mAccountManager = (AccountManager) getSystemService(Context.ACCOUNT_SERVICE); |
Jessica Hummel | b5f001d | 2014-01-10 16:39:07 +0000 | [diff] [blame] | 93 | |
Sander Alewijnse | 606780d | 2014-10-29 10:51:39 +0000 | [diff] [blame] | 94 | if (savedInstanceState != null) { |
| 95 | mCancelStatus = savedInstanceState.getInt(KEY_CANCELSTATUS, CANCELSTATUS_PROVISIONING); |
| 96 | mPendingProvisioningResult = savedInstanceState.getParcelable(KEY_PENDING_INTENT); |
Adam Connors | 7f8c6e7 | 2014-04-01 12:14:54 +0100 | [diff] [blame] | 97 | } |
Sander Alewijnse | 606780d | 2014-10-29 10:51:39 +0000 | [diff] [blame] | 98 | |
| 99 | final LayoutInflater inflater = getLayoutInflater(); |
| 100 | View contentView = inflater.inflate(R.layout.progress, null); |
| 101 | setContentView(contentView); |
| 102 | TextView textView = (TextView) findViewById(R.id.prog_text); |
| 103 | if (textView != null) textView.setText(getString(R.string.setting_up_workspace)); |
| 104 | |
| 105 | |
| 106 | if (mCancelStatus == CANCELSTATUS_CONFIRMING) { |
| 107 | showCancelProvisioningDialog(); |
| 108 | } else if (mCancelStatus == CANCELSTATUS_CANCELLING) { |
| 109 | showCancelProgressDialog(); |
Esteban Talavera | 928febf | 2014-08-01 14:37:08 +0100 | [diff] [blame] | 110 | } |
Sander Alewijnse | 3dc234a | 2014-11-27 18:30:11 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | |
| 114 | @Override |
| 115 | protected void onResume() { |
| 116 | super.onResume(); |
Adam Connors | 7f8c6e7 | 2014-04-01 12:14:54 +0100 | [diff] [blame] | 117 | |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 118 | // Setup broadcast receiver for feedback from service. |
| 119 | mServiceMessageReceiver = new ServiceMessageReceiver(); |
| 120 | IntentFilter filter = new IntentFilter(); |
Sander Alewijnse | 30bc13c | 2014-11-03 12:21:30 +0000 | [diff] [blame] | 121 | filter.addAction(ProfileOwnerProvisioningService.ACTION_PROVISIONING_SUCCESS); |
| 122 | filter.addAction(ProfileOwnerProvisioningService.ACTION_PROVISIONING_ERROR); |
| 123 | filter.addAction(ProfileOwnerProvisioningService.ACTION_PROVISIONING_CANCELLED); |
Jessica Hummel | 81fe104 | 2014-06-23 17:10:38 +0100 | [diff] [blame] | 124 | LocalBroadcastManager.getInstance(this).registerReceiver(mServiceMessageReceiver, filter); |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 125 | |
Sander Alewijnse | 3dc234a | 2014-11-27 18:30:11 +0000 | [diff] [blame] | 126 | // Start service async to make sure the UI is loaded first. |
| 127 | final Handler handler = new Handler(getMainLooper()); |
| 128 | handler.post(new Runnable() { |
| 129 | @Override |
| 130 | public void run() { |
| 131 | Intent intent = new Intent(ProfileOwnerProvisioningActivity.this, |
| 132 | ProfileOwnerProvisioningService.class); |
| 133 | intent.putExtras(getIntent()); |
| 134 | startService(intent); |
| 135 | } |
| 136 | }); |
Sander Alewijnse | 2d0a093 | 2014-07-10 15:12:37 +0100 | [diff] [blame] | 137 | } |
| 138 | |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 139 | class ServiceMessageReceiver extends BroadcastReceiver { |
| 140 | @Override |
| 141 | public void onReceive(Context context, Intent intent) { |
Rubin Xu | 8857747 | 2014-10-06 11:00:17 +0100 | [diff] [blame] | 142 | if (mCancelStatus == CANCELSTATUS_CONFIRMING) { |
| 143 | // Store the incoming intent and only process it after the user has responded to |
| 144 | // the cancel dialog |
| 145 | mPendingProvisioningResult = intent; |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 146 | return; |
| 147 | } |
Rubin Xu | 8857747 | 2014-10-06 11:00:17 +0100 | [diff] [blame] | 148 | handleProvisioningResult(intent); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | private void handleProvisioningResult(Intent intent) { |
| 153 | String action = intent.getAction(); |
Sander Alewijnse | 30bc13c | 2014-11-03 12:21:30 +0000 | [diff] [blame] | 154 | if (ProfileOwnerProvisioningService.ACTION_PROVISIONING_SUCCESS.equals(action)) { |
Rubin Xu | 8857747 | 2014-10-06 11:00:17 +0100 | [diff] [blame] | 155 | if (mCancelStatus == CANCELSTATUS_CANCELLING) { |
| 156 | return; |
| 157 | } |
Sander Alewijnse | 606780d | 2014-10-29 10:51:39 +0000 | [diff] [blame] | 158 | |
Rubin Xu | 8857747 | 2014-10-06 11:00:17 +0100 | [diff] [blame] | 159 | ProvisionLogger.logd("Successfully provisioned." |
Sander Alewijnse | 30bc13c | 2014-11-03 12:21:30 +0000 | [diff] [blame] | 160 | + "Finishing ProfileOwnerProvisioningActivity"); |
Sander Alewijnse | 606780d | 2014-10-29 10:51:39 +0000 | [diff] [blame] | 161 | |
| 162 | Intent pendingIntent = (Intent) intent.getParcelableExtra( |
Sander Alewijnse | 30bc13c | 2014-11-03 12:21:30 +0000 | [diff] [blame] | 163 | ProfileOwnerProvisioningService.EXTRA_PENDING_SUCCESS_INTENT); |
Sander Alewijnse | 606780d | 2014-10-29 10:51:39 +0000 | [diff] [blame] | 164 | int serialNumber = intent.getIntExtra( |
Sander Alewijnse | 30bc13c | 2014-11-03 12:21:30 +0000 | [diff] [blame] | 165 | ProfileOwnerProvisioningService.EXTRA_PROFILE_USER_SERIAL_NUMBER, -1); |
Sander Alewijnse | 606780d | 2014-10-29 10:51:39 +0000 | [diff] [blame] | 166 | |
Alexandra Gherghina | d74aac8 | 2014-11-20 16:29:29 +0000 | [diff] [blame] | 167 | int userId = intent.getIntExtra(ProfileOwnerProvisioningService.EXTRA_PROFILE_USER_ID, |
| 168 | -1); |
Sander Alewijnse | 606780d | 2014-10-29 10:51:39 +0000 | [diff] [blame] | 169 | onProvisioningSuccess(pendingIntent, userId, serialNumber); |
Sander Alewijnse | 30bc13c | 2014-11-03 12:21:30 +0000 | [diff] [blame] | 170 | } else if (ProfileOwnerProvisioningService.ACTION_PROVISIONING_ERROR.equals(action)) { |
Rubin Xu | 8857747 | 2014-10-06 11:00:17 +0100 | [diff] [blame] | 171 | if (mCancelStatus == CANCELSTATUS_CANCELLING){ |
| 172 | return; |
| 173 | } |
| 174 | String errorLogMessage = intent.getStringExtra( |
Sander Alewijnse | 30bc13c | 2014-11-03 12:21:30 +0000 | [diff] [blame] | 175 | ProfileOwnerProvisioningService.EXTRA_LOG_MESSAGE_KEY); |
Rubin Xu | 8857747 | 2014-10-06 11:00:17 +0100 | [diff] [blame] | 176 | ProvisionLogger.logd("Error reported: " + errorLogMessage); |
Sander Alewijnse | 606780d | 2014-10-29 10:51:39 +0000 | [diff] [blame] | 177 | error(R.string.managed_provisioning_error_text, errorLogMessage); |
Sander Alewijnse | 30bc13c | 2014-11-03 12:21:30 +0000 | [diff] [blame] | 178 | } if (ProfileOwnerProvisioningService.ACTION_PROVISIONING_CANCELLED.equals(action)) { |
Rubin Xu | 8857747 | 2014-10-06 11:00:17 +0100 | [diff] [blame] | 179 | if (mCancelStatus != CANCELSTATUS_CANCELLING) { |
| 180 | return; |
| 181 | } |
| 182 | mCancelProgressDialog.dismiss(); |
Sander Alewijnse | 30bc13c | 2014-11-03 12:21:30 +0000 | [diff] [blame] | 183 | ProfileOwnerProvisioningActivity.this.setResult(Activity.RESULT_CANCELED); |
| 184 | stopService(new Intent(ProfileOwnerProvisioningActivity.this, |
| 185 | ProfileOwnerProvisioningService.class)); |
| 186 | ProfileOwnerProvisioningActivity.this.finish(); |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
Jessica Hummel | c4d5d2b | 2014-02-27 15:37:01 +0000 | [diff] [blame] | 190 | @Override |
| 191 | public void onBackPressed() { |
Sander Alewijnse | 606780d | 2014-10-29 10:51:39 +0000 | [diff] [blame] | 192 | if (mCancelStatus == CANCELSTATUS_PROVISIONING) { |
Rubin Xu | 8857747 | 2014-10-06 11:00:17 +0100 | [diff] [blame] | 193 | showCancelProvisioningDialog(); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | private void showCancelProvisioningDialog() { |
| 198 | mCancelStatus = CANCELSTATUS_CONFIRMING; |
| 199 | AlertDialog alertDialog = new AlertDialog.Builder(this) |
| 200 | .setCancelable(false) |
| 201 | .setTitle(R.string.profile_owner_cancel_title) |
| 202 | .setMessage(R.string.profile_owner_cancel_message) |
| 203 | .setNegativeButton(R.string.profile_owner_cancel_cancel, |
| 204 | new DialogInterface.OnClickListener() { |
| 205 | @Override |
| 206 | public void onClick(DialogInterface dialog,int id) { |
| 207 | mCancelStatus = CANCELSTATUS_PROVISIONING; |
| 208 | if (mPendingProvisioningResult != null) { |
| 209 | handleProvisioningResult(mPendingProvisioningResult); |
| 210 | } |
| 211 | } |
| 212 | }) |
| 213 | .setPositiveButton(R.string.profile_owner_cancel_ok, |
| 214 | new DialogInterface.OnClickListener() { |
| 215 | @Override |
| 216 | public void onClick(DialogInterface dialog,int id) { |
Sander Alewijnse | 606780d | 2014-10-29 10:51:39 +0000 | [diff] [blame] | 217 | confirmCancel(); |
Rubin Xu | 8857747 | 2014-10-06 11:00:17 +0100 | [diff] [blame] | 218 | } |
| 219 | }) |
| 220 | .create(); |
| 221 | alertDialog.show(); |
| 222 | } |
| 223 | |
| 224 | protected void showCancelProgressDialog() { |
| 225 | mCancelProgressDialog = new ProgressDialog(this); |
| 226 | mCancelProgressDialog.setMessage(getText(R.string.profile_owner_cancelling)); |
| 227 | mCancelProgressDialog.setCancelable(false); |
| 228 | mCancelProgressDialog.setCanceledOnTouchOutside(false); |
| 229 | mCancelProgressDialog.show(); |
Jessica Hummel | b5f001d | 2014-01-10 16:39:07 +0000 | [diff] [blame] | 230 | } |
Jessica Hummel | 3925923 | 2014-02-03 18:12:58 +0000 | [diff] [blame] | 231 | |
Sander Alewijnse | 606780d | 2014-10-29 10:51:39 +0000 | [diff] [blame] | 232 | public void error(int resourceId, String logText) { |
| 233 | ProvisionLogger.loge(logText); |
| 234 | new AlertDialog.Builder(this) |
| 235 | .setTitle(R.string.provisioning_error_title) |
| 236 | .setMessage(getString(resourceId)) |
| 237 | .setCancelable(false) |
| 238 | .setPositiveButton(R.string.device_owner_error_ok, new DialogInterface.OnClickListener() { |
| 239 | @Override |
| 240 | public void onClick(DialogInterface dialog,int id) { |
| 241 | confirmCancel(); |
| 242 | } |
| 243 | }).show(); |
| 244 | } |
| 245 | |
| 246 | private void confirmCancel() { |
| 247 | mCancelStatus = CANCELSTATUS_CANCELLING; |
Sander Alewijnse | 30bc13c | 2014-11-03 12:21:30 +0000 | [diff] [blame] | 248 | Intent intent = new Intent(ProfileOwnerProvisioningActivity.this, |
| 249 | ProfileOwnerProvisioningService.class); |
Sander Alewijnse | 606780d | 2014-10-29 10:51:39 +0000 | [diff] [blame] | 250 | intent.setAction(ACTION_CANCEL_PROVISIONING); |
| 251 | startService(intent); |
| 252 | showCancelProgressDialog(); |
| 253 | } |
| 254 | |
| 255 | /** |
Sander Alewijnse | 606780d | 2014-10-29 10:51:39 +0000 | [diff] [blame] | 256 | * Notify the mdm that provisioning has completed. When the mdm has received the intent, stop |
Sander Alewijnse | 30bc13c | 2014-11-03 12:21:30 +0000 | [diff] [blame] | 257 | * the service and notify the {@link ProfileOwnerProvisioningActivity} so that it can finish itself. |
Sander Alewijnse | 606780d | 2014-10-29 10:51:39 +0000 | [diff] [blame] | 258 | */ |
| 259 | private void onProvisioningSuccess(Intent pendingSuccessIntent, int userId, int serialNumber) { |
| 260 | mCancelStatus = CANCELSTATUS_FINALIZING; |
| 261 | Settings.Secure.putIntForUser(getContentResolver(), Settings.Secure.USER_SETUP_COMPLETE, |
| 262 | 1 /* true- > setup complete */, userId); |
| 263 | |
| 264 | UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE); |
| 265 | UserHandle userHandle = userManager.getUserForSerialNumber(serialNumber); |
| 266 | |
| 267 | // Use an ordered broadcast, so that we only finish when the mdm has received it. |
| 268 | // Avoids a lag in the transition between provisioning and the mdm. |
| 269 | BroadcastReceiver mdmReceivedSuccessReceiver = new BroadcastReceiver() { |
| 270 | @Override |
| 271 | public void onReceive(Context context, Intent intent) { |
| 272 | ProvisionLogger.logd("ACTION_PROFILE_PROVISIONING_COMPLETE broadcast received by" |
| 273 | + " mdm"); |
Sander Alewijnse | 30bc13c | 2014-11-03 12:21:30 +0000 | [diff] [blame] | 274 | ProfileOwnerProvisioningActivity.this.setResult(Activity.RESULT_OK); |
Alexandra Gherghina | 902c46f | 2014-11-20 16:29:29 +0000 | [diff] [blame] | 275 | |
| 276 | // Now cleanup the primary profile if necessary |
| 277 | if (getIntent().hasExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE)) { |
| 278 | ProvisionLogger.logd("Cleaning up account from the primary user."); |
| 279 | final Account account = (Account) getIntent().getParcelableExtra( |
| 280 | EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE); |
| 281 | new AsyncTask<Void, Void, Void>() { |
| 282 | @Override |
| 283 | protected Void doInBackground(Void... params) { |
| 284 | removeAccount(account); |
| 285 | return null; |
| 286 | } |
| 287 | }.execute(); |
| 288 | } |
| 289 | |
Sander Alewijnse | 30bc13c | 2014-11-03 12:21:30 +0000 | [diff] [blame] | 290 | ProfileOwnerProvisioningActivity.this.finish(); |
| 291 | stopService(new Intent(ProfileOwnerProvisioningActivity.this, |
| 292 | ProfileOwnerProvisioningService.class)); |
Sander Alewijnse | 606780d | 2014-10-29 10:51:39 +0000 | [diff] [blame] | 293 | } |
| 294 | }; |
| 295 | |
| 296 | sendOrderedBroadcastAsUser(pendingSuccessIntent, userHandle, null, |
| 297 | mdmReceivedSuccessReceiver, null, Activity.RESULT_OK, null, null); |
| 298 | ProvisionLogger.logd("Provisioning complete broadcast has been sent to user " |
| 299 | + userHandle.getIdentifier()); |
| 300 | } |
| 301 | |
Alexandra Gherghina | 902c46f | 2014-11-20 16:29:29 +0000 | [diff] [blame] | 302 | private void removeAccount(Account account) { |
| 303 | try { |
Alexandra Gherghina | 88a1a70 | 2014-12-04 09:50:46 +0000 | [diff] [blame] | 304 | AccountManagerFuture<Bundle> bundle = mAccountManager.removeAccount(account, |
| 305 | this, null /* callback */, null /* handler */); |
| 306 | if (bundle.getResult().getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)) { |
Alexandra Gherghina | 902c46f | 2014-11-20 16:29:29 +0000 | [diff] [blame] | 307 | ProvisionLogger.logw("Account removed from the primary user."); |
| 308 | } else { |
| 309 | ProvisionLogger.logw("Could not remove account from the primary user."); |
| 310 | } |
| 311 | } catch (OperationCanceledException | AuthenticatorException | IOException e) { |
| 312 | ProvisionLogger.logw("Exception removing account from the primary user.", e); |
| 313 | } |
| 314 | } |
Sander Alewijnse | 606780d | 2014-10-29 10:51:39 +0000 | [diff] [blame] | 315 | |
| 316 | @Override |
| 317 | protected void onSaveInstanceState(Bundle outState) { |
| 318 | outState.putInt(KEY_CANCELSTATUS, mCancelStatus); |
| 319 | outState.putParcelable(KEY_PENDING_INTENT, mPendingProvisioningResult); |
| 320 | } |
| 321 | |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 322 | @Override |
Sander Alewijnse | 3dc234a | 2014-11-27 18:30:11 +0000 | [diff] [blame] | 323 | public void onPause() { |
Jessica Hummel | 81fe104 | 2014-06-23 17:10:38 +0100 | [diff] [blame] | 324 | LocalBroadcastManager.getInstance(this).unregisterReceiver(mServiceMessageReceiver); |
Sander Alewijnse | 3dc234a | 2014-11-27 18:30:11 +0000 | [diff] [blame] | 325 | super.onPause(); |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 326 | } |
Jessica Hummel | 8119e12 | 2014-01-10 12:13:49 +0000 | [diff] [blame] | 327 | } |
Jessica Hummel | c4d5d2b | 2014-02-27 15:37:01 +0000 | [diff] [blame] | 328 | |