/app/src/main/java/biz/dealnote/messenger/player/ui/RepeatingImageButton.java

https://github.com/PhoenixDevTeam/Phoenix-for-VK · Java · 161 lines · 113 code · 18 blank · 30 comment · 13 complexity · e1a53b5e81a6045387d50a7ef78b1b82 MD5 · raw file

  1. /*
  2. * Copyright (C) 2008 The Android Open Source Project Licensed under the Apache
  3. * License, Version 2.0 (the "License"); you may not use this file except in
  4. * compliance with the License. You may obtain a copy of the License at
  5. * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
  6. * or agreed to in writing, software distributed under the License is
  7. * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  8. * KIND, either express or implied. See the License for the specific language
  9. * governing permissions and limitations under the License.
  10. */
  11. package biz.dealnote.messenger.player.ui;
  12. import android.content.Context;
  13. import android.os.SystemClock;
  14. import android.util.AttributeSet;
  15. import android.view.KeyEvent;
  16. import android.view.MotionEvent;
  17. import android.view.View;
  18. import android.view.View.OnClickListener;
  19. import android.widget.ImageButton;
  20. import androidx.appcompat.widget.AppCompatImageButton;
  21. import biz.dealnote.messenger.R;
  22. import biz.dealnote.messenger.player.util.MusicUtils;
  23. import biz.dealnote.messenger.settings.CurrentTheme;
  24. /**
  25. * A {@link ImageButton} that will repeatedly call a 'listener' method as long
  26. * as the button is pressed, otherwise functions like a typecal
  27. * {@link ImageButton}
  28. */
  29. public class RepeatingImageButton extends AppCompatImageButton implements OnClickListener {
  30. private static final long sInterval = 400;
  31. private long mStartTime;
  32. private int mRepeatCount;
  33. private RepeatListener mListener;
  34. public RepeatingImageButton(final Context context, final AttributeSet attrs) {
  35. super(context, attrs);
  36. setFocusable(true);
  37. setLongClickable(true);
  38. setOnClickListener(this);
  39. updateState();
  40. }
  41. @Override
  42. public void onClick(final View view) {
  43. switch (view.getId()) {
  44. case R.id.action_button_previous:
  45. MusicUtils.previous(getContext());
  46. break;
  47. case R.id.action_button_next:
  48. MusicUtils.next();
  49. default:
  50. break;
  51. }
  52. }
  53. public void setRepeatListener(final RepeatListener l) {
  54. mListener = l;
  55. }
  56. @Override
  57. public boolean performLongClick() {
  58. mStartTime = SystemClock.elapsedRealtime();
  59. mRepeatCount = 0;
  60. post(mRepeater);
  61. return true;
  62. }
  63. @Override
  64. public boolean onTouchEvent(final MotionEvent event) {
  65. if (event.getAction() == MotionEvent.ACTION_UP) {
  66. /* Remove the repeater, but call the hook one more time */
  67. removeCallbacks(mRepeater);
  68. if (mStartTime != 0) {
  69. doRepeat(true);
  70. mStartTime = 0;
  71. }
  72. }
  73. return super.onTouchEvent(event);
  74. }
  75. @Override
  76. public boolean onKeyDown(final int keyCode, final KeyEvent event) {
  77. switch (keyCode) {
  78. case KeyEvent.KEYCODE_DPAD_CENTER:
  79. case KeyEvent.KEYCODE_ENTER:
  80. /*
  81. * Need to call super to make long press work, but return true
  82. * so that the application doesn't get the down event
  83. */
  84. super.onKeyDown(keyCode, event);
  85. return true;
  86. }
  87. return super.onKeyDown(keyCode, event);
  88. }
  89. @Override
  90. public boolean onKeyUp(final int keyCode, final KeyEvent event) {
  91. switch (keyCode) {
  92. case KeyEvent.KEYCODE_DPAD_CENTER:
  93. case KeyEvent.KEYCODE_ENTER:
  94. /* Remove the repeater, but call the hook one more time */
  95. removeCallbacks(mRepeater);
  96. if (mStartTime != 0) {
  97. doRepeat(true);
  98. mStartTime = 0;
  99. }
  100. }
  101. return super.onKeyUp(keyCode, event);
  102. }
  103. private final Runnable mRepeater = new Runnable() {
  104. @Override
  105. public void run() {
  106. doRepeat(false);
  107. if (isPressed()) {
  108. postDelayed(this, sInterval);
  109. }
  110. }
  111. };
  112. /**
  113. * @param shouldRepeat If True the repeat count stops at -1, false if to add
  114. * incrementally add the repeat count
  115. */
  116. private void doRepeat(final boolean shouldRepeat) {
  117. final long now = SystemClock.elapsedRealtime();
  118. if (mListener != null) {
  119. mListener.onRepeat(this, now - mStartTime, shouldRepeat ? -1 : mRepeatCount++);
  120. }
  121. }
  122. public void updateState() {
  123. setColorFilter(CurrentTheme.getIconColorStatic(getContext()));
  124. switch (getId()) {
  125. case R.id.action_button_next:
  126. setImageResource(R.drawable.page_last);
  127. break;
  128. case R.id.action_button_previous:
  129. setImageResource(R.drawable.page_first);
  130. break;
  131. default:
  132. break;
  133. }
  134. }
  135. public interface RepeatListener {
  136. /**
  137. * @param v View to be set
  138. * @param duration Duration of the long press
  139. * @param repeatcount The number of repeat counts
  140. */
  141. void onRepeat(View v, long duration, int repeatcount);
  142. }
  143. }