From ceb8f62ffcf90f71c4604e1a0f44e41dc9cf6ff9 Mon Sep 17 00:00:00 2001 From: John Pulford Date: Sat, 21 Sep 2013 08:46:14 -0700 Subject: [PATCH] Working PAUSEPLAY/FORWARD/REVERSE hardware keys integration (Tested with pebble) on sdk version 8 and above, still runs on 3+. --- AndroidManifest.xml | 5 ++ .../receiver/RemoteControlReceiver.java | 89 +++++++++++++++++++ .../presentation/activity/HomeActivity.java | 25 +++++- 3 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 src/org/xbmc/android/remote/business/receiver/RemoteControlReceiver.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index fd7496cb..361645c2 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -105,6 +105,11 @@ android:resource="@xml/app_widget_remote" /> + + + + + diff --git a/src/org/xbmc/android/remote/business/receiver/RemoteControlReceiver.java b/src/org/xbmc/android/remote/business/receiver/RemoteControlReceiver.java new file mode 100644 index 00000000..caa49880 --- /dev/null +++ b/src/org/xbmc/android/remote/business/receiver/RemoteControlReceiver.java @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2005-2011 Team XBMC + * http://xbmc.org + * + * This Program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This Program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with XBMC Remote; see the file license. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +package org.xbmc.android.remote.business.receiver; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.view.KeyEvent; + +import org.xbmc.android.remote.business.Command; +import org.xbmc.android.remote.business.ManagerFactory; +import org.xbmc.android.remote.presentation.controller.RemoteController; +import org.xbmc.api.business.IEventClientManager; +import org.xbmc.api.business.INotifiableManager; +import org.xbmc.api.presentation.INotifiableController; +import org.xbmc.eventclient.ButtonCodes; + +/** + * @author John Pulford + */ +public class RemoteControlReceiver extends BroadcastReceiver implements INotifiableController { + + @Override + public void onReceive(Context context, Intent intent) { + IEventClientManager mEventClientManager = ManagerFactory.getEventClientManager(this); + + if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) { + String keyPressAction = ""; + KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); + if (KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE == event.getKeyCode()) { + // Handle key press. + keyPressAction = ButtonCodes.REMOTE_PLAY; + } + if (KeyEvent.KEYCODE_MEDIA_PREVIOUS == event.getKeyCode()) { + // Handle key press. + keyPressAction = ButtonCodes.REMOTE_REVERSE; + } + if (KeyEvent.KEYCODE_MEDIA_NEXT == event.getKeyCode()) { + // Handle key press. + keyPressAction = ButtonCodes.REMOTE_FORWARD; + } + if (keyPressAction.length() > 0) { + mEventClientManager.sendButton("R1", keyPressAction, false, true, true, (short)0, (byte)0); + } + } + } + + + // Not convinced these methods need to do anything in this instance + // but someone with a longer history with the project may have a better idea. + @Override + public void onWrongConnectionState(int state, INotifiableManager manager, Command source) { + + } + + @Override + public void onError(Exception e) { + + } + + @Override + public void onMessage(String message) { + + } + + @Override + public void runOnUI(Runnable action) { + + } +} \ No newline at end of file diff --git a/src/org/xbmc/android/remote/presentation/activity/HomeActivity.java b/src/org/xbmc/android/remote/presentation/activity/HomeActivity.java index 87d89855..38164fa6 100644 --- a/src/org/xbmc/android/remote/presentation/activity/HomeActivity.java +++ b/src/org/xbmc/android/remote/presentation/activity/HomeActivity.java @@ -25,6 +25,7 @@ import org.xbmc.android.remote.business.CacheManager; import org.xbmc.android.remote.business.ManagerFactory; import org.xbmc.android.remote.business.receiver.AndroidBroadcastReceiver; +import org.xbmc.android.remote.business.receiver.RemoteControlReceiver; import org.xbmc.android.remote.presentation.controller.HomeController; import org.xbmc.android.remote.presentation.controller.HomeController.ProgressThread; import org.xbmc.android.remote.presentation.notification.NowPlayingNotificationManager; @@ -38,10 +39,13 @@ import android.app.AlertDialog; import android.app.Dialog; import android.app.ProgressDialog; +import android.content.ComponentName; +import android.content.Context; import android.content.DialogInterface; import android.content.IntentFilter; import android.content.DialogInterface.OnCancelListener; import android.content.Intent; +import android.media.AudioManager; import android.os.Build; import android.os.Bundle; import android.os.Handler; @@ -85,7 +89,8 @@ public class HomeActivity extends Activity { private ProgressThread mProgressThread; private ProgressDialog mProgressDialog; - + + private AudioManager mAudioManager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -121,8 +126,15 @@ public void onClick(View v) { IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_SCREEN_OFF); filter.addAction(Intent.ACTION_SCREEN_ON); - + //Register Receivers registerReceiver(new AndroidBroadcastReceiver(), filter); + + //Register for hardware media keys, if possible. + mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); + if (Build.VERSION.SDK_INT >= 8) { + // Start listening for button presses + mAudioManager.registerMediaButtonEventReceiver(new ComponentName(getPackageName(), RemoteControlReceiver.class.getName())); + } } @@ -262,7 +274,14 @@ protected void onPause() { mConfigurationManager.onActivityPause(); mEventClientManager.setController(null); } - + + @Override + protected void onDestroy() { + if (Build.VERSION.SDK_INT >= 8) { + // Stop listening for button presses + mAudioManager.registerMediaButtonEventReceiver(new ComponentName(getPackageName(), RemoteControlReceiver.class.getName())); + } + } @Override public boolean onKeyDown(int keyCode, KeyEvent event) {