Add configurable up/down swipe actions

More actions to come...
This commit is contained in:
Christopher Eby committed 2011-09-03 00:31:55 -05:00
1 parent 9bc3e97e9d
commit 3f99f98a56
7 files changed
+167 -28

No files matched your search

+12
View File
@@ -42,6 +42,15 @@ THE SOFTWARE.
<item>Info In Immobile Layer</item>
<item>Info In Immobile Layer (Fullscreen Cover)</item>
</string-array>
<string-array name="swipe_action_entries">
<item>Do Nothing</item>
<item>Open Library</item>
<item>Play/Pause</item>
<item>Next Song</item>
<item>Previous Song</item>
<item>Toggle Repeat</item>
<item>Toggle Shuffle</item>
</string-array>
<string-array name="entry_values">
<!-- Note: even if this was an integer-array, these would be converted
to strings anyway.
@@ -50,5 +59,8 @@ THE SOFTWARE.
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
</string-array>
</resources>
+4
View File
@@ -118,6 +118,10 @@ THE SOFTWARE.
<string name="pref_playback_view">Playback View</string>
<string name="display_mode_title">Display Mode</string>
<string name="display_mode_summary">The appearance and position of the song art and info.</string>
<string name="swipe_up_action_title">Swipe Up Action</string>
<string name="swipe_up_action_summary">What to do when a swipe up gesture is performed</string>
<string name="swipe_down_action_title">Swipe Down Action</string>
<string name="swipe_down_action_summary">What to do when a swipe down gesture is performed</string>
<string name="disable_cover_art_title">Disable Cover Art</string>
<string name="disable_cover_art_summary">Do not show cover art anywhere in the application</string>
+14
View File
@@ -68,6 +68,20 @@ THE SOFTWARE.
android:entries="@array/display_mode_entries"
android:entryValues="@array/entry_values"
android:defaultValue="0" />
<ListPreference
android:key="swipe_up_action"
android:title="@string/swipe_up_action_title"
android:summary="@string/swipe_up_action_summary"
android:entries="@array/swipe_action_entries"
android:entryValues="@array/entry_values"
android:defaultValue="0" />
<ListPreference
android:key="swipe_down_action"
android:title="@string/swipe_down_action_title"
android:summary="@string/swipe_down_action_summary"
android:entries="@array/swipe_action_entries"
android:entryValues="@array/entry_values"
android:defaultValue="0" />
<CheckBoxPreference
android:key="disable_cover_art"
android:title="@string/disable_cover_art_title"
+41 -22
View File
@@ -61,6 +61,8 @@ public final class CoverView extends View implements Handler.Callback {
public interface Callback {
public void nextSong();
public void previousSong();
public void upSwipe();
public void downSwipe();
}
private Callback mCallback;
@@ -78,6 +80,7 @@ public final class CoverView extends View implements Handler.Callback {
private Scroller mScroller;
private VelocityTracker mVelocityTracker;
private float mLastMotionX;
private float mLastMotionY;
private float mStartX;
private float mStartY;
private int mTentativeCover = -1;
@@ -227,37 +230,35 @@ public final class CoverView extends View implements Handler.Callback {
mStartX = x;
mStartY = y;
mLastMotionX = x;
mLastMotionY = y;
mHandler.sendEmptyMessageDelayed(MSG_LONG_CLICK, ViewConfiguration.getLongPressTimeout());
break;
case MotionEvent.ACTION_MOVE: {
int deltaX = (int) (mLastMotionX - x);
mLastMotionX = x;
float deltaX = mLastMotionX - x;
float deltaY = mLastMotionY - y;
if (deltaX < 0) {
int availableToScroll = scrollX - (mTimelinePos == 0 ? width : 0);
if (availableToScroll > 0)
scrollBy(Math.max(-availableToScroll, deltaX), 0);
} else if (deltaX > 0) {
int availableToScroll = getWidth() * 2 - scrollX;
if (availableToScroll > 0)
scrollBy(Math.min(availableToScroll, deltaX), 0);
if (Math.abs(deltaX) > Math.abs(deltaY)) {
if (deltaX < 0) {
int availableToScroll = scrollX - (mTimelinePos == 0 ? width : 0);
if (availableToScroll > 0)
scrollBy(Math.max(-availableToScroll, (int)deltaX), 0);
} else if (deltaX > 0) {
int availableToScroll = width * 2 - scrollX;
if (availableToScroll > 0)
scrollBy(Math.min(availableToScroll, (int)deltaX), 0);
}
}
if (Math.abs(mStartX - x) + Math.abs(mStartY - y) > 10)
mHandler.removeMessages(MSG_LONG_CLICK);
mLastMotionX = x;
mLastMotionY = y;
break;
}
case MotionEvent.ACTION_UP: {
mHandler.removeMessages(MSG_LONG_CLICK);
if (Math.abs(mStartX - x) + Math.abs(mStartY - y) < 10) {
// A long press was performed and thus the normal action should
// not be executed.
if (mIgnoreNextUp)
mIgnoreNextUp = false;
else
performClick();
}
VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(250);
@@ -268,10 +269,28 @@ public final class CoverView extends View implements Handler.Callback {
int nearestCover = (scrollX + width / 2) / width;
int whichCover = Math.max(min, Math.min(nearestCover, max));
if (velocity > SNAP_VELOCITY && whichCover != min)
--whichCover;
else if (velocity < -SNAP_VELOCITY && whichCover != max)
++whichCover;
if (velocity > SNAP_VELOCITY)
whichCover = min;
else if (velocity < -SNAP_VELOCITY)
whichCover = max;
float diffX = Math.abs(mStartX - x);
float diffY = Math.abs(mStartY - y);
if (diffX + diffY < 10) {
// A long press was performed and thus the normal action should
// not be executed.
if (mIgnoreNextUp)
mIgnoreNextUp = false;
else
performClick();
whichCover = 1;
} else if (diffY > diffX) {
if (mStartY - y > 0)
mCallback.upSwipe();
else
mCallback.downSwipe();
whichCover = 1;
}
int newX = whichCover * width;
int delta = newX - scrollX;
@@ -243,7 +243,7 @@ public class FullPlaybackActivity extends PlaybackActivity implements SeekBar.On
{
switch (item.getItemId()) {
case MENU_LIBRARY:
startActivity(new Intent(this, SongSelector.class));
openLibrary();
return true;
default:
return super.onOptionsItemSelected(item);
@@ -253,7 +253,7 @@ public class FullPlaybackActivity extends PlaybackActivity implements SeekBar.On
@Override
public boolean onSearchRequested()
{
startActivity(new Intent(this, SongSelector.class));
openLibrary();
return false;
}
@@ -360,7 +360,7 @@ public class FullPlaybackActivity extends PlaybackActivity implements SeekBar.On
public boolean onLongClick(View view)
{
if (view.getId() == R.id.cover_view) {
setState(ContextApplication.getService().toggleFlag(PlaybackService.FLAG_PLAYING));
playPause();
return true;
}
+89 -3
View File
@@ -24,18 +24,31 @@ package org.kreed.vanilla;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class PlaybackActivity extends Activity implements Handler.Callback, View.OnClickListener, CoverView.Callback {
public static final int ACTION_NOTHING = 0;
public static final int ACTION_LIBRARY = 1;
public static final int ACTION_PLAY_PAUSE = 2;
public static final int ACTION_NEXT_SONG = 3;
public static final int ACTION_PREVIOUS_SONG = 4;
public static final int ACTION_REPEAT = 5;
public static final int ACTION_SHUFFLE = 6;
public static int mUpAction;
public static int mDownAction;
protected Handler mHandler;
protected Looper mLooper;
@@ -55,6 +68,10 @@ public class PlaybackActivity extends Activity implements Handler.Callback, View
HandlerThread thread = new HandlerThread(getClass().getName());
thread.start();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
mUpAction = Integer.parseInt(prefs.getString("swipe_up_action", "0"));
mDownAction = Integer.parseInt(prefs.getString("swipe_down_action", "0"));
mLooper = thread.getLooper();
mHandler = new Handler(mLooper, this);
}
@@ -147,6 +164,11 @@ public class PlaybackActivity extends Activity implements Handler.Callback, View
setSong(ContextApplication.getService().previousSong());
}
public void playPause()
{
setState(ContextApplication.getService().toggleFlag(PlaybackService.FLAG_PLAYING));
}
public void onClick(View view)
{
switch (view.getId()) {
@@ -154,7 +176,7 @@ public class PlaybackActivity extends Activity implements Handler.Callback, View
nextSong();
break;
case R.id.play_pause:
setState(ContextApplication.getService().toggleFlag(PlaybackService.FLAG_PLAYING));
playPause();
break;
case R.id.previous:
previousSong();
@@ -294,10 +316,10 @@ public class PlaybackActivity extends Activity implements Handler.Callback, View
ContextApplication.quit();
return true;
case MENU_SHUFFLE:
setState(ContextApplication.getService().toggleFlag(PlaybackService.FLAG_SHUFFLE));
toggleShuffle();
return true;
case MENU_REPEAT:
setState(ContextApplication.getService().toggleFlag(PlaybackService.FLAG_REPEAT));
toggleRepeat();
return true;
case MENU_PREFS:
startActivity(new Intent(this, PreferencesActivity.class));
@@ -312,4 +334,68 @@ public class PlaybackActivity extends Activity implements Handler.Callback, View
{
return false;
}
/**
* Toggle shuffle mode on/off
*/
public void toggleShuffle()
{
setState(ContextApplication.getService().toggleFlag(PlaybackService.FLAG_SHUFFLE));
}
/**
* Toggle repeat mode on/off
*/
public void toggleRepeat()
{
setState(ContextApplication.getService().toggleFlag(PlaybackService.FLAG_REPEAT));
}
/**
* Open the library activity.
*/
public void openLibrary()
{
startActivity(new Intent(this, SongSelector.class));
}
public void performAction(int action)
{
switch (action) {
case ACTION_NOTHING:
break;
case ACTION_LIBRARY:
openLibrary();
break;
case ACTION_PLAY_PAUSE:
playPause();
break;
case ACTION_NEXT_SONG:
nextSong();
break;
case ACTION_PREVIOUS_SONG:
previousSong();
break;
case ACTION_REPEAT:
toggleRepeat();
break;
case ACTION_SHUFFLE:
toggleShuffle();
break;
default:
throw new IllegalArgumentException("Invalid action: " + action);
}
}
@Override
public void upSwipe()
{
performAction(mUpAction);
}
@Override
public void downSwipe()
{
performAction(mDownAction);
}
}
@@ -425,6 +425,10 @@ public final class PlaybackService extends Service implements Handler.Callback,
}
} else if ("notification_inverted_color".equals(key)) {
updateNotification();
} else if ("swipe_up_action".equals(key)) {
PlaybackActivity.mUpAction = Integer.parseInt(settings.getString(key, "0"));
} else if ("swipe_down_action".equals(key)) {
PlaybackActivity.mDownAction = Integer.parseInt(settings.getString(key, "0"));
}
}