Home | History | Annotate | Download | only in settings
      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;
     18 
     19 import android.content.Context;
     20 import android.content.res.TypedArray;
     21 import android.preference.Preference;
     22 import android.util.AttributeSet;
     23 import android.view.View;
     24 import android.view.ViewGroup;
     25 import android.widget.AdapterView;
     26 import android.widget.AdapterView.OnItemSelectedListener;
     27 import android.widget.ArrayAdapter;
     28 import android.widget.Spinner;
     29 
     30 import java.util.ArrayList;
     31 
     32 public class DropDownPreference extends Preference {
     33     private final Context mContext;
     34     private final ArrayAdapter<String> mAdapter;
     35     private final Spinner mSpinner;
     36     private final ArrayList<Object> mValues = new ArrayList<Object>();
     37 
     38     private Callback mCallback;
     39     private int mSelectedPosition = -1;
     40 
     41     public DropDownPreference(Context context) {
     42         this(context, null);
     43     }
     44 
     45     public DropDownPreference(Context context, AttributeSet attrs) {
     46         super(context, attrs);
     47         mContext = context;
     48         mAdapter = new ArrayAdapter<String>(mContext,
     49                 android.R.layout.simple_spinner_dropdown_item);
     50 
     51         mSpinner = new Spinner(mContext);
     52 
     53         mSpinner.setVisibility(View.INVISIBLE);
     54         mSpinner.setAdapter(mAdapter);
     55         mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
     56             @Override
     57             public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
     58                 setSelectedItem(position, true);
     59             }
     60 
     61             @Override
     62             public void onNothingSelected(AdapterView<?> parent) {
     63                 // noop
     64             }
     65         });
     66         setPersistent(false);
     67         setOnPreferenceClickListener(new OnPreferenceClickListener() {
     68             @Override
     69             public boolean onPreferenceClick(Preference preference) {
     70                 mSpinner.performClick();
     71                 return true;
     72             }
     73         });
     74 
     75         // Support XML specification like ListPreferences do.
     76         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DropDownPreference);
     77         CharSequence[] entries = a.getTextArray(R.styleable.DropDownPreference_android_entries);
     78         CharSequence[] values = a.getTextArray(R.styleable.DropDownPreference_android_entryValues);
     79         if (entries != null && values != null) {
     80             for (int i= 0; i < entries.length; i++) {
     81                 addItem(entries[i].toString(), values[i]);
     82             }
     83         }
     84     }
     85 
     86     public void setDropDownWidth(int dimenResId) {
     87         mSpinner.setDropDownWidth(mContext.getResources().getDimensionPixelSize(dimenResId));
     88     }
     89 
     90     public void setCallback(Callback callback) {
     91         mCallback = callback;
     92     }
     93 
     94     public void setSelectedItem(int position) {
     95         setSelectedItem(position, false);
     96     }
     97 
     98     public void setSelectedItem(int position, boolean fromSpinner) {
     99         if (fromSpinner && position == mSelectedPosition) {
    100             return;
    101         }
    102         final Object value = mValues.get(position);
    103         if (mCallback != null && !mCallback.onItemSelected(position, value)) {
    104             return;
    105         }
    106         mSpinner.setSelection(position);
    107         mSelectedPosition = mSpinner.getSelectedItemPosition();
    108         setSummary(mAdapter.getItem(position));
    109         final boolean disableDependents = value == null;
    110         notifyDependencyChange(disableDependents);
    111     }
    112 
    113     public void setSelectedValue(Object value) {
    114         final int i = mValues.indexOf(value);
    115         if (i > -1) {
    116             setSelectedItem(i);
    117         }
    118     }
    119 
    120     public void addItem(int captionResid, Object value) {
    121         addItem(mContext.getResources().getString(captionResid), value);
    122     }
    123 
    124     public void addItem(String caption, Object value) {
    125         mAdapter.add(caption);
    126         mValues.add(value);
    127     }
    128 
    129     public int getItemCount() {
    130         return mAdapter.getCount();
    131     }
    132 
    133     public void clearItems(){
    134         mAdapter.clear();
    135         mValues.clear();
    136     }
    137 
    138     @Override
    139     protected void onBindView(View view) {
    140         super.onBindView(view);
    141         if (view.equals(mSpinner.getParent())) return;
    142         if (mSpinner.getParent() != null) {
    143             ((ViewGroup)mSpinner.getParent()).removeView(mSpinner);
    144         }
    145         final ViewGroup vg = (ViewGroup)view;
    146         vg.addView(mSpinner, 0);
    147         final ViewGroup.LayoutParams lp = mSpinner.getLayoutParams();
    148         lp.width = 0;
    149         mSpinner.setLayoutParams(lp);
    150     }
    151 
    152     public interface Callback {
    153         boolean onItemSelected(int pos, Object value);
    154     }
    155 }
    156