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