OSDN Git Service

10cc3b3ebf5fc6fb71cdf9cb188989e61d2deaa7
[android-x86/packages-apps-Eleven.git] / src / com / cyngn / eleven / ui / activities / HomeActivity.java
1 /*
2  * Copyright (C) 2014 The CyanogenMod Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package com.cyngn.eleven.ui.activities;
17
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;
40
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";
50
51     private static final int NEW_PHOTO = 1;
52
53     private String mKey;
54     private boolean mLoadedBaseFragment = false;
55     private Handler mHandler = new Handler();
56
57     @Override
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);
64         }
65
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();
70
71             mLoadedBaseFragment = true;
72         }
73
74         getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
75             @Override
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();
84
85                     if (topFragment instanceof MusicBrowserPhoneFragment) {
86                         getActionBar().setIcon(R.drawable.ic_launcher);
87                         getActionBar().setHomeButtonEnabled(false);
88                     } else {
89                         getActionBar().setIcon(R.drawable.ic_action_back_padded);
90                         getActionBar().setHomeButtonEnabled(true);
91                     }
92                 }
93             }
94         });
95     }
96
97     public Fragment getTopFragment() {
98         return getSupportFragmentManager().findFragmentById(R.id.activity_base_content);
99     }
100
101     public void postRemoveFragment(final Fragment frag) {
102         mHandler.post(new Runnable() {
103             @Override
104             public void run() {
105                 getSupportFragmentManager().beginTransaction().remove(frag).commit();
106             }
107         });
108     }
109
110     @Override
111     protected void onNewIntent(Intent intent) {
112         super.onNewIntent(intent);
113
114         parseIntent(intent);
115     }
116
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();
122
123             if (action.equals(ACTION_VIEW_SMART_PLAYLIST)) {
124                 long playlistId = intent.getExtras().getLong(Config.SMART_PLAYLIST_TYPE);
125                 switch (Config.SmartPlaylistType.getTypeById(playlistId)) {
126                     case LastAdded:
127                         targetFragment = new LastAddedFragment();
128                         break;
129                     case RecentlyPlayed:
130                         targetFragment = new RecentFragment();
131                         break;
132                     case TopTracks:
133                         targetFragment = new TopTracksFragment();
134                         break;
135                 }
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();
142             }
143
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);
157                 } else {
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);
164                 }
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); }
169
170                 transaction.commit();
171             }
172         }
173     }
174
175     @Override
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();
182
183                 new Thread(new Runnable() {
184                     @Override
185                     public void run() {
186                         Bitmap bitmap = ImageFetcher.decodeSampledBitmapFromUri(getContentResolver(),
187                                 selectedImage);
188
189                         ImageFetcher imageFetcher = ApolloUtils.getImageFetcher(HomeActivity.this);
190                         imageFetcher.addBitmapToCache(mKey, bitmap);
191
192                         MusicUtils.refresh();
193                     }
194                 }).start();
195             }
196         }
197     }
198
199     /**
200      * Starts an activity for result that returns an image from the Gallery.
201      */
202     public void selectNewPhoto(String key) {
203         mKey = 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);
208     }
209 }