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.text.TextUtils; 21 import android.util.AttributeSet; 22 import android.widget.ArrayAdapter; 23 24 import com.android.emergency.ReloadablePreferenceInterface; 25 26 /** 27 * {@link AutoCompleteEditTextPreference} that prepopulates the edit text view with the name of the 28 * user provided in settings. 29 */ 30 public class NameAutoCompletePreference extends AutoCompleteEditTextPreference implements 31 ReloadablePreferenceInterface { NameAutoCompletePreference(Context context, AttributeSet attrs)32 public NameAutoCompletePreference(Context context, AttributeSet attrs) { 33 super(context, attrs); 34 getAutoCompleteTextView().setAdapter(createAdapter()); 35 } 36 createAdapter()37 private ArrayAdapter createAdapter() { 38 UserManager userManager = 39 (UserManager) getContext().getSystemService(Context.USER_SERVICE); 40 String[] autocompleteSuggestions = userManager.isUserNameSet() 41 ? new String[] {userManager.getUserName()} : new String[] {}; 42 return new ArrayAdapter<String>(getContext(), 43 android.R.layout.simple_dropdown_item_1line, autocompleteSuggestions); 44 } 45 46 47 @Override reloadFromPreference()48 public void reloadFromPreference() { 49 setText(getPersistedString("")); 50 } 51 52 @Override isNotSet()53 public boolean isNotSet() { 54 return TextUtils.isEmpty(getText()); 55 } 56 57 @Override getSummary()58 public CharSequence getSummary() { 59 String text = getText(); 60 return TextUtils.isEmpty(text) ? super.getSummary() : text; 61 } 62 } 63