Skip to content

Commit

Permalink
optimize 'HeadsetHookHelper'
Browse files Browse the repository at this point in the history
  • Loading branch information
jrfeng committed Sep 23, 2020
1 parent 9e643dd commit ff9ed86
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion helper/src/main/java/media/helper/HeadsetHookHelper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package media.helper;

import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.view.KeyEvent;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -106,10 +108,12 @@ private static class ClickCounter {
private int mCount;
private Timer mTimer;
private OnHeadsetHookClickListener mHeadsetHookClickListener;
private Handler mMainHandler;

ClickCounter(int clickInterval, OnHeadsetHookClickListener listener) {
mClickInterval = clickInterval;
mHeadsetHookClickListener = listener;
mMainHandler = new Handler(Looper.getMainLooper());
}

void reset() {
Expand All @@ -127,12 +131,21 @@ private void startTimer() {

mTimer = new Timer();
mTimer.schedule(new TimerTask() {
@Override
public void run() {
notifyClicked();
}
}, mClickInterval);
}

private void notifyClicked() {
mMainHandler.post(new Runnable() {
@Override
public void run() {
mHeadsetHookClickListener.onHeadsetHookClicked(mCount);
mCount = 0;
}
}, mClickInterval);
});
}

private void cancelTimer() {
Expand Down

0 comments on commit ff9ed86

Please sign in to comment.