Use an enum for swipe actions

This commit is contained in:
Christopher Eby 2011-10-21 17:35:17 -05:00
parent 344017035c
commit 87514b4107
8 changed files with 111 additions and 73 deletions

View File

@ -198,7 +198,6 @@ THE SOFTWARE.
<item>Forrige Låt</item>
<item>Ters Gjenta</item>
<item>Ters Stokke</item>
<item>Ters Tilfeldig</item>
<item>Kø Aktuelle Album</item>
<item>Kø Aktuelle Artist</item>
<item>Kø Aktuelle Gjanger</item>

View File

@ -204,7 +204,6 @@ THE SOFTWARE.
<item>Nasledujúca skladba</item>
<item>Predchádzajúca skladba</item>
<item>Prepnúť opakovanie</item>
<item>Prepnúť náhodné usporiadanie</item>
<item>Prepnúť náhodný výber</item>
<item>Pridať do zoznamu aktuálny album</item>
<item>Pridať do zoznamu aktuálneho interpreta</item>

View File

@ -178,6 +178,7 @@ THE SOFTWARE.
<item>Info In Immobile Layer (Fullscreen Cover)</item>
</string-array>
<string-array name="swipe_action_entries">
<!-- This must match the order of swipe_action_values exactly -->
<item>Do Nothing</item>
<item>Open Library</item>
<item>Play/Pause</item>
@ -185,7 +186,6 @@ THE SOFTWARE.
<item>Previous Song</item>
<item>Toggle Repeat</item>
<item>Toggle Shuffle</item>
<item>Toggle Random</item>
<item>Enqueue Current Album</item>
<item>Enqueue Current Artist</item>
<item>Enqueue Current Genre</item>

View File

@ -33,14 +33,21 @@ THE SOFTWARE.
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
<item>9</item>
<item>10</item>
<item>11</item>
<item>12</item>
</string-array>
<string-array name="swipe_action_values">
<!-- This must match the order of swipe_action_entries exactly and
contain only values from PlaybackActivity.Action enum. -->
<item>Nothing</item>
<item>Library</item>
<item>PlayPause</item>
<item>NextSong</item>
<item>PreviousSong</item>
<item>Repeat</item>
<item>Shuffle</item>
<item>EnqueueAlbum</item>
<item>EnqueueArtist</item>
<item>EnqueueGenre</item>
<item>ClearQueue</item>
<item>ToggleControls</item>
</string-array>
</resources>

View File

@ -83,29 +83,29 @@ THE SOFTWARE.
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" />
android:entryValues="@array/swipe_action_values"
android:defaultValue="Nothing" />
<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" />
android:entryValues="@array/swipe_action_values"
android:defaultValue="Nothing" />
<ListPreference
android:key="cover_press_action"
android:title="@string/cover_press_action_title"
android:summary="@string/cover_press_action_summary"
android:entries="@array/swipe_action_entries"
android:entryValues="@array/entry_values"
android:defaultValue="12" />
android:entryValues="@array/swipe_action_values"
android:defaultValue="ToggleControls" />
<ListPreference
android:key="cover_longpress_action"
android:title="@string/cover_longpress_action_title"
android:summary="@string/cover_longpress_action_summary"
android:entries="@array/swipe_action_entries"
android:entryValues="@array/entry_values"
android:defaultValue="2" />
android:entryValues="@array/swipe_action_values"
android:defaultValue="PlayPause" />
<CheckBoxPreference
android:key="disable_cover_art"
android:title="@string/disable_cover_art_title"

View File

@ -41,7 +41,13 @@ import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
public class FullPlaybackActivity extends PlaybackActivity implements SeekBar.OnSeekBarChangeListener {
/**
* The primary playback screen with playback controls and large cover display.
*/
public class FullPlaybackActivity extends PlaybackActivity
implements SeekBar.OnSeekBarChangeListener
, View.OnLongClickListener
{
public static final int DISPLAY_INFO_OVERLAP = 0;
public static final int DISPLAY_INFO_BELOW = 1;
public static final int DISPLAY_INFO_WIDGETS = 2;
@ -84,6 +90,9 @@ public class FullPlaybackActivity extends PlaybackActivity implements SeekBar.On
*/
private int mDisplayMode;
private Action mCoverPressAction;
private Action mCoverLongPressAction;
/**
* Cached StringBuilder for formatting track position.
*/
@ -166,6 +175,9 @@ public class FullPlaybackActivity extends PlaybackActivity implements SeekBar.On
finish();
startActivity(new Intent(this, FullPlaybackActivity.class));
}
mCoverPressAction = getAction(settings, "cover_press_action", Action.ToggleControls);
mCoverLongPressAction = getAction(settings, "cover_longpress_action", Action.PlayPause);
}
@Override
@ -209,8 +221,8 @@ public class FullPlaybackActivity extends PlaybackActivity implements SeekBar.On
view.setClickable(true);
view.setOnClickListener(this);
addContentView(view,
new ViewGroup.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT));
new ViewGroup.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT));
mOverlayText = view;
} else {
mOverlayText.setVisibility(View.VISIBLE);
@ -447,9 +459,9 @@ public class FullPlaybackActivity extends PlaybackActivity implements SeekBar.On
}
@Override
public void performAction(int action)
public void performAction(Action action)
{
if (action == PlaybackActivity.ACTION_TOGGLE_CONTROLS) {
if (action == Action.ToggleControls) {
setControlsVisible(!mControlsVisible);
mHandler.sendEmptyMessage(MSG_SAVE_CONTROLS);
} else {
@ -466,6 +478,9 @@ public class FullPlaybackActivity extends PlaybackActivity implements SeekBar.On
}
switch (view.getId()) {
case R.id.cover_view:
performAction(mCoverPressAction);
break;
case R.id.end_action:
cycleFinishAction();
break;
@ -477,4 +492,15 @@ public class FullPlaybackActivity extends PlaybackActivity implements SeekBar.On
break;
}
}
@Override
public boolean onLongClick(View view)
{
if (view.getId() == R.id.cover_view) {
performAction(mCoverLongPressAction);
return true;
}
return false;
}
}

