OSDN Git Service

31fa5b9fa1faf56241357e49e1ca4dc8635363bc
[android-x86/packages-apps-Eleven.git] / src / com / cyanogenmod / eleven / utils / NavUtils.java
1 /*
2  * Copyright (C) 2012 Andrew Neal
3  * Copyright (C) 2014 The CyanogenMod Project
4  * Licensed under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with the
6  * License. You may obtain a copy of the License at
7  * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
8  * or agreed to in writing, software distributed under the License is
9  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
10  * KIND, either express or implied. See the License for the specific language
11  * governing permissions and limitations under the License.
12  */
13
14 package com.cyanogenmod.eleven.utils;
15
16 import android.app.Activity;
17 import android.app.SearchManager;
18 import android.content.ActivityNotFoundException;
19 import android.content.Intent;
20 import android.content.pm.PackageManager;
21 import android.content.pm.ResolveInfo;
22 import android.media.audiofx.AudioEffect;
23 import android.os.Bundle;
24 import android.provider.MediaStore;
25 import android.util.Log;
26 import android.widget.Toast;
27
28 import com.cyanogenmod.eleven.Config;
29 import com.cyanogenmod.eleven.R;
30 import com.cyanogenmod.eleven.ui.activities.HomeActivity;
31 import com.cyanogenmod.eleven.ui.activities.SearchActivity;
32 import com.cyanogenmod.eleven.ui.activities.SettingsActivity;
33
34 import java.util.List;
35
36 /**
37  * Various navigation helpers.
38  *
39  * @author Andrew Neal (andrewdneal@gmail.com)
40  */
41 public final class NavUtils {
42
43     /**
44      * Opens the profile of an artist.
45      *
46      * @param context The {@link Activity} to use.
47      * @param artistName The name of the artist
48      */
49     public static void openArtistProfile(final Activity context, final String artistName) {
50         // Create a new bundle to transfer the artist info
51         final Bundle bundle = new Bundle();
52         bundle.putLong(Config.ID, MusicUtils.getIdForArtist(context, artistName));
53         bundle.putString(Config.MIME_TYPE, MediaStore.Audio.Artists.CONTENT_TYPE);
54         bundle.putString(Config.ARTIST_NAME, artistName);
55
56         // Create the intent to launch the profile activity
57         final Intent intent = new Intent(context, HomeActivity.class);
58         intent.setAction(HomeActivity.ACTION_VIEW_ARTIST_DETAILS);
59         intent.putExtras(bundle);
60         context.startActivity(intent);
61     }
62
63     /**
64      * Opens the profile of an album.
65      *
66      * @param context The {@link Activity} to use.
67      * @param albumName The name of the album
68      * @param artistName The name of the album artist
69      * @param albumId The id of the album
70      */
71     public static void openAlbumProfile(final Activity context,
72             final String albumName, final String artistName, final long albumId) {
73
74         // Create a new bundle to transfer the album info
75         final Bundle bundle = new Bundle();
76         bundle.putString(Config.ALBUM_YEAR, MusicUtils.getReleaseDateForAlbum(context, albumId));
77         bundle.putInt(Config.SONG_COUNT, MusicUtils.getSongCountForAlbumInt(context, albumId));
78         bundle.putString(Config.ARTIST_NAME, artistName);
79         bundle.putString(Config.MIME_TYPE, MediaStore.Audio.Albums.CONTENT_TYPE);
80         bundle.putLong(Config.ID, albumId);
81         bundle.putString(Config.NAME, albumName);
82
83         // Create the intent to launch the profile activity
84         final Intent intent = new Intent(context, HomeActivity.class);
85         intent.setAction(HomeActivity.ACTION_VIEW_ALBUM_DETAILS);
86         intent.putExtras(bundle);
87         context.startActivity(intent);
88     }
89
90     public static void openSmartPlaylist(final Activity context, final Config.SmartPlaylistType type) {
91         // Create the intent to launch the profile activity
92         final Intent intent = new Intent(context, HomeActivity.class);
93         intent.setAction(HomeActivity.ACTION_VIEW_SMART_PLAYLIST);
94         intent.putExtra(Config.SMART_PLAYLIST_TYPE, type.mId);
95         context.startActivity(intent);
96     }
97
98     /**
99      * Opens the playlist view
100      *
101      * @param context The {@link Activity} to use.
102      * @param playlistId the id of the playlist
103      * @param playlistName the playlist name
104      */
105     public static void openPlaylist(final Activity context, final long playlistId,
106                                     final String playlistName) {
107         final Bundle bundle = new Bundle();
108         bundle.putLong(Config.ID, playlistId);
109         bundle.putString(Config.MIME_TYPE, MediaStore.Audio.Playlists.CONTENT_TYPE);
110         bundle.putString(Config.NAME, playlistName);
111
112         // Create the intent to launch the profile activity
113         final Intent intent = new Intent(context, HomeActivity.class);
114         intent.setAction(HomeActivity.ACTION_VIEW_PLAYLIST_DETAILS);
115         intent.putExtras(bundle);
116         context.startActivity(intent);
117     }
118
119     /**
120      * @return the intent to launch the effects panel/dsp manager
121      */
122     private static Intent createEffectsIntent() {
123         final Intent effects = new Intent(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL);
124         effects.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, MusicUtils.getAudioSessionId());
125         return effects;
126     }
127
128     /**
129      * Opens the sound effects panel or DSP manager in CM
130      *
131      * @param context The {@link Activity} to use.
132      * @param requestCode The request code passed into startActivityForResult
133      */
134     public static void openEffectsPanel(final Activity context, final int requestCode) {
135         try {
136             // The google MusicFX apps need to be started using startActivityForResult
137             context.startActivityForResult(createEffectsIntent(), requestCode);
138         } catch (final ActivityNotFoundException notFound) {
139             Toast.makeText(context, context.getString(R.string.no_effects_for_you),
140                     Toast.LENGTH_SHORT).show();
141         }
142     }
143
144     /**
145      * @return true if there is an effects panel/DSK Manager
146      */
147     public static boolean hasEffectsPanel(final Activity activity) {
148         final PackageManager packageManager = activity.getPackageManager();
149         return packageManager.resolveActivity(createEffectsIntent(),
150                 PackageManager.MATCH_DEFAULT_ONLY) != null;
151     }
152
153     /**
154      * Opens to {@link SettingsActivity}.
155      *
156      * @param activity The {@link Activity} to use.
157      */
158     public static void openSettings(final Activity activity) {
159         final Intent intent = new Intent(activity, SettingsActivity.class);
160         activity.startActivity(intent);
161     }
162
163     /**
164      * Opens to {@link com.cyanogenmod.eleven.ui.activities.SearchActivity}.
165      *
166      * @param activity The {@link Activity} to use.
167      * @param query The search query.
168      */
169     public static void openSearch(final Activity activity, final String query) {
170         final Bundle bundle = new Bundle();
171         final Intent intent = new Intent(activity, SearchActivity.class);
172         intent.putExtra(SearchManager.QUERY, query);
173         intent.putExtras(bundle);
174         activity.startActivity(intent);
175     }
176
177     /**
178      * Opens to {@link com.cyanogenmod.eleven.ui.activities.HomeActivity}.
179      *
180      * @param activity The {@link Activity} to use.
181      */
182     public static void goHome(final Activity activity, final int browseIndex) {
183         final Intent intent = new Intent(activity, HomeActivity.class);
184         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK
185                 | Intent.FLAG_ACTIVITY_NEW_TASK);
186         intent.putExtra(HomeActivity.EXTRA_BROWSE_PAGE_IDX, browseIndex);
187         activity.startActivity(intent);
188     }
189 }