Skip to content

Commit

Permalink
Use hardware volume keys from anywhere inside Kore (#453)
Browse files Browse the repository at this point in the history
* Allow the RemoteActivity and BaseMediaActivity to show a volume controller dialog when hardware volume keys are pressed
** The dialog handles the key events and makes callbacks to the activities to handle showing the dialog itself

* Minor improvements
** Make HighlightButton render in the IDE GUI Editor
** Minor code cleanup
** set currentActivePlayerId = -1; on playerOnStop in BaseMediaActivity to be more consistent

resolves #235
  • Loading branch information
calebzor authored and SyncedSynapse committed Oct 8, 2017
1 parent 8fb24ee commit 108fb88
Show file tree
Hide file tree
Showing 9 changed files with 400 additions and 65 deletions.
15 changes: 5 additions & 10 deletions app/src/main/java/org/xbmc/kore/ui/BaseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,15 @@
*/
public abstract class BaseActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
@Override
protected void onCreate(Bundle savedInstanceState) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
setTheme(UIUtils.getThemeResourceId(
prefs.getString(Settings.KEY_PREF_THEME, Settings.DEFAULT_PREF_THEME)));
super.onCreate(savedInstanceState);
}

@Override
public void onPause() {
super.onPause();
}

// @Override
// @Override
// public boolean onCreateOptionsMenu(Menu menu) {
// getMenuInflater().inflate(R.menu.global, menu);
// return super.onCreateOptionsMenu(menu);
Expand All @@ -50,8 +45,8 @@ public void onPause() {
// @Override
// public boolean onOptionsItemSelected(MenuItem item) {
// switch (item.getItemId()) {
// case R.id.action_settings:
// return true;
// case R.id.action_settings:
// return true;
// default:
// break;
// }
Expand Down
34 changes: 32 additions & 2 deletions app/src/main/java/org/xbmc/kore/ui/BaseMediaActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.transition.TransitionInflater;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
Expand All @@ -48,6 +49,9 @@
import org.xbmc.kore.jsonrpc.type.PlayerType;
import org.xbmc.kore.ui.generic.NavigationDrawerFragment;
import org.xbmc.kore.ui.sections.remote.RemoteActivity;
import org.xbmc.kore.ui.volumecontrollers.OnHardwareVolumeKeyPressedCallback;
import org.xbmc.kore.ui.volumecontrollers.VolumeControllerDialogFragmentListener;
import org.xbmc.kore.ui.volumecontrollers.VolumeKeyActionHandler;
import org.xbmc.kore.ui.widgets.MediaProgressIndicator;
import org.xbmc.kore.ui.widgets.NowPlayingPanel;
import org.xbmc.kore.ui.widgets.VolumeLevelIndicator;
Expand All @@ -63,7 +67,8 @@ public abstract class BaseMediaActivity extends BaseActivity
implements HostConnectionObserver.ApplicationEventsObserver,
HostConnectionObserver.PlayerEventsObserver,
NowPlayingPanel.OnPanelButtonsClickListener,
MediaProgressIndicator.OnProgressChangeListener {
MediaProgressIndicator.OnProgressChangeListener,
OnHardwareVolumeKeyPressedCallback {
private static final String TAG = LogUtils.makeLogTag(BaseMediaActivity.class);

private static final String NAVICON_ISARROW = "navstate";
Expand All @@ -79,6 +84,7 @@ public abstract class BaseMediaActivity extends BaseActivity

private HostManager hostManager;
private HostConnectionObserver hostConnectionObserver;
private VolumeKeyActionHandler volumeKeyActionHandler;

private boolean showNowPlayingPanel;

Expand Down Expand Up @@ -180,7 +186,6 @@ protected void onResume() {
.getBoolean(Settings.KEY_PREF_SHOW_NOW_PLAYING_PANEL,
Settings.DEFAULT_PREF_SHOW_NOW_PLAYING_PANEL);


if(showNowPlayingPanel) {
setupNowPlayingPanel();
} else {
Expand All @@ -205,6 +210,30 @@ public void onPause() {
hostConnectionObserver.unregisterPlayerObserver(this);
}

/**
* Override hardware volume keys and send to Kodi
*/
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (volumeKeyActionHandler == null) {
volumeKeyActionHandler = new VolumeKeyActionHandler(hostManager, this, this);
}
return volumeKeyActionHandler.handleDispatchKeyEvent(event) || super.dispatchKeyEvent(
event);
}

@Override
public void onHardwareVolumeKeyPressed() {
showVolumeChangeDialog();
}

private void showVolumeChangeDialog() {
VolumeControllerDialogFragmentListener volumeControllerDialogFragment =
new VolumeControllerDialogFragmentListener();
volumeControllerDialogFragment.show(getSupportFragmentManager(),
VolumeControllerDialogFragmentListener.class.getName());
}

public boolean getDrawerIndicatorIsArrow() {
return drawerIndicatorIsArrow;
}
Expand Down Expand Up @@ -296,6 +325,7 @@ public void playerOnPause(PlayerType.GetActivePlayersReturnType getActivePlayerR

@Override
public void playerOnStop() {
currentActivePlayerId = -1;
//We delay hiding the panel to prevent hiding the panel when playing
// the next item in a playlist
callbackHandler.removeCallbacks(hidePanelRunnable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import org.xbmc.kore.jsonrpc.method.Playlist;
import org.xbmc.kore.jsonrpc.method.System;
import org.xbmc.kore.jsonrpc.method.VideoLibrary;
import org.xbmc.kore.jsonrpc.type.GlobalType;
import org.xbmc.kore.jsonrpc.type.ListType;
import org.xbmc.kore.jsonrpc.type.PlayerType;
import org.xbmc.kore.jsonrpc.type.PlaylistType;
Expand All @@ -59,6 +58,9 @@
import org.xbmc.kore.ui.generic.SendTextDialogFragment;
import org.xbmc.kore.ui.sections.hosts.AddHostActivity;
import org.xbmc.kore.ui.views.CirclePageIndicator;
import org.xbmc.kore.ui.volumecontrollers.OnHardwareVolumeKeyPressedCallback;
import org.xbmc.kore.ui.volumecontrollers.VolumeControllerDialogFragmentListener;
import org.xbmc.kore.ui.volumecontrollers.VolumeKeyActionHandler;
import org.xbmc.kore.utils.LogUtils;
import org.xbmc.kore.utils.TabsAdapter;
import org.xbmc.kore.utils.UIUtils;
Expand All @@ -77,8 +79,8 @@
public class RemoteActivity extends BaseActivity
implements HostConnectionObserver.PlayerEventsObserver,
NowPlayingFragment.NowPlayingListener,
SendTextDialogFragment.SendTextDialogListener {
private static final String TAG = LogUtils.makeLogTag(RemoteActivity.class);
SendTextDialogFragment.SendTextDialogListener, OnHardwareVolumeKeyPressedCallback {
private static final String TAG = LogUtils.makeLogTag(RemoteActivity.class);


private static final int NOWPLAYING_FRAGMENT_ID = 1;
Expand All @@ -97,6 +99,8 @@ public class RemoteActivity extends BaseActivity

private NavigationDrawerFragment navigationDrawerFragment;

private VolumeKeyActionHandler volumeKeyActionHandler;

@InjectView(R.id.background_image) ImageView backgroundImage;
@InjectView(R.id.pager_indicator) CirclePageIndicator pageIndicator;
@InjectView(R.id.pager) ViewPager viewPager;
Expand Down Expand Up @@ -201,54 +205,20 @@ public void onPause() {
hostConnectionObserver = null;
}

/**
* Override hardware volume keys and send to Kodi
*/
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
// Check whether we should intercept this
boolean useVolumeKeys = PreferenceManager
.getDefaultSharedPreferences(this)
.getBoolean(Settings.KEY_PREF_USE_HARDWARE_VOLUME_KEYS,
Settings.DEFAULT_PREF_USE_HARDWARE_VOLUME_KEYS);
if (useVolumeKeys) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_DOWN) {
new Application
.SetVolume(GlobalType.IncrementDecrement.INCREMENT)
.execute(hostManager.getConnection(), null, null);
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
new Application
.SetVolume(GlobalType.IncrementDecrement.DECREMENT)
.execute(hostManager.getConnection(), null, null);
}
return true;
}
}

return super.dispatchKeyEvent(event);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!navigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen if the drawer is not showing.
// Otherwise, let the drawer decide what to show in the action bar.
if (!navigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen if the drawer is not showing.
// Otherwise, let the drawer decide what to show in the action bar.
getMenuInflater().inflate(R.menu.remote, menu);
}
}
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here.
switch (item.getItemId()) {
switch (item.getItemId()) {
case R.id.action_wake_up:
UIUtils.sendWolAsync(this, hostManager.getHostInfo());
return true;
Expand Down Expand Up @@ -298,11 +268,30 @@ public boolean onOptionsItemSelected(MenuItem item) {
AudioLibrary.Scan actionScanAudio = new AudioLibrary.Scan();
actionScanAudio.execute(hostManager.getConnection(), null, null);
return true;
default:
break;
}
default:
break;
}

return super.onOptionsItemSelected(item);
return super.onOptionsItemSelected(item);
}

/**
* Override hardware volume keys and send to Kodi
*/
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (volumeKeyActionHandler == null) {
volumeKeyActionHandler = new VolumeKeyActionHandler(hostManager, this, this);
}
return volumeKeyActionHandler.handleDispatchKeyEvent(event) || super.dispatchKeyEvent(
event);
}

private void showVolumeChangeDialog() {
VolumeControllerDialogFragmentListener volumeControllerDialogFragment =
new VolumeControllerDialogFragmentListener();
volumeControllerDialogFragment.show(getSupportFragmentManager(),
VolumeControllerDialogFragmentListener.class.getName());
}

/**
Expand Down Expand Up @@ -704,4 +693,16 @@ private void refreshPlaylist() {
playlistFragment.forceRefreshPlaylist();
}
}

@Override
public void onHardwareVolumeKeyPressed() {
int currentPage = viewPager.getCurrentItem();
if (!isPageWithVolumeController(currentPage)) {
showVolumeChangeDialog();
}
}

private boolean isPageWithVolumeController(int currentPage) {
return currentPage == 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.xbmc.kore.ui.volumecontrollers;

public interface OnHardwareVolumeKeyPressedCallback {
void onHardwareVolumeKeyPressed();
}
Loading

0 comments on commit 108fb88

Please sign in to comment.