Home | History | Annotate | Download | only in appsettings
      1 /*
      2  * Copyright (C) 2015 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 
     17 package com.android.messaging.ui.appsettings;
     18 
     19 import android.content.Context;
     20 import android.preference.EditTextPreference;
     21 import android.support.v4.text.BidiFormatter;
     22 import android.support.v4.text.TextDirectionHeuristicsCompat;
     23 import android.text.InputType;
     24 import android.text.TextUtils;
     25 import android.util.AttributeSet;
     26 import android.view.View;
     27 
     28 import com.android.messaging.R;
     29 import com.android.messaging.util.PhoneUtils;
     30 
     31 /**
     32  * Preference that displays a phone number and allows editing via a dialog.
     33  * <p>
     34  * A default number can be assigned, which is shown in the preference view and
     35  * used to populate the dialog editor when the preference value is not set. If
     36  * the user sets the preference to a number equivalent to the default, the
     37  * underlying preference is cleared.
     38  */
     39 public class PhoneNumberPreference extends EditTextPreference {
     40 
     41     private String mDefaultPhoneNumber;
     42     private int mSubId;
     43 
     44     public PhoneNumberPreference(final Context context, final AttributeSet attrs) {
     45         super(context, attrs);
     46         mDefaultPhoneNumber = "";
     47     }
     48 
     49     public void setDefaultPhoneNumber(final String phoneNumber, final int subscriptionId) {
     50         mDefaultPhoneNumber = phoneNumber;
     51         mSubId = subscriptionId;
     52     }
     53 
     54     @Override
     55     protected void onBindView(final View view) {
     56         // Show the preference value if it's set, or the default number if not.
     57         // If we don't have a default, fall back to a static string (e.g. Unknown).
     58         String value = getText();
     59         if (TextUtils.isEmpty(value)) {
     60           value = mDefaultPhoneNumber;
     61         }
     62         final String displayValue = (!TextUtils.isEmpty(value))
     63                 ? PhoneUtils.get(mSubId).formatForDisplay(value)
     64                 : getContext().getString(R.string.unknown_phone_number_pref_display_value);
     65         final BidiFormatter bidiFormatter = BidiFormatter.getInstance();
     66         final String phoneNumber = bidiFormatter.unicodeWrap
     67                         (displayValue, TextDirectionHeuristicsCompat.LTR);
     68         // Set the value as the summary and let the superclass populate the views
     69         setSummary(phoneNumber);
     70         super.onBindView(view);
     71     }
     72 
     73     @Override
     74     protected void onBindDialogView(final View view) {
     75         super.onBindDialogView(view);
     76 
     77         final String value = getText();
     78 
     79         // If the preference is empty, populate the EditText with the default number instead.
     80         if (TextUtils.isEmpty(value) && !TextUtils.isEmpty(mDefaultPhoneNumber)) {
     81             final BidiFormatter bidiFormatter = BidiFormatter.getInstance();
     82             final String phoneNumber = bidiFormatter.unicodeWrap
     83                 (PhoneUtils.get(mSubId).getCanonicalBySystemLocale(mDefaultPhoneNumber),
     84                             TextDirectionHeuristicsCompat.LTR);
     85             getEditText().setText(phoneNumber);
     86         }
     87         getEditText().setInputType(InputType.TYPE_CLASS_PHONE);
     88     }
     89 
     90     @Override
     91     protected void onDialogClosed(final boolean positiveResult) {
     92         if (positiveResult && mDefaultPhoneNumber != null) {
     93             final String value = getEditText().getText().toString();
     94             final PhoneUtils phoneUtils = PhoneUtils.get(mSubId);
     95             final String phoneNumber = phoneUtils.getCanonicalBySystemLocale(value);
     96             final String defaultPhoneNumber = phoneUtils.getCanonicalBySystemLocale(
     97                     mDefaultPhoneNumber);
     98 
     99             // If the new value is the default, clear the preference.
    100             if (phoneNumber.equals(defaultPhoneNumber)) {
    101                 setText("");
    102                 return;
    103             }
    104         }
    105         super.onDialogClosed(positiveResult);
    106     }
    107 
    108     @Override
    109     public void setText(final String text) {
    110         super.setText(text);
    111 
    112         // EditTextPreference doesn't show the value on the preference view, but we do.
    113         // We thus need to force a rebind of the view when a new value is set.
    114         notifyChanged();
    115     }
    116 }
    117