1 /* 2 * Copyright (C) 2011 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.browser.addbookmark; 18 19 import android.content.Context; 20 import android.view.View; 21 import android.util.AttributeSet; 22 import android.widget.AdapterView; 23 import android.widget.Spinner; 24 25 /** 26 * Special Spinner class with its own callback for when the selection is set, which 27 * can be ignored by calling setSelectionIgnoringSelectionChange 28 */ 29 public class FolderSpinner extends Spinner 30 implements AdapterView.OnItemSelectedListener { 31 private OnSetSelectionListener mOnSetSelectionListener; 32 private boolean mFireSetSelection; 33 34 /** 35 * Callback for knowing when the selection has been manually set. Does not 36 * get called until the selected view has changed. 37 */ 38 public interface OnSetSelectionListener { 39 public void onSetSelection(long id); 40 } 41 42 public FolderSpinner(Context context, AttributeSet attrs) { 43 super(context, attrs); 44 super.setOnItemSelectedListener(this); 45 } 46 47 @Override 48 public void setOnItemSelectedListener(AdapterView.OnItemSelectedListener l) { 49 // Disallow setting an OnItemSelectedListener, since it is used by us 50 // to fire onSetSelection. 51 throw new RuntimeException("Cannot set an OnItemSelectedListener on a FolderSpinner"); 52 } 53 54 public void setOnSetSelectionListener(OnSetSelectionListener l) { 55 mOnSetSelectionListener = l; 56 } 57 58 /** 59 * Call setSelection, without firing the callback 60 * @param position New position to select. 61 */ 62 public void setSelectionIgnoringSelectionChange(int position) { 63 super.setSelection(position); 64 } 65 66 @Override 67 public void setSelection(int position) { 68 mFireSetSelection = true; 69 int oldPosition = getSelectedItemPosition(); 70 super.setSelection(position); 71 if (mOnSetSelectionListener != null) { 72 if (oldPosition == position) { 73 long id = getAdapter().getItemId(position); 74 // Normally this is not called because the item did not actually 75 // change, but in this case, we still want it to be called. 76 onItemSelected(this, null, position, id); 77 } 78 } 79 } 80 81 @Override 82 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 83 if (mFireSetSelection) { 84 mOnSetSelectionListener.onSetSelection(id); 85 mFireSetSelection = false; 86 } 87 } 88 89 @Override 90 public void onNothingSelected(AdapterView<?> parent) {} 91 } 92 93