Merge lp:~karni/ubuntuone-android-music/fix-mediastore-conflict into lp:ubuntuone-android-music

Proposed by Michał Karnicki on 2012-04-17
Status: Merged
Approved by: Chad Miller on 2012-04-17
Approved revision: 564
Merged at revision: 564
Proposed branch: lp:~karni/ubuntuone-android-music/fix-mediastore-conflict
Merge into: lp:ubuntuone-android-music
Diff against target: 82 lines (+20/-10)
2 files modified
src/net/sourceforge/subsonic/androidapp/service/MediaStoreService.java (+20/-9)
src/net/sourceforge/subsonic/androidapp/util/Util.java (+0/-1)
To merge this branch: bzr merge lp:~karni/ubuntuone-android-music/fix-mediastore-conflict
Reviewer Review Type Date Requested Status
Chad Miller (community) 2012-04-17 Approve on 2012-04-17
Review via email: mp+102315@code.launchpad.net

Description of the change

This branch defaults song year to 1 if it's equal to zero and adds a little safer code around cursor handling.

To post a comment you must log in.
564. By Michał Karnicki on 2012-04-17

Don't include YEAR rather than bogus value 1.

Chad Miller (cmiller) wrote :

Nice.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/net/sourceforge/subsonic/androidapp/service/MediaStoreService.java'
2--- src/net/sourceforge/subsonic/androidapp/service/MediaStoreService.java 2010-11-30 13:36:58 +0000
3+++ src/net/sourceforge/subsonic/androidapp/service/MediaStoreService.java 2012-04-17 14:46:18 +0000
4@@ -20,6 +20,8 @@
5
6 import java.io.File;
7
8+import net.sourceforge.subsonic.androidapp.domain.MusicDirectory;
9+import net.sourceforge.subsonic.androidapp.util.FileUtil;
10 import android.content.ContentResolver;
11 import android.content.ContentValues;
12 import android.content.Context;
13@@ -27,8 +29,6 @@
14 import android.net.Uri;
15 import android.provider.MediaStore;
16 import android.util.Log;
17-import net.sourceforge.subsonic.androidapp.domain.MusicDirectory;
18-import net.sourceforge.subsonic.androidapp.util.FileUtil;
19
20 /**
21 * @author Sindre Mehus
22@@ -57,20 +57,32 @@
23 values.put(MediaStore.Audio.AudioColumns.ARTIST, song.getArtist());
24 values.put(MediaStore.Audio.AudioColumns.ALBUM, song.getAlbum());
25 values.put(MediaStore.Audio.AudioColumns.TRACK, song.getTrack());
26- values.put(MediaStore.Audio.AudioColumns.YEAR, song.getYear());
27+ // Protect from failed SQLite constraint.
28+ if (song.getYear() != 0) {
29+ values.put(MediaStore.Audio.AudioColumns.YEAR, song.getYear());
30+ }
31 values.put(MediaStore.MediaColumns.DATA, songFile.getAbsolutePath());
32 values.put(MediaStore.MediaColumns.MIME_TYPE, song.getContentType());
33 values.put(MediaStore.Audio.AudioColumns.IS_MUSIC, 1);
34
35 Uri uri = contentResolver.insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values);
36
37+ if (uri == null) {
38+ Log.w(TAG, "Audio MediaStore insert failed.");
39+ return;
40+ }
41+
42 // Look up album, and add cover art if found.
43 Cursor cursor = contentResolver.query(uri, new String[]{MediaStore.Audio.AudioColumns.ALBUM_ID}, null, null, null);
44- if (cursor.moveToFirst()) {
45- int albumId = cursor.getInt(0);
46- insertAlbumArt(albumId, downloadFile);
47+ try {
48+ if (cursor != null && cursor.moveToFirst()) {
49+ int albumId = cursor.getInt(0);
50+ insertAlbumArt(albumId, downloadFile);
51+ }
52+ } finally {
53+ if (cursor != null)
54+ cursor.close();
55 }
56- cursor.close();
57 }
58
59 public void deleteFromMediaStore(DownloadFile downloadFile) {
60@@ -79,9 +91,8 @@
61 File file = downloadFile.getCompleteFile();
62
63 int n = contentResolver.delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
64- MediaStore.Audio.AudioColumns.TITLE_KEY + "=? AND " +
65 MediaStore.MediaColumns.DATA + "=?",
66- new String[]{MediaStore.Audio.keyFor(song.getTitle()), file.getAbsolutePath()});
67+ new String[]{file.getAbsolutePath()});
68 if (n > 0) {
69 Log.i(TAG, "Deleting media store row for " + song);
70 }
71
72=== modified file 'src/net/sourceforge/subsonic/androidapp/util/Util.java'
73--- src/net/sourceforge/subsonic/androidapp/util/Util.java 2011-11-04 00:07:03 +0000
74+++ src/net/sourceforge/subsonic/androidapp/util/Util.java 2012-04-17 14:46:18 +0000
75@@ -56,7 +56,6 @@
76 import android.content.DialogInterface;
77 import android.content.Intent;
78 import android.content.SharedPreferences;
79-import android.content.SharedPreferences.Editor;
80 import android.content.pm.PackageInfo;
81 import android.content.pm.PackageManager;
82 import android.content.res.Resources;

Subscribers

People subscribed via source and target branches

to status/vote changes: