Clover Coverage Report - Subsonic-Android Coverage Report
Coverage timestamp: ven dic 19 2014 17:57:13 EST
../../../../../img/srcFileCovDistChart6.png 65% of files have more coverage
80   244   34   3,48
18   190   0,43   23
23     1,48  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  OfflineMusicService       Line # 52 80 34 60,3% 0.60330576
 
No Tests
 
1    /*
2    This file is part of Subsonic.
3   
4    Subsonic is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8   
9    Subsonic is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12    GNU General Public License for more details.
13   
14    You should have received a copy of the GNU General Public License
15    along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
16   
17    Copyright 2009 (C) Sindre Mehus
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 Sindre Mehus
51    */
 
52    public class OfflineMusicService extends RESTMusicService {
53   
 
54  0 toggle @Override
55    public boolean isLicenseValid(Context context, ProgressListener progressListener) throws Exception {
56  0 return true;
57    }
58   
 
59  1 toggle @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   
 
75  0 toggle @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   
 
93  70 toggle 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   
 
107  70 toggle 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   
 
129  3 toggle @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   
 
141  0 toggle @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   
 
146  0 toggle @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   
 
151  0 toggle @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   
 
156  0 toggle @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   
 
161  0 toggle @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   
 
166  0 toggle @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   
 
171  0 toggle @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   
 
176  0 toggle @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   
 
181  0 toggle @Override
182    public String getVideoUrl(Context context, String id) {
183  0 return null;
184    }
185   
 
186  1 toggle @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   
 
191  0 toggle @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   
 
196  1 toggle @Override
197    public JukeboxStatus stopJukebox(Context context, ProgressListener progressListener) throws Exception {
198  1 throw new OfflineException("Jukebox not available in offline mode");
199    }
200   
 
201  0 toggle @Override
202    public JukeboxStatus startJukebox(Context context, ProgressListener progressListener) throws Exception {
203  0 throw new OfflineException("Jukebox not available in offline mode");
204    }
205   
 
206  0 toggle @Override
207    public JukeboxStatus getJukeboxStatus(Context context, ProgressListener progressListener) throws Exception {
208  0 throw new OfflineException("Jukebox not available in offline mode");
209    }
210   
 
211  0 toggle @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   
 
216  2 toggle @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   
 
235  24 toggle 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    }