2 * Copyright (C) 2014 The CyanogenMod Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.cyanogenmod.eleven.ui.fragments.profile;
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;
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;
38 public abstract class SmartPlaylistFragment extends BasicSongFragment
39 implements ConfirmDialog.ConfirmCallback, IChildFragment {
41 * LoaderCallbacks identifier
43 private static final int LOADER = 0;
44 private static final int CLEAR_REQUEST = 1;
45 private PopupMenuHelper mActionMenuHelper;
48 public int getLoaderId() { return LOADER; }
51 public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
52 setHasOptionsMenu(true);
53 return super.onCreateView(inflater, container, savedInstanceState);
57 protected Config.IdType getFragmentSourceType() {
58 return Config.IdType.Playlist;
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());
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);
75 mActionMenuHelper.onPreparePopupMenu(0);
76 mActionMenuHelper.createPopupMenu(menu);
78 inflater.inflate(R.menu.clear_list, menu);
79 super.onCreateOptionsMenu(menu, inflater);
83 public boolean onOptionsItemSelected(final MenuItem item) {
84 switch(item.getItemId()) {
85 case R.id.menu_shuffle_item:
90 this, CLEAR_REQUEST, getClearTitleId(), R.string.clear);
93 if(mActionMenuHelper.onMenuItemClick(item)) { return true; }
95 return super.onOptionsItemSelected(item);
99 public void confirmOk(int requestCode) {
100 if(requestCode == CLEAR_REQUEST) {
108 public void playAll(int position) {
109 playAll(position, false);
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
120 long[] songIds = getSongIdsFromAdapter();
121 if (songIds != null) {
122 MusicUtils.playAll(getActivity(), songIds, position, getSmartPlaylistType().mId,
123 Config.IdType.Playlist, shuffle);
127 public PagerAdapter.MusicFragments getMusicFragmentParent() {
128 return PagerAdapter.MusicFragments.PLAYLIST;
131 protected abstract SmartPlaylistType getSmartPlaylistType();
133 /** text for menu item that shuffles items in this playlist */
134 protected abstract int getShuffleTitleId();
136 /** text for confirmation dialog that clears this playlist */
137 protected abstract int getClearTitleId();
139 /** action that clears this playlist */
140 protected abstract void clearList();