OSDN Git Service

Eleven: much material
[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 View onCreateView(LayoutInflater inflater, ViewGroup container,
52             Bundle savedInstanceState) {
53         setHasOptionsMenu(true);
54         return super.onCreateView(inflater, container, savedInstanceState);
55     }
56
57     @Override
58     protected Config.IdType getFragmentSourceType() {
59         return Config.IdType.Playlist;
60     }
61
62     @Override
63     public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
64         inflater.inflate(R.menu.shuffle_item, menu);
65         menu.findItem(R.id.menu_shuffle_item).setTitle(getShuffleTitleId());
66
67         // use the same popup menu to provide actions for smart playlist
68         // as is used in the PlaylistFragment
69         mActionMenuHelper = new PlaylistPopupMenuHelper(
70                 getActivity(), getChildFragmentManager(), PopupMenuType.SmartPlaylist) {
71             public Playlist getPlaylist(int position) {
72                 SmartPlaylistType type = getSmartPlaylistType();
73                 return new Playlist(type.mId, getString(type.mTitleId), 0);
74             }
75         };
76         mActionMenuHelper.onPreparePopupMenu(0);
77         mActionMenuHelper.createPopupMenu(menu);
78
79         inflater.inflate(R.menu.clear_list, menu);
80         super.onCreateOptionsMenu(menu, inflater);
81     }
82
83     @Override
84     public boolean onOptionsItemSelected(final MenuItem item) {
85         switch(item.getItemId()) {
86             case R.id.menu_shuffle_item:
87                 playAll(-1, true);
88                 return true;
89             case R.id.clear_list:
90                 ConfirmDialog.show(
91                     this, CLEAR_REQUEST, getClearTitleId(), R.string.clear);
92                 return true;
93             default:
94                 if(mActionMenuHelper.onMenuItemClick(item)) { return true; }
95         }
96         return super.onOptionsItemSelected(item);
97     }
98
99     @Override
100     public void confirmOk(int requestCode) {
101         if(requestCode == CLEAR_REQUEST) {
102             mAdapter.unload();
103             clearList();
104             restartLoader();
105         }
106     }
107
108     @Override
109     public void playAll(int position) {
110         playAll(position, false);
111     }
112
113     public void playAll(int position, boolean shuffle) {
114         // we grab the song ids from the adapter instead of querying the cursor because the user
115         // expects what they see to be what they play.  The counter argument of updating the list
116         // could be made, but refreshing the smart playlists so often will be annoying and
117         // confusing for the user so this is an intermediate compromise.  An example is the top
118         // tracks list is based on the # of times you play a song, but near the beginning each
119         // song being played will change the list and the compromise is to update only when you
120         // enter the page.
121         long[] songIds = getSongIdsFromAdapter();
122         if (songIds != null) {
123             MusicUtils.playAll(getActivity(), songIds, position, getSmartPlaylistType().mId,
124                     Config.IdType.Playlist, shuffle);
125         }
126     }
127
128     public PagerAdapter.MusicFragments getMusicFragmentParent() {
129         return PagerAdapter.MusicFragments.PLAYLIST;
130     }
131
132     protected abstract SmartPlaylistType getSmartPlaylistType();
133
134     /** text for menu item that shuffles items in this playlist */
135     protected abstract int getShuffleTitleId();
136
137     /** text for confirmation dialog that clears this playlist */
138     protected abstract int getClearTitleId();
139
140     /** action that clears this playlist */
141     protected abstract void clearList();
142 }