blob: fff96c77940ba5018adc81929e8d7e16eb41eca9 [file] [log] [blame]
Mike Lockwood7d7fb632010-12-01 18:46:23 -05001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mike Lockwood0cd01362010-12-30 11:54:33 -050017package android.mtp;
Mike Lockwood7d7fb632010-12-01 18:46:23 -050018
19import android.content.IContentProvider;
20import android.database.Cursor;
21import android.net.Uri;
22import android.os.RemoteException;
23import android.provider.MediaStore;
24import android.provider.MediaStore.Audio;
25import android.provider.MediaStore.Files;
26import android.provider.MediaStore.Images;
27import android.provider.MediaStore.MediaColumns;
28import android.util.Log;
29
30import java.util.ArrayList;
31
32class MtpPropertyGroup {
33
34 private static final String TAG = "MtpPropertyGroup";
35
36 private class Property {
37 // MTP property code
38 int code;
39 // MTP data type
40 int type;
41 // column index for our query
42 int column;
43
44 Property(int code, int type, int column) {
45 this.code = code;
46 this.type = type;
47 this.column = column;
48 }
49 }
50
51 private final MtpDatabase mDatabase;
52 private final IContentProvider mProvider;
53 private final String mVolumeName;
54 private final Uri mUri;
55
56 // list of all properties in this group
57 private final Property[] mProperties;
58
59 // list of columns for database query
60 private String[] mColumns;
61
62 private static final String ID_WHERE = Files.FileColumns._ID + "=?";
63 private static final String ID_FORMAT_WHERE = ID_WHERE + " AND "
64 + Files.FileColumns.FORMAT + "=?";
65 private static final String PARENT_WHERE = Files.FileColumns.PARENT + "=?";
66 private static final String PARENT_FORMAT_WHERE = PARENT_WHERE + " AND "
67 + Files.FileColumns.FORMAT + "=?";
68 // constructs a property group for a list of properties
69 public MtpPropertyGroup(MtpDatabase database, IContentProvider provider, String volume,
70 int[] properties) {
71 mDatabase = database;
72 mProvider = provider;
73 mVolumeName = volume;
74 mUri = Files.getMtpObjectsUri(volume);
75
76 int count = properties.length;
77 ArrayList<String> columns = new ArrayList<String>(count);
78 columns.add(Files.FileColumns._ID);
79
80 mProperties = new Property[count];
81 for (int i = 0; i < count; i++) {
82 mProperties[i] = createProperty(properties[i], columns);
83 }
84 count = columns.size();
85 mColumns = new String[count];
86 for (int i = 0; i < count; i++) {
87 mColumns[i] = columns.get(i);
88 }
89 }
90
91 private Property createProperty(int code, ArrayList<String> columns) {
92 String column = null;
93 int type;
94
95 switch (code) {
96 case MtpConstants.PROPERTY_STORAGE_ID:
97 // no query needed until we support multiple storage units
98 type = MtpConstants.TYPE_UINT32;
99 break;
100 case MtpConstants.PROPERTY_OBJECT_FORMAT:
101 column = Files.FileColumns.FORMAT;
102 type = MtpConstants.TYPE_UINT16;
103 break;
104 case MtpConstants.PROPERTY_PROTECTION_STATUS:
105 // protection status is always 0
106 type = MtpConstants.TYPE_UINT16;
107 break;
108 case MtpConstants.PROPERTY_OBJECT_SIZE:
109 column = Files.FileColumns.SIZE;
110 type = MtpConstants.TYPE_UINT64;
111 break;
112 case MtpConstants.PROPERTY_OBJECT_FILE_NAME:
113 column = Files.FileColumns.DATA;
114 type = MtpConstants.TYPE_STR;
115 break;
116 case MtpConstants.PROPERTY_NAME:
117 column = MediaColumns.TITLE;
118 type = MtpConstants.TYPE_STR;
119 break;
120 case MtpConstants.PROPERTY_DATE_MODIFIED:
121 column = Files.FileColumns.DATE_MODIFIED;
122 type = MtpConstants.TYPE_STR;
123 break;
124 case MtpConstants.PROPERTY_DATE_ADDED:
125 column = Files.FileColumns.DATE_ADDED;
126 type = MtpConstants.TYPE_STR;
127 break;
128 case MtpConstants.PROPERTY_ORIGINAL_RELEASE_DATE:
129 column = Audio.AudioColumns.YEAR;
130 type = MtpConstants.TYPE_STR;
131 break;
132 case MtpConstants.PROPERTY_PARENT_OBJECT:
133 column = Files.FileColumns.PARENT;
134 type = MtpConstants.TYPE_UINT32;
135 break;
136 case MtpConstants.PROPERTY_PERSISTENT_UID:
137 // PUID is concatenation of storageID and object handle
138 type = MtpConstants.TYPE_UINT128;
139 break;
140 case MtpConstants.PROPERTY_DURATION:
141 column = Audio.AudioColumns.DURATION;
142 type = MtpConstants.TYPE_UINT32;
143 break;
144 case MtpConstants.PROPERTY_TRACK:
145 column = Audio.AudioColumns.TRACK;
146 type = MtpConstants.TYPE_UINT16;
147 break;
148 case MtpConstants.PROPERTY_DISPLAY_NAME:
149 column = MediaColumns.DISPLAY_NAME;
150 type = MtpConstants.TYPE_STR;
151 break;
152 case MtpConstants.PROPERTY_ARTIST:
153 type = MtpConstants.TYPE_STR;
154 break;
155 case MtpConstants.PROPERTY_ALBUM_NAME:
156 type = MtpConstants.TYPE_STR;
157 break;
158 case MtpConstants.PROPERTY_ALBUM_ARTIST:
159 column = Audio.AudioColumns.ALBUM_ARTIST;
160 type = MtpConstants.TYPE_STR;
161 break;
162 case MtpConstants.PROPERTY_GENRE:
163 // genre requires a special query
164 type = MtpConstants.TYPE_STR;
165 break;
166 case MtpConstants.PROPERTY_COMPOSER:
167 column = Audio.AudioColumns.COMPOSER;
168 type = MtpConstants.TYPE_STR;
169 break;
170 case MtpConstants.PROPERTY_DESCRIPTION:
171 column = Images.ImageColumns.DESCRIPTION;
172 type = MtpConstants.TYPE_STR;
173 break;
174 default:
175 type = MtpConstants.TYPE_UNDEFINED;
176 Log.e(TAG, "unsupported property " + code);
177 break;
178 }
179
180 if (column != null) {
181 columns.add(column);
182 return new Property(code, type, columns.size() - 1);
183 } else {
184 return new Property(code, type, -1);
185 }
186 }
187
188 private String queryString(int id, String column) {
189 Cursor c = null;
190 try {
191 // for now we are only reading properties from the "objects" table
192 c = mProvider.query(mUri,
193 new String [] { Files.FileColumns._ID, column },
194 ID_WHERE, new String[] { Integer.toString(id) }, null);
195 if (c != null && c.moveToNext()) {
196 return c.getString(1);
197 } else {
198 return "";
199 }
200 } catch (Exception e) {
201 return null;
202 } finally {
203 if (c != null) {
204 c.close();
205 }
206 }
207 }
208
209 private String queryAudio(int id, String column) {
210 Cursor c = null;
211 try {
212 c = mProvider.query(Audio.Media.getContentUri(mVolumeName),
213 new String [] { Files.FileColumns._ID, column },
214 ID_WHERE, new String[] { Integer.toString(id) }, null);
215 if (c != null && c.moveToNext()) {
216 return c.getString(1);
217 } else {
218 return "";
219 }
220 } catch (Exception e) {
221 return null;
222 } finally {
223 if (c != null) {
224 c.close();
225 }
226 }
227 }
228
229 private String queryGenre(int id) {
230 Cursor c = null;
231 try {
232 Uri uri = Audio.Genres.getContentUriForAudioId(mVolumeName, id);
233 c = mProvider.query(uri,
234 new String [] { Files.FileColumns._ID, Audio.GenresColumns.NAME },
235 null, null, null);
236 if (c != null && c.moveToNext()) {
237 return c.getString(1);
238 } else {
239 return "";
240 }
241 } catch (Exception e) {
242 Log.e(TAG, "queryGenre exception", e);
243 return null;
244 } finally {
245 if (c != null) {
246 c.close();
247 }
248 }
249 }
250
251 private Long queryLong(int id, String column) {
252 Cursor c = null;
253 try {
254 // for now we are only reading properties from the "objects" table
255 c = mProvider.query(mUri,
256 new String [] { Files.FileColumns._ID, column },
257 ID_WHERE, new String[] { Integer.toString(id) }, null);
258 if (c != null && c.moveToNext()) {
259 return new Long(c.getLong(1));
260 }
261 } catch (Exception e) {
262 } finally {
263 if (c != null) {
264 c.close();
265 }
266 }
267 return null;
268 }
269
270 private static String nameFromPath(String path) {
271 // extract name from full path
272 int start = 0;
273 int lastSlash = path.lastIndexOf('/');
274 if (lastSlash >= 0) {
275 start = lastSlash + 1;
276 }
277 int end = path.length();
278 if (end - start > 255) {
279 end = start + 255;
280 }
281 return path.substring(start, end);
282 }
283
284 MtpPropertyList getPropertyList(int handle, int format, int depth, int storageID) {
285 Log.d(TAG, "getPropertyList handle: " + handle + " format: " + format + " depth: " + depth);
286 if (depth > 1) {
287 // we only support depth 0 and 1
288 // depth 0: single object, depth 1: immediate children
289 return new MtpPropertyList(0, MtpConstants.RESPONSE_SPECIFICATION_BY_DEPTH_UNSUPPORTED);
290 }
291
292 String where;
293 String[] whereArgs;
294 if (format == 0) {
295 whereArgs = new String[] { Integer.toString(handle) };
296 if (depth == 1) {
297 where = PARENT_WHERE;
298 } else {
299 where = ID_WHERE;
300 }
301 } else {
302 whereArgs = new String[] { Integer.toString(handle), Integer.toString(format) };
303 if (depth == 1) {
304 where = PARENT_FORMAT_WHERE;
305 } else {
306 where = ID_FORMAT_WHERE;
307 }
308 }
309
310 Cursor c = null;
311 try {
312 // don't query if not necessary
313 if (depth > 0 || mColumns.length > 1) {
314 c = mProvider.query(mUri, mColumns, where, whereArgs, null);
315 if (c == null) {
316 return new MtpPropertyList(0, MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE);
317 }
318 }
319
320 int count = (c == null ? 1 : c.getCount());
321 MtpPropertyList result = new MtpPropertyList(count * mProperties.length,
322 MtpConstants.RESPONSE_OK);
323
324 // iterate over all objects in the query
325 for (int objectIndex = 0; objectIndex < count; objectIndex++) {
326 if (c != null) {
327 c.moveToNext();
328 }
329 if (depth == 1) {
330 handle = (int)c.getLong(0);
331 }
332
333 // iterate over all properties in the query for the given object
334 for (int propertyIndex = 0; propertyIndex < mProperties.length; propertyIndex++) {
335 Property property = mProperties[propertyIndex];
336 int propertyCode = property.code;
337 int column = property.column;
338
339 // handle some special cases
340 switch (propertyCode) {
341 case MtpConstants.PROPERTY_STORAGE_ID:
342 result.append(handle, propertyCode, MtpConstants.TYPE_UINT32,
343 storageID);
344 break;
345 case MtpConstants.PROPERTY_PROTECTION_STATUS:
346 // protection status is always 0
347 result.append(handle, propertyCode, MtpConstants.TYPE_UINT16, 0);
348 break;
349 case MtpConstants.PROPERTY_OBJECT_FILE_NAME:
350 // special case - need to extract file name from full path
351 String value = c.getString(column);
352 if (value != null) {
353 result.append(handle, propertyCode, nameFromPath(value));
354 } else {
355 result.setResult(MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE);
356 }
357 break;
358 case MtpConstants.PROPERTY_NAME:
359 // first try title
360 String name = c.getString(column);
361 // then try name
362 if (name == null) {
363 name = queryString(handle, Audio.PlaylistsColumns.NAME);
364 }
365 // if title and name fail, extract name from full path
366 if (name == null) {
367 name = queryString(handle, Files.FileColumns.DATA);
368 if (name != null) {
369 name = nameFromPath(name);
370 }
371 }
372 if (name != null) {
373 result.append(handle, propertyCode, name);
374 } else {
375 result.setResult(MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE);
376 }
377 break;
378 case MtpConstants.PROPERTY_DATE_MODIFIED:
379 case MtpConstants.PROPERTY_DATE_ADDED:
380 // convert from seconds to DateTime
381 result.append(handle, propertyCode, format_date_time(c.getInt(column)));
382 break;
383 case MtpConstants.PROPERTY_ORIGINAL_RELEASE_DATE:
384 // release date is stored internally as just the year
385 int year = c.getInt(column);
386 String dateTime = Integer.toString(year) + "0101T000000";
387 result.append(handle, propertyCode, dateTime);
388 break;
389 case MtpConstants.PROPERTY_PERSISTENT_UID:
390 // PUID is concatenation of storageID and object handle
391 long puid = storageID;
392 puid <<= 32;
393 puid += handle;
394 result.append(handle, propertyCode, MtpConstants.TYPE_UINT128, puid);
395 break;
396 case MtpConstants.PROPERTY_TRACK:
397 result.append(handle, propertyCode, MtpConstants.TYPE_UINT16,
398 c.getInt(column) % 1000);
399 break;
400 case MtpConstants.PROPERTY_ARTIST:
401 result.append(handle, propertyCode,
402 queryAudio(handle, Audio.AudioColumns.ARTIST));
403 break;
404 case MtpConstants.PROPERTY_ALBUM_NAME:
405 result.append(handle, propertyCode,
406 queryAudio(handle, Audio.AudioColumns.ALBUM));
407 break;
408 case MtpConstants.PROPERTY_GENRE:
409 String genre = queryGenre(handle);
410 if (genre != null) {
411 result.append(handle, propertyCode, genre);
412 } else {
413 result.setResult(MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE);
414 }
415 break;
416 default:
417 if (property.type == MtpConstants.TYPE_STR) {
418 result.append(handle, propertyCode, c.getString(column));
419 } else if (property.type == MtpConstants.TYPE_UNDEFINED) {
420 result.append(handle, propertyCode, property.type, 0);
421 } else {
422 result.append(handle, propertyCode, property.type,
423 c.getLong(column));
424 }
425 break;
426 }
427 }
428 }
429
430 return result;
431 } catch (RemoteException e) {
432 return new MtpPropertyList(0, MtpConstants.RESPONSE_GENERAL_ERROR);
433 } finally {
434 if (c != null) {
435 c.close();
436 }
437 }
438 // impossible to get here, so no return statement
439 }
440
441 private native String format_date_time(long seconds);
442}