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 package com.android.managedprovisioning.preprovisioning.consent;
17 
18 import static java.util.Objects.requireNonNull;
19 
20 import android.annotation.DrawableRes;
21 import android.app.Activity;
22 import android.view.View;
23 import android.widget.ImageView;
24 import android.widget.TextView;
25 
26 import com.android.managedprovisioning.R;
27 import com.android.managedprovisioning.common.CarSetupWizardLayoutHelper;
28 import com.android.managedprovisioning.common.ProvisionLogger;
29 import com.android.managedprovisioning.common.TouchTargetEnforcer;
30 import com.android.managedprovisioning.common.Utils;
31 import com.android.managedprovisioning.preprovisioning.PreProvisioningActivityBridgeCallbacks;
32 import com.android.managedprovisioning.preprovisioning.PreProvisioningActivityController.UiParams;
33 import com.android.server.utils.Slogf;
34 
35 /**
36  * Implements functionality for the consent screen.
37  */
38 public final class CarConsentUiHelper implements ConsentUiHelper {
39     private static final String TAG = CarConsentUiHelper.class.getSimpleName();
40     private final Activity mActivity;
41     private final TouchTargetEnforcer mTouchTargetEnforcer;
42     private final ConsentUiHelperCallback mCallback;
43     private final Utils mUtils;
44     private final PreProvisioningActivityBridgeCallbacks mBridgeCallbacks;
45 
CarConsentUiHelper(Activity activity, ConsentUiHelperCallback callback, Utils utils, PreProvisioningActivityBridgeCallbacks bridgeCallbacks)46     public CarConsentUiHelper(Activity activity, ConsentUiHelperCallback callback, Utils utils,
47                 PreProvisioningActivityBridgeCallbacks bridgeCallbacks) {
48         Slogf.d(TAG, "created new instance..");
49         mActivity = requireNonNull(activity);
50         mCallback = requireNonNull(callback);
51         mTouchTargetEnforcer =
52                 new TouchTargetEnforcer(activity.getResources().getDisplayMetrics().density);
53         mUtils = requireNonNull(utils);
54         mBridgeCallbacks = requireNonNull(bridgeCallbacks);
55     }
56 
57     @Override
onStart()58     public void onStart() {
59         // Do nothing.
60         Slogf.d(TAG, "onStart..");
61     }
62 
63     @Override
onStop()64     public void onStop() {
65         // Do nothing.
66         Slogf.d(TAG, "onStop..");
67     }
68 
69     @Override
initiateUi(UiParams uiParams)70     public void initiateUi(UiParams uiParams) {
71         int titleResId = 0;
72         int headerResId = 0;
73         int subLayoutId = R.layout.preprovision_layout;
74 
75         if (mUtils.isProfileOwnerAction(uiParams.provisioningAction)) {
76             titleResId = R.string.setup_profile;
77             headerResId = R.string.work_profile_provisioning_accept_header_post_suw;
78         } else if (mUtils.isDeviceOwnerAction(uiParams.provisioningAction)) {
79             titleResId = R.string.setup_device;
80             headerResId = R.string.fully_managed_device_provisioning_accept_header;
81         }
82         mCallback.onInitiateUi(
83                 CarSetupWizardLayoutHelper.MAIN_LAYOUT_RES_ID,
84                 headerResId);
85         mActivity.setTitle(titleResId);
86 
87         // Set the base layout
88         CarSetupWizardLayoutHelper layoutHelper = new CarSetupWizardLayoutHelper(mActivity);
89         layoutHelper.setBaseLayout(subLayoutId, /* isDoubleColumnAllowed= */ true);
90         layoutHelper.setHeaderText(headerResId, R.string.view_terms);
91         layoutHelper.setupPrimaryToolbarButton(R.string.accept_and_continue,
92                 v -> onTermsAccepted());
93 
94         // Set preprovisioning screen specific resources
95         setupImageView(R.drawable.ic_car_cockpit);
96         setupViewTermsButton();
97     }
98 
setupImageView(@rawableRes int imageResId)99     private void setupImageView(@DrawableRes int imageResId) {
100         ImageView illustration = mActivity.findViewById(R.id.illustration);
101         illustration.setImageResource(imageResId);
102     }
103 
onTermsAccepted()104     private void onTermsAccepted() {
105         ProvisionLogger.logi("Next button (next_button) is clicked.");
106         mBridgeCallbacks.onTermsAccepted();
107     }
108 
setupViewTermsButton()109     private void setupViewTermsButton() {
110         TextView description = mActivity.findViewById(R.id.description);
111 
112         description.setTextColor(mUtils.getAccentColor(mActivity));
113         description.setOnClickListener(v -> mBridgeCallbacks.onTermsButtonClicked());
114         mTouchTargetEnforcer.enforce(description, (View) description.getParent());
115     }
116 }
117