1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
/*
This file is part of Subsonic.
Subsonic is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Subsonic is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
Copyright 2014 (C) Scott Jackson
*/
package github.daneren2005.dsub.fragments;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import github.daneren2005.dsub.R;
import github.daneren2005.dsub.adapter.SectionAdapter;
import github.daneren2005.dsub.adapter.SimilarArtistAdapter;
import github.daneren2005.dsub.domain.Artist;
import github.daneren2005.dsub.domain.ArtistInfo;
import github.daneren2005.dsub.domain.MusicDirectory;
import github.daneren2005.dsub.service.DownloadService;
import github.daneren2005.dsub.service.MusicService;
import github.daneren2005.dsub.service.MusicServiceFactory;
import github.daneren2005.dsub.util.Constants;
import github.daneren2005.dsub.util.ProgressListener;
import github.daneren2005.dsub.util.Util;
import github.daneren2005.dsub.view.UpdateView;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
public class SimilarArtistFragment extends SelectRecyclerFragment<Artist> {
private static final String TAG = SimilarArtistFragment.class.getSimpleName();
private ArtistInfo info;
private String artistId;
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
artist = true;
artistId = getArguments().getString(Constants.INTENT_EXTRA_NAME_ARTIST);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_play_now:
playAll(false);
return true;
case R.id.menu_shuffle:
playAll(true);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onCreateContextMenu(Menu menu, MenuInflater menuInflater, UpdateView<Artist> updateView, Artist item) {
if(!Artist.MISSING_ID.equals(item.getId())) {
onCreateContextMenuSupport(menu, menuInflater, updateView, item);
recreateContextMenu(menu);
}
}
@Override
public boolean onContextItemSelected(MenuItem menuItem, UpdateView<Artist> updateView, Artist artist) {
return onContextItemSelected(menuItem, artist);
}
@Override
public void onItemClicked(UpdateView<Artist> updateView, Artist artist) {
if(Artist.MISSING_ID.equals(artist.getId())) {
String url = "http://www.last.fm/music/" + URLEncoder.encode(artist.getName());
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
} else {
SubsonicFragment fragment = new SelectDirectoryFragment();
Bundle args = new Bundle();
args.putString(Constants.INTENT_EXTRA_NAME_ID, artist.getId());
args.putString(Constants.INTENT_EXTRA_NAME_NAME, artist.getName());
args.putBoolean(Constants.INTENT_EXTRA_NAME_ARTIST, true);
fragment.setArguments(args);
replaceFragment(fragment);
}
}
@Override
public int getOptionsMenu() {
return R.menu.similar_artists;
}
@Override
public SectionAdapter getAdapter(List<Artist> artists) {
if(info.getMissingArtists().isEmpty()) {
return new SimilarArtistAdapter(context, artists, this);
} else {
List<String> headers = new ArrayList<>();
headers.add(null);
headers.add(context.getResources().getString(R.string.menu_similar_artists_missing));
List<Artist> missingArtists = new ArrayList<>();
for(String artistName: info.getMissingArtists()) {
Artist artist = new Artist(Artist.MISSING_ID, artistName);
missingArtists.add(artist);
}
return new SimilarArtistAdapter(context, headers, Arrays.asList(artists, missingArtists), this);
}
}
@Override
public List<Artist> getObjects(MusicService musicService, boolean refresh, ProgressListener listener) throws Exception {
info = musicService.getArtistInfo(artistId, refresh, true, context, listener);
return info.getSimilarArtists();
}
@Override
public int getTitleResource() {
return R.string.menu_similar_artists;
}
private void playAll(final boolean shuffle) {
new RecursiveLoader(context) {
@Override
protected Boolean doInBackground() throws Throwable {
musicService = MusicServiceFactory.getMusicService(context);
MusicDirectory root = new MusicDirectory();
for(Artist artist: objects) {
if(Util.isTagBrowsing(context) && !Util.isOffline(context)) {
root.addChildren(musicService.getArtist(artist.getId(), artist.getName(), false, context, this).getChildren());
} else {
root.addChildren(musicService.getMusicDirectory(artist.getId(), artist.getName(), false, context, this).getChildren());
}
}
if(shuffle) {
root.shuffleChildren();
}
songs = new LinkedList<MusicDirectory.Entry>();
getSongsRecursively(root, songs);
DownloadService downloadService = getDownloadService();
if (!songs.isEmpty() && downloadService != null) {
downloadService.clear();
downloadService.download(songs, false, true, false, false);
}
return true;
}
}.execute();
}
}
|