2015-02-12 16:00:45 +01:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge;
|
|
|
|
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2015-04-13 22:25:23 +02:00
|
|
|
import android.media.AudioManager;
|
2015-02-12 16:00:45 +01:00
|
|
|
import android.os.SystemClock;
|
|
|
|
import android.view.KeyEvent;
|
|
|
|
|
2015-05-12 06:28:11 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2015-03-26 12:06:26 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommandMusicControl;
|
|
|
|
|
2015-02-12 16:00:45 +01:00
|
|
|
public class GBMusicControlReceiver extends BroadcastReceiver {
|
2015-05-12 06:28:11 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(GBMusicControlReceiver.class);
|
2015-02-12 16:00:45 +01:00
|
|
|
|
|
|
|
public static final String ACTION_MUSICCONTROL = "nodomain.freeyourgadget.gadgetbridge.musiccontrol";
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
2015-03-26 12:06:26 +01:00
|
|
|
GBDeviceCommandMusicControl.Command musicCmd = GBDeviceCommandMusicControl.Command.values()[intent.getIntExtra("command", 0)];
|
2015-04-13 22:25:23 +02:00
|
|
|
int keyCode = -1;
|
|
|
|
int volumeAdjust = AudioManager.ADJUST_LOWER;
|
|
|
|
|
2015-03-26 12:06:26 +01:00
|
|
|
switch (musicCmd) {
|
|
|
|
case NEXT:
|
2015-02-12 16:00:45 +01:00
|
|
|
keyCode = KeyEvent.KEYCODE_MEDIA_NEXT;
|
|
|
|
break;
|
2015-03-26 12:06:26 +01:00
|
|
|
case PREVIOUS:
|
2015-02-12 16:00:45 +01:00
|
|
|
keyCode = KeyEvent.KEYCODE_MEDIA_PREVIOUS;
|
|
|
|
break;
|
2015-03-26 12:06:26 +01:00
|
|
|
case PLAY:
|
2015-02-12 16:00:45 +01:00
|
|
|
keyCode = KeyEvent.KEYCODE_MEDIA_PLAY;
|
|
|
|
break;
|
2015-03-26 12:06:26 +01:00
|
|
|
case PAUSE:
|
2015-02-12 16:00:45 +01:00
|
|
|
keyCode = KeyEvent.KEYCODE_MEDIA_PAUSE;
|
|
|
|
break;
|
2015-03-26 12:06:26 +01:00
|
|
|
case PLAYPAUSE:
|
2015-02-12 16:00:45 +01:00
|
|
|
keyCode = KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE;
|
|
|
|
break;
|
2015-04-13 22:25:23 +02:00
|
|
|
case VOLUMEUP:
|
|
|
|
// change default and fall through, :P
|
|
|
|
volumeAdjust = AudioManager.ADJUST_RAISE;
|
|
|
|
case VOLUMEDOWN:
|
|
|
|
AudioManager audioManager =
|
|
|
|
(AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
|
|
|
|
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, volumeAdjust, 0);
|
|
|
|
break;
|
2015-02-12 16:00:45 +01:00
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-13 22:25:23 +02:00
|
|
|
if (keyCode != -1) {
|
|
|
|
long eventtime = SystemClock.uptimeMillis();
|
2015-02-12 16:00:45 +01:00
|
|
|
|
2015-04-13 22:25:23 +02:00
|
|
|
Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
|
|
|
|
KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, keyCode, 0);
|
|
|
|
downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent);
|
|
|
|
context.sendOrderedBroadcast(downIntent, null);
|
2015-02-12 16:00:45 +01:00
|
|
|
|
2015-04-13 22:25:23 +02:00
|
|
|
Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
|
|
|
|
KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, keyCode, 0);
|
|
|
|
upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent);
|
|
|
|
context.sendOrderedBroadcast(upIntent, null);
|
|
|
|
}
|
2015-02-12 16:00:45 +01:00
|
|
|
}
|
|
|
|
}
|