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.car.settings.admin;
17 
18 import android.car.Car;
19 import android.car.admin.CarDevicePolicyManager;
20 import android.content.Intent;
21 import android.os.Bundle;
22 import android.provider.Settings;
23 import android.widget.Button;
24 
25 import androidx.fragment.app.FragmentActivity;
26 
27 import com.android.car.admin.ui.ManagedDeviceTextView;
28 import com.android.car.settings.R;
29 import com.android.car.settings.common.ConfirmationDialogFragment;
30 import com.android.car.settings.common.Logger;
31 import com.android.internal.annotations.VisibleForTesting;
32 import com.android.settingslib.core.lifecycle.HideNonSystemOverlayMixin;
33 
34 /**
35  * Shows a disclaimer dialog when a new user is added in a device that is managed by a device owner.
36  *
37  * <p>The dialog text will contain the message from
38  * {@code ManagedDeviceTextView.getManagedDeviceText}.
39  *
40  * <p>The dialog contains two buttons: one to acknowlege the disclaimer; the other to launch
41  * {@code Settings.ACTION_ENTERPRISE_PRIVACY_SETTINGS} for more details. Note: when
42  * {@code Settings.ACTION_ENTERPRISE_PRIVACY_SETTINGS} is closed, the same dialog will be shown.
43  *
44  * <p>Clicking anywhere outside the dialog will dimiss the dialog.
45  */
46 public final class NewUserDisclaimerActivity extends FragmentActivity {
47     @VisibleForTesting
48     static final Logger LOG = new Logger(NewUserDisclaimerActivity.class);
49     @VisibleForTesting
50     static final String DIALOG_TAG = "NewUserDisclaimerActivity.ConfirmationDialogFragment";
51     private static final int LEARN_MORE_RESULT_CODE = 1;
52 
53     private Car mCar;
54     private CarDevicePolicyManager mCarDevicePolicyManager;
55     private Button mAcceptButton;
56     private ConfirmationDialogFragment mConfirmationDialog;
57     private boolean mLearnMoreLaunched;
58 
59     @Override
onCreate(Bundle savedInstanceState)60     protected void onCreate(Bundle savedInstanceState) {
61         super.onCreate(savedInstanceState);
62         getLifecycle().addObserver(new HideNonSystemOverlayMixin(this));
63         getCarDevicePolicyManager();
64         setupConfirmationDialog();
65     }
66 
67     @Override
onResume()68     protected void onResume() {
69         super.onResume();
70         showConfirmationDialog();
71         getCarDevicePolicyManager().setUserDisclaimerShown(getUser());
72     }
73 
74     @Override
onActivityResult(int requestCode, int resultCode, Intent data)75     public void onActivityResult(int requestCode, int resultCode, Intent data) {
76         super.onActivityResult(requestCode, resultCode, data);
77         if (requestCode != LEARN_MORE_RESULT_CODE) {
78             LOG.w("onActivityResult(), invalid request code: " + requestCode);
79             return;
80         }
81         mLearnMoreLaunched = false;
82     }
83 
setupConfirmationDialog()84     private ConfirmationDialogFragment setupConfirmationDialog() {
85         String managedByOrganizationText = ManagedDeviceTextView.getManagedDeviceText(this)
86                 .toString();
87         String managedProfileText = getResources().getString(
88                 R.string.new_user_managed_device_text);
89 
90         mConfirmationDialog = new ConfirmationDialogFragment.Builder(getApplicationContext())
91                 .setTitle(R.string.new_user_managed_device_title)
92                 .setMessage(managedByOrganizationText + System.lineSeparator()
93                         + System.lineSeparator() + managedProfileText)
94                 .setPositiveButton(R.string.new_user_managed_device_acceptance,
95                         arguments -> onAccept())
96                 .setNeutralButton(R.string.new_user_managed_device_learn_more,
97                         arguments -> onLearnMoreClicked())
98                 .setDismissListener((arguments, positiveResult) -> onDialogDimissed())
99                 .build();
100 
101         return mConfirmationDialog;
102     }
103 
showConfirmationDialog()104     private void showConfirmationDialog() {
105         if (mConfirmationDialog == null) {
106             setupConfirmationDialog();
107         }
108         mConfirmationDialog.show(getSupportFragmentManager(), DIALOG_TAG);
109     }
110 
onAccept()111     private void onAccept() {
112         LOG.d("user accepted");
113         getCarDevicePolicyManager().setUserDisclaimerAcknowledged(getUser());
114         setResult(RESULT_OK);
115         finish();
116     }
117 
onLearnMoreClicked()118     private void onLearnMoreClicked() {
119         mLearnMoreLaunched = true;
120         startActivityForResult(new Intent(Settings.ACTION_ENTERPRISE_PRIVACY_SETTINGS),
121                 LEARN_MORE_RESULT_CODE);
122     }
123 
onDialogDimissed()124     private void onDialogDimissed() {
125         if (mLearnMoreLaunched) {
126             return;
127         }
128         finish();
129     }
130 
getCarDevicePolicyManager()131     private CarDevicePolicyManager getCarDevicePolicyManager() {
132         LOG.d("getCarDevicePolicyManager for user: " + getUser());
133         if (mCarDevicePolicyManager != null) {
134             return mCarDevicePolicyManager;
135         }
136         if (mCar == null) {
137             mCar = Car.createCar(this);
138         }
139         mCarDevicePolicyManager = (CarDevicePolicyManager) mCar.getCarManager(
140                 Car.CAR_DEVICE_POLICY_SERVICE);
141         return mCarDevicePolicyManager;
142     }
143 }
144