Bug with headset (double and triple click) on Android Oreo solved

This commit is contained in:
VictorBG 2017-12-22 12:32:53 +01:00
commit 872b23628d
3 changed files with 23 additions and 5 deletions

View file

@ -41,6 +41,8 @@ public class MediaButtonIntentReceiver extends BroadcastReceiver {
private static final int DOUBLE_CLICK = 400; private static final int DOUBLE_CLICK = 400;
private static final int MULTIPLE_CLICK = 150;
private static WakeLock mWakeLock = null; private static WakeLock mWakeLock = null;
private static int mClickCounter = 0; private static int mClickCounter = 0;
private static long mLastClickTime = 0; private static long mLastClickTime = 0;
@ -89,6 +91,16 @@ public class MediaButtonIntentReceiver extends BroadcastReceiver {
} }
} }
static int clicks;
static long multipleClickStart;
static Runnable resetMultipleClick = () -> {
multipleClickStart = 0;
clicks = 0;
};
static Handler timeMultipleClick;
public static boolean handleIntent(final Context context, final Intent intent) { public static boolean handleIntent(final Context context, final Intent intent) {
final String intentAction = intent.getAction(); final String intentAction = intent.getAction();
if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) { if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
@ -108,6 +120,7 @@ public class MediaButtonIntentReceiver extends BroadcastReceiver {
break; break;
case KeyEvent.KEYCODE_HEADSETHOOK: case KeyEvent.KEYCODE_HEADSETHOOK:
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
//On Android Oreo it always call this
command = MusicService.ACTION_TOGGLE_PAUSE; command = MusicService.ACTION_TOGGLE_PAUSE;
break; break;
case KeyEvent.KEYCODE_MEDIA_NEXT: case KeyEvent.KEYCODE_MEDIA_NEXT:
@ -133,13 +146,18 @@ public class MediaButtonIntentReceiver extends BroadcastReceiver {
// The service may or may not be running, but we need to send it // The service may or may not be running, but we need to send it
// a command. // a command.
if (keycode == KeyEvent.KEYCODE_HEADSETHOOK) { int keycodeToCompare = KeyEvent.KEYCODE_HEADSETHOOK;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
keycodeToCompare = KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE;
}
if (keycode == keycodeToCompare) {
if (eventTime - mLastClickTime >= DOUBLE_CLICK) { if (eventTime - mLastClickTime >= DOUBLE_CLICK) {
mClickCounter = 0; mClickCounter = 0;
} }
mClickCounter++; mClickCounter++;
if (DEBUG) Log.v(TAG, "Got headset click, count = " + mClickCounter); if (DEBUG)
Log.v(TAG, "Got headset click, count = " + mClickCounter);
mHandler.removeMessages(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT); mHandler.removeMessages(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT);
Message msg = mHandler.obtainMessage( Message msg = mHandler.obtainMessage(

View file

@ -4,7 +4,7 @@ buildscript {
google() google()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:3.0.1' classpath 'com.android.tools.build:gradle:3.1.0-alpha05'
} }
} }

View file

@ -1,6 +1,6 @@
#Sun Dec 10 13:25:20 CET 2017 #Fri Dec 22 10:41:21 CET 2017
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip distributionUrl=https\://services.gradle.org/distributions-snapshots/gradle-4.4-20171031235950+0000-all.zip