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