1 /*
2  * Copyright (C) 2020 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.sim.smartForwarding;
18 
19 import static com.android.settings.sim.smartForwarding.SmartForwardingUtils.getPhoneNumber;
20 
21 import android.app.settings.SettingsEnums;
22 import android.os.Bundle;
23 import android.text.InputType;
24 import android.widget.EditText;
25 
26 import androidx.annotation.NonNull;
27 import androidx.preference.EditTextPreference;
28 import androidx.preference.EditTextPreference.OnBindEditTextListener;
29 import androidx.preference.Preference;
30 import androidx.preference.PreferenceFragmentCompat;
31 
32 import com.android.settings.R;
33 import com.android.settingslib.core.instrumentation.Instrumentable;
34 
35 public class MDNHandlerHeaderFragment extends PreferenceFragmentCompat
36         implements Preference.OnPreferenceChangeListener, OnBindEditTextListener, Instrumentable {
37 
38     public static final String KEY_SLOT0_PHONE_NUMBER = "slot0_phone_number";
39     public static final String KEY_SLOT1_PHONE_NUMBER = "slot1_phone_number";
40 
MDNHandlerHeaderFragment()41     public MDNHandlerHeaderFragment() {}
42 
43     @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)44     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
45         setPreferencesFromResource(R.xml.smart_forwarding_mdn_handler_header, rootKey);
46 
47         EditTextPreference slot0EditText = findPreference(KEY_SLOT0_PHONE_NUMBER);
48         slot0EditText.setOnBindEditTextListener(this);
49         slot0EditText.setOnPreferenceChangeListener(this);
50         String slot0PhoneNumber = getPhoneNumber(getContext(), 0);
51         slot0EditText.setSummary(slot0PhoneNumber);
52         slot0EditText.setText(slot0PhoneNumber);
53 
54         EditTextPreference slot1EditText = findPreference(KEY_SLOT1_PHONE_NUMBER);
55         slot1EditText.setOnPreferenceChangeListener(this);
56         slot1EditText.setOnBindEditTextListener(this);
57         String slot1PhoneNumber = getPhoneNumber(getContext(), 1);
58         slot1EditText.setSummary(slot1PhoneNumber);
59         slot1EditText.setText(slot1PhoneNumber);
60     }
61 
62     @Override
onPreferenceChange(Preference preference, Object newValue)63     public boolean onPreferenceChange(Preference preference, Object newValue) {
64         preference.setSummary(newValue.toString());
65         return true;
66     }
67 
68     @Override
onBindEditText(@onNull EditText editText)69     public void onBindEditText(@NonNull EditText editText) {
70         editText.setInputType(InputType.TYPE_CLASS_PHONE);
71         editText.setSingleLine(true);
72     }
73 
74     @Override
getMetricsCategory()75     public int getMetricsCategory() {
76         return SettingsEnums.MOBILE_NETWORK;
77     }
78 }
79