1 /*
2  * Copyright (C) 2022 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.inputmethod;
18 
19 import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
20 
21 import android.app.Activity;
22 import android.app.AlertDialog;
23 import android.app.Dialog;
24 import android.app.settings.SettingsEnums;
25 import android.hardware.input.InputManager;
26 import android.os.Bundle;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.view.Window;
30 import android.widget.Button;
31 
32 import androidx.fragment.app.DialogFragment;
33 
34 import com.android.settings.R;
35 import com.android.settings.overlay.FeatureFactory;
36 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
37 
38 public class ModifierKeysResetDialogFragment extends DialogFragment {
39 
40     private static final String MODIFIER_KEYS_CAPS_LOCK = "modifier_keys_caps_lock";
41     private static final String MODIFIER_KEYS_CTRL = "modifier_keys_ctrl";
42     private static final String MODIFIER_KEYS_META = "modifier_keys_meta";
43     private static final String MODIFIER_KEYS_ALT = "modifier_keys_alt";
44 
45     private MetricsFeatureProvider mMetricsFeatureProvider;
46 
47     private String[] mKeys = {
48             MODIFIER_KEYS_CAPS_LOCK,
49             MODIFIER_KEYS_CTRL,
50             MODIFIER_KEYS_META,
51             MODIFIER_KEYS_ALT};
52 
ModifierKeysResetDialogFragment()53     public ModifierKeysResetDialogFragment() {}
54 
55     @Override
onCreateDialog(Bundle savedInstanceState)56     public Dialog onCreateDialog(Bundle savedInstanceState) {
57         super.onCreateDialog(savedInstanceState);
58 
59         Activity activity = getActivity();
60         mMetricsFeatureProvider = FeatureFactory.getFeatureFactory().getMetricsFeatureProvider();
61         InputManager inputManager = activity.getSystemService(InputManager.class);
62         View dialoglayout =
63                 LayoutInflater.from(activity).inflate(R.layout.modifier_key_reset_dialog, null);
64         AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activity);
65         dialogBuilder.setView(dialoglayout);
66         AlertDialog modifierKeyResetDialog = dialogBuilder.create();
67 
68         Button restoreButton = dialoglayout.findViewById(R.id.modifier_key_reset_restore_button);
69         restoreButton.setOnClickListener(v -> {
70             mMetricsFeatureProvider.action(activity, SettingsEnums.ACTION_CLEAR_REMAPPINGS);
71             inputManager.clearAllModifierKeyRemappings();
72             dismiss();
73             activity.recreate();
74         });
75 
76         Button cancelButton = dialoglayout.findViewById(R.id.modifier_key_reset_cancel_button);
77         cancelButton.setOnClickListener(v -> {
78             dismiss();
79         });
80 
81         final Window window = modifierKeyResetDialog.getWindow();
82         window.setType(TYPE_SYSTEM_DIALOG);
83 
84         return modifierKeyResetDialog;
85     }
86 }
87