2 * Copyright (C) 2014 The CyanogenMod Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.lineageos.eleven.ui.activities;
18 import android.Manifest;
19 import android.animation.ArgbEvaluator;
20 import android.animation.ObjectAnimator;
21 import android.content.Intent;
22 import android.content.pm.PackageManager;
23 import android.graphics.Bitmap;
24 import android.graphics.Color;
25 import android.net.Uri;
26 import android.os.AsyncTask;
27 import android.os.Build;
28 import android.os.Bundle;
29 import android.os.Handler;
30 import android.provider.MediaStore;
31 import android.support.v4.app.Fragment;
32 import android.support.v4.app.FragmentManager;
33 import android.support.v4.app.FragmentTransaction;
34 import android.text.TextUtils;
35 import android.util.Log;
36 import android.view.MenuItem;
37 import android.view.Window;
39 import org.lineageos.eleven.Config;
40 import org.lineageos.eleven.R;
41 import org.lineageos.eleven.cache.ImageFetcher;
42 import org.lineageos.eleven.ui.fragments.AlbumDetailFragment;
43 import org.lineageos.eleven.ui.fragments.ArtistDetailFragment;
44 import org.lineageos.eleven.ui.fragments.AudioPlayerFragment;
45 import org.lineageos.eleven.ui.fragments.IChildFragment;
46 import org.lineageos.eleven.ui.fragments.ISetupActionBar;
47 import org.lineageos.eleven.ui.fragments.PlaylistDetailFragment;
48 import org.lineageos.eleven.ui.fragments.RecentFragment;
49 import org.lineageos.eleven.ui.fragments.phone.MusicBrowserPhoneFragment;
50 import org.lineageos.eleven.ui.fragments.profile.LastAddedFragment;
51 import org.lineageos.eleven.ui.fragments.profile.TopTracksFragment;
52 import org.lineageos.eleven.utils.ElevenUtils;
53 import org.lineageos.eleven.utils.BitmapWithColors;
54 import org.lineageos.eleven.utils.MusicUtils;
55 import org.lineageos.eleven.utils.NavUtils;
57 import java.util.ArrayList;
59 public class HomeActivity extends SlidingPanelActivity implements
60 FragmentManager.OnBackStackChangedListener {
61 private static final String TAG = "HomeActivity";
62 private static final String ACTION_PREFIX = HomeActivity.class.getName();
63 public static final String ACTION_VIEW_ARTIST_DETAILS = ACTION_PREFIX + ".view.ArtistDetails";
64 public static final String ACTION_VIEW_ALBUM_DETAILS = ACTION_PREFIX + ".view.AlbumDetails";
65 public static final String ACTION_VIEW_PLAYLIST_DETAILS = ACTION_PREFIX + ".view.PlaylistDetails";
66 public static final String ACTION_VIEW_SMART_PLAYLIST = ACTION_PREFIX + ".view.SmartPlaylist";
67 public static final String EXTRA_BROWSE_PAGE_IDX = "BrowsePageIndex";
69 private static final String STATE_KEY_BASE_FRAGMENT = "BaseFragment";
71 private static final int NEW_PHOTO = 1;
72 public static final int EQUALIZER = 2;
74 private static final int PERMISSION_REQUEST_STORAGE = 1;
75 private Bundle mSavedInstanceState;
78 private boolean mLoadedBaseFragment = false;
79 private boolean mHasPendingPlaybackRequest = false;
80 private Handler mHandler = new Handler();
81 private boolean mBrowsePanelActive = true;
84 * Used by the up action to determine how to handle this
86 protected boolean mTopLevelActivity = false;
89 protected void onCreate(Bundle savedInstanceState) {
90 super.onCreate(savedInstanceState);
92 mSavedInstanceState = savedInstanceState;
94 if (!needRequestStoragePermission()) {
100 // if we've been launched by an intent, parse it
101 Intent launchIntent = getIntent();
102 boolean intentHandled = false;
103 if (launchIntent != null) {
104 intentHandled = parseIntentForFragment(launchIntent);
107 // if the intent didn't cause us to load a fragment, load the music browse one
108 if (mSavedInstanceState == null && !mLoadedBaseFragment) {
109 final MusicBrowserPhoneFragment fragment = new MusicBrowserPhoneFragment();
110 if (launchIntent != null) {
111 fragment.setDefaultPageIdx(launchIntent.getIntExtra(EXTRA_BROWSE_PAGE_IDX,
112 MusicBrowserPhoneFragment.INVALID_PAGE_INDEX));
114 getSupportFragmentManager().beginTransaction()
115 .replace(R.id.activity_base_content, fragment)
118 mLoadedBaseFragment = true;
119 mTopLevelActivity = true;
122 getSupportFragmentManager().addOnBackStackChangedListener(this);
125 // if we are resuming from a saved instance state
126 if (mSavedInstanceState != null) {
127 // track which fragments are loaded and if this is the top level activity
128 mTopLevelActivity = mSavedInstanceState.getBoolean(STATE_KEY_BASE_FRAGMENT);
129 mLoadedBaseFragment = mTopLevelActivity;
131 // update the action bar based on the top most fragment
132 onBackStackChanged();
134 // figure which panel we are on and update the status bar
135 mBrowsePanelActive = (getCurrentPanel() == Panel.Browse);
136 updateStatusBarColor();
139 // if intent wasn't UI related, process it as a audio playback request
140 if (!intentHandled) {
141 handlePlaybackIntent(launchIntent);
144 mSavedInstanceState = null;
148 protected void onSaveInstanceState(Bundle outState) {
149 super.onSaveInstanceState(outState);
150 outState.putBoolean(STATE_KEY_BASE_FRAGMENT, mTopLevelActivity);
153 public Fragment getTopFragment() {
154 return getSupportFragmentManager().findFragmentById(R.id.activity_base_content);
157 public void postRemoveFragment(final Fragment frag) {
158 mHandler.post(new Runnable() {
161 // removing the fragment doesn't cause the backstack event to be triggered even if
162 // it is the top fragment, so if it is the top fragment, we will just manually
163 // call pop back stack
164 if (frag == getTopFragment()) {
165 getSupportFragmentManager().popBackStack();
167 getSupportFragmentManager().beginTransaction().remove(frag).commit();
174 protected void onNewIntent(Intent intent) {
175 super.onNewIntent(intent);
177 // parse intent to ascertain whether the intent is inter UI communication
178 boolean intentHandled = parseIntentForFragment(intent);
179 // since this activity is marked 'singleTop' (launch mode), an existing activity instance
180 // could be sent media play requests
181 if ( !intentHandled) {
182 handlePlaybackIntent(intent);
187 public void onMetaChanged() {
188 super.onMetaChanged();
189 updateStatusBarColor();
193 protected void onSlide(float slideOffset) {
194 boolean isInBrowser = getCurrentPanel() == Panel.Browse && slideOffset < 0.7f;
195 if (isInBrowser != mBrowsePanelActive) {
196 mBrowsePanelActive = isInBrowser;
197 updateStatusBarColor();
202 public void onWindowFocusChanged(boolean hasFocus) {
203 super.onWindowFocusChanged(hasFocus);
205 if (getCurrentPanel() == Panel.MusicPlayer) {
206 getAudioPlayerFragment().setVisualizerVisible(hasFocus);
210 private void updateStatusBarColor() {
211 if (mBrowsePanelActive || MusicUtils.getCurrentAlbumId() < 0) {
212 updateStatusBarColor(Color.TRANSPARENT);
214 new AsyncTask<Void, Void, BitmapWithColors>() {
216 protected BitmapWithColors doInBackground(Void... params) {
217 ImageFetcher imageFetcher = ImageFetcher.getInstance(HomeActivity.this);
218 return imageFetcher.getArtwork(
219 MusicUtils.getAlbumName(), MusicUtils.getCurrentAlbumId(),
220 MusicUtils.getArtistName(), true);
223 protected void onPostExecute(BitmapWithColors bmc) {
224 updateVisualizerColor(bmc != null
225 ? bmc.getContrastingColor() : Color.TRANSPARENT);
226 updateStatusBarColor(bmc != null
227 ? bmc.getVibrantDarkColor() : Color.TRANSPARENT);
233 private void updateVisualizerColor(int color) {
234 if (color == Color.TRANSPARENT) {
235 color = getResources().getColor(R.color.visualizer_fill_color);
238 // check for null since updatestatusBarColor is a async task
239 AudioPlayerFragment fragment = getAudioPlayerFragment();
240 if (fragment != null) {
241 fragment.setVisualizerColor(color);
245 private void updateStatusBarColor(int color) {
246 if (color == Color.TRANSPARENT) {
247 color = getResources().getColor(R.color.primary_dark);
249 final Window window = getWindow();
250 ObjectAnimator animator = ObjectAnimator.ofInt(window,
251 "statusBarColor", window.getStatusBarColor(), color);
252 animator.setEvaluator(new ArgbEvaluator());
253 animator.setDuration(300);
257 private boolean parseIntentForFragment(Intent intent) {
258 boolean handled = false;
259 if (intent.getAction() != null) {
260 final String action = intent.getAction();
261 Fragment targetFragment = null;
262 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
264 if (action.equals(ACTION_VIEW_SMART_PLAYLIST)) {
265 long playlistId = intent.getExtras().getLong(Config.SMART_PLAYLIST_TYPE);
266 switch (Config.SmartPlaylistType.getTypeById(playlistId)) {
268 targetFragment = new LastAddedFragment();
271 targetFragment = new RecentFragment();
274 targetFragment = new TopTracksFragment();
277 } else if (action.equals(ACTION_VIEW_PLAYLIST_DETAILS)) {
278 targetFragment = new PlaylistDetailFragment();
279 } else if (action.equals(ACTION_VIEW_ALBUM_DETAILS)) {
280 targetFragment = new AlbumDetailFragment();
281 } else if (action.equals(ACTION_VIEW_ARTIST_DETAILS)) {
282 targetFragment = new ArtistDetailFragment();
285 if (targetFragment != null) {
286 targetFragment.setArguments(intent.getExtras());
287 // If we ever come back to this because of memory concerns because
288 // none of the fragments are being removed from memory, we can fix this
289 // by using "replace" instead of "add". The caveat is that the performance of
290 // returning to previous fragments is a bit more sluggish because the fragment
291 // view needs to be recreated. If we do remove that, we can remove the back stack
292 // change listener code above
293 transaction.add(R.id.activity_base_content, targetFragment);
294 if (mLoadedBaseFragment) {
295 transaction.addToBackStack(null);
296 showPanel(Panel.Browse);
298 // else mark the fragment as loaded so we don't load the music browse fragment.
299 // this happens when they launch search which is its own activity and then
300 // browse through that back to home activity
301 mLoadedBaseFragment = true;
302 getActionBar().setDisplayHomeAsUpEnabled(true);
304 // the current top fragment is about to be hidden by what we are replacing
305 // it with -- so tell that fragment not to make its action bar menu items visible
306 Fragment oldTop = getTopFragment();
307 if (oldTop != null) {
308 oldTop.setMenuVisibility(false);
311 transaction.commit();
319 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
320 super.onActivityResult(requestCode, resultCode, data);
321 if (requestCode == NEW_PHOTO && !TextUtils.isEmpty(mKey)) {
322 if (resultCode == RESULT_OK) {
323 MusicUtils.removeFromCache(this, mKey);
324 final Uri selectedImage = data.getData();
326 new Thread(new Runnable() {
329 Bitmap bitmap = ImageFetcher.decodeSampledBitmapFromUri(getContentResolver(),
332 ImageFetcher imageFetcher = ElevenUtils.getImageFetcher(HomeActivity.this);
333 imageFetcher.addBitmapToCache(mKey, bitmap);
335 MusicUtils.refresh();
343 * Starts an activity for result that returns an image from the Gallery.
345 public void selectNewPhoto(String key) {
347 // Now open the gallery
348 final Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
349 intent.setType("image/*");
350 startActivityForResult(intent, NEW_PHOTO);
354 public boolean onOptionsItemSelected(final MenuItem item) {
355 switch (item.getItemId()) {
356 case android.R.id.home:
361 return super.onOptionsItemSelected(item);
365 * Navigates to the top Activity and places the view to the correct page
367 protected void navigateToTop() {
368 final Fragment topFragment = getTopFragment();
369 int targetFragmentIndex = MusicBrowserPhoneFragment.INVALID_PAGE_INDEX;
370 if (topFragment instanceof IChildFragment) {
371 targetFragmentIndex = ((IChildFragment)topFragment).getMusicFragmentParent().ordinal();
374 // If we are the top activity in the stack (as determined by the activity that has loaded
375 // the MusicBrowserPhoneFragment) then clear the back stack and move the browse fragment
376 // to the appropriate page as per Android up standards
377 if (mTopLevelActivity) {
379 MusicBrowserPhoneFragment musicFragment = (MusicBrowserPhoneFragment) getTopFragment();
380 musicFragment.setDefaultPageIdx(targetFragmentIndex);
381 showPanel(Panel.Browse);
383 // I've tried all other combinations with parent activities, support.NavUtils and
384 // there is no easy way to achieve what we want that I'm aware of, so clear everything
385 // and jump to the right page
386 NavUtils.goHome(this, targetFragmentIndex);
391 * Immediately clears the backstack
393 protected void clearBackStack() {
394 final FragmentManager fragmentManager = getSupportFragmentManager();
395 if (fragmentManager.getBackStackEntryCount() > 0) {
396 final int id = fragmentManager.getBackStackEntryAt(0).getId();
397 fragmentManager.popBackStackImmediate(id, FragmentManager.POP_BACK_STACK_INCLUSIVE);
402 public void handlePendingPlaybackRequests() {
403 if (mHasPendingPlaybackRequest) {
404 Intent unhandledIntent = getIntent();
405 handlePlaybackIntent(unhandledIntent);
410 * Checks whether the passed intent contains a playback request,
411 * and starts playback if that's the case
412 * @return true if the intent was consumed
414 private boolean handlePlaybackIntent(Intent intent) {
416 if (intent == null) {
418 } else if ( !MusicUtils.isPlaybackServiceConnected() ) {
419 mHasPendingPlaybackRequest = true;
423 String mimeType = intent.getType();
424 boolean handled = false;
426 if (MediaStore.Audio.Playlists.CONTENT_TYPE.equals(mimeType)) {
427 long id = parseIdFromIntent(intent, "playlistId", "playlist", -1);
429 MusicUtils.playPlaylist(this, id, false);
432 } else if (MediaStore.Audio.Albums.CONTENT_TYPE.equals(mimeType)) {
433 long id = parseIdFromIntent(intent, "albumId", "album", -1);
435 int position = intent.getIntExtra("position", 0);
436 MusicUtils.playAlbum(this, id, position, false);
439 } else if (MediaStore.Audio.Artists.CONTENT_TYPE.equals(mimeType)) {
440 long id = parseIdFromIntent(intent, "artistId", "artist", -1);
442 int position = intent.getIntExtra("position", 0);
443 MusicUtils.playArtist(this, id, position, false);
448 // reset intent as it was handled as a playback request
450 setIntent(new Intent());
457 private long parseIdFromIntent(Intent intent, String longKey,
458 String stringKey, long defaultId) {
459 long id = intent.getLongExtra(longKey, -1);
461 String idString = intent.getStringExtra(stringKey);
462 if (idString != null) {
464 id = Long.parseLong(idString);
465 } catch (NumberFormatException e) {
466 Log.e(TAG, e.getMessage());
474 public void onBackStackChanged() {
475 Fragment topFragment = getTopFragment();
476 if (topFragment != null) {
477 // the fragment that has come back to the top should now have its menu items
478 // added to the action bar -- so tell it to make it menu items visible
479 topFragment.setMenuVisibility(true);
480 ISetupActionBar setupActionBar = (ISetupActionBar) topFragment;
481 setupActionBar.setupActionBar();
483 getActionBar().setDisplayHomeAsUpEnabled(
484 !(topFragment instanceof MusicBrowserPhoneFragment));
489 public void onRequestPermissionsResult(int requestCode, String permissions[],
490 int[] grantResults) {
491 switch (requestCode) {
492 case PERMISSION_REQUEST_STORAGE: {
493 if (checkPermissionGrantResults(grantResults)) {
502 private boolean needRequestStoragePermission() {
503 boolean needRequest = false;
504 String[] permissions = {
505 Manifest.permission.WRITE_EXTERNAL_STORAGE,
506 Manifest.permission.READ_EXTERNAL_STORAGE
508 ArrayList<String> permissionList = new ArrayList<>();
509 for (String permission : permissions) {
510 if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
511 permissionList.add(permission);
517 int count = permissionList.size();
519 String[] permissionArray = new String[count];
520 for (int i = 0; i < count; i++) {
521 permissionArray[i] = permissionList.get(i);
524 requestPermissions(permissionArray, PERMISSION_REQUEST_STORAGE);
531 private boolean checkPermissionGrantResults(int[] grantResults) {
532 for (int result : grantResults) {
533 if (result != PackageManager.PERMISSION_GRANTED) {