Make SongSelector an Activity again

This commit is contained in:
Christopher Eby 2010-04-23 22:14:35 -05:00
parent c7e10009c2
commit 2ca9c39f1c
6 changed files with 271 additions and 270 deletions

View File

@ -15,6 +15,9 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="SongSelector"
android:theme="@android:style/Theme.Black.NoTitleBar" />
<activity
android:name="MiniPlaybackActivity"
android:theme="@android:style/Theme.Dialog"
@ -45,4 +48,4 @@
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<supports-screens android:smallScreens="true" />
</manifest>
</manifest>

View File

@ -137,11 +137,15 @@ public final class CoverView extends View implements Handler.Callback {
public void setPlaybackService(IPlaybackService service)
{
mService = service;
try {
mTimelinePos = mService.getTimelinePos();
} catch (RemoteException e) {
mService = null;
if (mService != null) {
try {
mTimelinePos = mService.getTimelinePos();
} catch (RemoteException e) {
mService = null;
}
}
refreshSongs();
}
@ -428,7 +432,6 @@ public final class CoverView extends View implements Handler.Callback {
if (mCallback != null)
mCallback.songChanged(mSongs[STORE_SIZE / 2]);
mHandler.sendMessage(mHandler.obtainMessage(SET_SONG, delta, 0));
mHandler.sendEmptyMessage(i);
}
@ -563,6 +566,7 @@ public final class CoverView extends View implements Handler.Callback {
return true;
}
@Override
public void computeScroll()
{
if (mScroller.computeScrollOffset()) {
@ -575,20 +579,13 @@ public final class CoverView extends View implements Handler.Callback {
}
private static final int GO = 10;
private static final int SET_SONG = 11;
public boolean handleMessage(Message message)
{
try {
switch (message.what) {
case GO:
if (message.arg1 == 0)
mService.toggleFlag(PlaybackService.FLAG_PLAYING);
else
shiftCover(message.arg1);
break;
case SET_SONG:
mService.setCurrentSong(message.arg1);
shiftCover(message.arg1);
break;
default:
int i = message.what;

View File

@ -21,12 +21,10 @@ package org.kreed.vanilla;
import org.kreed.vanilla.IPlaybackService;
import org.kreed.vanilla.R;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.preference.PreferenceManager;
@ -40,12 +38,8 @@ import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
public class FullPlaybackActivity extends PlaybackActivity implements View.OnClickListener, SeekBar.OnSeekBarChangeListener, Handler.Callback, CoverView.Callback {
private IPlaybackService mService;
private Handler mHandler = new Handler(this);
public class FullPlaybackActivity extends PlaybackActivity implements View.OnClickListener, SeekBar.OnSeekBarChangeListener, CoverView.Callback {
private RelativeLayout mMessageOverlay;
private View mControlsTop;
private View mControlsBottom;
@ -60,17 +54,6 @@ public class FullPlaybackActivity extends PlaybackActivity implements View.OnCli
private boolean mSeekBarTracking;
private boolean mPaused;
private static final int SONG_SELECTOR = 8;
private static final int MENU_QUIT = 0;
private static final int MENU_DISPLAY = 1;
private static final int MENU_PREFS = 2;
private static final int MENU_LIBRARY = 3;
private static final int MENU_SHUFFLE = 4;
private static final int MENU_PLAYBACK = 5;
private static final int MENU_REPEAT = 6;
public static final int MENU_SEARCH = 7;
@Override
public void onCreate(Bundle icicle)
{
@ -78,7 +61,7 @@ public class FullPlaybackActivity extends PlaybackActivity implements View.OnCli
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
if (icicle == null && settings.getBoolean("selector_on_startup", false))
showDialog(SONG_SELECTOR);
startActivity(new Intent(this, SongSelector.class));
setContentView(R.layout.full_playback);
@ -162,8 +145,6 @@ public class FullPlaybackActivity extends PlaybackActivity implements View.OnCli
{
super.setService(service);
mService = service;
if (service != null) {
try {
mDuration = service.getDuration();
@ -180,96 +161,47 @@ public class FullPlaybackActivity extends PlaybackActivity implements View.OnCli
if (mService != null) {
try {
mDuration = mService.getDuration();
mHandler.sendEmptyMessage(UPDATE_PROGRESS);
mHandler.sendEmptyMessage(MSG_UPDATE_PROGRESS);
} catch (RemoteException e) {
setService(null);
}
}
if (mSongSelector != null)
mSongSelector.change(intent);
}
public void fillMenu(Menu menu, boolean fromDialog)
{
if (fromDialog) {
menu.add(0, MENU_PLAYBACK, 0, R.string.playback_view).setIcon(android.R.drawable.ic_menu_gallery);
menu.add(0, MENU_SEARCH, 0, R.string.search).setIcon(android.R.drawable.ic_menu_search);
} else {
menu.add(0, MENU_LIBRARY, 0, R.string.library).setIcon(android.R.drawable.ic_menu_add);
menu.add(0, MENU_DISPLAY, 0, R.string.display_mode).setIcon(android.R.drawable.ic_menu_gallery);
}
menu.add(0, MENU_PREFS, 0, R.string.settings).setIcon(android.R.drawable.ic_menu_preferences);
menu.add(0, MENU_SHUFFLE, 0, R.string.shuffle_enable).setIcon(R.drawable.ic_menu_shuffle);
menu.add(0, MENU_REPEAT, 0, R.string.repeat_enable).setIcon(R.drawable.ic_menu_refresh);
menu.add(0, MENU_QUIT, 0, R.string.quit).setIcon(android.R.drawable.ic_menu_close_clear_cancel);
mSongSelector.onServiceChange(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
fillMenu(menu, false);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu)
{
boolean isShuffling = (mState & PlaybackService.FLAG_SHUFFLE) != 0;
menu.findItem(MENU_SHUFFLE).setTitle(isShuffling ? R.string.shuffle_disable : R.string.shuffle_enable);
boolean isRepeating = (mState & PlaybackService.FLAG_REPEAT) != 0;
menu.findItem(MENU_REPEAT).setTitle(isRepeating ? R.string.repeat_disable : R.string.repeat_enable);
return true;
menu.add(0, MENU_LIBRARY, 0, R.string.library).setIcon(android.R.drawable.ic_menu_add);
menu.add(0, MENU_DISPLAY, 0, R.string.display_mode).setIcon(android.R.drawable.ic_menu_gallery);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId()) {
case MENU_QUIT:
ContextApplication.quit(this);
break;
case MENU_SHUFFLE:
mHandler.sendMessage(mHandler.obtainMessage(TOGGLE_FLAG, PlaybackService.FLAG_SHUFFLE, 0));
break;
case MENU_REPEAT:
mHandler.sendMessage(mHandler.obtainMessage(TOGGLE_FLAG, PlaybackService.FLAG_REPEAT, 0));
break;
case MENU_PREFS:
startActivity(new Intent(this, PreferencesActivity.class));
break;
case MENU_PLAYBACK:
dismissDialog(SONG_SELECTOR);
break;
case MENU_LIBRARY:
showDialog(SONG_SELECTOR);
break;
startActivity(new Intent(this, SongSelector.class));
return true;
case MENU_DISPLAY:
mCoverView.toggleDisplayMode();
mHandler.sendEmptyMessage(SAVE_DISPLAY_MODE);
break;
mHandler.sendEmptyMessage(MSG_SAVE_DISPLAY_MODE);
return true;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
@Override
public boolean onSearchRequested()
{
showDialog(SONG_SELECTOR);
startActivity(new Intent(this, SongSelector.class));
return false;
}
@Override
public Dialog onCreateDialog(int id)
{
if (id == SONG_SELECTOR) {
mSongSelector = new SongSelector(this);
return mSongSelector;
}
return null;
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
@ -283,6 +215,9 @@ public class FullPlaybackActivity extends PlaybackActivity implements View.OnCli
return super.onKeyUp(keyCode, event);
}
/**
* Converts a duration in milliseconds to [HH:]MM:SS
*/
private String stringForTime(int ms)
{
int seconds = ms / 1000;
@ -298,6 +233,9 @@ public class FullPlaybackActivity extends PlaybackActivity implements View.OnCli
return String.format("%02d:%02d", minutes, seconds);
}
/**
* Update seek bar progress and schedule another update in one second
*/
private void updateProgress()
{
if (mPaused || mControlsTop.getVisibility() != View.VISIBLE || (mState & PlaybackService.FLAG_PLAYING) == 0)
@ -315,11 +253,13 @@ public class FullPlaybackActivity extends PlaybackActivity implements View.OnCli
mSeekBar.setProgress(mDuration == 0 ? 0 : (int)(1000 * position / mDuration));
mSeekText.setText(stringForTime((int)position) + " / " + stringForTime(mDuration));
// Try to update right when the duration increases by one second
long next = 1000 - position % 1000;
mHandler.removeMessages(UPDATE_PROGRESS);
mHandler.sendEmptyMessageDelayed(UPDATE_PROGRESS, next);
mHandler.removeMessages(MSG_UPDATE_PROGRESS);
mHandler.sendEmptyMessageDelayed(MSG_UPDATE_PROGRESS, next);
}
@Override
public void onClick(View view)
{
if (view == mCoverView) {
@ -339,45 +279,25 @@ public class FullPlaybackActivity extends PlaybackActivity implements View.OnCli
}
}
private static final int UPDATE_PROGRESS = 1;
private static final int SAVE_DISPLAY_MODE = 2;
private static final int TOGGLE_FLAG = 3;
private static final int MSG_UPDATE_PROGRESS = 10;
private static final int MSG_SAVE_DISPLAY_MODE = 11;
@Override
public boolean handleMessage(Message message)
{
switch (message.what) {
case UPDATE_PROGRESS:
case MSG_UPDATE_PROGRESS:
updateProgress();
break;
case SAVE_DISPLAY_MODE:
return true;
case MSG_SAVE_DISPLAY_MODE:
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(FullPlaybackActivity.this);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("separate_info", mCoverView.hasSeparateInfo());
editor.commit();
break;
case TOGGLE_FLAG:
int flag = message.arg1;
boolean enabling = (mState & flag) == 0;
int text = -1;
if (flag == PlaybackService.FLAG_SHUFFLE)
text = enabling ? R.string.shuffle_enabling : R.string.shuffle_disabling;
else if (flag == PlaybackService.FLAG_REPEAT)
text = enabling ? R.string.repeat_enabling : R.string.repeat_disabling;
if (text != -1)
Toast.makeText(FullPlaybackActivity.this, text, Toast.LENGTH_SHORT).show();
try {
mService.toggleFlag(flag);
} catch (RemoteException e) {
setService(null);
}
break;
return true;
default:
return false;
return super.handleMessage(message);
}
return true;
}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
@ -406,4 +326,4 @@ public class FullPlaybackActivity extends PlaybackActivity implements View.OnCli
if (mSongSelector != null)
mSongSelector.updateSong(song);
}
}
}

View File

@ -29,9 +29,11 @@ import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
/**
* Playback activity that displays itself like a dialog, i.e. as a small window
* with a border. Includes a CoverView and control buttons.
*/
public class MiniPlaybackActivity extends PlaybackActivity implements View.OnClickListener {
private View mOpenButton;
private View mKillButton;
private ImageView mPlayPauseButton;
@Override
@ -44,10 +46,10 @@ public class MiniPlaybackActivity extends PlaybackActivity implements View.OnCli
mCoverView = (CoverView)findViewById(R.id.cover_view);
mOpenButton = findViewById(R.id.open_button);
mOpenButton.setOnClickListener(this);
mKillButton = findViewById(R.id.kill_button);
mKillButton.setOnClickListener(this);
View openButton = findViewById(R.id.open_button);
openButton.setOnClickListener(this);
View killButton = findViewById(R.id.kill_button);
killButton.setOnClickListener(this);
View previousButton = findViewById(R.id.previous);
previousButton.setOnClickListener(this);
mPlayPauseButton = (ImageView)findViewById(R.id.play_pause);
@ -65,23 +67,28 @@ public class MiniPlaybackActivity extends PlaybackActivity implements View.OnCli
mPlayPauseButton.setImageResource((state & PlaybackService.FLAG_PLAYING) == 0 ? R.drawable.play : R.drawable.pause);
}
@Override
public void onClick(View view)
{
if (view == mKillButton) {
switch (view.getId()) {
case R.id.kill_button:
ContextApplication.quit(this);
} else if (view == mOpenButton) {
break;
case R.id.open_button:
startActivity(new Intent(this, FullPlaybackActivity.class));
finish();
} else {
break;
default:
super.onClick(view);
}
}
}
/*
/**
* Custom layout that acts like a very simple vertical LinearLayout with
* special case: CoverViews will be made square at all costs.
*
*/
/*
* I would prefer this to be a nested class, but it does not seem like
* Android's layout inflater supports referencing nested classes in XML.
*/

View File

@ -26,14 +26,23 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class PlaybackActivity extends Activity implements ServiceConnection, Handler.Callback {
Handler mHandler = new Handler(this);
public abstract class PlaybackActivity extends Activity implements ServiceConnection {
CoverView mCoverView;
IPlaybackService mService;
int mState;
@Override
public void onCreate(Bundle state)
@ -103,13 +112,17 @@ public abstract class PlaybackActivity extends Activity implements ServiceConnec
try {
switch (view.getId()) {
case R.id.next:
mCoverView.go(1);
if (mCoverView != null)
mCoverView.go(1);
mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_SONG, 1, 0));
break;
case R.id.play_pause:
mCoverView.go(0);
mHandler.sendMessage(mHandler.obtainMessage(MSG_TOGGLE_FLAG, PlaybackService.FLAG_PLAYING, 0));
break;
case R.id.previous:
mCoverView.go(-1);
if (mCoverView != null)
mCoverView.go(-1);
mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_SONG, -1, 0));
break;
}
} catch (RemoteException e) {
@ -118,12 +131,31 @@ public abstract class PlaybackActivity extends Activity implements ServiceConnec
}
}
protected abstract void setState(int state);
/**
* Sets <code>mState</code> to <code>state</code>. Override to implement
* further behavior in subclasses.
*
* @param state PlaybackService state
*/
protected void setState(int state)
{
mState = state;
}
/**
* Sets up components when the PlaybackService is bound to. Override to
* implement further post-connection behavior.
*
* @param service PlaybackService interface
*/
protected void setService(IPlaybackService service)
{
if (service != null) {
mService = service;
if (mCoverView != null)
mCoverView.setPlaybackService(service);
if (service != null) {
try {
setState(service.getState());
} catch (RemoteException e) {
@ -150,9 +182,105 @@ public abstract class PlaybackActivity extends Activity implements ServiceConnec
@Override
public void onReceive(Context context, Intent intent)
{
mCoverView.onReceive(intent);
if (mCoverView != null)
mCoverView.onReceive(intent);
if (PlaybackService.EVENT_CHANGED.equals(intent.getAction()))
onServiceChange(intent);
}
};
}
static final int MENU_QUIT = 0;
static final int MENU_DISPLAY = 1;
static final int MENU_PREFS = 2;
static final int MENU_LIBRARY = 3;
static final int MENU_SHUFFLE = 4;
static final int MENU_PLAYBACK = 5;
static final int MENU_REPEAT = 6;
static final int MENU_SEARCH = 7;
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(0, MENU_PREFS, 0, R.string.settings).setIcon(android.R.drawable.ic_menu_preferences);
menu.add(0, MENU_SHUFFLE, 0, R.string.shuffle_enable).setIcon(R.drawable.ic_menu_shuffle);
menu.add(0, MENU_REPEAT, 0, R.string.repeat_enable).setIcon(R.drawable.ic_menu_refresh);
menu.add(0, MENU_QUIT, 0, R.string.quit).setIcon(android.R.drawable.ic_menu_close_clear_cancel);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu)
{
boolean isShuffling = (mState & PlaybackService.FLAG_SHUFFLE) != 0;
menu.findItem(MENU_SHUFFLE).setTitle(isShuffling ? R.string.shuffle_disable : R.string.shuffle_enable);
boolean isRepeating = (mState & PlaybackService.FLAG_REPEAT) != 0;
menu.findItem(MENU_REPEAT).setTitle(isRepeating ? R.string.repeat_disable : R.string.repeat_enable);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId()) {
case MENU_QUIT:
ContextApplication.quit(this);
return true;
case MENU_SHUFFLE:
mHandler.sendMessage(mHandler.obtainMessage(MSG_TOGGLE_FLAG, PlaybackService.FLAG_SHUFFLE, 0));
return true;
case MENU_REPEAT:
mHandler.sendMessage(mHandler.obtainMessage(MSG_TOGGLE_FLAG, PlaybackService.FLAG_REPEAT, 0));
return true;
case MENU_PREFS:
startActivity(new Intent(this, PreferencesActivity.class));
return true;
default:
return false;
}
}
/**
* Tell PlaybackService to toggle a state flag (passed as arg1) and display
* a message notifying that the flag was toggled.
*/
static final int MSG_TOGGLE_FLAG = 0;
/**
* Tell PlaybackService to move to a song a position delta (passed as arg1).
*/
static final int MSG_SET_SONG = 1;
public boolean handleMessage(Message message)
{
switch (message.what) {
case MSG_TOGGLE_FLAG:
int flag = message.arg1;
boolean enabling = (mState & flag) == 0;
int text = -1;
if (flag == PlaybackService.FLAG_SHUFFLE)
text = enabling ? R.string.shuffle_enabling : R.string.shuffle_disabling;
else if (flag == PlaybackService.FLAG_REPEAT)
text = enabling ? R.string.repeat_enabling : R.string.repeat_disabling;
if (text != -1)
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
try {
mService.toggleFlag(flag);
} catch (RemoteException e) {
setService(null);
}
break;
case MSG_SET_SONG:
try {
mService.setCurrentSong(message.arg1);
} catch (RemoteException e) {
setService(null);
}
break;
default:
return false;
}
return true;
}
}

View File

@ -20,9 +20,7 @@ package org.kreed.vanilla;
import org.kreed.vanilla.R;
import android.app.Dialog;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
@ -31,9 +29,7 @@ import android.graphics.Color;
import android.graphics.drawable.PaintDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.text.Editable;
@ -47,7 +43,6 @@ import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Filter;
import android.widget.LinearLayout;
@ -56,11 +51,7 @@ import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
public class SongSelector extends Dialog implements AdapterView.OnItemClickListener, TextWatcher, View.OnClickListener, TabHost.OnTabChangeListener, Filter.FilterListener, Handler.Callback {
private static final int MSG_INIT = 0;
private Handler mHandler = new Handler(this);
public class SongSelector extends PlaybackActivity implements AdapterView.OnItemClickListener, TextWatcher, View.OnClickListener, TabHost.OnTabChangeListener, Filter.FilterListener {
private TabHost mTabHost;
private View mSearchBox;
@ -74,7 +65,6 @@ public class SongSelector extends Dialog implements AdapterView.OnItemClickListe
private ViewGroup mLimiterViews;
private boolean mOnStartUp;
private int mDefaultAction;
private boolean mDefaultIsLastAction;
@ -91,24 +81,21 @@ public class SongSelector extends Dialog implements AdapterView.OnItemClickListe
ListView view = (ListView)findViewById(id);
view.setOnItemClickListener(this);
view.setOnCreateContextMenuListener(this);
view.setAdapter(new MediaAdapter(getContext(), store, fields, fieldKeys, true));
}
public SongSelector(Context context)
{
super(context, android.R.style.Theme_NoTitleBar);
view.setAdapter(new MediaAdapter(this, store, fields, fieldKeys, true));
}
@Override
public void onCreate(Bundle state)
{
super.onCreate(state);
setContentView(R.layout.song_selector);
mTabHost = (TabHost)findViewById(android.R.id.tabhost);
mTabHost.setup();
mTabHost.setOnTabChangedListener(this);
Resources res = getContext().getResources();
Resources res = getResources();
mTabHost.addTab(mTabHost.newTabSpec("tab_artists").setIndicator(res.getText(R.string.artists), res.getDrawable(R.drawable.tab_artists)).setContent(R.id.artist_list));
mTabHost.addTab(mTabHost.newTabSpec("tab_albums").setIndicator(res.getText(R.string.albums), res.getDrawable(R.drawable.tab_albums)).setContent(R.id.album_list));
mTabHost.addTab(mTabHost.newTabSpec("tab_songs").setIndicator(res.getText(R.string.songs), res.getDrawable(R.drawable.tab_songs)).setContent(R.id.song_list));
@ -129,44 +116,30 @@ public class SongSelector extends Dialog implements AdapterView.OnItemClickListe
@Override
public void onStart()
{
// reset queue pos
Context context = getContext();
context.startService(new Intent(context, PlaybackService.class));
}
super.onStart();
@Override
public void onWindowFocusChanged(boolean hasFocus)
{
if (hasFocus) {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getContext());
int inputType = settings.getBoolean("filter_suggestions", false) ? InputType.TYPE_CLASS_TEXT : InputType.TYPE_TEXT_VARIATION_FILTER;
mTextFilter.setInputType(inputType);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
int inputType = settings.getBoolean("filter_suggestions", false) ? InputType.TYPE_CLASS_TEXT : InputType.TYPE_TEXT_VARIATION_FILTER;
mTextFilter.setInputType(inputType);
mOnStartUp = settings.getBoolean("selector_on_startup", false);
boolean status = settings.getBoolean("selector_status", false);
if (status && mStatus == null) {
mStatus = findViewById(R.id.status);
if (mStatus == null)
mStatus = ((ViewStub)findViewById(R.id.status_stub)).inflate();
mStatusText = (TextView)mStatus.findViewById(R.id.status_text);
mPlayPauseButton = (ControlButton)mStatus.findViewById(R.id.play_pause);
ControlButton next = (ControlButton)mStatus.findViewById(R.id.next);
boolean status = settings.getBoolean("selector_status", false);
if (status && mStatus == null) {
mStatus = findViewById(R.id.status);
if (mStatus == null)
mStatus = ((ViewStub)findViewById(R.id.status_stub)).inflate();
mStatusText = (TextView)mStatus.findViewById(R.id.status_text);
mPlayPauseButton = (ControlButton)mStatus.findViewById(R.id.play_pause);
ControlButton next = (ControlButton)mStatus.findViewById(R.id.next);
mPlayPauseButton.setOnClickListener(this);
next.setOnClickListener(this);
FullPlaybackActivity owner = (FullPlaybackActivity)getOwnerActivity();
updateState(owner.mState);
updateSong(owner.mCoverView.getCurrentSong());
} else if (!status && mStatus != null) {
mStatus.setVisibility(View.GONE);
mStatus = null;
}
mDefaultAction = Integer.parseInt(settings.getString("default_action_int", "0"));
mLastActedId = 0;
mPlayPauseButton.setOnClickListener(this);
next.setOnClickListener(this);
} else if (!status && mStatus != null) {
mStatus.setVisibility(View.GONE);
mStatus = null;
}
mDefaultAction = Integer.parseInt(settings.getString("default_action_int", "0"));
mLastActedId = 0;
}
@Override
@ -177,10 +150,8 @@ public class SongSelector extends Dialog implements AdapterView.OnItemClickListe
if (mSearchBoxVisible) {
mTextFilter.setText("");
setSearchBoxVisible(false);
} else if (mOnStartUp) {
getOwnerActivity().finish();
} else {
dismiss();
finish();
}
return true;
case KeyEvent.KEYCODE_SEARCH:
@ -205,17 +176,17 @@ public class SongSelector extends Dialog implements AdapterView.OnItemClickListe
private void sendSongIntent(MediaAdapter.MediaView view, int action)
{
int res = action == PlaybackService.ACTION_PLAY ? R.string.playing : R.string.enqueued;
String text = getContext().getResources().getString(res, view.getTitle());
Toast.makeText(getContext(), text, Toast.LENGTH_SHORT).show();
String text = getResources().getString(res, view.getTitle());
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
long id = view.getMediaId();
int field = view.getFieldCount();
Intent intent = new Intent(getContext(), PlaybackService.class);
Intent intent = new Intent(this, PlaybackService.class);
intent.putExtra("type", field);
intent.putExtra("action", action);
intent.putExtra("id", id);
getContext().startService(intent);
startService(intent);
mLastActedId = id;
}
@ -237,7 +208,7 @@ public class SongSelector extends Dialog implements AdapterView.OnItemClickListe
if (mediaView.isExpanderPressed())
expand(mediaView);
else if (id == mLastActedId)
dismiss();
finish();
else
sendSongIntent(mediaView, mDefaultAction);
}
@ -278,7 +249,7 @@ public class SongSelector extends Dialog implements AdapterView.OnItemClickListe
PaintDrawable background = new PaintDrawable(Color.GRAY);
background.setCornerRadius(5);
TextView view = new TextView(getContext());
TextView view = new TextView(this);
view.setSingleLine();
view.setEllipsize(TextUtils.TruncateAt.MARQUEE);
view.setText(limiter[i] + " | X");
@ -297,25 +268,15 @@ public class SongSelector extends Dialog implements AdapterView.OnItemClickListe
updateLimiterViews();
}
@Override
public void onClick(View view)
{
int id = view.getId();
if (view == mClearButton) {
if (mTextFilter.getText().length() == 0)
setSearchBoxVisible(false);
else
mTextFilter.setText("");
} else if (id == R.id.play_pause) {
try {
((FullPlaybackActivity)getOwnerActivity()).mCoverView.go(0);
} catch (RemoteException e) {
}
} else if (id == R.id.next) {
try {
((FullPlaybackActivity)getOwnerActivity()).mCoverView.go(1);
} catch (RemoteException e) {
}
} else {
} else if (view.getTag() != null) {
int i = (Integer)view.getTag();
String[] limiter;
if (i == 0) {
@ -333,6 +294,8 @@ public class SongSelector extends Dialog implements AdapterView.OnItemClickListe
}
updateLimiterViews();
} else {
super.onClick(view);
}
}
@ -373,43 +336,27 @@ public class SongSelector extends Dialog implements AdapterView.OnItemClickListe
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
((FullPlaybackActivity)getOwnerActivity()).fillMenu(menu, true);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu)
{
return ((FullPlaybackActivity)getOwnerActivity()).onPrepareOptionsMenu(menu);
menu.add(0, MENU_PLAYBACK, 0, R.string.playback_view).setIcon(android.R.drawable.ic_menu_gallery);
menu.add(0, MENU_SEARCH, 0, R.string.search).setIcon(android.R.drawable.ic_menu_search);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId() == FullPlaybackActivity.MENU_SEARCH) {
switch (item.getItemId()) {
case MENU_SEARCH:
setSearchBoxVisible(!mSearchBoxVisible);
return true;
}
return ((FullPlaybackActivity)getOwnerActivity()).onOptionsItemSelected(item);
}
// onContextItemSelected and friends are not called when they should be. Do
// so here to work around this bug.
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item)
{
switch (featureId) {
case Window.FEATURE_CONTEXT_MENU:
return onContextItemSelected(item);
case Window.FEATURE_OPTIONS_PANEL:
return onOptionsItemSelected(item);
case MENU_PLAYBACK:
startActivity(new Intent(this, FullPlaybackActivity.class));
return true;
default:
return super.onMenuItemSelected(featureId, item);
return super.onOptionsItemSelected(item);
}
}
@ -423,9 +370,12 @@ public class SongSelector extends Dialog implements AdapterView.OnItemClickListe
@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event)
{
return PlaybackActivity.handleKeyLongPress(getContext(), keyCode);
return PlaybackActivity.handleKeyLongPress(this, keyCode);
}
private static final int MSG_INIT = 10;
@Override
public boolean handleMessage(Message message)
{
switch (message.what) {
@ -436,9 +386,9 @@ public class SongSelector extends Dialog implements AdapterView.OnItemClickListe
ListView view = (ListView)findViewById(R.id.song_list);
view.setOnItemClickListener(SongSelector.this);
view.setOnCreateContextMenuListener(SongSelector.this);
view.setAdapter(new SongMediaAdapter(getContext()));
view.setAdapter(new SongMediaAdapter(this));
ContentResolver resolver = ContextApplication.getContext().getContentResolver();
ContentResolver resolver = getContentResolver();
resolver.registerContentObserver(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, true, mObserver);
break;
default:
@ -457,28 +407,6 @@ public class SongSelector extends Dialog implements AdapterView.OnItemClickListe
}
};
public void updateSong(Song song)
{
if (mStatusText != null)
mStatusText.setText(song == null ? getContext().getResources().getText(R.string.none) : song.title);
}
public void updateState(int state)
{
if (mPlayPauseButton != null) {
boolean playing = (state & PlaybackService.FLAG_PLAYING) != 0;
mPlayPauseButton.setImageResource(playing ? R.drawable.pause : R.drawable.play);
}
}
public void change(Intent intent)
{
if (PlaybackService.EVENT_CHANGED.equals(intent.getAction())) {
updateSong((Song)intent.getParcelableExtra("song"));
updateState(intent.getIntExtra("state", 0));
}
}
private void setSearchBoxVisible(boolean visible)
{
mSearchBoxVisible = visible;
@ -486,4 +414,22 @@ public class SongSelector extends Dialog implements AdapterView.OnItemClickListe
if (visible)
mSearchBox.requestFocus();
}
@Override
protected void onServiceChange(Intent intent)
{
if (mStatusText != null) {
Song song = intent.getParcelableExtra("song");
mStatusText.setText(song == null ? getResources().getText(R.string.none) : song.title);
}
}
@Override
protected void setState(int state)
{
if (mPlayPauseButton != null) {
boolean playing = (state & PlaybackService.FLAG_PLAYING) != 0;
mPlayPauseButton.setImageResource(playing ? R.drawable.pause : R.drawable.play);
}
}
}