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 |
|
|
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 |
35 |
|
|
|
|
| 18,4% |
Uncovered Elements: 40 (49) |
Complexity: 8 |
Complexity Density: 0,22 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
43 |
164
|
public MediaStoreService(Context context) {... |
44 |
164
|
this.context = context; |
45 |
|
} |
46 |
|
|
|
|
| 0% |
Uncovered Elements: 21 (21) |
Complexity: 2 |
Complexity Density: 0,11 |
|
47 |
0
|
public void saveInMediaStore(DownloadFile downloadFile) {... |
48 |
0
|
MusicDirectory.Entry song = downloadFile.getSong(); |
49 |
0
|
File songFile = downloadFile.getCompleteFile(); |
50 |
|
|
51 |
|
|
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 |
|
|
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 |
|
|
|
|
| 75% |
Uncovered Elements: 2 (8) |
Complexity: 2 |
Complexity Density: 0,33 |
|
76 |
4
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 15 (15) |
Complexity: 3 |
Complexity Density: 0,27 |
|
90 |
0
|
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 |
|
|
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 |
|
} |