1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.settings.development.autofill;
16 
17 import android.content.Context;
18 import android.provider.Settings;
19 import android.text.InputType;
20 import android.util.AttributeSet;
21 import android.util.Log;
22 import android.view.View;
23 import android.widget.EditText;
24 
25 import com.android.settings.Utils;
26 import com.android.settingslib.CustomEditTextPreferenceCompat;
27 
28 /**
29  * Base class for Autofill integer properties that are backed by
30  * {@link android.provider.Settings.Global}.
31  */
32 abstract class AbstractGlobalSettingsPreference extends CustomEditTextPreferenceCompat {
33 
34     private static final String TAG = "AbstractGlobalSettingsPreference";
35 
36     private final String mKey;
37     private final int mDefaultValue;
38 
39     private final AutofillDeveloperSettingsObserver mObserver;
40 
AbstractGlobalSettingsPreference(Context context, AttributeSet attrs, String key, int defaultValue)41     protected AbstractGlobalSettingsPreference(Context context, AttributeSet attrs,
42             String key, int defaultValue) {
43         super(context, attrs);
44 
45         mKey = key;
46         mDefaultValue = defaultValue;
47         mObserver = new AutofillDeveloperSettingsObserver(context, () -> updateSummary());
48     }
49 
50     @Override
onAttached()51     public void onAttached() {
52         super.onAttached();
53 
54         mObserver.register();
55         updateSummary();
56     }
57 
58     @Override
onDetached()59     public void onDetached() {
60         mObserver.unregister();
61 
62         super.onDetached();
63     }
64 
getCurrentValue()65     private String getCurrentValue() {
66         final int value = Settings.Global.getInt(getContext().getContentResolver(),
67                 mKey, mDefaultValue);
68 
69         return Integer.toString(value);
70     }
71 
updateSummary()72     private void updateSummary() {
73         setSummary(getCurrentValue());
74 
75     }
76 
77     @Override
onBindDialogView(View view)78     protected void onBindDialogView(View view) {
79         super.onBindDialogView(view);
80 
81         EditText editText = view.findViewById(android.R.id.edit);
82         if (editText != null) {
83             editText.setInputType(InputType.TYPE_CLASS_NUMBER);
84             editText.setText(getCurrentValue());
85             Utils.setEditTextCursorPosition(editText);
86         }
87     }
88 
89     @Override
onDialogClosed(boolean positiveResult)90     protected void onDialogClosed(boolean positiveResult) {
91         if (positiveResult) {
92             final String stringValue = getText();
93             int newValue = mDefaultValue;
94             try {
95                 newValue = Integer.parseInt(stringValue);
96             } catch (Exception e) {
97                 Log.e(TAG, "Error converting '" + stringValue + "' to integer. Using "
98                         + mDefaultValue + " instead");
99             }
100             Settings.Global.putInt(getContext().getContentResolver(), mKey, newValue);
101         }
102     }
103 }
104