1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-13 21:49:27 +01:00
Gadgetbridge/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBMusicControlReceiver.java
Andreas Shimokawa 67a5917597 Change the way how device commands or replys to information requests are passed back to the App
This was necessary to pack more complex and more specific information regarding apps etc.
2015-03-26 12:06:26 +01:00

53 lines
2.0 KiB
Java

package nodomain.freeyourgadget.gadgetbridge;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;
import android.view.KeyEvent;
import nodomain.freeyourgadget.gadgetbridge.protocol.GBDeviceCommandMusicControl;
public class GBMusicControlReceiver extends BroadcastReceiver {
private final String TAG = this.getClass().getSimpleName();
public static final String ACTION_MUSICCONTROL = "nodomain.freeyourgadget.gadgetbridge.musiccontrol";
@Override
public void onReceive(Context context, Intent intent) {
GBDeviceCommandMusicControl.Command musicCmd = GBDeviceCommandMusicControl.Command.values()[intent.getIntExtra("command", 0)];
int keyCode;
switch (musicCmd) {
case NEXT:
keyCode = KeyEvent.KEYCODE_MEDIA_NEXT;
break;
case PREVIOUS:
keyCode = KeyEvent.KEYCODE_MEDIA_PREVIOUS;
break;
case PLAY:
keyCode = KeyEvent.KEYCODE_MEDIA_PLAY;
break;
case PAUSE:
keyCode = KeyEvent.KEYCODE_MEDIA_PAUSE;
break;
case PLAYPAUSE:
keyCode = KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE;
break;
default:
return;
}
long eventtime = SystemClock.uptimeMillis();
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);
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);
}
}