View Javadoc

1   /***
2    *     Ambient - A music player for the Android platform
3    Copyright (C) 2007 Martin Vysny
4    
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9    
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14  
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package sk.baka.ambient.activity.main;
20  
21  import java.util.Arrays;
22  import java.util.Collections;
23  import java.util.List;
24  
25  import sk.baka.ambient.ActionsEnum;
26  import sk.baka.ambient.AmbientApplication;
27  import sk.baka.ambient.IPlaylistPlayerListener;
28  import sk.baka.ambient.R;
29  import sk.baka.ambient.activity.main.cb.AbstractCollectionController;
30  import sk.baka.ambient.collection.TrackMetadataBean;
31  import sk.baka.ambient.collection.TrackOriginEnum;
32  import sk.baka.ambient.collection.local.CollectionImpl;
33  import sk.baka.ambient.commons.Interval;
34  import sk.baka.ambient.commons.MiscUtils;
35  import sk.baka.ambient.playerservice.PlayerStateEnum;
36  import sk.baka.ambient.playlist.PlaylistItem;
37  import sk.baka.ambient.playlist.Random;
38  import sk.baka.ambient.playlist.Repeat;
39  import sk.baka.ambient.views.gesturelist.GesturesListView;
40  import android.app.Activity;
41  import android.content.Intent;
42  import android.net.Uri;
43  import android.widget.TextView;
44  
45  /***
46   * Controls the Magnatune view
47   * 
48   * @author Martin Vysny
49   */
50  public final class MagnatuneController extends AbstractCollectionController
51  		implements IPlaylistPlayerListener {
52  
53  	/***
54  	 * Creates new Magnatune controller.
55  	 * 
56  	 * @param mainActivity
57  	 * @param playlistView
58  	 */
59  	public MagnatuneController(final Activity mainActivity,
60  			final GesturesListView playlistView) {
61  		super(R.id.magnatunebrowser, R.id.magnatuneList, mainActivity,
62  				playlistView, new CollectionImpl(AmbientApplication
63  						.getInstance().getLibrary()), R.id.magnatunePath);
64  		initButtonBar(R.id.magnatuneButtons, actions);
65  		((CollectionImpl) collection).filterOrigin(TrackOriginEnum.Magnatune);
66  		updateData();
67  	}
68  
69  	/***
70  	 * The actions to display on the Task switcher.
71  	 */
72  	public static final List<ActionsEnum> actions = Collections
73  			.unmodifiableList(Arrays.asList(ActionsEnum.Back,
74  					ActionsEnum.MagnatuneRescan, ActionsEnum.CollectionYear,
75  					ActionsEnum.BuyAlbum, ActionsEnum.ShowLicense,
76  					ActionsEnum.ShowArtistPage));
77  
78  	public void playbackStateChanged(PlayerStateEnum state) {
79  		// ignore
80  	}
81  
82  	public void playlistChanged(Interval target) {
83  		// ignore
84  	}
85  
86  	public void randomChanged(Random random) {
87  		// ignore
88  	}
89  
90  	public void repeatChanged(Repeat repeat) {
91  		// ignore
92  	}
93  
94  	public void trackChanged(final PlaylistItem track, final boolean play,
95  			final int positionMillis) {
96  		final String artistDesc = track == null ? null : track.getTrack()
97  				.getArtistDesc();
98  		final TextView view = (TextView) mainView
99  				.findViewById(R.id.magnatuneArtistDesc);
100 		view.setText(MiscUtils.emptyIfNull(artistDesc));
101 	}
102 
103 	public void trackPositionChanged(final int position, final boolean playing) {
104 		// ignore
105 	}
106 
107 	@Override
108 	public void update(Interval select) {
109 		trackChanged(app.getPlaylist().getCurrentlyPlayingItem(), false, 0);
110 		super.update(select);
111 	}
112 
113 	@Override
114 	protected void onAction(ActionsEnum action) {
115 		if (action == ActionsEnum.BuyAlbum) {
116 			buyAlbum();
117 			return;
118 		}
119 		if (action == ActionsEnum.ShowLicense) {
120 			showLicense();
121 			return;
122 		}
123 		if (action == ActionsEnum.ShowArtistPage) {
124 			showArtistPage();
125 			return;
126 		}
127 		if (action == ActionsEnum.MagnatuneRescan) {
128 			if (!app.getStateHandler().getStartupState().online) {
129 				app.error(MainActivity.class, true, mainActivity
130 						.getString(R.string.inOfflineMode), null);
131 			} else {
132 				app.getLibrary().queueScanner(TrackOriginEnum.Magnatune);
133 			}
134 			return;
135 		}
136 		super.onAction(action);
137 	}
138 
139 	private TrackMetadataBean getCurrent() {
140 		final TrackMetadataBean currentTrack = app.getPlaylist()
141 				.getCurrentlyPlayingTrack();
142 		if (currentTrack == null) {
143 			app.error(MainActivity.class, true, mainActivity
144 					.getString(R.string.no_playing_track), null);
145 		}
146 		return currentTrack;
147 	}
148 
149 	private void buyAlbum() {
150 		final TrackMetadataBean currentTrack = getCurrent();
151 		if (currentTrack == null) {
152 			return;
153 		}
154 		if (MiscUtils.isEmptyOrWhitespace(currentTrack.getBuyURL())) {
155 			app.error(MainActivity.class, true, mainActivity
156 					.getString(R.string.no_track_meta), null);
157 			return;
158 		}
159 		mainActivity.startActivity(new Intent(Intent.ACTION_VIEW, Uri
160 				.parse(currentTrack.getBuyURL())));
161 	}
162 
163 	private void showLicense() {
164 		final TrackMetadataBean currentTrack = getCurrent();
165 		if (currentTrack == null) {
166 			return;
167 		}
168 		if (MiscUtils.isEmptyOrWhitespace(currentTrack.getLicense())) {
169 			app.error(MainActivity.class, true, mainActivity
170 					.getString(R.string.no_track_meta), null);
171 			return;
172 		}
173 		mainActivity.startActivity(new Intent(Intent.ACTION_VIEW, Uri
174 				.parse(currentTrack.getLicense())));
175 	}
176 
177 	private void showArtistPage() {
178 		final TrackMetadataBean currentTrack = getCurrent();
179 		if (currentTrack == null)
180 			return;
181 		if (MiscUtils.isEmptyOrWhitespace(currentTrack.getArtistURL())) {
182 			app.error(MainActivity.class, true, mainActivity
183 					.getString(R.string.no_track_meta), null);
184 			return;
185 		}
186 		mainActivity.startActivity(new Intent(Intent.ACTION_VIEW, Uri
187 				.parse(currentTrack.getArtistURL())));
188 	}
189 
190 	@Override
191 	protected void performZoom(boolean zoom) {
192 		super.performZoom(zoom);
193 		initButtonBar(R.id.magnatuneButtons, actions);
194 	}
195 }