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 com.cyngn.eleven.ui.activities;
18 import android.content.Intent;
19 import android.graphics.Bitmap;
20 import android.net.Uri;
21 import android.os.Bundle;
22 import android.os.Handler;
23 import android.support.v4.app.Fragment;
24 import android.support.v4.app.FragmentManager;
25 import android.support.v4.app.FragmentTransaction;
26 import android.text.TextUtils;
27 import com.cyngn.eleven.Config;
28 import com.cyngn.eleven.R;
29 import com.cyngn.eleven.cache.ImageFetcher;
30 import com.cyngn.eleven.ui.fragments.AlbumDetailFragment;
31 import com.cyngn.eleven.ui.fragments.ArtistDetailFragment;
32 import com.cyngn.eleven.ui.fragments.ISetupActionBar;
33 import com.cyngn.eleven.ui.fragments.PlaylistDetailFragment;
34 import com.cyngn.eleven.ui.fragments.RecentFragment;
35 import com.cyngn.eleven.ui.fragments.phone.MusicBrowserPhoneFragment;
36 import com.cyngn.eleven.ui.fragments.profile.LastAddedFragment;
37 import com.cyngn.eleven.ui.fragments.profile.TopTracksFragment;
38 import com.cyngn.eleven.utils.ApolloUtils;
39 import com.cyngn.eleven.utils.MusicUtils;
41 public class HomeActivity extends SlidingPanelActivity {
42 private static final String ACTION_PREFIX = HomeActivity.class.getName();
43 public static final String ACTION_VIEW_BROWSE = ACTION_PREFIX + ".view.Browse";
44 public static final String ACTION_VIEW_MUSIC_PLAYER = ACTION_PREFIX + ".view.MusicPlayer";
45 public static final String ACTION_VIEW_QUEUE = ACTION_PREFIX + ".view.Queue";
46 public static final String ACTION_VIEW_ARTIST_DETAILS = ACTION_PREFIX + ".view.ArtistDetails";
47 public static final String ACTION_VIEW_ALBUM_DETAILS = ACTION_PREFIX + ".view.AlbumDetails";
48 public static final String ACTION_VIEW_PLAYLIST_DETAILS = ACTION_PREFIX + ".view.PlaylistDetails";
49 public static final String ACTION_VIEW_SMART_PLAYLIST = ACTION_PREFIX + ".view.SmartPlaylist";
51 private static final int NEW_PHOTO = 1;
54 private boolean mLoadedBaseFragment = false;
55 private Handler mHandler = new Handler();
58 protected void onCreate(Bundle savedInstanceState) {
59 super.onCreate(savedInstanceState);
60 // if we've been launched by an intent, parse it
61 Intent launchIntent = getIntent();
62 if (launchIntent != null) {
63 parseIntent(launchIntent);
66 // if the intent didn't cause us to load a fragment, load the music browse one
67 if (!mLoadedBaseFragment) {
68 getSupportFragmentManager().beginTransaction()
69 .replace(R.id.activity_base_content, new MusicBrowserPhoneFragment()).commit();
71 mLoadedBaseFragment = true;
74 getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
76 public void onBackStackChanged() {
77 Fragment topFragment = getTopFragment();
78 if (topFragment != null) {
79 // the fragment that has come back to the top should now have its menu items
80 // added to the action bar -- so tell it to make it menu items visible
81 topFragment.setMenuVisibility(true);
82 ISetupActionBar setupActionBar = (ISetupActionBar) topFragment;
83 setupActionBar.setupActionBar();
85 if (topFragment instanceof MusicBrowserPhoneFragment) {
86 getActionBar().setIcon(R.drawable.ic_launcher);
87 getActionBar().setHomeButtonEnabled(false);
89 getActionBar().setIcon(R.drawable.ic_action_back_padded);
90 getActionBar().setHomeButtonEnabled(true);
97 public Fragment getTopFragment() {
98 return getSupportFragmentManager().findFragmentById(R.id.activity_base_content);
101 public void postRemoveFragment(final Fragment frag) {
102 mHandler.post(new Runnable() {
105 getSupportFragmentManager().beginTransaction().remove(frag).commit();
111 protected void onNewIntent(Intent intent) {
112 super.onNewIntent(intent);
117 private void parseIntent(Intent intent) {
118 if (intent.getAction() != null) {
119 final String action = intent.getAction();
120 Fragment targetFragment = null;
121 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
123 if (action.equals(ACTION_VIEW_SMART_PLAYLIST)) {
124 long playlistId = intent.getExtras().getLong(Config.SMART_PLAYLIST_TYPE);
125 switch (Config.SmartPlaylistType.getTypeById(playlistId)) {
127 targetFragment = new LastAddedFragment();
130 targetFragment = new RecentFragment();
133 targetFragment = new TopTracksFragment();
136 } else if (action.equals(ACTION_VIEW_PLAYLIST_DETAILS)) {
137 targetFragment = new PlaylistDetailFragment();
138 } else if (action.equals(ACTION_VIEW_ALBUM_DETAILS)) {
139 targetFragment = new AlbumDetailFragment();
140 } else if (action.equals(ACTION_VIEW_ARTIST_DETAILS)) {
141 targetFragment = new ArtistDetailFragment();
144 if (targetFragment != null) {
145 targetFragment.setArguments(intent.getExtras());
146 transaction.setCustomAnimations(0, 0, 0, R.anim.fade_out);
147 // If we ever come back to this because of memory concerns because
148 // none of the fragments are being removed from memory, we can fix this
149 // by using "replace" instead of "add". The caveat is that the performance of
150 // returning to previous fragments is a bit more sluggish because the fragment
151 // view needs to be recreated. If we do remove that, we can remove the back stack
152 // change listener code above
153 transaction.add(R.id.activity_base_content, targetFragment);
154 if (mLoadedBaseFragment) {
155 transaction.addToBackStack(null);
156 showPanel(Panel.Browse);
158 // else mark the fragment as loaded so we don't load the music browse fragment.
159 // this happens when they launch search which is its own activity and then
160 // browse through that back to home activity
161 mLoadedBaseFragment = true;
162 getActionBar().setIcon(R.drawable.ic_action_back_padded);
163 getActionBar().setHomeButtonEnabled(true);
165 // the current top fragment is about to be hidden by what we are replacing
166 // it with -- so tell that fragment not to make its action bar menu items visible
167 Fragment oldTop = getTopFragment();
168 if(oldTop != null) { oldTop.setMenuVisibility(false); }
170 transaction.commit();
176 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
177 super.onActivityResult(requestCode, resultCode, data);
178 if (requestCode == NEW_PHOTO && !TextUtils.isEmpty(mKey)) {
179 if (resultCode == RESULT_OK) {
180 MusicUtils.removeFromCache(this, mKey);
181 final Uri selectedImage = data.getData();
183 new Thread(new Runnable() {
186 Bitmap bitmap = ImageFetcher.decodeSampledBitmapFromUri(getContentResolver(),
189 ImageFetcher imageFetcher = ApolloUtils.getImageFetcher(HomeActivity.this);
190 imageFetcher.addBitmapToCache(mKey, bitmap);
192 MusicUtils.refresh();
200 * Starts an activity for result that returns an image from the Gallery.
202 public void selectNewPhoto(String key) {
204 // Now open the gallery
205 final Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
206 intent.setType("image/*");
207 startActivityForResult(intent, NEW_PHOTO);