EmergencyEditTextPreference.java revision 2c0518a7dfd5b7cd36e88d9f2df064b1e756bd83
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.support.v7.preference.PreferenceViewHolder;
20import android.text.TextUtils;
21import android.util.AttributeSet;
22import android.view.View;
23import android.widget.TextView;
24
25import com.android.emergency.ReloadablePreferenceInterface;
26import com.android.settingslib.CustomEditTextPreference;
27
28/**
29 * Custom {@link EditTextPreference} that allows us to refresh and update the summary.
30 */
31public class EmergencyEditTextPreference extends CustomEditTextPreference
32        implements ReloadablePreferenceInterface {
33
34    private static final int MAX_LINES = 50;
35
36    public EmergencyEditTextPreference(Context context, AttributeSet attrs) {
37        super(context, attrs);
38    }
39
40    @Override
41    public void reloadFromPreference() {
42        setText(getPersistedString(""));
43    }
44
45    @Override
46    public boolean isNotSet() {
47        return TextUtils.isEmpty(getText());
48    }
49
50    @Override
51    public CharSequence getSummary() {
52        String text = getText();
53        return TextUtils.isEmpty(text) ? super.getSummary() : text;
54    }
55
56    @Override
57    public void onBindViewHolder(PreferenceViewHolder holder) {
58        super.onBindViewHolder(holder);
59        final TextView summaryView = (TextView) holder.findViewById(
60                com.android.internal.R.id.summary);
61        summaryView.setMaxLines(MAX_LINES);
62    }
63
64    @Override
65    protected void onBindDialogView(View view) {
66        super.onBindDialogView(view);
67        getEditText().setSelection(getEditText().getText().length());
68    }
69}
70