Simplify hardware volume key handling

There's no need to have so many classes to handle the hardware volume keys. This PR simplifies it, without duplicating any more code than was already duplicated.
This commit is contained in:
Synced Synapse 2018-01-30 09:13:21 +00:00 committed by Martijn Brekhof
parent d0bd784629
commit db81d44e62
5 changed files with 54 additions and 133 deletions

View File

@ -49,9 +49,7 @@ import org.xbmc.kore.jsonrpc.type.ListType;
import org.xbmc.kore.jsonrpc.type.PlayerType; import org.xbmc.kore.jsonrpc.type.PlayerType;
import org.xbmc.kore.ui.generic.NavigationDrawerFragment; import org.xbmc.kore.ui.generic.NavigationDrawerFragment;
import org.xbmc.kore.ui.sections.remote.RemoteActivity; import org.xbmc.kore.ui.sections.remote.RemoteActivity;
import org.xbmc.kore.ui.volumecontrollers.OnHardwareVolumeKeyPressedCallback; import org.xbmc.kore.ui.generic.VolumeControllerDialogFragmentListener;
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.MediaProgressIndicator;
import org.xbmc.kore.ui.widgets.NowPlayingPanel; import org.xbmc.kore.ui.widgets.NowPlayingPanel;
import org.xbmc.kore.ui.widgets.VolumeLevelIndicator; import org.xbmc.kore.ui.widgets.VolumeLevelIndicator;
@ -67,8 +65,7 @@ public abstract class BaseMediaActivity extends BaseActivity
implements HostConnectionObserver.ApplicationEventsObserver, implements HostConnectionObserver.ApplicationEventsObserver,
HostConnectionObserver.PlayerEventsObserver, HostConnectionObserver.PlayerEventsObserver,
NowPlayingPanel.OnPanelButtonsClickListener, NowPlayingPanel.OnPanelButtonsClickListener,
MediaProgressIndicator.OnProgressChangeListener, MediaProgressIndicator.OnProgressChangeListener {
OnHardwareVolumeKeyPressedCallback {
private static final String TAG = LogUtils.makeLogTag(BaseMediaActivity.class); private static final String TAG = LogUtils.makeLogTag(BaseMediaActivity.class);
private static final String NAVICON_ISARROW = "navstate"; private static final String NAVICON_ISARROW = "navstate";
@ -84,7 +81,6 @@ public abstract class BaseMediaActivity extends BaseActivity
private HostManager hostManager; private HostManager hostManager;
private HostConnectionObserver hostConnectionObserver; private HostConnectionObserver hostConnectionObserver;
private VolumeKeyActionHandler volumeKeyActionHandler;
private boolean showNowPlayingPanel; private boolean showNowPlayingPanel;
@ -122,7 +118,7 @@ public abstract class BaseMediaActivity extends BaseActivity
.findFragmentById(R.id.navigation_drawer); .findFragmentById(R.id.navigation_drawer);
navigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout)); navigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));
Toolbar toolbar = (Toolbar)findViewById(R.id.default_toolbar); Toolbar toolbar = findViewById(R.id.default_toolbar);
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar(); ActionBar actionBar = getSupportActionBar();
@ -215,23 +211,12 @@ public abstract class BaseMediaActivity extends BaseActivity
*/ */
@Override @Override
public boolean dispatchKeyEvent(KeyEvent event) { public boolean dispatchKeyEvent(KeyEvent event) {
if (volumeKeyActionHandler == null) { boolean handled = VolumeControllerDialogFragmentListener.handleVolumeKeyEvent(this, event);
volumeKeyActionHandler = new VolumeKeyActionHandler(hostManager, this, this); if (handled) {
new VolumeControllerDialogFragmentListener()
.show(getSupportFragmentManager(), VolumeControllerDialogFragmentListener.class.getName());
} }
return volumeKeyActionHandler.handleDispatchKeyEvent(event) || super.dispatchKeyEvent( return handled || super.dispatchKeyEvent(event);
event);
}
@Override
public void onHardwareVolumeKeyPressed() {
showVolumeChangeDialog();
}
private void showVolumeChangeDialog() {
VolumeControllerDialogFragmentListener volumeControllerDialogFragment =
new VolumeControllerDialogFragmentListener();
volumeControllerDialogFragment.show(getSupportFragmentManager(),
VolumeControllerDialogFragmentListener.class.getName());
} }
public boolean getDrawerIndicatorIsArrow() { public boolean getDrawerIndicatorIsArrow() {
@ -240,7 +225,7 @@ public abstract class BaseMediaActivity extends BaseActivity
/** /**
* Sets the title and drawer indicator of the toolbar * Sets the title and drawer indicator of the toolbar
* @param title * @param title toolbar title
* @param showArrowIndicator true if the toolbar should show the back arrow indicator, * @param showArrowIndicator true if the toolbar should show the back arrow indicator,
* false if it should show the drawer icon * false if it should show the drawer icon
*/ */

View File

@ -1,22 +1,26 @@
package org.xbmc.kore.ui.volumecontrollers; package org.xbmc.kore.ui.generic;
import android.app.Dialog; import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatDialogFragment; import android.support.v7.app.AppCompatDialogFragment;
import android.view.KeyEvent;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import org.xbmc.kore.R; import org.xbmc.kore.R;
import org.xbmc.kore.Settings;
import org.xbmc.kore.host.HostConnectionObserver; import org.xbmc.kore.host.HostConnectionObserver;
import org.xbmc.kore.host.HostManager; import org.xbmc.kore.host.HostManager;
import org.xbmc.kore.jsonrpc.ApiCallback; import org.xbmc.kore.jsonrpc.ApiCallback;
import org.xbmc.kore.jsonrpc.ApiMethod; import org.xbmc.kore.jsonrpc.ApiMethod;
import org.xbmc.kore.jsonrpc.method.Application; import org.xbmc.kore.jsonrpc.method.Application;
import org.xbmc.kore.jsonrpc.type.GlobalType;
import org.xbmc.kore.ui.widgets.HighlightButton; import org.xbmc.kore.ui.widgets.HighlightButton;
import org.xbmc.kore.ui.widgets.VolumeLevelIndicator; import org.xbmc.kore.ui.widgets.VolumeLevelIndicator;
import org.xbmc.kore.utils.LogUtils; import org.xbmc.kore.utils.LogUtils;
@ -26,7 +30,7 @@ import butterknife.InjectView;
public class VolumeControllerDialogFragmentListener extends AppCompatDialogFragment public class VolumeControllerDialogFragmentListener extends AppCompatDialogFragment
implements HostConnectionObserver.ApplicationEventsObserver, implements HostConnectionObserver.ApplicationEventsObserver,
OnHardwareVolumeKeyPressedCallback, VolumeLevelIndicator.VolumeBarTouchTrackerListener { VolumeLevelIndicator.VolumeBarTouchTrackerListener {
private static final String TAG = LogUtils.makeLogTag(VolumeControllerDialogFragmentListener.class); private static final String TAG = LogUtils.makeLogTag(VolumeControllerDialogFragmentListener.class);
private static final int AUTO_DISMISS_DELAY = 2000; private static final int AUTO_DISMISS_DELAY = 2000;
@ -39,7 +43,6 @@ public class VolumeControllerDialogFragmentListener extends AppCompatDialogFragm
VolumeLevelIndicator volumeLevelIndicator; VolumeLevelIndicator volumeLevelIndicator;
private Handler callbackHandler = new Handler(); private Handler callbackHandler = new Handler();
private VolumeKeyActionHandler volumeKeyActionHandler;
private HostManager hostManager = null; private HostManager hostManager = null;
private ApiCallback<Integer> defaultIntActionCallback = ApiMethod.getDefaultActionCallback(); private ApiCallback<Integer> defaultIntActionCallback = ApiMethod.getDefaultActionCallback();
private View.OnClickListener onMuteToggleOnClickListener = new View.OnClickListener() { private View.OnClickListener onMuteToggleOnClickListener = new View.OnClickListener() {
@ -79,15 +82,15 @@ public class VolumeControllerDialogFragmentListener extends AppCompatDialogFragm
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
if (volumeKeyActionHandler == null) {
volumeKeyActionHandler = new VolumeKeyActionHandler(hostManager, getContext(), this);
}
getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() { getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override @Override
public boolean onKey(android.content.DialogInterface dialog, int keyCode, public boolean onKey(android.content.DialogInterface dialog, int keyCode,
android.view.KeyEvent event) { android.view.KeyEvent event) {
boolean handled = handleVolumeKeyEvent(getContext(), event);
return volumeKeyActionHandler.handleDispatchKeyEvent(event); if (handled) {
delayedDismissDialog();
}
return handled;
} }
}); });
} }
@ -156,11 +159,6 @@ public class VolumeControllerDialogFragmentListener extends AppCompatDialogFragm
volumeMuteButton.setHighlight(muted); volumeMuteButton.setHighlight(muted);
} }
@Override
public void onHardwareVolumeKeyPressed() {
delayedDismissDialog();
}
private void delayedDismissDialog() { private void delayedDismissDialog() {
cancelDismissDialog(); cancelDismissDialog();
callbackHandler.postDelayed(dismissDialog, AUTO_DISMISS_DELAY); callbackHandler.postDelayed(dismissDialog, AUTO_DISMISS_DELAY);
@ -180,4 +178,28 @@ public class VolumeControllerDialogFragmentListener extends AppCompatDialogFragm
public void onStopTrackingTouch() { public void onStopTrackingTouch() {
delayedDismissDialog(); delayedDismissDialog();
} }
public static boolean handleVolumeKeyEvent(Context context, KeyEvent event) {
boolean shouldInterceptKey =
android.support.v7.preference.PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(Settings.KEY_PREF_USE_HARDWARE_VOLUME_KEYS,
Settings.DEFAULT_PREF_USE_HARDWARE_VOLUME_KEYS);
if (shouldInterceptKey) {
int action = event.getAction();
int keyCode = event.getKeyCode();
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
String volume = (keyCode == KeyEvent.KEYCODE_VOLUME_UP)?
GlobalType.IncrementDecrement.INCREMENT:
GlobalType.IncrementDecrement.DECREMENT;
if (action == KeyEvent.ACTION_DOWN) {
new Application.SetVolume(volume)
.execute(HostManager.getInstance(context).getConnection(), null, null);
}
return true;
}
}
return false;
}
} }

