OSDN Git Service

Eleven: Use appropriate String value comparison
[android-x86/packages-apps-Eleven.git] / src / com / cyanogenmod / eleven / ui / fragments / profile / SmartPlaylistFragment.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.fragments.profile;
17
18 import android.os.Bundle;
19 import android.view.LayoutInflater;
20 import android.view.Menu;
21 import android.view.MenuInflater;
22 import android.view.MenuItem;
23 import android.view.View;
24 import android.view.ViewGroup;
25
26 import com.cyanogenmod.eleven.Config.SmartPlaylistType;
27 import com.cyanogenmod.eleven.Config;
28 import com.cyanogenmod.eleven.R;
29 import com.cyanogenmod.eleven.adapters.PagerAdapter;
30 import com.cyanogenmod.eleven.menu.ConfirmDialog;
31 import com.cyanogenmod.eleven.model.Playlist;
32 import com.cyanogenmod.eleven.ui.fragments.IChildFragment;
33 import com.cyanogenmod.eleven.utils.MusicUtils;
34 import com.cyanogenmod.eleven.utils.PlaylistPopupMenuHelper;
35 import com.cyanogenmod.eleven.utils.PopupMenuHelper;
36 import com.cyanogenmod.eleven.utils.PopupMenuHelper.PopupMenuType;
37
38 public abstract class SmartPlaylistFragment extends BasicSongFragment
39         implements ConfirmDialog.ConfirmCallback, IChildFragment {
40     /**
41      * LoaderCallbacks identifier
42      */
43     private static final int LOADER = 0;
44     private static final int CLEAR_REQUEST = 1;
45     private PopupMenuHelper mActionMenuHelper;
46
47     @Override
48     public int getLoaderId() { return LOADER; }
49
50     @Override
51     public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
52         setHasOptionsMenu(true);
53         return super.onCreateView(inflater, container, savedInstanceState);
54     }
55
56     @Override
57     protected Config.IdType getFragmentSourceType() {
58         return Config.IdType.Playlist;
59     }
60
61     @Override
62     public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
63         inflater.inflate(R.menu.shuffle_item, menu);
64         menu.findItem(R.id.menu_shuffle_item).setTitle(getShuffleTitleId());
65
66         // use the same popup menu to provide actions for smart playlist
67         // as is used in the PlaylistFragment
68         mActionMenuHelper = new PlaylistPopupMenuHelper(
69                 getActivity(), getChildFragmentManager(), PopupMenuType.SmartPlaylist) {
70             public Playlist getPlaylist(int position) {
71                 SmartPlaylistType type = getSmartPlaylistType();
72                 return new Playlist(type.mId, getString(type.mTitleId), 0);
73             }
74         };
75         mActionMenuHelper.onPreparePopupMenu(0);
76         mActionMenuHelper.createPopupMenu(menu);
77
78         inflater.inflate(R.menu.clear_list, menu);
79         super.onCreateOptionsMenu(menu, inflater);
80     }
81
82     @Override
83     public boolean onOptionsItemSelected(final MenuItem item) {
84         switch(item.getItemId()) {
85             case R.id.menu_shuffle_item:
86                 playAll(-1, true);
87                 return true;
88             case R.id.clear_list:
89                 ConfirmDialog.show(
90                     this, CLEAR_REQUEST, getClearTitleId(), R.string.clear);
91                 return true;
92             default:
93                 if(mActionMenuHelper.onMenuItemClick(item)) { return true; }
94         }
95         return super.onOptionsItemSelected(item);
96     }
97
98     @Override
99     public void confirmOk(int requestCode) {
100         if(requestCode == CLEAR_REQUEST) {
101             mAdapter.unload();
102             clearList();
103             restartLoader();
104         }
105     }
106
107     @Override
108     public void playAll(int position) {
109         playAll(position, false);
110     }
111
112     public void playAll(int position, boolean shuffle) {
113         // we grab the song ids from the adapter instead of querying the cursor because the user
114         // expects what they see to be what they play.  The counter argument of updating the list
115         // could be made, but refreshing the smart playlists so often will be annoying and
116         // confusing for the user so this is an intermediate compromise.  An example is the top
117         // tracks list is based on the # of times you play a song, but near the beginning each
118         // song being played will change the list and the compromise is to update only when you
119         // enter the page.
120         long[] songIds = getSongIdsFromAdapter();
121         if (songIds != null) {
122             MusicUtils.playAll(getActivity(), songIds, position, getSmartPlaylistType().mId,
123                     Config.IdType.Playlist, shuffle);
124         }
125     }
126
127     public PagerAdapter.MusicFragments getMusicFragmentParent() {
128         return PagerAdapter.MusicFragments.PLAYLIST;
129     }
130
131     protected abstract SmartPlaylistType getSmartPlaylistType();
132
133     /** text for menu item that shuffles items in this playlist */
134     protected abstract int getShuffleTitleId();
135
136     /** text for confirmation dialog that clears this playlist */
137     protected abstract int getClearTitleId();
138
139     /** action that clears this playlist */
140     protected abstract void clearList();
141 }