aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/github/daneren2005/dsub/service/MediaStoreService.java
blob: 0aa3269f3439501995757f7d5472a82b75a096e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 This file is part of Subsonic.

 Subsonic is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 Subsonic is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Subsonic.  If not, see <http://www.gnu.org/licenses/>.

 Copyright 2009 (C) Sindre Mehus
 */
package github.daneren2005.dsub.service;

import java.io.File;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;
import github.daneren2005.dsub.domain.MusicDirectory;
import github.daneren2005.dsub.util.FileUtil;
import github.daneren2005.dsub.util.Util;

/**
 * @author Sindre Mehus
 */
public class MediaStoreService {

	private static final String TAG = MediaStoreService.class.getSimpleName();
	private static final Uri ALBUM_ART_URI = Uri.parse("content://media/external/audio/albumart");

	private final Context context;

	public MediaStoreService(Context context) {
		this.context = context;
	}

	public void saveInMediaStore(DownloadFile downloadFile) {
		MusicDirectory.Entry song = downloadFile.getSong();
		File songFile = downloadFile.getCompleteFile();

		// Delete existing row in case the song has been downloaded before.
		deleteFromMediaStore(downloadFile);

		ContentResolver contentResolver = context.getContentResolver();
		ContentValues values = new ContentValues();
		if(!song.isVideo()) {
			values.put(MediaStore.MediaColumns.TITLE, song.getTitle());
			values.put(MediaStore.MediaColumns.DATA, songFile.getAbsolutePath());
			values.put(MediaStore.Audio.AudioColumns.ARTIST, song.getArtist());
			values.put(MediaStore.Audio.AudioColumns.ALBUM, song.getAlbum());
			if (song.getDuration() != null) {
				values.put(MediaStore.Audio.AudioColumns.DURATION, song.getDuration() * 1000L);
			}
			if (song.getTrack() != null) {
				values.put(MediaStore.Audio.AudioColumns.TRACK, song.getTrack());
			}
			if (song.getYear() != null) {
				values.put(MediaStore.Audio.AudioColumns.YEAR, song.getYear());
			}
			if(song.getTranscodedContentType() != null) {
				values.put(MediaStore.MediaColumns.MIME_TYPE, song.getTranscodedContentType());
			} else {
				values.put(MediaStore.MediaColumns.MIME_TYPE, song.getContentType());
			}
			values.put(MediaStore.Audio.AudioColumns.IS_MUSIC, 1);

			Uri uri = contentResolver.insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values);

			// Look up album, and add cover art if found.
			Cursor cursor = contentResolver.query(uri, new String[]{MediaStore.Audio.AudioColumns.ALBUM_ID}, null, null, null);
			if (cursor.moveToFirst()) {
				int albumId = cursor.getInt(0);
				insertAlbumArt(albumId, downloadFile);
			}

			cursor.close();
		} else {
			values.put(MediaStore.Video.VideoColumns.TITLE, song.getTitle());
			values.put(MediaStore.Video.VideoColumns.DISPLAY_NAME, song.getTitle());
			values.put(MediaStore.Video.VideoColumns.ARTIST, song.getArtist());
			values.put(MediaStore.Video.VideoColumns.DATA, songFile.getAbsolutePath());
			if (song.getDuration() != null) {
				values.put(MediaStore.Video.VideoColumns.DURATION, song.getDuration() * 1000L);
			}

			String videoPlayerType = Util.getVideoPlayerType(context);
			if("hls".equals(videoPlayerType)) {
				// HLS should be able to transcode to mp4 automatically
				values.put(MediaStore.MediaColumns.MIME_TYPE, "video/mpeg");
			} else if("raw".equals(videoPlayerType) || song.getTranscodedContentType() == null) {
				// Download the original video without any transcoding
				values.put(MediaStore.MediaColumns.MIME_TYPE, song.getContentType());
			} else {
				values.put(MediaStore.MediaColumns.MIME_TYPE, song.getTranscodedContentType());
			}

			Uri uri = contentResolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
			if(uri == null) {
				Log.e(TAG, "Failed to insert");
			}
		}
	}

	public void deleteFromMediaStore(DownloadFile downloadFile) {
		ContentResolver contentResolver = context.getContentResolver();
		MusicDirectory.Entry song = downloadFile.getSong();
		File file = downloadFile.getCompleteFile();

		Uri uri;
		if(song.isVideo()) {
			uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
		} else {
			uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
		}

		int n = contentResolver.delete(uri,
				MediaStore.MediaColumns.DATA + "=?",
				new String[]{file.getAbsolutePath()});
		if (n > 0) {
			Log.i(TAG, "Deleting media store row for " + song);
		}
	}

	public void deleteFromMediaStore(File file) {
		ContentResolver contentResolver = context.getContentResolver();

		Uri uri;
		if(FileUtil.isVideoFile(file)) {
			uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
		} else {
			uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
		}

		int n = contentResolver.delete(uri,
				MediaStore.MediaColumns.DATA + "=?",
				new String[]{file.getAbsolutePath()});
		if (n > 0) {
			Log.i(TAG, "Deleting media store row for " + file);
		}
	}

	public void renameInMediaStore(File start, File end) {
		ContentResolver contentResolver = context.getContentResolver();

		ContentValues values = new ContentValues();
		values.put(MediaStore.MediaColumns.DATA, end.getAbsolutePath());

		int n = contentResolver.update(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
				values,
				MediaStore.MediaColumns.DATA + "=?",
				new String[]{start.getAbsolutePath()});
		if (n > 0) {
			Log.i(TAG, "Rename media store row for " + start + " to " + end);
		}
	}

	private void insertAlbumArt(int albumId, DownloadFile downloadFile) {
		ContentResolver contentResolver = context.getContentResolver();

		Cursor cursor = contentResolver.query(Uri.withAppendedPath(ALBUM_ART_URI, String.valueOf(albumId)), null, null, null, null);
		if (!cursor.moveToFirst()) {

			// No album art found, add it.
			File albumArtFile = FileUtil.getAlbumArtFile(context, downloadFile.getSong());
			if (albumArtFile.exists()) {
				ContentValues values = new ContentValues();
				values.put(MediaStore.Audio.AlbumColumns.ALBUM_ID, albumId);
				values.put(MediaStore.MediaColumns.DATA, albumArtFile.getPath());
				contentResolver.insert(ALBUM_ART_URI, values);
				Log.i(TAG, "Added album art: " + albumArtFile);
			}
		}
		cursor.close();
	}

}