1 /* 2 * Copyright (C) 2019 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.car.developeroptions.datausage; 16 17 import android.content.Context; 18 import android.util.AttributeSet; 19 import android.view.View; 20 import android.widget.AdapterView; 21 22 import androidx.preference.Preference; 23 import androidx.preference.PreferenceViewHolder; 24 25 import com.android.car.developeroptions.R; 26 import com.android.settingslib.widget.settingsspinner.SettingsSpinner; 27 28 public class SpinnerPreference extends Preference implements CycleAdapter.SpinnerInterface { 29 30 private CycleAdapter mAdapter; 31 private AdapterView.OnItemSelectedListener mListener; 32 private Object mCurrentObject; 33 private int mPosition; 34 SpinnerPreference(Context context, AttributeSet attrs)35 public SpinnerPreference(Context context, AttributeSet attrs) { 36 super(context, attrs); 37 setLayoutResource(R.layout.data_usage_cycles); 38 } 39 40 @Override setAdapter(CycleAdapter cycleAdapter)41 public void setAdapter(CycleAdapter cycleAdapter) { 42 mAdapter = cycleAdapter; 43 notifyChanged(); 44 } 45 46 @Override setOnItemSelectedListener(AdapterView.OnItemSelectedListener listener)47 public void setOnItemSelectedListener(AdapterView.OnItemSelectedListener listener) { 48 mListener = listener; 49 } 50 51 @Override getSelectedItem()52 public Object getSelectedItem() { 53 return mCurrentObject; 54 } 55 56 @Override setSelection(int position)57 public void setSelection(int position) { 58 mPosition = position; 59 mCurrentObject = mAdapter.getItem(mPosition); 60 notifyChanged(); 61 } 62 63 @Override onBindViewHolder(PreferenceViewHolder holder)64 public void onBindViewHolder(PreferenceViewHolder holder) { 65 super.onBindViewHolder(holder); 66 SettingsSpinner spinner = (SettingsSpinner) holder.findViewById(R.id.cycles_spinner); 67 spinner.setAdapter(mAdapter); 68 spinner.setSelection(mPosition); 69 spinner.setOnItemSelectedListener(mOnSelectedListener); 70 } 71 72 @Override performClick(View view)73 protected void performClick(View view) { 74 view.findViewById(R.id.cycles_spinner).performClick(); 75 } 76 77 private final AdapterView.OnItemSelectedListener mOnSelectedListener 78 = new AdapterView.OnItemSelectedListener() { 79 @Override 80 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 81 if (mPosition == position) return; 82 mPosition = position; 83 mCurrentObject = mAdapter.getItem(position); 84 mListener.onItemSelected(parent, view, position, id); 85 } 86 87 @Override 88 public void onNothingSelected(AdapterView<?> parent) { 89 mListener.onNothingSelected(parent); 90 } 91 }; 92 } 93