OSDN Git Service

eleven: Runtime permissions
[android-x86/packages-apps-Eleven.git] / src / com / cyanogenmod / 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.cyanogenmod.eleven.ui.activities;
17
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;
38
39 import com.cyanogenmod.eleven.Config;
40 import com.cyanogenmod.eleven.R;
41 import com.cyanogenmod.eleven.cache.ImageFetcher;
42 import com.cyanogenmod.eleven.ui.fragments.AlbumDetailFragment;
43 import com.cyanogenmod.eleven.ui.fragments.ArtistDetailFragment;
44 import com.cyanogenmod.eleven.ui.fragments.AudioPlayerFragment;
45 import com.cyanogenmod.eleven.ui.fragments.IChildFragment;
46 import com.cyanogenmod.eleven.ui.fragments.ISetupActionBar;
47 import com.cyanogenmod.eleven.ui.fragments.PlaylistDetailFragment;
48 import com.cyanogenmod.eleven.ui.fragments.RecentFragment;
49 import com.cyanogenmod.eleven.ui.fragments.phone.MusicBrowserPhoneFragment;
50 import com.cyanogenmod.eleven.ui.fragments.profile.LastAddedFragment;
51 import com.cyanogenmod.eleven.ui.fragments.profile.TopTracksFragment;
52 import com.cyanogenmod.eleven.utils.ApolloUtils;
53 import com.cyanogenmod.eleven.utils.BitmapWithColors;
54 import com.cyanogenmod.eleven.utils.MusicUtils;
55 import com.cyanogenmod.eleven.utils.NavUtils;
56
57 import java.util.ArrayList;
58
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";
68
69     private static final String STATE_KEY_BASE_FRAGMENT = "BaseFragment";
70
71     private static final int NEW_PHOTO = 1;
72     public static final int EQUALIZER = 2;
73
74     private static final int PERMISSION_REQUEST_STORAGE = 1;
75     private Bundle mSavedInstanceState;
76
77     private String mKey;
78     private boolean mLoadedBaseFragment = false;
79     private boolean mHasPendingPlaybackRequest = false;
80     private Handler mHandler = new Handler();
81     private boolean mBrowsePanelActive = true;
82
83     /**
84      * Used by the up action to determine how to handle this
85      */
86     protected boolean mTopLevelActivity = false;
87
88     @Override
89     protected void onCreate(Bundle savedInstanceState) {
90         super.onCreate(savedInstanceState);
91
92         mSavedInstanceState = savedInstanceState;
93
94         if (!needRequestStoragePermission()) {
95             init();
96         }
97     }
98
99     private void init() {
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);
105         }
106
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));
113             }
114             getSupportFragmentManager().beginTransaction()
115                     .replace(R.id.activity_base_content, fragment)
116                     .commit();
117
118             mLoadedBaseFragment = true;
119             mTopLevelActivity = true;
120         }
121
122         getSupportFragmentManager().addOnBackStackChangedListener(this);
123
124
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;
130
131             // update the action bar based on the top most fragment
132             onBackStackChanged();
133
134             // figure which panel we are on and update the status bar
135             mBrowsePanelActive = (getCurrentPanel() == Panel.Browse);
136             updateStatusBarColor();
137         }
138
139         // if intent wasn't UI related, process it as a audio playback request
140         if (!intentHandled) {
141             handlePlaybackIntent(launchIntent);
142         }
143
144         mSavedInstanceState = null;
145     }
146
147     @Override
148     protected void onSaveInstanceState(Bundle outState) {
149         super.onSaveInstanceState(outState);
150         outState.putBoolean(STATE_KEY_BASE_FRAGMENT, mTopLevelActivity);
151     }
152
153     public Fragment getTopFragment() {
154         return getSupportFragmentManager().findFragmentById(R.id.activity_base_content);
155     }
156
157     public void postRemoveFragment(final Fragment frag) {
158         mHandler.post(new Runnable() {
159             @Override
160             public void run() {
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();
166                 } else {
167                     getSupportFragmentManager().beginTransaction().remove(frag).commit();
168                 }
169             }
170         });
171     }
172
173     @Override
174     protected void onNewIntent(Intent intent) {
175         super.onNewIntent(intent);
176
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);
183         }
184     }
185
186     @Override
187     public void onMetaChanged() {
188         super.onMetaChanged();
189         updateStatusBarColor();
190     }
191
192     @Override
193     protected void onSlide(float slideOffset) {
194         boolean isInBrowser = getCurrentPanel() == Panel.Browse && slideOffset < 0.7f;
195         if (isInBrowser != mBrowsePanelActive) {
196             mBrowsePanelActive = isInBrowser;
197             updateStatusBarColor();
198         }
199     }
200
201     @Override
202     public void onWindowFocusChanged(boolean hasFocus) {
203         super.onWindowFocusChanged(hasFocus);
204
205         getAudioPlayerFragment().setVisualizerVisible(hasFocus
206                 && getCurrentPanel() == Panel.MusicPlayer);
207     }
208
209     private void updateStatusBarColor() {
210         if (mBrowsePanelActive || MusicUtils.getCurrentAlbumId() < 0) {
211             updateStatusBarColor(Color.TRANSPARENT);
212         } else {
213             new AsyncTask<Void, Void, BitmapWithColors>() {
214                 @Override
215                 protected BitmapWithColors doInBackground(Void... params) {
216                     ImageFetcher imageFetcher = ImageFetcher.getInstance(HomeActivity.this);
217                     return imageFetcher.getArtwork(
218                             MusicUtils.getAlbumName(), MusicUtils.getCurrentAlbumId(),
219                             MusicUtils.getArtistName(), true);
220                 }
221                 @Override
222                 protected void onPostExecute(BitmapWithColors bmc) {
223                     updateVisualizerColor(bmc != null
224                             ? bmc.getContrastingColor() : Color.TRANSPARENT);
225                     updateStatusBarColor(bmc != null
226                             ? bmc.getVibrantDarkColor() : Color.TRANSPARENT);
227                 }
228             }.execute();
229         }
230     }
231
232     private void updateVisualizerColor(int color) {
233         if (color == Color.TRANSPARENT) {
234             color = getResources().getColor(R.color.visualizer_fill_color);
235         }
236
237         // check for null since updatestatusBarColor is a async task
238         AudioPlayerFragment fragment = getAudioPlayerFragment();
239         if (fragment != null) {
240             fragment.setVisualizerColor(color);
241         }
242     }
243
244     private void updateStatusBarColor(int color) {
245         if (color == Color.TRANSPARENT) {
246             color = getResources().getColor(R.color.primary_dark);
247         }
248         final Window window = getWindow();
249         ObjectAnimator animator = ObjectAnimator.ofInt(window,
250                 "statusBarColor", window.getStatusBarColor(), color);
251         animator.setEvaluator(new ArgbEvaluator());
252         animator.setDuration(300);
253         animator.start();
254     }
255
256     private boolean parseIntentForFragment(Intent intent) {
257         boolean handled = false;
258         if (intent.getAction() != null) {
259             final String action = intent.getAction();
260             Fragment targetFragment = null;
261             FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
262
263             if (action.equals(ACTION_VIEW_SMART_PLAYLIST)) {
264                 long playlistId = intent.getExtras().getLong(Config.SMART_PLAYLIST_TYPE);
265                 switch (Config.SmartPlaylistType.getTypeById(playlistId)) {
266                     case LastAdded:
267                         targetFragment = new LastAddedFragment();
268                         break;
269                     case RecentlyPlayed:
270                         targetFragment = new RecentFragment();
271                         break;
272                     case TopTracks:
273                         targetFragment = new TopTracksFragment();
274                         break;
275                 }
276             } else if (action.equals(ACTION_VIEW_PLAYLIST_DETAILS)) {
277                 targetFragment = new PlaylistDetailFragment();
278             } else if (action.equals(ACTION_VIEW_ALBUM_DETAILS)) {
279                 targetFragment = new AlbumDetailFragment();
280             } else if (action.equals(ACTION_VIEW_ARTIST_DETAILS)) {
281                 targetFragment = new ArtistDetailFragment();
282             }
283
284             if (targetFragment != null) {
285                 targetFragment.setArguments(intent.getExtras());
286                 transaction.setCustomAnimations(0, 0, 0, R.anim.fade_out);
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);
297                 } else {
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);
303                 }
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);
309                 }
310
311                 transaction.commit();
312                 handled = true;
313             }
314         }
315         return handled;
316     }
317
318     @Override
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();
325
326                 new Thread(new Runnable() {
327                     @Override
328                     public void run() {
329                         Bitmap bitmap = ImageFetcher.decodeSampledBitmapFromUri(getContentResolver(),
330                                 selectedImage);
331
332                         ImageFetcher imageFetcher = ApolloUtils.getImageFetcher(HomeActivity.this);
333                         imageFetcher.addBitmapToCache(mKey, bitmap);
334
335                         MusicUtils.refresh();
336                     }
337                 }).start();
338             }
339         }
340     }
341
342     /**
343      * Starts an activity for result that returns an image from the Gallery.
344      */
345     public void selectNewPhoto(String key) {
346         mKey = 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);
351     }
352
353     @Override
354     public boolean onOptionsItemSelected(final MenuItem item) {
355         switch (item.getItemId()) {
356             case android.R.id.home:
357                 navigateToTop();
358                 return true;
359         }
360
361         return super.onOptionsItemSelected(item);
362     }
363
364     /**
365      * Navigates to the top Activity and places the view to the correct page
366      */
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();
372         }
373
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) {
378             clearBackStack();
379             MusicBrowserPhoneFragment musicFragment = (MusicBrowserPhoneFragment) getTopFragment();
380             musicFragment.setDefaultPageIdx(targetFragmentIndex);
381             showPanel(Panel.Browse);
382         } else {
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);
387         }
388     }
389
390     /**
391      * Immediately clears the backstack
392      */
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);
398         }
399     }
400
401     @Override
402     public void handlePendingPlaybackRequests() {
403         if (mHasPendingPlaybackRequest) {
404             Intent unhandledIntent = getIntent();
405             handlePlaybackIntent(unhandledIntent);
406         }
407     }
408
409     /**
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
413      */
414     private boolean handlePlaybackIntent(Intent intent) {
415
416         if (intent == null) {
417             return false;
418         } else if ( !MusicUtils.isPlaybackServiceConnected() ) {
419             mHasPendingPlaybackRequest = true;
420             return false;
421         }
422
423         String mimeType = intent.getType();
424         boolean handled = false;
425
426         if (MediaStore.Audio.Playlists.CONTENT_TYPE.equals(mimeType)) {
427             long id = parseIdFromIntent(intent, "playlistId", "playlist", -1);
428             if (id >= 0) {
429                 MusicUtils.playPlaylist(this, id, false);
430                 handled = true;
431             }
432         } else if (MediaStore.Audio.Albums.CONTENT_TYPE.equals(mimeType)) {
433             long id = parseIdFromIntent(intent, "albumId", "album", -1);
434             if (id >= 0) {
435                 int position = intent.getIntExtra("position", 0);
436                 MusicUtils.playAlbum(this, id, position, false);
437                 handled = true;
438             }
439         } else if (MediaStore.Audio.Artists.CONTENT_TYPE.equals(mimeType)) {
440             long id = parseIdFromIntent(intent, "artistId", "artist", -1);
441             if (id >= 0) {
442                 int position = intent.getIntExtra("position", 0);
443                 MusicUtils.playArtist(this, id, position, false);
444                 handled = true;
445             }
446         }
447
448         // reset intent as it was handled as a playback request
449         if (handled) {
450             setIntent(new Intent());
451         }
452
453         return handled;
454
455     }
456
457     private long parseIdFromIntent(Intent intent, String longKey,
458                                    String stringKey, long defaultId) {
459         long id = intent.getLongExtra(longKey, -1);
460         if (id < 0) {
461             String idString = intent.getStringExtra(stringKey);
462             if (idString != null) {
463                 try {
464                     id = Long.parseLong(idString);
465                 } catch (NumberFormatException e) {
466                     Log.e(TAG, e.getMessage());
467                 }
468             }
469         }
470         return id;
471     }
472
473     @Override
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();
482
483             getActionBar().setDisplayHomeAsUpEnabled(
484                     !(topFragment instanceof MusicBrowserPhoneFragment));
485         }
486     }
487
488     @Override
489     public void onRequestPermissionsResult(int requestCode, String permissions[],
490             int[] grantResults) {
491         switch (requestCode) {
492             case PERMISSION_REQUEST_STORAGE: {
493                 if (checkPermissionGrantResults(grantResults)) {
494                     init();
495                 } else {
496                     finish();
497                 }
498             }
499         }
500     }
501
502     private boolean needRequestStoragePermission() {
503         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return false;
504
505         boolean needRequest = false;
506         String[] permissions = {
507                 Manifest.permission.WRITE_EXTERNAL_STORAGE,
508                 Manifest.permission.READ_EXTERNAL_STORAGE
509         };
510         ArrayList<String> permissionList = new ArrayList<String>();
511         for (String permission : permissions) {
512             if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
513                 permissionList.add(permission);
514                 needRequest = true;
515             }
516         }
517
518         if (needRequest) {
519             int count = permissionList.size();
520             if (count > 0) {
521                 String[] permissionArray = new String[count];
522                 for (int i = 0; i < count; i++) {
523                     permissionArray[i] = permissionList.get(i);
524                 }
525
526                 requestPermissions(permissionArray, PERMISSION_REQUEST_STORAGE);
527             }
528         }
529
530         return needRequest;
531     }
532
533     private boolean checkPermissionGrantResults(int[] grantResults) {
534         for (int result : grantResults) {
535             if (result != PackageManager.PERMISSION_GRANTED) {
536                 return false;
537             }
538         }
539         return true;
540     }
541
542 }