OSDN Git Service

5aeb70fbf7e80b8fe3c8c23c94682cf0ecfd818c
[android-x86/packages-apps-Eleven.git] / src / com / cyanogenmod / eleven / ui / fragments / AlbumDetailFragment.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;
17
18 import android.os.Bundle;
19 import android.support.v4.app.LoaderManager;
20 import android.view.View;
21 import android.widget.ImageView;
22 import android.widget.ListView;
23 import android.widget.TextView;
24 import com.cyanogenmod.eleven.Config;
25 import com.cyanogenmod.eleven.R;
26 import com.cyanogenmod.eleven.adapters.AlbumDetailSongAdapter;
27 import com.cyanogenmod.eleven.adapters.DetailSongAdapter;
28 import com.cyanogenmod.eleven.adapters.PagerAdapter;
29 import com.cyanogenmod.eleven.cache.ImageFetcher;
30 import com.cyanogenmod.eleven.model.Album;
31 import com.cyanogenmod.eleven.model.Song;
32 import com.cyanogenmod.eleven.utils.AlbumPopupMenuHelper;
33 import com.cyanogenmod.eleven.utils.GenreFetcher;
34 import com.cyanogenmod.eleven.utils.MusicUtils;
35 import com.cyanogenmod.eleven.utils.PopupMenuHelper;
36 import com.cyanogenmod.eleven.utils.SongPopupMenuHelper;
37 import com.cyanogenmod.eleven.widgets.IPopupMenuCallback;
38 import com.cyanogenmod.eleven.widgets.LoadingEmptyContainer;
39
40 import java.util.List;
41
42 public class AlbumDetailFragment extends DetailFragment implements IChildFragment {
43     private static final int LOADER_ID = 1;
44
45     private ListView mSongs;
46     private DetailSongAdapter mSongAdapter;
47     private TextView mAlbumDuration;
48     private TextView mGenre;
49     private ImageView mAlbumArt;
50     private PopupMenuHelper mSongMenuHelper;
51     private long mAlbumId;
52     private String mArtistName;
53     private String mAlbumName;
54     private LoadingEmptyContainer mLoadingEmptyContainer;
55
56     @Override
57     protected int getLayoutToInflate() {
58         return R.layout.activity_album_detail;
59     }
60
61     @Override
62     protected String getTitle() {
63         return getArguments().getString(Config.ARTIST_NAME);
64     }
65
66     @Override
67     protected void onViewCreated() {
68         super.onViewCreated();
69
70         Bundle arguments = getArguments();
71         String artistName = arguments.getString(Config.ARTIST_NAME);
72
73         setupPopupMenuHelper();
74         setupHeader(artistName, arguments);
75         setupSongList();
76
77         LoaderManager lm = getLoaderManager();
78         lm.initLoader(LOADER_ID, arguments, mSongAdapter);
79     }
80
81     @Override // DetailFragment
82     protected PopupMenuHelper createActionMenuHelper() {
83         return new AlbumPopupMenuHelper(getActivity(), getChildFragmentManager()) {
84             public Album getAlbum(int position) {
85                 return new Album(mAlbumId, mAlbumName, mArtistName, -1, null);
86             }
87         };
88     }
89
90     @Override // DetailFragment
91     protected int getShuffleTitleId() { return R.string.menu_shuffle_album; }
92
93     @Override // DetailFragment
94     protected void playShuffled() {
95         MusicUtils.playAlbum(getActivity(), mAlbumId, -1, true);
96     }
97
98     private void setupHeader(String artist, Bundle arguments) {
99         mAlbumId = arguments.getLong(Config.ID);
100         mArtistName = artist;
101         mAlbumName = arguments.getString(Config.NAME);
102         String year = arguments.getString(Config.ALBUM_YEAR);
103         int songCount = arguments.getInt(Config.SONG_COUNT);
104
105         mAlbumArt = (ImageView)mRootView.findViewById(R.id.album_art);
106         mAlbumArt.setContentDescription(mAlbumName);
107         ImageFetcher.getInstance(getActivity()).loadAlbumImage(artist, mAlbumName, mAlbumId, mAlbumArt);
108
109         TextView title = (TextView)mRootView.findViewById(R.id.title);
110         title.setText(mAlbumName);
111
112         setupCountAndYear(mRootView, year, songCount);
113
114         // will be updated once we have song data
115         mAlbumDuration = (TextView)mRootView.findViewById(R.id.duration);
116         mGenre = (TextView)mRootView.findViewById(R.id.genre);
117     }
118
119     private void setupCountAndYear(View root, String year, int songCount) {
120         TextView songCountAndYear = (TextView)root.findViewById(R.id.song_count_and_year);
121         if(songCount > 0) {
122             String countText = getResources().
123                     getQuantityString(R.plurals.Nsongs, songCount, songCount);
124             if(year == null) {
125                 songCountAndYear.setText(countText);
126             } else {
127                 songCountAndYear.setText(getString(R.string.combine_two_strings, countText, year));
128             }
129         } else if(year != null) {
130             songCountAndYear.setText(year);
131         }
132     }
133
134     private void setupPopupMenuHelper() {
135         mSongMenuHelper = new SongPopupMenuHelper(getActivity(), getChildFragmentManager()) {
136             @Override
137             public Song getSong(int position) {
138                 return mSongAdapter.getItem(position);
139             }
140
141             @Override
142             protected long getSourceId() {
143                 return mAlbumId;
144             }
145
146             @Override
147             protected Config.IdType getSourceType() {
148                 return Config.IdType.Album;
149             }
150         };
151     }
152
153     private void setupSongList() {
154         mSongs = (ListView)mRootView.findViewById(R.id.songs);
155         mSongAdapter = new AlbumDetailSongAdapter(getActivity(), this) {
156             @Override
157             protected void onLoading() {
158                 mLoadingEmptyContainer.showLoading();
159             }
160
161             @Override
162             protected void onNoResults() {
163                 getContainingActivity().postRemoveFragment(AlbumDetailFragment.this);
164             }
165         };
166         mSongAdapter.setPopupMenuClickedListener(new IPopupMenuCallback.IListener() {
167             @Override
168             public void onPopupMenuClicked(View v, int position) {
169                 mSongMenuHelper.showPopupMenu(v, position);
170             }
171         });
172         mSongs.setAdapter(mSongAdapter);
173         mSongs.setOnItemClickListener(mSongAdapter);
174         mLoadingEmptyContainer =
175                 (LoadingEmptyContainer)mRootView.findViewById(R.id.loading_empty_container);
176         mSongs.setEmptyView(mLoadingEmptyContainer);
177     }
178
179     /** called back by song loader */
180     public void update(List<Song> songs) {
181         /** compute total run time for album */
182         int duration = 0;
183         for(Song s : songs) { duration += s.mDuration; }
184         mAlbumDuration.setText(MusicUtils.makeLongTimeString(getActivity(), duration));
185
186         /** use the first song on the album to get a genre */
187         if(!songs.isEmpty()) {
188             GenreFetcher.fetch(getActivity(), (int) songs.get(0).mSongId, mGenre);
189         }
190     }
191
192     @Override
193     public void restartLoader() {
194         getLoaderManager().restartLoader(LOADER_ID, getArguments(), mSongAdapter);
195         ImageFetcher.getInstance(getActivity()).loadAlbumImage(mArtistName, mAlbumName, mAlbumId,
196                 mAlbumArt);
197     }
198
199     @Override
200     public void onMetaChanged() {
201         super.onMetaChanged();
202
203         mSongAdapter.setCurrentlyPlayingTrack(MusicUtils.getCurrentTrack());
204     }
205
206     @Override
207     public PagerAdapter.MusicFragments getMusicFragmentParent() {
208         return PagerAdapter.MusicFragments.ALBUM;
209     }
210 }