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.Context;
23 import android.content.Intent;
24 import android.os.IBinder;
25 import android.os.UserHandle;
26 
27 import com.android.managedprovisioning.analytics.MetricsWriterFactory;
28 import com.android.managedprovisioning.analytics.ProvisioningAnalyticsTracker;
29 import com.android.managedprovisioning.common.ManagedProvisioningSharedPreferences;
30 import com.android.managedprovisioning.common.PolicyComplianceUtils;
31 import com.android.managedprovisioning.common.ProvisionLogger;
32 import com.android.managedprovisioning.common.SettingsFacade;
33 import com.android.managedprovisioning.common.Utils;
34 import com.android.managedprovisioning.finalization.DpcReceivedSuccessReceiver.Callback;
35 import com.android.managedprovisioning.model.ProvisioningParams;
36 
37 /**
38  * A {@link Service} which sends the
39  * {@link android.app.admin.DeviceAdminReceiver.ACTION_PROFILE_PROVISIONING_COMPLETE} broadcast to
40  * the DPC. It keeps the ManagedProvisioning process alive while the user is on the DPC screen.
41  */
42 public class SendDpcBroadcastService extends Service implements Callback {
43 
44     public static String EXTRA_PROVISIONING_PARAMS =
45             "com.android.managedprovisioning.PROVISIONING_PARAMS";
46     private SettingsFacade mSettingsFacade = new SettingsFacade();
47 
48     @Override
onStartCommand(Intent intent, int flags, int startId)49     public int onStartCommand(Intent intent, int flags, int startId) {
50         final Context context = getApplicationContext();
51         ProvisioningParams params = intent.getParcelableExtra(EXTRA_PROVISIONING_PARAMS);
52         Utils utils = new Utils();
53         ProvisioningIntentProvider helper = new ProvisioningIntentProvider();
54         UserHandle managedProfileUserHandle = utils.getManagedProfile(context);
55         if (params.flowType == ProvisioningParams.FLOW_TYPE_ADMIN_INTEGRATED) {
56             new PrimaryProfileFinalizationHelper(params.accountToMigrate, managedProfileUserHandle,
57                     params.inferDeviceAdminPackageName())
58                 .finalizeProvisioningInPrimaryProfile(/* context */ this, /* callback */ this);
59         } else {
60             sendDpcReceivedSuccessReceiver(
61                     context, params, utils, helper, managedProfileUserHandle);
62         }
63 
64         maybeLaunchDpc(context, params, utils, helper, managedProfileUserHandle);
65 
66         return START_STICKY;
67     }
68 
maybeLaunchDpc(Context context, ProvisioningParams params, Utils utils, ProvisioningIntentProvider helper, UserHandle managedProfileUserHandle)69     private void maybeLaunchDpc(Context context, ProvisioningParams params, Utils utils,
70             ProvisioningIntentProvider helper, UserHandle managedProfileUserHandle) {
71         final ProvisioningAnalyticsTracker provisioningAnalyticsTracker =
72                 new ProvisioningAnalyticsTracker(
73                         MetricsWriterFactory.getMetricsWriter(context, mSettingsFacade),
74                         new ManagedProvisioningSharedPreferences(context));
75 
76         PolicyComplianceUtils policyComplianceUtils = new PolicyComplianceUtils();
77         helper.maybeLaunchDpc(
78                 params, managedProfileUserHandle.getIdentifier(),
79                 utils, context, provisioningAnalyticsTracker, policyComplianceUtils,
80                 mSettingsFacade);
81     }
82 
sendDpcReceivedSuccessReceiver(Context context, ProvisioningParams params, Utils utils, ProvisioningIntentProvider helper, UserHandle managedProfileUserHandle)83     private void sendDpcReceivedSuccessReceiver(Context context, ProvisioningParams params,
84             Utils utils, ProvisioningIntentProvider helper, UserHandle managedProfileUserHandle) {
85         Intent completeIntent =
86                 helper.createProvisioningCompleteIntent(params,
87                         managedProfileUserHandle.getIdentifier(), utils, context);
88         // Use an ordered broadcast, so that we only finish when the DPC has received it.
89         // Avoids a lag in the transition between provisioning and the DPC.
90         BroadcastReceiver dpcReceivedSuccessReceiver =
91                 new DpcReceivedSuccessReceiver(params.accountToMigrate,
92                         managedProfileUserHandle, params.inferDeviceAdminPackageName(),
93                         this);
94         sendOrderedBroadcastAsUser(completeIntent, managedProfileUserHandle, null,
95                 dpcReceivedSuccessReceiver, null, Activity.RESULT_OK, null, null);
96         ProvisionLogger.logd("Provisioning complete broadcast has been sent to user "
97                 + managedProfileUserHandle.getIdentifier());
98     }
99 
100     @Override
onBind(Intent intent)101     public IBinder onBind(Intent intent) {
102         return null;
103     }
104 
105     @Override
cleanup()106     public void cleanup() {
107         stopSelf();
108     }
109 }
110