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