View File

@ -44,7 +44,6 @@ public class MiniPlaybackActivity extends PlaybackActivity {
mCoverView = (CoverView)findViewById(R.id.cover_view);
mCoverView.setOnClickListener(this);
mCoverView.setOnLongClickListener(this);
mCoverView.setup(mLooper, this, CoverBitmap.STYLE_OVERLAPPING_BOX);
View previousButton = findViewById(R.id.previous);

View File

@ -41,29 +41,33 @@ import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.Toast;
/**
* Base activity for activities that contain playback controls. Handles
* communication with the PlaybackService and response to state and song
* changes.
*/
public class PlaybackActivity extends Activity
implements Handler.Callback,
View.OnClickListener,
View.OnLongClickListener,
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 final int ACTION_ENQUEUE_ALBUM = 8;
public static final int ACTION_ENQUEUE_ARTIST = 9;
public static final int ACTION_ENQUEUE_GENRE = 10;
public static final int ACTION_CLEAR_QUEUE = 11;
public static final int ACTION_TOGGLE_CONTROLS = 12;
enum Action {
Nothing,
Library,
PlayPause,
NextSong,
PreviousSong,
Repeat,
Shuffle,
EnqueueAlbum,
EnqueueArtist,
EnqueueGenre,
ClearQueue,
ToggleControls,
}
private int mUpAction;
private int mDownAction;
private int mCoverPressAction;
private int mCoverLongPressAction;
private Action mUpAction;
private Action mDownAction;
protected Handler mHandler;
protected Looper mLooper;
@ -99,6 +103,26 @@ public class PlaybackActivity extends Activity
super.onDestroy();
}
/**
* Retrieve an action from the given SharedPreferences.
*
* @param prefs The SharedPreferences instance to load from.
* @param key The preference key.
* @param def The value to return if the key is not found or cannot be loaded.
* @return An action
*/
public static Action getAction(SharedPreferences prefs, String key, Action def)
{
try {
String pref = prefs.getString(key, null);
if (pref == null)
return def;
return Enum.valueOf(Action.class, pref);
} catch (Exception e) {
return def;
}
}
@Override
public void onStart()
{
@ -110,10 +134,8 @@ public class PlaybackActivity extends Activity
startService(new Intent(this, PlaybackService.class));
SharedPreferences prefs = PlaybackService.getSettings(this);
mUpAction = Integer.parseInt(prefs.getString("swipe_up_action", "0"));
mDownAction = Integer.parseInt(prefs.getString("swipe_down_action", "0"));
mCoverPressAction = Integer.parseInt(prefs.getString("cover_press_action", "12"));
mCoverLongPressAction = Integer.parseInt(prefs.getString("cover_longpress_action", "2"));
mUpAction = getAction(prefs, "swipe_up_action", Action.Nothing);
mDownAction = getAction(prefs, "swipe_down_action", Action.Nothing);
Window window = getWindow();
if (prefs.getBoolean("disable_lockscreen", false))
@ -194,23 +216,9 @@ public class PlaybackActivity extends Activity
case R.id.previous:
previousSong();
break;
case R.id.cover_view:
performAction(mCoverPressAction);
break;
}
}
@Override
public boolean onLongClick(View view)
{
if (view.getId() == R.id.cover_view) {
performAction(mCoverLongPressAction);
return true;
}
return false;
}
/**
* Called when the PlaybackService state has changed.
*
@ -366,39 +374,39 @@ public class PlaybackActivity extends Activity
PlaybackService.get(this).enqueueFromCurrent(type);
}
public void performAction(int action)
public void performAction(Action action)
{
switch (action) {
case ACTION_NOTHING:
case Nothing:
break;
case ACTION_LIBRARY:
case Library:
openLibrary();
break;
case ACTION_PLAY_PAUSE:
case PlayPause:
playPause();
break;
case ACTION_NEXT_SONG:
case NextSong:
nextSong();
break;
case ACTION_PREVIOUS_SONG:
case PreviousSong:
previousSong();
break;
case ACTION_REPEAT:
case Repeat:
cycleFinishAction();
break;
case ACTION_SHUFFLE:
case Shuffle:
cycleShuffle();
break;
case ACTION_ENQUEUE_ALBUM:
case EnqueueAlbum:
enqueue(MediaUtils.TYPE_ALBUM);
break;
case ACTION_ENQUEUE_ARTIST:
case EnqueueArtist:
enqueue(MediaUtils.TYPE_ARTIST);
break;
case ACTION_ENQUEUE_GENRE:
case EnqueueGenre:
enqueue(MediaUtils.TYPE_GENRE);
break;
case ACTION_CLEAR_QUEUE:
case ClearQueue:
PlaybackService.get(this).clearQueue();
Toast.makeText(this, R.string.queue_cleared, Toast.LENGTH_SHORT).show();
break;