Android Open Source - yammp Repeating Image Button






From Project

Back to project page yammp.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project yammp listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2008 The Android Open Source Project
 */*from  w  w  w  . ja v  a  2  s  . com*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.yammp.widget;

import android.content.Context;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;

/**
 * A button that will repeatedly call a 'listener' method as long as the button
 * is pressed.
 */
public class RepeatingImageButton extends ImageButton {

  private long mStartTime;

  private int mRepeatCount;
  private RepeatListener mListener;
  private long mInterval = 500;

  private Runnable mRepeater = new Runnable() {

    @Override
    public void run() {

      doRepeat(false);
      if (isPressed()) {
        postDelayed(this, mInterval);
      }
    }
  };

  public RepeatingImageButton(Context context) {

    this(context, null);
  }

  public RepeatingImageButton(Context context, AttributeSet attrs) {

    this(context, attrs, android.R.attr.imageButtonStyle);
  }

  public RepeatingImageButton(Context context, AttributeSet attrs, int defStyle) {

    super(context, attrs, defStyle);
    setFocusable(true);
    setLongClickable(true);
  }

  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {

    switch (keyCode) {
      case KeyEvent.KEYCODE_DPAD_CENTER:
      case KeyEvent.KEYCODE_ENTER:
        // need to call super to make long press work, but return
        // true so that the application doesn't get the down event.
        super.onKeyDown(keyCode, event);
        return true;
    }
    return super.onKeyDown(keyCode, event);
  }

  @Override
  public boolean onKeyUp(int keyCode, KeyEvent event) {

    switch (keyCode) {
      case KeyEvent.KEYCODE_DPAD_CENTER:
      case KeyEvent.KEYCODE_ENTER:
        // remove the repeater, but call the hook one more time
        removeCallbacks(mRepeater);
        if (mStartTime != 0) {
          doRepeat(true);
          mStartTime = 0;
        }
    }
    return super.onKeyUp(keyCode, event);
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {

    if (event.getAction() == MotionEvent.ACTION_UP) {
      // remove the repeater, but call the hook one more time
      removeCallbacks(mRepeater);
      if (mStartTime != 0) {
        doRepeat(true);
        mStartTime = 0;
      }
    }
    return super.onTouchEvent(event);
  }

  @Override
  public boolean performLongClick() {

    mStartTime = SystemClock.elapsedRealtime();
    mRepeatCount = 0;
    post(mRepeater);
    return true;
  }

  /**
   * Sets the listener to be called while the button is pressed and the
   * interval in milliseconds with which it will be called.
   * 
   * @param l
   *            The listener that will be called
   * @param interval
   *            The interval in milliseconds for calls
   */
  public void setRepeatListener(RepeatListener l, long interval) {

    mListener = l;
    mInterval = interval;
  }

  private void doRepeat(boolean last) {

    long now = SystemClock.elapsedRealtime();
    if (mListener != null) {
      mListener.onRepeat(this, now - mStartTime, last ? -1 : mRepeatCount++);
    }
  }

  public interface RepeatListener {

    /**
     * This method will be called repeatedly at roughly the interval
     * specified in setRepeatListener(), for as long as the button is
     * pressed.
     * 
     * @param v
     *            The button as a View.
     * @param duration
     *            The number of milliseconds the button has been pressed so
     *            far.
     * @param repeatcount
     *            The number of previous calls in this sequence. If this is
     *            going to be the last call in this sequence (i.e. the user
     *            just stopped pressing the button), the value will be -1.
     */
    void onRepeat(View v, long duration, int repeatcount);
  }
}




Java Source Code List

org.yammp.Constants.java
org.yammp.MediaAppWidgetProvider4x1.java
org.yammp.MediaAppWidgetProvider4x2.java
org.yammp.MediaButtonIntentReceiver.java
org.yammp.MusicPlaybackService.java
org.yammp.app.AlbumFragment.java
org.yammp.app.AppearanceSettingsActivity.java
org.yammp.app.ArtistFragment.java
org.yammp.app.Equalizer.java
org.yammp.app.GenreFragment.java
org.yammp.app.LyricsFragment.java
org.yammp.app.MusicBrowserActivity.java
org.yammp.app.MusicBrowserFragment.java
org.yammp.app.MusicPlaybackActivity.java
org.yammp.app.MusicSettingsActivity.java
org.yammp.app.PlaylistFragment.java
org.yammp.app.PluginFragment.java
org.yammp.app.PluginsManagerActivity.java
org.yammp.app.QueryBrowserActivity.java
org.yammp.app.QueryFragment.java
org.yammp.app.TrackBrowserActivity.java
org.yammp.app.TrackFragment.java
org.yammp.dialog.DeleteDialog.java
org.yammp.dialog.PlayShortcut.java
org.yammp.dialog.PlaylistDialog.java
org.yammp.dialog.PlaylistPickerDialog.java
org.yammp.dialog.PlaylistPicker.java
org.yammp.dialog.ScanningProgress.java
org.yammp.dialog.SearchDialog.java
org.yammp.dialog.SleepTimerDialog.java
org.yammp.dialog.VerticalTextSpinnerDialog.java
org.yammp.dialog.WeekSelector.java
org.yammp.util.ColorAnalyser.java
org.yammp.util.EqualizerWrapper.java
org.yammp.util.ImageDownloader.java
org.yammp.util.LazyImageLoader.java
org.yammp.util.LyricsDownloader.java
org.yammp.util.LyricsParser.java
org.yammp.util.LyricsSplitter.java
org.yammp.util.MusicUtils.java
org.yammp.util.PreferencesEditor.java
org.yammp.util.ServiceToken.java
org.yammp.util.ShakeListener.java
org.yammp.util.SortCursor.java
org.yammp.util.VisualizerCompatAudioFX.java
org.yammp.util.VisualizerCompatScoop.java
org.yammp.util.VisualizerCompat.java
org.yammp.util.VisualizerWrapper.java
org.yammp.view.EqualizerView.java
org.yammp.view.SliderView.java
org.yammp.view.TouchPaintView.java
org.yammp.view.VerticalTextSpinner.java
org.yammp.view.VisualizerViewFftSpectrum.java
org.yammp.view.VisualizerViewWaveForm.java
org.yammp.widget.CheckableRelativeLayout.java
org.yammp.widget.RepeatingImageButton.java
org.yammp.widget.SeparatedListAdapter.java
org.yammp.widget.TextScrollView.java
org.yammp.widget.TouchInterceptor.java