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 { 32 public NameAutoCompletePreference(Context context, AttributeSet attrs) { 33 super(context, attrs); 34 getAutoCompleteTextView().setAdapter(createAdapter()); 35 } 36 37 private ArrayAdapter createAdapter() { 38 UserManager userManager = 39 (UserManager) getContext().getSystemService(Context.USER_SERVICE); 40 String[] autocompleteSuggestions = {userManager.getUserName()}; 41 return new ArrayAdapter<String>(getContext(), 42 android.R.layout.simple_dropdown_item_1line, autocompleteSuggestions); 43 } 44 45 46 @Override 47 public void reloadFromPreference() { 48 setText(getPersistedString("")); 49 } 50 51 @Override 52 public boolean isNotSet() { 53 return TextUtils.isEmpty(getText()); 54 } 55 56 @Override 57 public CharSequence getSummary() { 58 String text = getText(); 59 return TextUtils.isEmpty(text) ? super.getSummary() : text; 60 } 61 } 62