1 /*
2  * Copyright (C) 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.settings.security;
18 
19 import static android.app.admin.DevicePolicyResources.Strings.Settings.WORK_PROFILE_UNIFY_LOCKS_DETAIL;
20 import static android.app.admin.DevicePolicyResources.Strings.Settings.WORK_PROFILE_UNIFY_LOCKS_NONCOMPLIANT;
21 
22 import android.app.Dialog;
23 import android.app.admin.DevicePolicyManager;
24 import android.app.settings.SettingsEnums;
25 import android.content.DialogInterface;
26 import android.os.Bundle;
27 
28 import androidx.appcompat.app.AlertDialog;
29 import androidx.fragment.app.FragmentManager;
30 
31 import com.android.settings.R;
32 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
33 
34 public class UnificationConfirmationDialog extends InstrumentedDialogFragment {
35 
36     static final String TAG_UNIFICATION_DIALOG = "unification_dialog";
37     private static final String EXTRA_COMPLIANT = "compliant";
38 
newInstance(boolean compliant)39     public static UnificationConfirmationDialog newInstance(boolean compliant) {
40         UnificationConfirmationDialog
41                 dialog = new UnificationConfirmationDialog();
42         Bundle args = new Bundle();
43         args.putBoolean(EXTRA_COMPLIANT, compliant);
44         dialog.setArguments(args);
45         return dialog;
46     }
47 
show(SecuritySettings host)48     public void show(SecuritySettings host) {
49         final FragmentManager manager = host.getChildFragmentManager();
50         if (manager.findFragmentByTag(TAG_UNIFICATION_DIALOG) == null) {
51             // Prevent opening multiple dialogs if tapped on button quickly
52             show(manager, TAG_UNIFICATION_DIALOG);
53         }
54     }
55 
56     @Override
onCreateDialog(Bundle savedInstanceState)57     public Dialog onCreateDialog(Bundle savedInstanceState) {
58         final SecuritySettings parentFragment = ((SecuritySettings) getParentFragment());
59         final boolean compliant = getArguments().getBoolean(EXTRA_COMPLIANT);
60 
61         String overrideMessageId = compliant ? WORK_PROFILE_UNIFY_LOCKS_DETAIL
62                 : WORK_PROFILE_UNIFY_LOCKS_NONCOMPLIANT;
63         int defaultMessageId = compliant ? R.string.lock_settings_profile_unification_dialog_body
64                 : R.string.lock_settings_profile_unification_dialog_uncompliant_body;
65 
66         return new AlertDialog.Builder(getActivity())
67                 .setTitle(R.string.lock_settings_profile_unification_dialog_title)
68                 .setMessage(getContext().getSystemService(DevicePolicyManager.class).getResources()
69                         .getString(overrideMessageId, () -> getString(defaultMessageId)))
70                 .setPositiveButton(
71                         compliant ? R.string.lock_settings_profile_unification_dialog_confirm
72                                 : R.string
73                                       .lock_settings_profile_unification_dialog_uncompliant_confirm,
74                         (dialog, whichButton) -> parentFragment.startUnification())
75                 .setNegativeButton(R.string.cancel, null)
76                 .create();
77     }
78 
79     @Override
onDismiss(DialogInterface dialog)80     public void onDismiss(DialogInterface dialog) {
81         super.onDismiss(dialog);
82         ((SecuritySettings) getParentFragment()).updateUnificationPreference();
83     }
84 
85     @Override
getMetricsCategory()86     public int getMetricsCategory() {
87         return SettingsEnums.DIALOG_UNIFICATION_CONFIRMATION;
88     }
89 }
90