1 /*
2  * Copyright 2018, 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 android.app.Activity;
20 import android.app.Service;
21 import android.content.BroadcastReceiver;
22 import android.content.Intent;
23 import android.os.IBinder;
24 import android.os.UserHandle;
25 
26 import com.android.managedprovisioning.common.ProvisionLogger;
27 import com.android.managedprovisioning.common.Utils;
28 import com.android.managedprovisioning.finalization.DpcReceivedSuccessReceiver.Callback;
29 import com.android.managedprovisioning.model.ProvisioningParams;
30 
31 /**
32  * A {@link Service} which sends the
33  * {@link android.app.admin.DeviceAdminReceiver.ACTION_PROFILE_PROVISIONING_COMPLETE} broadcast to
34  * the DPC. It keeps the ManagedProvisioning process alive while the user is on the DPC screen.
35  */
36 public class SendDpcBroadcastService extends Service implements Callback {
37 
38     static String EXTRA_PROVISIONING_PARAMS =
39             "com.android.managedprovisioning.PROVISIONING_PARAMS";
40 
41     @Override
onStartCommand(Intent intent, int flags, int startId)42     public int onStartCommand(Intent intent, int flags, int startId) {
43         ProvisioningParams params = intent.getParcelableExtra(EXTRA_PROVISIONING_PARAMS);
44         Utils utils = new Utils();
45         ProvisioningIntentProvider helper = new ProvisioningIntentProvider();
46         UserHandle managedProfileUserHandle = utils.getManagedProfile(getApplicationContext());
47         int managedProfileUserIdentifier = managedProfileUserHandle.getIdentifier();
48         Intent completeIntent =
49                 helper.createProvisioningCompleteIntent(params,
50                         managedProfileUserIdentifier, utils, getApplicationContext());
51         // Use an ordered broadcast, so that we only finish when the DPC has received it.
52         // Avoids a lag in the transition between provisioning and the DPC.
53         BroadcastReceiver dpcReceivedSuccessReceiver =
54                 new DpcReceivedSuccessReceiver(params.accountToMigrate,
55                         params.keepAccountMigrated, managedProfileUserHandle,
56                         params.inferDeviceAdminPackageName(), this,
57                         utils.isAdminIntegratedFlow(params));
58         sendOrderedBroadcastAsUser(completeIntent, managedProfileUserHandle, null,
59                 dpcReceivedSuccessReceiver, null, Activity.RESULT_OK, null, null);
60         ProvisionLogger.logd("Provisioning complete broadcast has been sent to user "
61                 + managedProfileUserIdentifier);
62 
63         helper.maybeLaunchDpc(
64                 params, managedProfileUserIdentifier, utils, getApplicationContext());
65 
66         return START_STICKY;
67     }
68 
69     @Override
onBind(Intent intent)70     public IBinder onBind(Intent intent) {
71         return null;
72     }
73 
74     @Override
cleanup()75     public void cleanup() {
76         stopSelf();
77     }
78 }
79