RepeatingImageButton.java revision 6cb8bc92e0ca524a76a6fa3f6814b43ea9a3b30d
1/* 2 * Copyright (C) 2008 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 17package com.android.music; 18 19import android.content.Context; 20import android.os.SystemClock; 21import android.util.AttributeSet; 22import android.view.KeyEvent; 23import android.view.MotionEvent; 24import android.view.View; 25import android.widget.ImageButton; 26 27/** 28 * A button that will repeatedly call a 'listener' method 29 * as long as the button is pressed. 30 */ 31public class RepeatingImageButton extends ImageButton { 32 33 private long mStartTime; 34 private int mRepeatCount; 35 private RepeatListener mListener; 36 private long mInterval = 500; 37 38 public RepeatingImageButton(Context context) { 39 this(context, null); 40 } 41 42 public RepeatingImageButton(Context context, AttributeSet attrs) { 43 this(context, attrs, android.R.attr.imageButtonStyle); 44 } 45 46 public RepeatingImageButton(Context context, AttributeSet attrs, int defStyle) { 47 super(context, attrs, defStyle); 48 setFocusable(true); 49 setLongClickable(true); 50 } 51 52 /** 53 * Sets the listener to be called while the button is pressed and 54 * the interval in milliseconds with which it will be called. 55 * @param l The listener that will be called 56 * @param interval The interval in milliseconds for calls 57 */ 58 public void setRepeatListener(RepeatListener l, long interval) { 59 mListener = l; 60 mInterval = interval; 61 } 62 63 @Override 64 public boolean performLongClick() { 65 mStartTime = SystemClock.elapsedRealtime(); 66 mRepeatCount = 0; 67 post(mRepeater); 68 return true; 69 } 70 71 @Override 72 public boolean onTouchEvent(MotionEvent event) { 73 if (event.getAction() == MotionEvent.ACTION_UP) { 74 // remove the repeater, but call the hook one more time 75 removeCallbacks(mRepeater); 76 if (mStartTime != 0) { 77 doRepeat(true); 78 mStartTime = 0; 79 } 80 } 81 return super.onTouchEvent(event); 82 } 83 84 @Override 85 public boolean onKeyUp(int keyCode, KeyEvent event) { 86 switch (keyCode) { 87 case KeyEvent.KEYCODE_DPAD_CENTER: 88 case KeyEvent.KEYCODE_ENTER: 89 // remove the repeater, but call the hook one more time 90 removeCallbacks(mRepeater); 91 if (mStartTime != 0) { 92 doRepeat(true); 93 mStartTime = 0; 94 } 95 } 96 return super.onKeyUp(keyCode, event); 97 } 98 99 private Runnable mRepeater = new Runnable() { 100 public void run() { 101 doRepeat(false); 102 if (isPressed()) { 103 postDelayed(this, mInterval); 104 } 105 } 106 }; 107 108 private void doRepeat(boolean last) { 109 long now = SystemClock.elapsedRealtime(); 110 if (mListener != null) { 111 mListener.onRepeat(this, now - mStartTime, last ? -1 : mRepeatCount++); 112 } 113 } 114 115 public interface RepeatListener { 116 /** 117 * This method will be called repeatedly at roughly the interval 118 * specified in setRepeatListener(), for as long as the button 119 * is pressed. 120 * @param v The button as a View. 121 * @param duration The number of milliseconds the button has been pressed so far. 122 * @param repeatcount The number of previous calls in this sequence. 123 * If this is going to be the last call in this sequence (i.e. the user 124 * just stopped pressing the button), the value will be -1. 125 */ 126 void onRepeat(View v, long duration, int repeatcount); 127 } 128} 129