Skip to content

Commit

Permalink
Simplify hardware volume key handling
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
SyncedSynapse authored and poisdeux committed Feb 6, 2018
1 parent d0bd784 commit db81d44
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 133 deletions.
33 changes: 9 additions & 24 deletions app/src/main/java/org/xbmc/kore/ui/BaseMediaActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@
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.generic.VolumeControllerDialogFragmentListener;
import org.xbmc.kore.ui.widgets.MediaProgressIndicator;
import org.xbmc.kore.ui.widgets.NowPlayingPanel;
import org.xbmc.kore.ui.widgets.VolumeLevelIndicator;
Expand All @@ -67,8 +65,7 @@ public abstract class BaseMediaActivity extends BaseActivity
implements HostConnectionObserver.ApplicationEventsObserver,
HostConnectionObserver.PlayerEventsObserver,
NowPlayingPanel.OnPanelButtonsClickListener,
MediaProgressIndicator.OnProgressChangeListener,
OnHardwareVolumeKeyPressedCallback {
MediaProgressIndicator.OnProgressChangeListener {
private static final String TAG = LogUtils.makeLogTag(BaseMediaActivity.class);

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

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

private boolean showNowPlayingPanel;

Expand Down Expand Up @@ -122,7 +118,7 @@ protected void onCreate(Bundle savedInstanceState) {
.findFragmentById(R.id.navigation_drawer);
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);

ActionBar actionBar = getSupportActionBar();
Expand Down Expand Up @@ -215,23 +211,12 @@ public void onPause() {
*/
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (volumeKeyActionHandler == null) {
volumeKeyActionHandler = new VolumeKeyActionHandler(hostManager, this, this);
boolean handled = VolumeControllerDialogFragmentListener.handleVolumeKeyEvent(this, event);
if (handled) {
new VolumeControllerDialogFragmentListener()
.show(getSupportFragmentManager(), VolumeControllerDialogFragmentListener.class.getName());
}
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());
return handled || super.dispatchKeyEvent(event);
}

public boolean getDrawerIndicatorIsArrow() {
Expand All @@ -240,7 +225,7 @@ public boolean getDrawerIndicatorIsArrow() {

/**
* 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,
* false if it should show the drawer icon
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
package org.xbmc.kore.ui.volumecontrollers;
package org.xbmc.kore.ui.generic;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatDialogFragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.xbmc.kore.R;
import org.xbmc.kore.Settings;
import org.xbmc.kore.host.HostConnectionObserver;
import org.xbmc.kore.host.HostManager;
import org.xbmc.kore.jsonrpc.ApiCallback;
import org.xbmc.kore.jsonrpc.ApiMethod;
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.VolumeLevelIndicator;
import org.xbmc.kore.utils.LogUtils;
Expand All @@ -26,7 +30,7 @@

public class VolumeControllerDialogFragmentListener extends AppCompatDialogFragment
implements HostConnectionObserver.ApplicationEventsObserver,
OnHardwareVolumeKeyPressedCallback, VolumeLevelIndicator.VolumeBarTouchTrackerListener {
VolumeLevelIndicator.VolumeBarTouchTrackerListener {

private static final String TAG = LogUtils.makeLogTag(VolumeControllerDialogFragmentListener.class);
private static final int AUTO_DISMISS_DELAY = 2000;
Expand All @@ -39,7 +43,6 @@ public class VolumeControllerDialogFragmentListener extends AppCompatDialogFragm
VolumeLevelIndicator volumeLevelIndicator;

private Handler callbackHandler = new Handler();
private VolumeKeyActionHandler volumeKeyActionHandler;
private HostManager hostManager = null;
private ApiCallback<Integer> defaultIntActionCallback = ApiMethod.getDefaultActionCallback();
private View.OnClickListener onMuteToggleOnClickListener = new View.OnClickListener() {
Expand Down Expand Up @@ -79,15 +82,15 @@ public void run() {
@Override
public void onResume() {
super.onResume();
if (volumeKeyActionHandler == null) {
volumeKeyActionHandler = new VolumeKeyActionHandler(hostManager, getContext(), this);
}
getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(android.content.DialogInterface dialog, int keyCode,
android.view.KeyEvent event) {

return volumeKeyActionHandler.handleDispatchKeyEvent(event);
boolean handled = handleVolumeKeyEvent(getContext(), event);
if (handled) {
delayedDismissDialog();
}
return handled;
}
});
}
Expand Down Expand Up @@ -156,11 +159,6 @@ public void applicationOnVolumeChanged(int volume, boolean muted) {
volumeMuteButton.setHighlight(muted);
}

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

private void delayedDismissDialog() {
cancelDismissDialog();
callbackHandler.postDelayed(dismissDialog, AUTO_DISMISS_DELAY);
Expand All @@ -180,4 +178,28 @@ public void onStartTrackingTouch() {
public void onStopTrackingTouch() {
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@
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.ui.generic.VolumeControllerDialogFragmentListener;
import org.xbmc.kore.utils.LogUtils;
import org.xbmc.kore.utils.TabsAdapter;
import org.xbmc.kore.utils.UIUtils;
Expand All @@ -78,7 +76,7 @@
public class RemoteActivity extends BaseActivity
implements HostConnectionObserver.PlayerEventsObserver,
NowPlayingFragment.NowPlayingListener,
SendTextDialogFragment.SendTextDialogListener, OnHardwareVolumeKeyPressedCallback {
SendTextDialogFragment.SendTextDialogListener {
private static final String TAG = LogUtils.makeLogTag(RemoteActivity.class);


Expand All @@ -98,8 +96,6 @@ public class RemoteActivity extends BaseActivity

private NavigationDrawerFragment navigationDrawerFragment;

private VolumeKeyActionHandler volumeKeyActionHandler;

private Future<Boolean> pendingShare;

private Future<Void> awaitingShare;
Expand Down Expand Up @@ -292,18 +288,15 @@ public boolean onOptionsItemSelected(MenuItem item) {
*/
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (volumeKeyActionHandler == null) {
volumeKeyActionHandler = new VolumeKeyActionHandler(hostManager, this, this);
}
return volumeKeyActionHandler.handleDispatchKeyEvent(event) || super.dispatchKeyEvent(
event);
}
boolean handled = VolumeControllerDialogFragmentListener.handleVolumeKeyEvent(this, event);

private void showVolumeChangeDialog() {
VolumeControllerDialogFragmentListener volumeControllerDialogFragment =
new VolumeControllerDialogFragmentListener();
volumeControllerDialogFragment.show(getSupportFragmentManager(),
VolumeControllerDialogFragmentListener.class.getName());
// Show volume change dialog if the event was handled and we are not in
// first page, which already contains a volume control
if (handled && (viewPager.getCurrentItem() != 0)) {
new VolumeControllerDialogFragmentListener()
.show(getSupportFragmentManager(), VolumeControllerDialogFragmentListener.class.getName());
}
return handled || super.dispatchKeyEvent(event);
}

/**
Expand Down Expand Up @@ -656,16 +649,4 @@ 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;
}
}

This file was deleted.

This file was deleted.

0 comments on commit db81d44

Please sign in to comment.