OSDN Git Service

Eleven: much material
[android-x86/packages-apps-Eleven.git] / src / com / cyanogenmod / eleven / ui / fragments / RecentFragment.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.ui.fragments;
15
16 import android.app.Activity;
17 import android.os.Bundle;
18 import android.support.v4.content.Loader;
19 import android.view.LayoutInflater;
20 import android.view.View;
21 import android.view.ViewGroup;
22
23 import com.cyanogenmod.eleven.Config;
24 import com.cyanogenmod.eleven.Config.SmartPlaylistType;
25 import com.cyanogenmod.eleven.R;
26 import com.cyanogenmod.eleven.adapters.SongAdapter;
27 import com.cyanogenmod.eleven.loaders.TopTracksLoader;
28 import com.cyanogenmod.eleven.menu.FragmentMenuItems;
29 import com.cyanogenmod.eleven.model.Song;
30 import com.cyanogenmod.eleven.sectionadapter.SectionCreator;
31 import com.cyanogenmod.eleven.sectionadapter.SectionListContainer;
32 import com.cyanogenmod.eleven.ui.activities.BaseActivity;
33 import com.cyanogenmod.eleven.ui.fragments.profile.SmartPlaylistFragment;
34 import com.cyanogenmod.eleven.utils.MusicUtils;
35 import com.cyanogenmod.eleven.widgets.NoResultsContainer;
36
37 import java.util.TreeSet;
38
39 /**
40  * This class is used to display all of the recently listened to songs by the
41  * user.
42  *
43  * @author Andrew Neal (andrewdneal@gmail.com)
44  */
45 public class RecentFragment extends SmartPlaylistFragment implements ISetupActionBar {
46
47     @Override
48     protected SmartPlaylistType getSmartPlaylistType() {
49         return Config.SmartPlaylistType.RecentlyPlayed;
50     }
51
52     @Override
53     protected void updateMenuIds(TreeSet<Integer> set) {
54         set.add(FragmentMenuItems.REMOVE_FROM_RECENT);
55     }
56
57     /**
58      * {@inheritDoc}
59      */
60     @Override
61     public Loader<SectionListContainer<Song>> onCreateLoader(final int id, final Bundle args) {
62         // show the loading progress bar
63         mLoadingEmptyContainer.showLoading();
64
65         TopTracksLoader loader = new TopTracksLoader(getActivity(),
66                 TopTracksLoader.QueryType.RecentSongs);
67         return new SectionCreator<Song>(getActivity(), loader, null);
68     }
69
70     /**
71      * {@inheritDoc}
72      */
73     @Override
74     public void onMetaChanged() {
75         super.onMetaChanged();
76
77         // refresh the list since a track playing means it should be recently played
78         restartLoader();
79     }
80
81     @Override
82     public void setupNoResultsContainer(NoResultsContainer empty) {
83         super.setupNoResultsContainer(empty);
84
85         empty.setMainText(R.string.empty_recent_main);
86         empty.setSecondaryText(R.string.empty_recent);
87     }
88
89     @Override
90     public final View onCreateView(LayoutInflater inflater, ViewGroup container,
91             Bundle savedInstanceState) {
92         setupActionBar();
93         return super.onCreateView(inflater, container, savedInstanceState);
94     }
95
96     @Override
97     public void setupActionBar() {
98         ((BaseActivity)getActivity()).setupActionBar(R.string.playlist_recently_played);
99         ((BaseActivity)getActivity()).setActionBarElevation(true);
100     }
101
102     @Override
103     protected long getFragmentSourceId() {
104         return Config.SmartPlaylistType.RecentlyPlayed.mId;
105     }
106
107     @Override
108     protected SongAdapter createAdapter() {
109         return new RecentAdapter(
110             getActivity(),
111             R.layout.list_item_normal,
112             getFragmentSourceId(),
113             getFragmentSourceType()
114         );
115     }
116
117     private class RecentAdapter extends SongAdapter {
118         public RecentAdapter(Activity context, int layoutId, long sourceId, Config.IdType sourceType) {
119             super(context, layoutId, sourceId, sourceType);
120         }
121
122         @Override
123         protected boolean showNowPlayingIndicator(Song song, int position) {
124             return position == 0 && super.showNowPlayingIndicator(song, position);
125         }
126     }
127
128     @Override
129     protected int getShuffleTitleId() { return R.string.menu_shuffle_recent; }
130
131     @Override
132     protected int getClearTitleId() { return R.string.clear_recent_title; }
133
134     @Override
135     protected void clearList() { MusicUtils.clearRecent(getActivity()); }
136 }