View File

@ -55,9 +55,7 @@ import org.xbmc.kore.ui.generic.NavigationDrawerFragment;
import org.xbmc.kore.ui.generic.SendTextDialogFragment; import org.xbmc.kore.ui.generic.SendTextDialogFragment;
import org.xbmc.kore.ui.sections.hosts.AddHostActivity; import org.xbmc.kore.ui.sections.hosts.AddHostActivity;
import org.xbmc.kore.ui.views.CirclePageIndicator; import org.xbmc.kore.ui.views.CirclePageIndicator;
import org.xbmc.kore.ui.volumecontrollers.OnHardwareVolumeKeyPressedCallback; import org.xbmc.kore.ui.generic.VolumeControllerDialogFragmentListener;
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.LogUtils;
import org.xbmc.kore.utils.TabsAdapter; import org.xbmc.kore.utils.TabsAdapter;
import org.xbmc.kore.utils.UIUtils; import org.xbmc.kore.utils.UIUtils;
@ -78,7 +76,7 @@ import butterknife.InjectView;
public class RemoteActivity extends BaseActivity public class RemoteActivity extends BaseActivity
implements HostConnectionObserver.PlayerEventsObserver, implements HostConnectionObserver.PlayerEventsObserver,
NowPlayingFragment.NowPlayingListener, NowPlayingFragment.NowPlayingListener,
SendTextDialogFragment.SendTextDialogListener, OnHardwareVolumeKeyPressedCallback { SendTextDialogFragment.SendTextDialogListener {
private static final String TAG = LogUtils.makeLogTag(RemoteActivity.class); private static final String TAG = LogUtils.makeLogTag(RemoteActivity.class);
@ -98,8 +96,6 @@ public class RemoteActivity extends BaseActivity
private NavigationDrawerFragment navigationDrawerFragment; private NavigationDrawerFragment navigationDrawerFragment;
private VolumeKeyActionHandler volumeKeyActionHandler;
private Future<Boolean> pendingShare; private Future<Boolean> pendingShare;
private Future<Void> awaitingShare; private Future<Void> awaitingShare;
@ -292,18 +288,15 @@ public class RemoteActivity extends BaseActivity
*/ */
@Override @Override
public boolean dispatchKeyEvent(KeyEvent event) { public boolean dispatchKeyEvent(KeyEvent event) {
if (volumeKeyActionHandler == null) { boolean handled = VolumeControllerDialogFragmentListener.handleVolumeKeyEvent(this, event);
volumeKeyActionHandler = new VolumeKeyActionHandler(hostManager, this, this);
}
return volumeKeyActionHandler.handleDispatchKeyEvent(event) || super.dispatchKeyEvent(
event);
}
private void showVolumeChangeDialog() { // Show volume change dialog if the event was handled and we are not in
VolumeControllerDialogFragmentListener volumeControllerDialogFragment = // first page, which already contains a volume control
new VolumeControllerDialogFragmentListener(); if (handled && (viewPager.getCurrentItem() != 0)) {
volumeControllerDialogFragment.show(getSupportFragmentManager(), new VolumeControllerDialogFragmentListener()
VolumeControllerDialogFragmentListener.class.getName()); .show(getSupportFragmentManager(), VolumeControllerDialogFragmentListener.class.getName());
}
return handled || super.dispatchKeyEvent(event);
} }
/** /**
@ -656,16 +649,4 @@ public class RemoteActivity extends BaseActivity
playlistFragment.forceRefreshPlaylist(); playlistFragment.forceRefreshPlaylist();
} }
} }
@Override
public void onHardwareVolumeKeyPressed() {
int currentPage = viewPager.getCurrentItem();
if (!isPageWithVolumeController(currentPage)) {
showVolumeChangeDialog();
}
}
private boolean isPageWithVolumeController(int currentPage) {
return currentPage == 0;
}
} }

View File

@ -1,5 +0,0 @@
package org.xbmc.kore.ui.volumecontrollers;
public interface OnHardwareVolumeKeyPressedCallback {
void onHardwareVolumeKeyPressed();
}

View File

@ -1,62 +0,0 @@
package org.xbmc.kore.ui.volumecontrollers;
import android.content.Context;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import org.xbmc.kore.Settings;
import org.xbmc.kore.host.HostManager;
import org.xbmc.kore.jsonrpc.method.Application;
import org.xbmc.kore.jsonrpc.type.GlobalType;
public class VolumeKeyActionHandler {
private final HostManager hostManager;
private final Context context;
private final OnHardwareVolumeKeyPressedCallback onHardwareVolumeKeyPressedCallback;
public VolumeKeyActionHandler(HostManager hostManager, Context context,
@Nullable OnHardwareVolumeKeyPressedCallback onHardwareVolumeKeyPressedCallback) {
this.hostManager = hostManager;
this.context = context;
this.onHardwareVolumeKeyPressedCallback = onHardwareVolumeKeyPressedCallback;
}
public boolean handleDispatchKeyEvent(KeyEvent event) {
if (shouldInterceptKey()) {
int action = event.getAction();
int keyCode = event.getKeyCode();
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
if (action == KeyEvent.ACTION_DOWN) {
notifyCallback();
setVolume(GlobalType.IncrementDecrement.INCREMENT);
}
return true;
}
else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
if (action == KeyEvent.ACTION_DOWN) {
notifyCallback();
setVolume(GlobalType.IncrementDecrement.DECREMENT);
}
return true;
}
}
return false;
}
private void setVolume(String volume) {
new Application.SetVolume(volume).execute(hostManager.getConnection(), null, null);
}
private boolean shouldInterceptKey() {
return android.support.v7.preference.PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(Settings.KEY_PREF_USE_HARDWARE_VOLUME_KEYS,
Settings.DEFAULT_PREF_USE_HARDWARE_VOLUME_KEYS);
}
private void notifyCallback() {
if (onHardwareVolumeKeyPressedCallback != null) {
onHardwareVolumeKeyPressedCallback.onHardwareVolumeKeyPressed();
}
}
}