• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 package com.android.emergency.preferences;
17 
18 import android.content.Context;
19 import android.content.res.TypedArray;
20 import androidx.preference.Preference;
21 import androidx.preference.PreferenceViewHolder;
22 import android.text.TextUtils;
23 import android.util.AttributeSet;
24 import android.view.View;
25 import android.widget.EditText;
26 import android.widget.TextView;
27 
28 import com.android.emergency.R;
29 import com.android.emergency.ReloadablePreferenceInterface;
30 import com.android.settingslib.CustomEditTextPreference;
31 
32 /**
33  * Custom {@link EditTextPreference} that allows us to refresh and update the summary.
34  */
35 public class EmergencyEditTextPreference extends CustomEditTextPreference
36         implements Preference.OnPreferenceChangeListener, ReloadablePreferenceInterface {
37 
38     private static final int MAX_LINES = 50;
39 
EmergencyEditTextPreference(Context context, AttributeSet attrs)40     public EmergencyEditTextPreference(Context context, AttributeSet attrs) {
41         super(context, attrs);
42         TypedArray a = context.obtainStyledAttributes(
43                 attrs, R.styleable.EmergencyEditTextPreference, 0, 0);
44         if (a.hasValue(R.styleable.EmergencyEditTextPreference_summary)) {
45             setSummary(a.getString(R.styleable.EmergencyEditTextPreference_summary));
46         }
47         a.recycle();
48     }
49 
50     @Override
reloadFromPreference()51     public void reloadFromPreference() {
52         setText(getPersistedString(""));
53     }
54 
55     @Override
isNotSet()56     public boolean isNotSet() {
57         return TextUtils.isEmpty(getText());
58     }
59 
60     @Override
getSummary()61     public CharSequence getSummary() {
62         String text = getText();
63         return TextUtils.isEmpty(text) ? super.getSummary() : text;
64     }
65 
66     @Override
onBindViewHolder(PreferenceViewHolder holder)67     public void onBindViewHolder(PreferenceViewHolder holder) {
68         super.onBindViewHolder(holder);
69         final TextView summaryView = (TextView) holder.findViewById(
70                 com.android.internal.R.id.summary);
71         summaryView.setMaxLines(MAX_LINES);
72     }
73 
74     @Override
onPreferenceChange(Preference preference, Object newValue)75     public boolean onPreferenceChange(Preference preference, Object newValue) {
76         String text = (String) newValue;
77         setSummary(text);
78         return true;
79     }
80 
81     @Override
onBindDialogView(View view)82     protected void onBindDialogView(View view) {
83         super.onBindDialogView(view);
84         final EditText editText = view.findViewById(android.R.id.edit);
85         editText.setSelection(editText.getText().length());
86     }
87 }
88