1 /*
2  * Copyright 2015, 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 
19 import static android.app.admin.DevicePolicyManager.ACTION_MANAGED_PROFILE_PROVISIONED;
20 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE;
21 
22 import android.accounts.Account;
23 import android.content.BroadcastReceiver;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.os.AsyncTask;
27 
28 import com.android.managedprovisioning.common.Utils;
29 
30 /**
31  * Class that acts as the final receiver of the intent ACTION_PROFILE_PROVISIONING_COMPLETE
32  * which is broadcasted using
33  * {@link Context#sendOrderedBroadcast(Intent, String, BroadcastReceiver, android.os.Handler, int, String, Bundle)}
34  * after profile owner or device owner provisioning is completed.
35  */
36 public class MdmReceivedSuccessReceiver extends BroadcastReceiver {
37 
38     private final Account mMigratedAccount;
39     private final String mMdmPackageName;
40 
41     private final Utils mUtils = new Utils();
42 
MdmReceivedSuccessReceiver(Account migratedAccount, String mdmPackageName)43     public MdmReceivedSuccessReceiver(Account migratedAccount, String mdmPackageName) {
44         mMigratedAccount = migratedAccount;
45         mMdmPackageName = mdmPackageName;
46     }
47 
48     @Override
onReceive(Context context, Intent intent)49     public void onReceive(Context context, Intent intent) {
50         ProvisionLogger.logd("ACTION_PROFILE_PROVISIONING_COMPLETE broadcast received by mdm");
51 
52         final Intent primaryProfileSuccessIntent = new Intent(ACTION_MANAGED_PROFILE_PROVISIONED);
53         primaryProfileSuccessIntent.setPackage(mMdmPackageName);
54         primaryProfileSuccessIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES |
55                 Intent.FLAG_RECEIVER_FOREGROUND);
56 
57         // Now cleanup the primary profile if necessary
58         if (mMigratedAccount != null) {
59             primaryProfileSuccessIntent.putExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE,
60                     mMigratedAccount);
61             finishAccountMigration(context, primaryProfileSuccessIntent);
62             // Note that we currently do not check if account migration worked
63         } else {
64             context.sendBroadcast(primaryProfileSuccessIntent);
65         }
66     }
67 
finishAccountMigration(final Context context, final Intent primaryProfileSuccessIntent)68     private void finishAccountMigration(final Context context,
69             final Intent primaryProfileSuccessIntent) {
70         new AsyncTask<Void, Void, Void>() {
71             @Override
72             protected Void doInBackground(Void... params) {
73                 mUtils.removeAccount(context, mMigratedAccount);
74                 context.sendBroadcast(primaryProfileSuccessIntent);
75                 return null;
76             }
77         }.execute();
78     }
79 }
80