Home | History | Annotate | Download | only in datausage
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
      5  * except in compliance with the License. You may obtain a copy of the License at
      6  *
      7  *      http://www.apache.org/licenses/LICENSE-2.0
      8  *
      9  * Unless required by applicable law or agreed to in writing, software distributed under the
     10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     11  * KIND, either express or implied. See the License for the specific language governing
     12  * permissions and limitations under the License.
     13  */
     14 
     15 package com.android.settings.datausage;
     16 
     17 import android.content.Context;
     18 import android.support.v7.preference.Preference;
     19 import android.support.v7.preference.PreferenceViewHolder;
     20 import android.util.AttributeSet;
     21 import android.view.View;
     22 import android.widget.AdapterView;
     23 import android.widget.Spinner;
     24 import com.android.settings.R;
     25 
     26 public class SpinnerPreference extends Preference implements CycleAdapter.SpinnerInterface {
     27 
     28     private CycleAdapter mAdapter;
     29     private AdapterView.OnItemSelectedListener mListener;
     30     private Object mCurrentObject;
     31     private int mPosition;
     32 
     33     public SpinnerPreference(Context context, AttributeSet attrs) {
     34         super(context, attrs);
     35         setLayoutResource(R.layout.data_usage_cycles);
     36     }
     37 
     38     @Override
     39     public void setAdapter(CycleAdapter cycleAdapter) {
     40         mAdapter = cycleAdapter;
     41         notifyChanged();
     42     }
     43 
     44     @Override
     45     public void setOnItemSelectedListener(AdapterView.OnItemSelectedListener listener) {
     46         mListener = listener;
     47     }
     48 
     49     @Override
     50     public Object getSelectedItem() {
     51         return mCurrentObject;
     52     }
     53 
     54     @Override
     55     public void setSelection(int position) {
     56         mPosition = position;
     57         mCurrentObject = mAdapter.getItem(mPosition);
     58         notifyChanged();
     59     }
     60 
     61     @Override
     62     public void onBindViewHolder(PreferenceViewHolder holder) {
     63         super.onBindViewHolder(holder);
     64         Spinner spinner = (Spinner) holder.findViewById(R.id.cycles_spinner);
     65         spinner.setAdapter(mAdapter);
     66         spinner.setSelection(mPosition);
     67         spinner.setOnItemSelectedListener(mOnSelectedListener);
     68     }
     69 
     70     @Override
     71     protected void performClick(View view) {
     72         view.findViewById(R.id.cycles_spinner).performClick();
     73     }
     74 
     75     private final AdapterView.OnItemSelectedListener mOnSelectedListener
     76             = new AdapterView.OnItemSelectedListener() {
     77         @Override
     78         public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
     79             if (mPosition == position) return;
     80             mPosition = position;
     81             mCurrentObject = mAdapter.getItem(position);
     82             mListener.onItemSelected(parent, view, position, id);
     83         }
     84 
     85         @Override
     86         public void onNothingSelected(AdapterView<?> parent) {
     87             mListener.onNothingSelected(parent);
     88         }
     89     };
     90 }
     91