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 View onCreateView(LayoutInflater inflater, ViewGroup container,
52 Bundle savedInstanceState) {
53 setHasOptionsMenu(true);
54 return super.onCreateView(inflater, container, savedInstanceState);
58 protected Config.IdType getFragmentSourceType() {
59 return Config.IdType.Playlist;
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());
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);
76 mActionMenuHelper.onPreparePopupMenu(0);
77 mActionMenuHelper.createPopupMenu(menu);
79 inflater.inflate(R.menu.clear_list, menu);
80 super.onCreateOptionsMenu(menu, inflater);
84 public boolean onOptionsItemSelected(final MenuItem item) {
85 switch(item.getItemId()) {
86 case R.id.menu_shuffle_item:
91 this, CLEAR_REQUEST, getClearTitleId(), R.string.clear);
94 if(mActionMenuHelper.onMenuItemClick(item)) { return true; }
96 return super.onOptionsItemSelected(item);
100 public void confirmOk(int requestCode) {
101 if(requestCode == CLEAR_REQUEST) {
109 public void playAll(int position) {
110 playAll(position, false);
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
121 long[] songIds = getSongIdsFromAdapter();
122 if (songIds != null) {
123 MusicUtils.playAll(getActivity(), songIds, position, getSmartPlaylistType().mId,
124 Config.IdType.Playlist, shuffle);
128 public PagerAdapter.MusicFragments getMusicFragmentParent() {
129 return PagerAdapter.MusicFragments.PLAYLIST;
132 protected abstract SmartPlaylistType getSmartPlaylistType();
134 /** text for menu item that shuffles items in this playlist */
135 protected abstract int getShuffleTitleId();
137 /** text for confirmation dialog that clears this playlist */
138 protected abstract int getClearTitleId();
140 /** action that clears this playlist */
141 protected abstract void clearList();