1 /*
2  * Copyright (C) 2021 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 java.util.Objects.requireNonNull;
20 
21 import android.annotation.Nullable;
22 import android.content.Context;
23 import android.content.Intent;
24 
25 import com.android.managedprovisioning.ManagedProvisioningScreens;
26 import com.android.managedprovisioning.ScreenManager;
27 import com.android.managedprovisioning.common.DeviceManagementRoleHolderHelper;
28 import com.android.managedprovisioning.common.SharedPreferences;
29 import com.google.android.setupcompat.util.WizardManagerHelper;
30 
31 /**
32  * A controller that contains business logic for the finalization forwarder.
33  *
34  * @see FinalizationForwarderActivity
35  */
36 public final class FinalizationForwarderController {
37 
38     private final DeviceManagementRoleHolderHelper mRoleHolderHelper;
39     private final Ui mUi;
40     private final SharedPreferences mSharedPreferences;
41     private final ScreenManager mScreenManager;
42 
43     interface Ui {
startRoleHolderFinalization()44         void startRoleHolderFinalization();
45 
startPlatformProvidedProvisioningFinalization()46         void startPlatformProvidedProvisioningFinalization();
47     }
48 
FinalizationForwarderController( DeviceManagementRoleHolderHelper roleHolderHelper, Ui ui, SharedPreferences sharedPreferences, ScreenManager screenManager)49     public FinalizationForwarderController(
50             DeviceManagementRoleHolderHelper roleHolderHelper,
51             Ui ui,
52             SharedPreferences sharedPreferences,
53             ScreenManager screenManager) {
54         mRoleHolderHelper = requireNonNull(roleHolderHelper);
55         mUi = requireNonNull(ui);
56         mSharedPreferences = requireNonNull(sharedPreferences);
57         mScreenManager = requireNonNull(screenManager);
58     }
59 
60     /**
61      * Returns a new {@link Intent} instance which resolves to the AOSP ManagedProvisioning
62      * finalization.
63      */
createPlatformProvidedProvisioningFinalizationIntent( Context context, @Nullable Intent parentActivityIntent)64     public Intent createPlatformProvidedProvisioningFinalizationIntent(
65             Context context, @Nullable Intent parentActivityIntent) {
66         requireNonNull(context);
67         Intent intent = new Intent(context, mScreenManager.getActivityClassForScreen(
68                 ManagedProvisioningScreens.FINALIZATION_INSIDE_SUW));
69         if (parentActivityIntent != null) {
70             WizardManagerHelper.copyWizardManagerExtras(parentActivityIntent, intent);
71         }
72         return intent;
73     }
74 
75     /**
76      * Returns a new {@link Intent} instance which resolves to the device management role holder
77      * finalization.
78      */
createRoleHolderFinalizationIntent( Context context, @Nullable Intent parentActivityIntent)79     public Intent createRoleHolderFinalizationIntent(
80             Context context, @Nullable Intent parentActivityIntent) {
81         requireNonNull(context);
82         return mRoleHolderHelper.createRoleHolderFinalizationIntent(parentActivityIntent);
83     }
84 
85     /**
86      * Starts the relevant {@link Ui} method depending on whether the provisioning finalization
87      * should be handled by the device management role holder or AOSP ManagedProvisioning.
88      */
forwardFinalization(Context context)89     public void forwardFinalization(Context context) {
90         requireNonNull(context);
91         if (mSharedPreferences.isProvisioningFlowDelegatedToRoleHolder()) {
92             mUi.startRoleHolderFinalization();
93         } else {
94             mUi.startPlatformProvidedProvisioningFinalization();
95         }
96     }
97 }
98