1 /*
2  * Copyright 2019, 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.content.Intent;
20 
21 import com.android.managedprovisioning.analytics.MetricsWriterFactory;
22 import com.android.managedprovisioning.analytics.ProvisioningAnalyticsTracker;
23 import com.android.managedprovisioning.common.ManagedProvisioningSharedPreferences;
24 import com.android.managedprovisioning.common.ProvisionLogger;
25 import com.android.managedprovisioning.common.SettingsFacade;
26 import com.android.managedprovisioning.common.TransitionHelper;
27 
28 /**
29  * This class is used to start the Device Policy Controller app while the Setup Wizard is still
30  * running.  It listens for a result from any child activity that is started, and returns its own
31  * result code when all child activities have completed.
32  */
33 public class FinalizationInsideSuwActivity extends FinalizationActivityBase {
34 
35     private static final int DPC_SETUP_REQUEST_CODE = 1;
36     private static final int FINAL_SCREEN_REQUEST_CODE = 2;
37 
FinalizationInsideSuwActivity()38     public FinalizationInsideSuwActivity() {
39         super(new TransitionHelper());
40     }
41 
42     @Override
createFinalizationController()43     protected FinalizationController createFinalizationController() {
44         return new FinalizationController(this, new FinalizationInsideSuwControllerLogic(this));
45     }
46 
47     @Override
onFinalizationCompletedWithChildActivityLaunched()48     protected void onFinalizationCompletedWithChildActivityLaunched() {
49         // Don't commit finalized state until child activity completes.
50     }
51 
52     @Override
onActivityResult(int requestCode, int resultCode, Intent data)53     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
54         switch (requestCode) {
55             // We use FINAL_SCREEN_REQUEST_CODE in the admin-integrated flow, because the DPC has
56             // already run earlier, so we don't run it again during finalization.  Instead, we show
57             // a generic UI indicating that provisioning is done.  In other flows, we start up the
58             // DPC during finalization, using DPC_SETUP_REQUEST_CODE.
59             case DPC_SETUP_REQUEST_CODE:
60                 logDpcSetupCompleted(resultCode);
61                 // Fall through; code below this applies to both request codes
62             case FINAL_SCREEN_REQUEST_CODE:
63                 ProvisionLogger.logi("onActivityResult: received "
64                         + "requestCode = " + requestCode + ", resultCode = " + resultCode);
65 
66                 // Inside of SUW, if this activity returns RESULT_CANCELED, we will go back to
67                 // the previous SUW action.  Don't finalize the user provisioning state in that
68                 // case, so that we run finalization again when the user goes forward again in SUW.
69                 if (resultCode != RESULT_CANCELED) {
70                     getFinalizationController().commitFinalizedState();
71                 }
72                 setResult(resultCode);
73                 getTransitionHelper().finishActivity(this);
74                 break;
75             default:
76                 ProvisionLogger.logw("onActivityResult: Unknown request code: " + requestCode);
77                 break;
78         }
79     }
80 
logDpcSetupCompleted(int resultCode)81     private void logDpcSetupCompleted(int resultCode) {
82         final ProvisioningAnalyticsTracker provisioningAnalyticsTracker =
83                 new ProvisioningAnalyticsTracker(
84                         MetricsWriterFactory.getMetricsWriter(this, new SettingsFacade()),
85                         new ManagedProvisioningSharedPreferences(this));
86         provisioningAnalyticsTracker.logDpcSetupCompleted(this, resultCode);
87     }
88 }
89