Home | History | Annotate | Download | only in preferences
      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  */
     16 package com.android.emergency.preferences;
     17 
     18 import android.content.Context;
     19 import android.os.UserManager;
     20 import android.support.annotation.Nullable;
     21 import android.text.TextUtils;
     22 import android.util.AttributeSet;
     23 import android.widget.ArrayAdapter;
     24 
     25 import com.android.emergency.ReloadablePreferenceInterface;
     26 import com.android.internal.annotations.VisibleForTesting;
     27 
     28 /**
     29  * {@link AutoCompleteEditTextPreference} that prepopulates the edit text view with the name of the
     30  * user provided in settings.
     31  */
     32 public class NameAutoCompletePreference extends AutoCompleteEditTextPreference implements
     33         ReloadablePreferenceInterface {
     34     private static final String[] EMPTY_STRING_ARRAY = new String[] {};
     35 
     36     private final SuggestionProvider mSuggestionProvider;
     37 
     38     public NameAutoCompletePreference(Context context, AttributeSet attrs) {
     39         this(context, attrs, new SuggestionProvider() {
     40             private final UserManager mUserManager =
     41                     (UserManager) context.getSystemService(Context.USER_SERVICE);
     42 
     43             @Override
     44             public boolean hasNameToSuggest() {
     45                 return mUserManager.isUserNameSet();
     46             }
     47 
     48             @Override
     49             public String getNameSuggestion() {
     50                 if (!hasNameToSuggest()) {
     51                     return null;
     52                 }
     53                 return mUserManager.getUserName();
     54             }
     55         });
     56     }
     57 
     58     @VisibleForTesting
     59     public NameAutoCompletePreference(Context context, AttributeSet attrs,
     60             SuggestionProvider suggestionProvider) {
     61         super(context, attrs);
     62         mSuggestionProvider = suggestionProvider;
     63         getAutoCompleteTextView().setAdapter(createAdapter());
     64     }
     65 
     66     @VisibleForTesting
     67     public String[] createAutocompleteSuggestions() {
     68         if (!mSuggestionProvider.hasNameToSuggest()) {
     69             return EMPTY_STRING_ARRAY;
     70         }
     71         return new String[] {mSuggestionProvider.getNameSuggestion()};
     72     }
     73 
     74     private ArrayAdapter createAdapter() {
     75         UserManager userManager =
     76                 (UserManager) getContext().getSystemService(Context.USER_SERVICE);
     77         String[] autocompleteSuggestions = createAutocompleteSuggestions();
     78         return new ArrayAdapter<String>(getContext(),
     79                 android.R.layout.simple_dropdown_item_1line, autocompleteSuggestions);
     80     }
     81 
     82 
     83     @Override
     84     public void reloadFromPreference() {
     85         setText(getPersistedString(""));
     86     }
     87 
     88     @Override
     89     public boolean isNotSet() {
     90         return TextUtils.isEmpty(getText());
     91     }
     92 
     93     @Override
     94     public CharSequence getSummary() {
     95         String text = getText();
     96         return TextUtils.isEmpty(text) ? super.getSummary() : text;
     97     }
     98 
     99     /**
    100      * Interface for suggesting a name.
    101      */
    102     public interface SuggestionProvider {
    103         /** @return whether this class has a name to suggest. */
    104         boolean hasNameToSuggest();
    105 
    106         /**
    107          * Gets a suggested name.
    108          * @return a suggest name, or {@code null} if there is no name to suggest.
    109          */
    110         @Nullable
    111         String getNameSuggestion();
    112     }
    113 }
    114