1 /* 2 * Copyright (C) 2014 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.settings.notification; 18 19 import android.content.Context; 20 import android.preference.Preference; 21 import android.util.AttributeSet; 22 import android.view.View; 23 import android.view.ViewGroup; 24 import android.widget.AdapterView; 25 import android.widget.ArrayAdapter; 26 import android.widget.Spinner; 27 import android.widget.AdapterView.OnItemSelectedListener; 28 29 import java.util.ArrayList; 30 31 public class DropDownPreference extends Preference { 32 private final Context mContext; 33 private final ArrayAdapter<String> mAdapter; 34 private final Spinner mSpinner; 35 private final ArrayList<Object> mValues = new ArrayList<Object>(); 36 37 private Callback mCallback; 38 DropDownPreference(Context context)39 public DropDownPreference(Context context) { 40 this(context, null); 41 } 42 DropDownPreference(Context context, AttributeSet attrs)43 public DropDownPreference(Context context, AttributeSet attrs) { 44 super(context, attrs); 45 mContext = context; 46 mAdapter = new ArrayAdapter<String>(mContext, 47 android.R.layout.simple_spinner_dropdown_item); 48 49 mSpinner = new Spinner(mContext); 50 51 mSpinner.setVisibility(View.INVISIBLE); 52 mSpinner.setAdapter(mAdapter); 53 mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() { 54 @Override 55 public void onItemSelected(AdapterView<?> parent, View v, int position, long id) { 56 setSelectedItem(position); 57 } 58 59 @Override 60 public void onNothingSelected(AdapterView<?> parent) { 61 // noop 62 } 63 }); 64 setPersistent(false); 65 setOnPreferenceClickListener(new OnPreferenceClickListener() { 66 @Override 67 public boolean onPreferenceClick(Preference preference) { 68 mSpinner.performClick(); 69 return true; 70 } 71 }); 72 } 73 setDropDownWidth(int dimenResId)74 public void setDropDownWidth(int dimenResId) { 75 mSpinner.setDropDownWidth(mContext.getResources().getDimensionPixelSize(dimenResId)); 76 } 77 setCallback(Callback callback)78 public void setCallback(Callback callback) { 79 mCallback = callback; 80 } 81 setSelectedItem(int position)82 public void setSelectedItem(int position) { 83 final Object value = mValues.get(position); 84 if (mCallback != null && !mCallback.onItemSelected(position, value)) { 85 return; 86 } 87 mSpinner.setSelection(position); 88 setSummary(mAdapter.getItem(position)); 89 final boolean disableDependents = value == null; 90 notifyDependencyChange(disableDependents); 91 } 92 setSelectedValue(Object value)93 public void setSelectedValue(Object value) { 94 final int i = mValues.indexOf(value); 95 if (i > -1) { 96 setSelectedItem(i); 97 } 98 } 99 addItem(int captionResid, Object value)100 public void addItem(int captionResid, Object value) { 101 addItem(mContext.getResources().getString(captionResid), value); 102 } 103 addItem(String caption, Object value)104 public void addItem(String caption, Object value) { 105 mAdapter.add(caption); 106 mValues.add(value); 107 } 108 clearItems()109 public void clearItems(){ 110 mAdapter.clear(); 111 mValues.clear(); 112 } 113 114 @Override onBindView(View view)115 protected void onBindView(View view) { 116 super.onBindView(view); 117 if (view.equals(mSpinner.getParent())) return; 118 if (mSpinner.getParent() != null) { 119 ((ViewGroup)mSpinner.getParent()).removeView(mSpinner); 120 } 121 final ViewGroup vg = (ViewGroup)view; 122 vg.addView(mSpinner, 0); 123 final ViewGroup.LayoutParams lp = mSpinner.getLayoutParams(); 124 lp.width = 0; 125 mSpinner.setLayoutParams(lp); 126 } 127 128 public interface Callback { onItemSelected(int pos, Object value)129 boolean onItemSelected(int pos, Object value); 130 } 131 } 132