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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
|
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.mtp;
import android.content.IContentProvider;
import android.database.Cursor;
import android.net.Uri;
import android.os.RemoteException;
import android.provider.MediaStore.Audio;
import android.provider.MediaStore.Files;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.MediaColumns;
import android.util.Log;
import java.util.ArrayList;
class MtpPropertyGroup {
private static final String TAG = "MtpPropertyGroup";
private class Property {
// MTP property code
int code;
// MTP data type
int type;
// column index for our query
int column;
Property(int code, int type, int column) {
this.code = code;
this.type = type;
this.column = column;
}
}
private final MtpDatabase mDatabase;
private final IContentProvider mProvider;
private final String mPackageName;
private final String mVolumeName;
private final Uri mUri;
// list of all properties in this group
private final Property[] mProperties;
// list of columns for database query
private String[] mColumns;
private static final String ID_WHERE = Files.FileColumns._ID + "=?";
private static final String FORMAT_WHERE = Files.FileColumns.FORMAT + "=?";
private static final String ID_FORMAT_WHERE = ID_WHERE + " AND " + FORMAT_WHERE;
private static final String PARENT_WHERE = Files.FileColumns.PARENT + "=?";
private static final String PARENT_FORMAT_WHERE = PARENT_WHERE + " AND " + FORMAT_WHERE;
// constructs a property group for a list of properties
public MtpPropertyGroup(MtpDatabase database, IContentProvider provider, String packageName,
String volume, int[] properties) {
mDatabase = database;
mProvider = provider;
mPackageName = packageName;
mVolumeName = volume;
mUri = Files.getMtpObjectsUri(volume);
int count = properties.length;
ArrayList<String> columns = new ArrayList<String>(count);
columns.add(Files.FileColumns._ID);
mProperties = new Property[count];
for (int i = 0; i < count; i++) {
mProperties[i] = createProperty(properties[i], columns);
}
count = columns.size();
mColumns = new String[count];
for (int i = 0; i < count; i++) {
mColumns[i] = columns.get(i);
}
}
private Property createProperty(int code, ArrayList<String> columns) {
String column = null;
int type;
switch (code) {
case MtpConstants.PROPERTY_STORAGE_ID:
column = Files.FileColumns.STORAGE_ID;
type = MtpConstants.TYPE_UINT32;
break;
case MtpConstants.PROPERTY_OBJECT_FORMAT:
column = Files.FileColumns.FORMAT;
type = MtpConstants.TYPE_UINT16;
break;
case MtpConstants.PROPERTY_PROTECTION_STATUS:
// protection status is always 0
type = MtpConstants.TYPE_UINT16;
break;
case MtpConstants.PROPERTY_OBJECT_SIZE:
column = Files.FileColumns.SIZE;
type = MtpConstants.TYPE_UINT64;
break;
case MtpConstants.PROPERTY_OBJECT_FILE_NAME:
column = Files.FileColumns.DATA;
type = MtpConstants.TYPE_STR;
break;
case MtpConstants.PROPERTY_NAME:
column = MediaColumns.TITLE;
type = MtpConstants.TYPE_STR;
break;
case MtpConstants.PROPERTY_DATE_MODIFIED:
column = Files.FileColumns.DATE_MODIFIED;
type = MtpConstants.TYPE_STR;
break;
case MtpConstants.PROPERTY_DATE_ADDED:
column = Files.FileColumns.DATE_ADDED;
type = MtpConstants.TYPE_STR;
break;
case MtpConstants.PROPERTY_ORIGINAL_RELEASE_DATE:
column = Audio.AudioColumns.YEAR;
type = MtpConstants.TYPE_STR;
break;
case MtpConstants.PROPERTY_PARENT_OBJECT:
column = Files.FileColumns.PARENT;
type = MtpConstants.TYPE_UINT32;
break;
case MtpConstants.PROPERTY_PERSISTENT_UID:
// PUID is concatenation of storageID and object handle
column = Files.FileColumns.STORAGE_ID;
type = MtpConstants.TYPE_UINT128;
break;
case MtpConstants.PROPERTY_DURATION:
column = Audio.AudioColumns.DURATION;
type = MtpConstants.TYPE_UINT32;
break;
case MtpConstants.PROPERTY_TRACK:
column = Audio.AudioColumns.TRACK;
type = MtpConstants.TYPE_UINT16;
break;
case MtpConstants.PROPERTY_DISPLAY_NAME:
column = MediaColumns.DISPLAY_NAME;
type = MtpConstants.TYPE_STR;
break;
case MtpConstants.PROPERTY_ARTIST:
type = MtpConstants.TYPE_STR;
break;
case MtpConstants.PROPERTY_ALBUM_NAME:
type = MtpConstants.TYPE_STR;
break;
case MtpConstants.PROPERTY_ALBUM_ARTIST:
column = Audio.AudioColumns.ALBUM_ARTIST;
type = MtpConstants.TYPE_STR;
break;
case MtpConstants.PROPERTY_GENRE:
// genre requires a special query
type = MtpConstants.TYPE_STR;
break;
case MtpConstants.PROPERTY_COMPOSER:
column = Audio.AudioColumns.COMPOSER;
type = MtpConstants.TYPE_STR;
break;
case MtpConstants.PROPERTY_DESCRIPTION:
column = Images.ImageColumns.DESCRIPTION;
type = MtpConstants.TYPE_STR;
break;
case MtpConstants.PROPERTY_AUDIO_WAVE_CODEC:
case MtpConstants.PROPERTY_AUDIO_BITRATE:
case MtpConstants.PROPERTY_SAMPLE_RATE:
// these are special cased
type = MtpConstants.TYPE_UINT32;
break;
case MtpConstants.PROPERTY_BITRATE_TYPE:
case MtpConstants.PROPERTY_NUMBER_OF_CHANNELS:
// these are special cased
type = MtpConstants.TYPE_UINT16;
break;
default:
type = MtpConstants.TYPE_UNDEFINED;
Log.e(TAG, "unsupported property " + code);
break;
}
if (column != null) {
columns.add(column);
return new Property(code, type, columns.size() - 1);
} else {
return new Property(code, type, -1);
}
}
private String queryString(int id, String column) {
Cursor c = null;
try {
// for now we are only reading properties from the "objects" table
c = mProvider.query(mPackageName, mUri,
new String [] { Files.FileColumns._ID, column },
ID_WHERE, new String[] { Integer.toString(id) }, null, null);
if (c != null && c.moveToNext()) {
return c.getString(1);
} else {
return "";
}
} catch (Exception e) {
return null;
} finally {
if (c != null) {
c.close();
}
}
}
private String queryAudio(int id, String column) {
Cursor c = null;
try {
c = mProvider.query(mPackageName, Audio.Media.getContentUri(mVolumeName),
new String [] { Files.FileColumns._ID, column },
ID_WHERE, new String[] { Integer.toString(id) }, null, null);
if (c != null && c.moveToNext()) {
return c.getString(1);
} else {
return "";
}
} catch (Exception e) {
return null;
} finally {
if (c != null) {
c.close();
}
}
}
private String queryGenre(int id) {
Cursor c = null;
try {
Uri uri = Audio.Genres.getContentUriForAudioId(mVolumeName, id);
c = mProvider.query(mPackageName, uri,
new String [] { Files.FileColumns._ID, Audio.GenresColumns.NAME },
null, null, null, null);
if (c != null && c.moveToNext()) {
return c.getString(1);
} else {
return "";
}
} catch (Exception e) {
Log.e(TAG, "queryGenre exception", e);
return null;
} finally {
if (c != null) {
c.close();
}
}
}
private Long queryLong(int id, String column) {
Cursor c = null;
try {
// for now we are only reading properties from the "objects" table
c = mProvider.query(mPackageName, mUri,
new String [] { Files.FileColumns._ID, column },
ID_WHERE, new String[] { Integer.toString(id) }, null, null);
if (c != null && c.moveToNext()) {
return new Long(c.getLong(1));
}
} catch (Exception e) {
} finally {
if (c != null) {
c.close();
}
}
return null;
}
private static String nameFromPath(String path) {
// extract name from full path
int start = 0;
int lastSlash = path.lastIndexOf('/');
if (lastSlash >= 0) {
start = lastSlash + 1;
}
int end = path.length();
if (end - start > 255) {
end = start + 255;
}
return path.substring(start, end);
}
MtpPropertyList getPropertyList(int handle, int format, int depth) {
//Log.d(TAG, "getPropertyList handle: " + handle + " format: " + format + " depth: " + depth);
if (depth > 1) {
// we only support depth 0 and 1
// depth 0: single object, depth 1: immediate children
return new MtpPropertyList(0, MtpConstants.RESPONSE_SPECIFICATION_BY_DEPTH_UNSUPPORTED);
}
String where;
String[] whereArgs;
if (format == 0) {
if (handle == 0xFFFFFFFF) {
// select all objects
where = null;
whereArgs = null;
} else {
whereArgs = new String[] { Integer.toString(handle) };
if (depth == 1) {
where = PARENT_WHERE;
} else {
where = ID_WHERE;
}
}
} else {
if (handle == 0xFFFFFFFF) {
// select all objects with given format
where = FORMAT_WHERE;
whereArgs = new String[] { Integer.toString(format) };
} else {
whereArgs = new String[] { Integer.toString(handle), Integer.toString(format) };
if (depth == 1) {
where = PARENT_FORMAT_WHERE;
} else {
where = ID_FORMAT_WHERE;
}
}
}
Cursor c = null;
try {
// don't query if not necessary
if (depth > 0 || handle == 0xFFFFFFFF || mColumns.length > 1) {
c = mProvider.query(mPackageName, mUri, mColumns, where, whereArgs, null, null);
if (c == null) {
return new MtpPropertyList(0, MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE);
}
}
int count = (c == null ? 1 : c.getCount());
MtpPropertyList result = new MtpPropertyList(count * mProperties.length,
MtpConstants.RESPONSE_OK);
// iterate over all objects in the query
for (int objectIndex = 0; objectIndex < count; objectIndex++) {
if (c != null) {
c.moveToNext();
handle = (int)c.getLong(0);
}
// iterate over all properties in the query for the given object
for (int propertyIndex = 0; propertyIndex < mProperties.length; propertyIndex++) {
Property property = mProperties[propertyIndex];
int propertyCode = property.code;
int column = property.column;
// handle some special cases
switch (propertyCode) {
case MtpConstants.PROPERTY_PROTECTION_STATUS:
// protection status is always 0
result.append(handle, propertyCode, MtpConstants.TYPE_UINT16, 0);
break;
case MtpConstants.PROPERTY_OBJECT_FILE_NAME:
// special case - need to extract file name from full path
String value = c.getString(column);
if (value != null) {
result.append(handle, propertyCode, nameFromPath(value));
} else {
result.setResult(MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE);
}
break;
case MtpConstants.PROPERTY_NAME:
// first try title
String name = c.getString(column);
// then try name
if (name == null) {
name = queryString(handle, Audio.PlaylistsColumns.NAME);
}
// if title and name fail, extract name from full path
if (name == null) {
name = queryString(handle, Files.FileColumns.DATA);
if (name != null) {
name = nameFromPath(name);
}
}
if (name != null) {
result.append(handle, propertyCode, name);
} else {
result.setResult(MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE);
}
break;
case MtpConstants.PROPERTY_DATE_MODIFIED:
case MtpConstants.PROPERTY_DATE_ADDED:
// convert from seconds to DateTime
result.append(handle, propertyCode, format_date_time(c.getInt(column)));
break;
case MtpConstants.PROPERTY_ORIGINAL_RELEASE_DATE:
// release date is stored internally as just the year
int year = c.getInt(column);
String dateTime = Integer.toString(year) + "0101T000000";
result.append(handle, propertyCode, dateTime);
break;
case MtpConstants.PROPERTY_PERSISTENT_UID:
// PUID is concatenation of storageID and object handle
long puid = c.getLong(column);
puid <<= 32;
puid += handle;
result.append(handle, propertyCode, MtpConstants.TYPE_UINT128, puid);
break;
case MtpConstants.PROPERTY_TRACK:
result.append(handle, propertyCode, MtpConstants.TYPE_UINT16,
c.getInt(column) % 1000);
break;
case MtpConstants.PROPERTY_ARTIST:
result.append(handle, propertyCode,
queryAudio(handle, Audio.AudioColumns.ARTIST));
break;
case MtpConstants.PROPERTY_ALBUM_NAME:
result.append(handle, propertyCode,
queryAudio(handle, Audio.AudioColumns.ALBUM));
break;
case MtpConstants.PROPERTY_GENRE:
String genre = queryGenre(handle);
if (genre != null) {
result.append(handle, propertyCode, genre);
} else {
result.setResult(MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE);
}
break;
case MtpConstants.PROPERTY_AUDIO_WAVE_CODEC:
case MtpConstants.PROPERTY_AUDIO_BITRATE:
case MtpConstants.PROPERTY_SAMPLE_RATE:
// we don't have these in our database, so return 0
result.append(handle, propertyCode, MtpConstants.TYPE_UINT32, 0);
break;
case MtpConstants.PROPERTY_BITRATE_TYPE:
case MtpConstants.PROPERTY_NUMBER_OF_CHANNELS:
// we don't have these in our database, so return 0
result.append(handle, propertyCode, MtpConstants.TYPE_UINT16, 0);
break;
default:
if (property.type == MtpConstants.TYPE_STR) {
result.append(handle, propertyCode, c.getString(column));
} else if (property.type == MtpConstants.TYPE_UNDEFINED) {
result.append(handle, propertyCode, property.type, 0);
} else {
result.append(handle, propertyCode, property.type,
c.getLong(column));
}
break;
}
}
}
return result;
} catch (RemoteException e) {
return new MtpPropertyList(0, MtpConstants.RESPONSE_GENERAL_ERROR);
} finally {
if (c != null) {
c.close();
}
}
// impossible to get here, so no return statement
}
private native String format_date_time(long seconds);
}
|