Home | History | Annotate | Download | only in finalization
      1 /*
      2  * Copyright 2016, 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.finalization;
     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 import static com.android.internal.util.Preconditions.checkNotNull;
     22 
     23 import android.accounts.Account;
     24 import android.content.BroadcastReceiver;
     25 import android.content.Context;
     26 import android.content.Intent;
     27 import android.os.AsyncTask;
     28 import android.os.UserHandle;
     29 import android.support.annotation.Nullable;
     30 
     31 import com.android.internal.annotations.VisibleForTesting;
     32 import com.android.managedprovisioning.common.ProvisionLogger;
     33 import com.android.managedprovisioning.common.Utils;
     34 
     35 /**
     36  * Class that acts as the final receiver of the intent ACTION_PROFILE_PROVISIONING_COMPLETE
     37  * which is broadcasted using
     38  * {@link Context#sendOrderedBroadcast(Intent, String, BroadcastReceiver, android.os.Handler, int, String, android.os.Bundle)}
     39  * after profile owner or device owner provisioning is completed.
     40  */
     41 public class DpcReceivedSuccessReceiver extends BroadcastReceiver {
     42 
     43     private final Account mMigratedAccount;
     44     private final String mMdmPackageName;
     45     private final boolean mKeepAccountMigrated;
     46     private final Utils mUtils;
     47     private final UserHandle mManagedUserHandle;
     48 
     49     public DpcReceivedSuccessReceiver(@Nullable Account migratedAccount,
     50             boolean keepAccountMigrated, UserHandle managedUserHandle, String mdmPackageName) {
     51         this(migratedAccount, keepAccountMigrated, managedUserHandle, mdmPackageName, new Utils());
     52     }
     53 
     54     @VisibleForTesting
     55     DpcReceivedSuccessReceiver(Account migratedAccount, boolean keepAccountMigrated,
     56         UserHandle managedUserHandle, String mdmPackageName, Utils utils) {
     57         mMigratedAccount = migratedAccount;
     58         mKeepAccountMigrated = keepAccountMigrated;
     59         mMdmPackageName = checkNotNull(mdmPackageName);
     60         mManagedUserHandle = checkNotNull(managedUserHandle);
     61         mUtils = checkNotNull(utils);
     62     }
     63 
     64     @Override
     65     public void onReceive(Context context, Intent intent) {
     66         ProvisionLogger.logd("ACTION_PROFILE_PROVISIONING_COMPLETE broadcast received by mdm");
     67 
     68         final Intent primaryProfileSuccessIntent = new Intent(ACTION_MANAGED_PROFILE_PROVISIONED);
     69         primaryProfileSuccessIntent.setPackage(mMdmPackageName);
     70         primaryProfileSuccessIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES |
     71                 Intent.FLAG_RECEIVER_FOREGROUND);
     72         primaryProfileSuccessIntent.putExtra(Intent.EXTRA_USER, mManagedUserHandle);
     73 
     74         // Now cleanup the primary profile if necessary
     75         if (mMigratedAccount != null) {
     76             primaryProfileSuccessIntent.putExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE,
     77                     mMigratedAccount);
     78             finishAccountMigration(context, primaryProfileSuccessIntent);
     79             // Note that we currently do not check if account migration worked
     80         } else {
     81             context.sendBroadcast(primaryProfileSuccessIntent);
     82         }
     83     }
     84 
     85     private void finishAccountMigration(final Context context,
     86             final Intent primaryProfileSuccessIntent) {
     87         new AsyncTask<Void, Void, Void>() {
     88             @Override
     89             protected Void doInBackground(Void... params) {
     90                 if (!mKeepAccountMigrated) {
     91                     mUtils.removeAccount(context, mMigratedAccount);
     92                 }
     93                 context.sendBroadcast(primaryProfileSuccessIntent);
     94                 return null;
     95             }
     96         }.execute();
     97     }
     98 }
     99