Clover Coverage Report - Subsonic-Android Coverage Report
Coverage timestamp: ven dic 19 2014 17:57:13 EST
../../../../../img/srcFileCovDistChart2.png 83% of files have more coverage
37   109   8   9,25
8   67   0,22   4
4     2  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  MediaStoreService       Line # 36 37 8 18,4% 0.18367347
 
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   
23    import android.content.ContentResolver;
24    import android.content.ContentValues;
25    import android.content.Context;
26    import android.database.Cursor;
27    import android.net.Uri;
28    import android.provider.MediaStore;
29    import android.util.Log;
30    import net.sourceforge.subsonic.androidapp.domain.MusicDirectory;
31    import net.sourceforge.subsonic.androidapp.util.FileUtil;
32   
33    /**
34    * @author Sindre Mehus
35    */
 
36    public class MediaStoreService {
37   
38    private static final String TAG = MediaStoreService.class.getSimpleName();
39    private static final Uri ALBUM_ART_URI = Uri.parse("content://media/external/audio/albumart");
40   
41    private final Context context;
42   
 
43  164 toggle public MediaStoreService(Context context) {
44  164 this.context = context;
45    }
46   
 
47  0 toggle public void saveInMediaStore(DownloadFile downloadFile) {
48  0 MusicDirectory.Entry song = downloadFile.getSong();
49  0 File songFile = downloadFile.getCompleteFile();
50   
51    // Delete existing row in case the song has been downloaded before.
52  0 deleteFromMediaStore(downloadFile);
53   
54  0 ContentResolver contentResolver = context.getContentResolver();
55  0 ContentValues values = new ContentValues();
56  0 values.put(MediaStore.MediaColumns.TITLE, song.getTitle());
57  0 values.put(MediaStore.Audio.AudioColumns.ARTIST, song.getArtist());
58  0 values.put(MediaStore.Audio.AudioColumns.ALBUM, song.getAlbum());
59  0 values.put(MediaStore.Audio.AudioColumns.TRACK, song.getTrack());
60  0 values.put(MediaStore.Audio.AudioColumns.YEAR, song.getYear());
61  0 values.put(MediaStore.MediaColumns.DATA, songFile.getAbsolutePath());
62  0 values.put(MediaStore.MediaColumns.MIME_TYPE, song.getContentType());
63  0 values.put(MediaStore.Audio.AudioColumns.IS_MUSIC, 1);
64   
65  0 Uri uri = contentResolver.insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values);
66   
67    // Look up album, and add cover art if found.
68  0 Cursor cursor = contentResolver.query(uri, new String[]{MediaStore.Audio.AudioColumns.ALBUM_ID}, null, null, null);
69  0 if (cursor.moveToFirst()) {
70  0 int albumId = cursor.getInt(0);
71  0 insertAlbumArt(albumId, downloadFile);
72    }
73  0 cursor.close();
74    }
75   
 
76  4 toggle public void deleteFromMediaStore(DownloadFile downloadFile) {
77  4 ContentResolver contentResolver = context.getContentResolver();
78  4 MusicDirectory.Entry song = downloadFile.getSong();
79  4 File file = downloadFile.getCompleteFile();
80   
81  4 int n = contentResolver.delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
82    MediaStore.Audio.AudioColumns.TITLE_KEY + "=? AND " +
83    MediaStore.MediaColumns.DATA + "=?",
84    new String[]{MediaStore.Audio.keyFor(song.getTitle()), file.getAbsolutePath()});
85  4 if (n > 0) {
86  0 Log.i(TAG, "Deleting media store row for " + song);
87    }
88    }
89   
 
90  0 toggle private void insertAlbumArt(int albumId, DownloadFile downloadFile) {
91  0 ContentResolver contentResolver = context.getContentResolver();
92   
93  0 Cursor cursor = contentResolver.query(Uri.withAppendedPath(ALBUM_ART_URI, String.valueOf(albumId)), null, null, null, null);
94  0 if (!cursor.moveToFirst()) {
95   
96    // No album art found, add it.
97  0 File albumArtFile = FileUtil.getAlbumArtFile(context, downloadFile.getSong());
98  0 if (albumArtFile.exists()) {
99  0 ContentValues values = new ContentValues();
100  0 values.put(MediaStore.Audio.AlbumColumns.ALBUM_ID, albumId);
101  0 values.put(MediaStore.MediaColumns.DATA, albumArtFile.getPath());
102  0 contentResolver.insert(ALBUM_ART_URI, values);
103  0 Log.i(TAG, "Added album art: " + albumArtFile);
104    }
105    }
106  0 cursor.close();
107    }
108   
109    }