blob: 7a6cfe53cb6904f00277eb2c368a889193a1d652 [file] [log] [blame]
mariagpuyol1d422e92016-02-17 18:29:08 -08001/*
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 */
mariagpuyol58748352016-03-09 15:36:28 -080016package com.android.emergency.preferences;
mariagpuyol1d422e92016-02-17 18:29:08 -080017
18import android.content.Context;
Juan Lang881b3de2017-08-17 15:28:35 -070019import android.content.res.TypedArray;
Juan Langee4540b2017-07-11 10:11:36 -070020import android.support.v7.preference.Preference;
Juan Lang2c0518a2017-05-10 17:00:32 -070021import android.support.v7.preference.PreferenceViewHolder;
mariagpuyol58748352016-03-09 15:36:28 -080022import android.text.TextUtils;
mariagpuyol1d422e92016-02-17 18:29:08 -080023import android.util.AttributeSet;
mariagpuyoldf6e53a2016-03-25 12:00:02 -070024import android.view.View;
Fan Zhangcbe03d92017-10-18 11:20:51 -070025import android.widget.EditText;
mariagpuyoldf6e53a2016-03-25 12:00:02 -070026import android.widget.TextView;
mariagpuyol1d422e92016-02-17 18:29:08 -080027
Juan Lang881b3de2017-08-17 15:28:35 -070028import com.android.emergency.R;
mariagpuyol58748352016-03-09 15:36:28 -080029import com.android.emergency.ReloadablePreferenceInterface;
Juan Lang2c0518a2017-05-10 17:00:32 -070030import com.android.settingslib.CustomEditTextPreference;
mariagpuyol58748352016-03-09 15:36:28 -080031
mariagpuyol1d422e92016-02-17 18:29:08 -080032/**
33 * Custom {@link EditTextPreference} that allows us to refresh and update the summary.
34 */
Juan Lang2c0518a2017-05-10 17:00:32 -070035public class EmergencyEditTextPreference extends CustomEditTextPreference
Juan Langee4540b2017-07-11 10:11:36 -070036 implements Preference.OnPreferenceChangeListener, ReloadablePreferenceInterface {
mariagpuyol1d422e92016-02-17 18:29:08 -080037
mariagpuyoldf6e53a2016-03-25 12:00:02 -070038 private static final int MAX_LINES = 50;
39
mariagpuyol1d422e92016-02-17 18:29:08 -080040 public EmergencyEditTextPreference(Context context, AttributeSet attrs) {
41 super(context, attrs);
Juan Lang881b3de2017-08-17 15:28:35 -070042 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();
mariagpuyol1d422e92016-02-17 18:29:08 -080048 }
49
50 @Override
51 public void reloadFromPreference() {
52 setText(getPersistedString(""));
53 }
54
55 @Override
mariagpuyol58748352016-03-09 15:36:28 -080056 public boolean isNotSet() {
57 return TextUtils.isEmpty(getText());
58 }
59
60 @Override
mariagpuyol1d422e92016-02-17 18:29:08 -080061 public CharSequence getSummary() {
62 String text = getText();
mariagpuyol58748352016-03-09 15:36:28 -080063 return TextUtils.isEmpty(text) ? super.getSummary() : text;
mariagpuyol1d422e92016-02-17 18:29:08 -080064 }
mariagpuyoldf6e53a2016-03-25 12:00:02 -070065
66 @Override
Juan Lang2c0518a2017-05-10 17:00:32 -070067 public void onBindViewHolder(PreferenceViewHolder holder) {
68 super.onBindViewHolder(holder);
69 final TextView summaryView = (TextView) holder.findViewById(
mariagpuyoldf6e53a2016-03-25 12:00:02 -070070 com.android.internal.R.id.summary);
71 summaryView.setMaxLines(MAX_LINES);
72 }
mariagpuyol3b2ce4c2017-02-02 11:53:00 -080073
74 @Override
Juan Langee4540b2017-07-11 10:11:36 -070075 public boolean onPreferenceChange(Preference preference, Object newValue) {
76 String text = (String) newValue;
77 setSummary(text);
78 return true;
79 }
80
81 @Override
mariagpuyol3b2ce4c2017-02-02 11:53:00 -080082 protected void onBindDialogView(View view) {
83 super.onBindDialogView(view);
Fan Zhangcbe03d92017-10-18 11:20:51 -070084 final EditText editText = view.findViewById(android.R.id.edit);
85 editText.setSelection(editText.getText().length());
mariagpuyol3b2ce4c2017-02-02 11:53:00 -080086 }
mariagpuyol1d422e92016-02-17 18:29:08 -080087}