1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
package net.sourceforge.subsonic.androidapp.service; |
20 |
|
|
21 |
|
import java.io.File; |
22 |
|
import java.io.FileInputStream; |
23 |
|
import java.io.InputStream; |
24 |
|
import java.util.ArrayList; |
25 |
|
import java.util.Collections; |
26 |
|
import java.util.HashSet; |
27 |
|
import java.util.LinkedList; |
28 |
|
import java.util.List; |
29 |
|
import java.util.Random; |
30 |
|
import java.util.Set; |
31 |
|
|
32 |
|
import android.content.Context; |
33 |
|
import android.graphics.Bitmap; |
34 |
|
import android.graphics.BitmapFactory; |
35 |
|
import net.sourceforge.subsonic.androidapp.domain.Artist; |
36 |
|
import net.sourceforge.subsonic.androidapp.domain.Indexes; |
37 |
|
import net.sourceforge.subsonic.androidapp.domain.JukeboxStatus; |
38 |
|
import net.sourceforge.subsonic.androidapp.domain.Lyrics; |
39 |
|
import net.sourceforge.subsonic.androidapp.domain.MusicDirectory; |
40 |
|
import net.sourceforge.subsonic.androidapp.domain.MusicFolder; |
41 |
|
import net.sourceforge.subsonic.androidapp.domain.Playlist; |
42 |
|
import net.sourceforge.subsonic.androidapp.domain.SearchCritera; |
43 |
|
import net.sourceforge.subsonic.androidapp.domain.SearchResult; |
44 |
|
import net.sourceforge.subsonic.androidapp.util.Constants; |
45 |
|
import net.sourceforge.subsonic.androidapp.util.FileUtil; |
46 |
|
import net.sourceforge.subsonic.androidapp.util.ProgressListener; |
47 |
|
import net.sourceforge.subsonic.androidapp.util.Util; |
48 |
|
|
49 |
|
|
50 |
|
@author |
51 |
|
|
|
|
| 60,3% |
Uncovered Elements: 48 (121) |
Complexity: 34 |
Complexity Density: 0,43 |
|
52 |
|
public class OfflineMusicService extends RESTMusicService { |
53 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
54 |
0
|
@Override... |
55 |
|
public boolean isLicenseValid(Context context, ProgressListener progressListener) throws Exception { |
56 |
0
|
return true; |
57 |
|
} |
58 |
|
|
|
|
| 91,7% |
Uncovered Elements: 1 (12) |
Complexity: 2 |
Complexity Density: 0,2 |
|
59 |
1
|
@Override... |
60 |
|
public Indexes getIndexes(String musicFolderId, boolean refresh, Context context, ProgressListener progressListener) throws Exception { |
61 |
1
|
List<Artist> artists = new ArrayList<Artist>(); |
62 |
1
|
File root = FileUtil.getMusicDirectory(context); |
63 |
1
|
for (File file : FileUtil.listFiles(root)) { |
64 |
5
|
if (file.isDirectory()) { |
65 |
5
|
Artist artist = new Artist(); |
66 |
5
|
artist.setId(file.getPath()); |
67 |
5
|
artist.setIndex(file.getName().substring(0, 1)); |
68 |
5
|
artist.setName(file.getName()); |
69 |
5
|
artists.add(artist); |
70 |
|
} |
71 |
|
} |
72 |
1
|
return new Indexes(0L, Collections.<Artist>emptyList(), artists); |
73 |
|
} |
74 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 2 |
Complexity Density: 0,2 |
|
75 |
0
|
@Override... |
76 |
|
public MusicDirectory getMusicDirectory(String id, boolean refresh, Context context, ProgressListener progressListener) throws Exception { |
77 |
0
|
File dir = new File(id); |
78 |
0
|
MusicDirectory result = new MusicDirectory(); |
79 |
0
|
result.setName(dir.getName()); |
80 |
|
|
81 |
0
|
Set<String> names = new HashSet<String>(); |
82 |
|
|
83 |
0
|
for (File file : FileUtil.listMusicFiles(dir)) { |
84 |
0
|
String name = getName(file); |
85 |
0
|
if (name != null & !names.contains(name)) { |
86 |
0
|
names.add(name); |
87 |
0
|
result.addChild(createEntry(context, file, name)); |
88 |
|
} |
89 |
|
} |
90 |
0
|
return result; |
91 |
|
} |
92 |
|
|
|
|
| 81,8% |
Uncovered Elements: 2 (11) |
Complexity: 5 |
Complexity Density: 0,71 |
|
93 |
70
|
private String getName(File file) {... |
94 |
70
|
String name = file.getName(); |
95 |
70
|
if (file.isDirectory()) { |
96 |
0
|
return name; |
97 |
|
} |
98 |
|
|
99 |
70
|
if (name.endsWith(".partial") || name.contains(".partial.") || name.equals(Constants.ALBUM_ART_FILE)) { |
100 |
4
|
return null; |
101 |
|
} |
102 |
|
|
103 |
66
|
name = name.replace(".complete", ""); |
104 |
66
|
return FileUtil.getBaseName(name); |
105 |
|
} |
106 |
|
|
|
|
| 90% |
Uncovered Elements: 2 (20) |
Complexity: 3 |
Complexity Density: 0,19 |
|
107 |
70
|
private MusicDirectory.Entry createEntry(Context context, File file, String name) {... |
108 |
70
|
MusicDirectory.Entry entry = new MusicDirectory.Entry(); |
109 |
70
|
entry.setDirectory(file.isDirectory()); |
110 |
70
|
entry.setId(file.getPath()); |
111 |
70
|
entry.setParent(file.getParent()); |
112 |
70
|
entry.setSize(file.length()); |
113 |
70
|
String root = FileUtil.getMusicDirectory(context).getPath(); |
114 |
70
|
entry.setPath(file.getPath().replaceFirst("^" + root + "/" , "")); |
115 |
70
|
if (file.isFile()) { |
116 |
70
|
entry.setArtist(file.getParentFile().getParentFile().getName()); |
117 |
70
|
entry.setAlbum(file.getParentFile().getName()); |
118 |
|
} |
119 |
70
|
entry.setTitle(name); |
120 |
70
|
entry.setSuffix(FileUtil.getExtension(file.getName().replace(".complete", ""))); |
121 |
|
|
122 |
70
|
File albumArt = FileUtil.getAlbumArtFile(context, entry); |
123 |
70
|
if (albumArt.exists()) { |
124 |
70
|
entry.setCoverArt(albumArt.getPath()); |
125 |
|
} |
126 |
70
|
return entry; |
127 |
|
} |
128 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0,17 |
|
129 |
3
|
@Override... |
130 |
|
public Bitmap getCoverArt(Context context, MusicDirectory.Entry entry, int size, boolean saveToFile, ProgressListener progressListener) throws Exception { |
131 |
3
|
InputStream in = new FileInputStream(entry.getCoverArt()); |
132 |
3
|
try { |
133 |
3
|
byte[] bytes = Util.toByteArray(in); |
134 |
3
|
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); |
135 |
3
|
return Bitmap.createScaledBitmap(bitmap, size, size, true); |
136 |
|
} finally { |
137 |
3
|
Util.close(in); |
138 |
|
} |
139 |
|
} |
140 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
141 |
0
|
@Override... |
142 |
|
public List<MusicFolder> getMusicFolders(Context context, ProgressListener progressListener) throws Exception { |
143 |
0
|
throw new OfflineException("Music folders not available in offline mode"); |
144 |
|
} |
145 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
146 |
0
|
@Override... |
147 |
|
public SearchResult search(SearchCritera criteria, Context context, ProgressListener progressListener) throws Exception { |
148 |
0
|
throw new OfflineException("Search not available in offline mode"); |
149 |
|
} |
150 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
151 |
0
|
@Override... |
152 |
|
public List<Playlist> getPlaylists(boolean refresh, Context context, ProgressListener progressListener) throws Exception { |
153 |
0
|
throw new OfflineException("Playlists not available in offline mode"); |
154 |
|
} |
155 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
156 |
0
|
@Override... |
157 |
|
public MusicDirectory getPlaylist(String id, Context context, ProgressListener progressListener) throws Exception { |
158 |
0
|
throw new OfflineException("Playlists not available in offline mode"); |
159 |
|
} |
160 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
161 |
0
|
@Override... |
162 |
|
public void createPlaylist(String id, String name, List<MusicDirectory.Entry> entries, Context context, ProgressListener progressListener) throws Exception { |
163 |
0
|
throw new OfflineException("Playlists not available in offline mode"); |
164 |
|
} |
165 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
166 |
0
|
@Override... |
167 |
|
public Lyrics getLyrics(String artist, String title, Context context, ProgressListener progressListener) throws Exception { |
168 |
0
|
throw new OfflineException("Lyrics not available in offline mode"); |
169 |
|
} |
170 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
171 |
0
|
@Override... |
172 |
|
public void scrobble(String id, boolean submission, Context context, ProgressListener progressListener) throws Exception { |
173 |
0
|
throw new OfflineException("Scrobbling not available in offline mode"); |
174 |
|
} |
175 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
176 |
0
|
@Override... |
177 |
|
public MusicDirectory getAlbumList(String type, int size, int offset, Context context, ProgressListener progressListener) throws Exception { |
178 |
0
|
throw new OfflineException("Album lists not available in offline mode"); |
179 |
|
} |
180 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
181 |
0
|
@Override... |
182 |
|
public String getVideoUrl(Context context, String id) { |
183 |
0
|
return null; |
184 |
|
} |
185 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
186 |
1
|
@Override... |
187 |
|
public JukeboxStatus updateJukeboxPlaylist(List<String> ids, Context context, ProgressListener progressListener) throws Exception { |
188 |
1
|
throw new OfflineException("Jukebox not available in offline mode"); |
189 |
|
} |
190 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
191 |
0
|
@Override... |
192 |
|
public JukeboxStatus skipJukebox(int index, int offsetSeconds, Context context, ProgressListener progressListener) throws Exception { |
193 |
0
|
throw new OfflineException("Jukebox not available in offline mode"); |
194 |
|
} |
195 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
196 |
1
|
@Override... |
197 |
|
public JukeboxStatus stopJukebox(Context context, ProgressListener progressListener) throws Exception { |
198 |
1
|
throw new OfflineException("Jukebox not available in offline mode"); |
199 |
|
} |
200 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
201 |
0
|
@Override... |
202 |
|
public JukeboxStatus startJukebox(Context context, ProgressListener progressListener) throws Exception { |
203 |
0
|
throw new OfflineException("Jukebox not available in offline mode"); |
204 |
|
} |
205 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
206 |
0
|
@Override... |
207 |
|
public JukeboxStatus getJukeboxStatus(Context context, ProgressListener progressListener) throws Exception { |
208 |
0
|
throw new OfflineException("Jukebox not available in offline mode"); |
209 |
|
} |
210 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
211 |
0
|
@Override... |
212 |
|
public JukeboxStatus setJukeboxGain(float gain, Context context, ProgressListener progressListener) throws Exception { |
213 |
0
|
throw new OfflineException("Jukebox not available in offline mode"); |
214 |
|
} |
215 |
|
|
|
|
| 86,7% |
Uncovered Elements: 2 (15) |
Complexity: 3 |
Complexity Density: 0,27 |
|
216 |
2
|
@Override... |
217 |
|
public MusicDirectory getRandomSongs(int size, Context context, ProgressListener progressListener) throws Exception { |
218 |
2
|
File root = FileUtil.getMusicDirectory(context); |
219 |
2
|
List<File> children = new LinkedList<File>(); |
220 |
2
|
listFilesRecursively(root, children); |
221 |
2
|
MusicDirectory result = new MusicDirectory(); |
222 |
|
|
223 |
2
|
if (children.isEmpty()) { |
224 |
0
|
return result; |
225 |
|
} |
226 |
2
|
Random random = new Random(); |
227 |
72
|
for (int i = 0; i < size; i++) { |
228 |
70
|
File file = children.get(random.nextInt(children.size())); |
229 |
70
|
result.addChild(createEntry(context, file, getName(file))); |
230 |
|
} |
231 |
|
|
232 |
2
|
return result; |
233 |
|
} |
234 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0,5 |
|
235 |
24
|
private void listFilesRecursively(File parent, List<File> children) {... |
236 |
24
|
for (File file : FileUtil.listMusicFiles(parent)) { |
237 |
41
|
if (file.isFile()) { |
238 |
19
|
children.add(file); |
239 |
|
} else { |
240 |
22
|
listFilesRecursively(file, children); |
241 |
|
} |
242 |
|
} |
243 |
|
} |
244 |
|
} |