A couple small but important updates to musikdroid:

(1) ensure playback is paused if/when the headset is unplugged
(2) added "double click" to go to next, and "triple click" to go to prev
This commit is contained in:
casey langen 2017-05-16 23:54:58 -07:00
parent 0201741689
commit 646742214d
3 changed files with 54 additions and 3 deletions

View File

@ -35,7 +35,7 @@ import io.reactivex.android.schedulers.AndroidSchedulers;
public class StreamingPlaybackService implements PlaybackService {
private static final String TAG = "StreamingPlayback";
private static final String REPEAT_MODE_PREF = "streaming_playback_repeat_mode";
private static final int PREV_TRACK_GRACE_PERIOD_MILLIS = 2000;
private static final int PREV_TRACK_GRACE_PERIOD_MILLIS = 3500;
private static final int MAX_TRACK_METADATA_CACHE_SIZE = 50;
private static final int PRECACHE_METADATA_SIZE = 10;
private static final int PAUSED_SERVICE_SHUTDOWN_DELAY_MS = 1000 * 60; /* 1 minute */

View File

@ -2,9 +2,12 @@ package io.casey.musikcube.remote.playback;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.IBinder;
import android.os.PowerManager;
import android.support.annotation.Nullable;
@ -18,6 +21,7 @@ import android.view.KeyEvent;
import io.casey.musikcube.remote.Application;
import io.casey.musikcube.remote.MainActivity;
import io.casey.musikcube.remote.R;
import io.casey.musikcube.remote.util.Debouncer;
import io.casey.musikcube.remote.util.Strings;
/* basically a stub service that exists to keep a connection active to the
@ -26,6 +30,7 @@ a partial wakelock to keep the radio from going to sleep. */
public class SystemService extends Service {
private static final String TAG = "SystemService";
private static final int NOTIFICATION_ID = 0xdeadbeef;
private static final int HEADSET_HOOK_DEBOUNCE_MS = 500;
private static final String ACTION_NOTIFICATION_PLAY = "io.casey.musikcube.remote.NOTIFICATION_PLAY";
private static final String ACTION_NOTIFICATION_PAUSE = "io.casey.musikcube.remote.NOTIFICATION_PAUSE";
private static final String ACTION_NOTIFICATION_NEXT = "io.casey.musikcube.remote.NOTIFICATION_NEXT";
@ -45,6 +50,7 @@ public class SystemService extends Service {
private PowerManager.WakeLock wakeLock;
private PowerManager powerManager;
private MediaSessionCompat mediaSession;
private int headsetHookPressCount = 0;
public static void wakeup() {
final Context c = Application.getInstance();
@ -64,11 +70,13 @@ public class SystemService extends Service {
public void onCreate() {
super.onCreate();
powerManager = (PowerManager) getSystemService(POWER_SERVICE);
registerReceivers();
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceivers();
}
@Override
@ -140,6 +148,20 @@ public class SystemService extends Service {
mediaSession.setActive(true);
}
private void registerReceivers() {
final IntentFilter filter = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
registerReceiver(headsetUnpluggedReceiver, filter);
}
private void unregisterReceivers() {
try {
unregisterReceiver(headsetUnpluggedReceiver);
}
catch (Exception ex) {
Log.e(TAG, "unable to unregister headset (un)plugged BroadcastReceiver");
}
}
private void updateMediaSessionPlaybackState() {
int mediaSessionState = PlaybackStateCompat.STATE_STOPPED;
@ -287,6 +309,22 @@ public class SystemService extends Service {
return false;
}
private Debouncer<Void> headsetHookDebouncer = new Debouncer<Void>(HEADSET_HOOK_DEBOUNCE_MS) {
@Override
protected void onDebounced(Void caller) {
if (headsetHookPressCount == 1) {
playback.pauseOrResume();
}
else if (headsetHookPressCount == 2) {
playback.next();
}
else if (headsetHookPressCount > 2) {
playback.prev();
}
headsetHookPressCount = 0;
}
};
private MediaSessionCompat.Callback mediaSessionCallback = new MediaSessionCompat.Callback() {
@Override
public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
@ -301,7 +339,9 @@ public class SystemService extends Service {
if (event.getRepeatCount() == 0 && action == KeyEvent.ACTION_DOWN) {
switch (keycode) {
case KeyEvent.KEYCODE_HEADSETHOOK:
return false;
++headsetHookPressCount;
headsetHookDebouncer.call();
return true;
case KeyEvent.KEYCODE_MEDIA_STOP:
playback.pause();
SystemService.shutdown();
@ -372,4 +412,15 @@ public class SystemService extends Service {
private PlaybackService.EventListener listener = () -> {
updateMediaSessionPlaybackState();
};
private BroadcastReceiver headsetUnpluggedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(AudioManager.ACTION_AUDIO_BECOMING_NOISY)) {
if (playback != null) {
playback.pause();
}
}
}
};
}

View File

@ -3,7 +3,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.1'
classpath 'com.android.tools.build:gradle:2.3.2'
classpath 'me.tatarka:gradle-retrolambda:3.5.0'
}
}