5 package com.andrew.apollo.activities;
\r
7 import android.content.BroadcastReceiver;
\r
8 import android.content.ComponentName;
\r
9 import android.content.Context;
\r
10 import android.content.Intent;
\r
11 import android.content.IntentFilter;
\r
12 import android.content.ServiceConnection;
\r
13 import android.content.pm.ActivityInfo;
\r
14 import android.content.res.Resources;
\r
15 import android.media.AudioManager;
\r
16 import android.media.audiofx.AudioEffect;
\r
17 import android.os.Bundle;
\r
18 import android.os.IBinder;
\r
19 import android.os.RemoteException;
\r
20 import android.provider.BaseColumns;
\r
21 import android.provider.MediaStore.Audio;
\r
22 import android.support.v4.app.FragmentActivity;
\r
23 import android.support.v4.view.ViewPager;
\r
24 import android.view.Menu;
\r
25 import android.view.MenuInflater;
\r
26 import android.view.MenuItem;
\r
27 import android.widget.FrameLayout;
\r
28 import android.widget.ImageView;
\r
29 import android.widget.TextView;
\r
31 import com.andrew.apollo.AudioPlayerFragment;
\r
32 import com.andrew.apollo.Constants;
\r
33 import com.andrew.apollo.IApolloService;
\r
34 import com.andrew.apollo.R;
\r
35 import com.andrew.apollo.adapters.PagerAdapter;
\r
36 import com.andrew.apollo.list.fragments.TracksFragment;
\r
37 import com.andrew.apollo.preferences.SettingsHolder;
\r
38 import com.andrew.apollo.service.ApolloService;
\r
39 import com.andrew.apollo.service.ServiceToken;
\r
40 import com.andrew.apollo.utils.ApolloUtils;
\r
41 import com.andrew.apollo.utils.MusicUtils;
\r
42 import com.andrew.apollo.utils.ThemeUtils;
\r
45 * @author Andrew Neal
\r
46 * @Note This is the "holder" for the @TracksFragment(queue) and @AudioPlayerFragment
\r
48 public class AudioPlayerHolder extends FragmentActivity implements ServiceConnection, Constants {
\r
50 private ServiceToken mToken;
\r
53 private static final int FAVORITE = 0;
\r
55 private static final int SEARCH = 1;
\r
57 private static final int EFFECTS_PANEL = 0;
\r
60 protected void onCreate(Bundle icicle) {
\r
61 // For the theme chooser and overflow MenuItem
\r
62 if (ThemeUtils.overflowLight(this)) {
\r
63 setTheme(R.style.Apollo_Holo);
\r
65 setTheme(R.style.Apollo_Holo_Light);
\r
67 // Landscape mode on phone isn't ready
\r
68 if (!ApolloUtils.isTablet(this))
\r
69 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
\r
71 // Control Media volume
\r
72 setVolumeControlStream(AudioManager.STREAM_MUSIC);
\r
75 setContentView(R.layout.audio_player_browser);
\r
77 // Set up the colorstrip
\r
80 // Set up the ActionBar
\r
85 super.onCreate(icicle);
\r
89 public void onServiceConnected(ComponentName name, IBinder obj) {
\r
90 MusicUtils.mService = IApolloService.Stub.asInterface(obj);
\r
94 public void onServiceDisconnected(ComponentName name) {
\r
95 MusicUtils.mService = null;
\r
99 * Update the MenuItems in the ActionBar
\r
101 private final BroadcastReceiver mMediaStatusReceiver = new BroadcastReceiver() {
\r
104 public void onReceive(Context context, Intent intent) {
\r
105 invalidateOptionsMenu();
\r
111 protected void onStart() {
\r
113 mToken = MusicUtils.bindToService(this, this);
\r
115 IntentFilter filter = new IntentFilter();
\r
116 filter.addAction(ApolloService.META_CHANGED);
\r
117 filter.addAction(ApolloService.PLAYSTATE_CHANGED);
\r
119 registerReceiver(mMediaStatusReceiver, filter);
\r
124 protected void onStop() {
\r
126 if (MusicUtils.mService != null) {
\r
127 MusicUtils.unbindFromService(mToken);
\r
131 unregisterReceiver(mMediaStatusReceiver);
\r
136 public boolean onCreateOptionsMenu(Menu menu) {
\r
137 menu.add(0, FAVORITE, 0, R.string.cd_favorite).setShowAsAction(
\r
138 MenuItem.SHOW_AS_ACTION_IF_ROOM);
\r
139 menu.add(0, SEARCH, 0, R.string.cd_search).setIcon(R.drawable.apollo_holo_light_search)
\r
140 .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
\r
142 MenuInflater inflater = getMenuInflater();
\r
143 inflater.inflate(R.menu.overflow_now_playing, menu);
\r
144 return super.onCreateOptionsMenu(menu);
\r
148 public boolean onPrepareOptionsMenu(Menu menu) {
\r
149 MenuItem favorite = menu.findItem(FAVORITE);
\r
150 MenuItem search = menu.findItem(SEARCH);
\r
151 if (MusicUtils.mService != null && MusicUtils.getCurrentAudioId() != -1) {
\r
152 if (MusicUtils.isFavorite(this, MusicUtils.getCurrentAudioId())) {
\r
153 favorite.setIcon(R.drawable.apollo_holo_light_favorite_selected);
\r
155 favorite.setIcon(R.drawable.apollo_holo_light_favorite_normal);
\r
157 ThemeUtils.setActionBarItem(this, favorite, "apollo_favorite_normal");
\r
161 ThemeUtils.setActionBarItem(this, search, "apollo_search");
\r
162 return super.onPrepareOptionsMenu(menu);
\r
166 public boolean onOptionsItemSelected(MenuItem item) {
\r
168 switch (item.getItemId()) {
\r
169 case android.R.id.home: {
\r
170 Intent intent = new Intent();
\r
171 intent.setClass(this, MusicLibrary.class);
\r
172 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
\r
173 startActivity(intent);
\r
178 MusicUtils.toggleFavorite();
\r
179 invalidateOptionsMenu();
\r
183 onSearchRequested();
\r
186 case R.id.add_to_playlist: {
\r
187 Intent intent = new Intent(INTENT_ADD_TO_PLAYLIST);
\r
188 long[] list = new long[1];
\r
189 list[0] = MusicUtils.getCurrentAudioId();
\r
190 intent.putExtra(INTENT_PLAYLIST_LIST, list);
\r
191 startActivity(intent);
\r
195 Intent i = new Intent(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL);
\r
196 i.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, MusicUtils.getCurrentAudioId());
\r
197 startActivityForResult(i, EFFECTS_PANEL);
\r
200 case R.id.play_store: {
\r
201 ApolloUtils.shopFor(this, MusicUtils.getArtistName());
\r
205 shareCurrentTrack();
\r
208 case R.id.settings: {
\r
209 startActivity(new Intent(this, SettingsHolder.class));
\r
215 return super.onOptionsItemSelected(item);
\r
218 private void initActionBar() {
\r
219 ApolloUtils.showUpTitleOnly(getActionBar());
\r
221 // The ActionBar Title and UP ids are hidden.
\r
222 int titleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
\r
223 int upId = Resources.getSystem().getIdentifier("up", "id", "android");
\r
225 TextView actionBarTitle = (TextView)findViewById(titleId);
\r
226 ImageView actionBarUp = (ImageView)findViewById(upId);
\r
229 ThemeUtils.setActionBarBackground(this, getActionBar(), "action_bar_background");
\r
230 ThemeUtils.setTextColor(this, actionBarTitle, "action_bar_title_color");
\r
231 ThemeUtils.initThemeChooser(this, actionBarUp, "action_bar_up", THEME_ITEM_BACKGROUND);
\r
235 * @return Share intent
\r
236 * @throws RemoteException
\r
238 private String shareCurrentTrack() {
\r
239 if (MusicUtils.getTrackName() == null || MusicUtils.getArtistName() == null) {
\r
243 Intent shareIntent = new Intent();
\r
244 String currentTrackMessage = getResources().getString(R.string.now_listening_to) + " "
\r
245 + MusicUtils.getTrackName() + " " + getResources().getString(R.string.by) + " "
\r
246 + MusicUtils.getArtistName();
\r
248 shareIntent.setAction(Intent.ACTION_SEND);
\r
249 shareIntent.setType("text/plain");
\r
250 shareIntent.putExtra(Intent.EXTRA_TEXT, currentTrackMessage);
\r
252 startActivity(Intent.createChooser(shareIntent,
\r
253 getResources().getString(R.string.share_track_using)));
\r
254 return currentTrackMessage;
\r
258 * Initiate ViewPager and PagerAdapter
\r
260 public void initPager() {
\r
261 // Initiate PagerAdapter
\r
262 PagerAdapter mPagerAdapter = new PagerAdapter(getSupportFragmentManager());
\r
263 Bundle bundle = new Bundle();
\r
264 bundle.putString(MIME_TYPE, Audio.Playlists.CONTENT_TYPE);
\r
265 bundle.putLong(BaseColumns._ID, PLAYLIST_QUEUE);
\r
266 mPagerAdapter.addFragment(new TracksFragment(bundle));
\r
268 mPagerAdapter.addFragment(new AudioPlayerFragment());
\r
270 // Initiate ViewPager
\r
271 ViewPager mViewPager = (ViewPager)findViewById(R.id.viewPager);
\r
272 mViewPager.setPageMargin(getResources().getInteger(R.integer.viewpager_margin_width));
\r
273 mViewPager.setPageMarginDrawable(R.drawable.viewpager_margin);
\r
274 mViewPager.setOffscreenPageLimit(mPagerAdapter.getCount());
\r
275 mViewPager.setAdapter(mPagerAdapter);
\r
276 mViewPager.setCurrentItem(1);
\r
279 ThemeUtils.initThemeChooser(this, mViewPager, "viewpager", THEME_ITEM_BACKGROUND);
\r
280 ThemeUtils.setMarginDrawable(this, mViewPager, "viewpager_margin");
\r
284 * For the theme chooser
\r
286 private void initColorstrip() {
\r
287 FrameLayout mColorstrip = (FrameLayout)findViewById(R.id.colorstrip);
\r
288 mColorstrip.setBackgroundColor(getResources().getColor(R.color.holo_blue_dark));
\r
289 ThemeUtils.setBackgroundColor(this, mColorstrip, "colorstrip");
\r