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
|
/*
* 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.ContentProviderClient;
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.util.Log;
import java.util.ArrayList;
/**
* MtpPropertyGroup represents a list of MTP properties.
* {@hide}
*/
class MtpPropertyGroup {
private static final String TAG = MtpPropertyGroup.class.getSimpleName();
private class Property {
int code;
int type;
int column;
Property(int code, int type, int column) {
this.code = code;
this.type = type;
this.column = column;
}
}
private final ContentProviderClient mProvider;
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 PATH_WHERE = Files.FileColumns.DATA + "=?";
// constructs a property group for a list of properties
public MtpPropertyGroup(ContentProviderClient provider, String volumeName, int[] properties) {
mProvider = provider;
mVolumeName = volumeName;
mUri = Files.getMtpObjectsUri(volumeName);
int count = properties.length;
ArrayList<String> columns = new ArrayList<>(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:
type = MtpConstants.TYPE_UINT32;
break;
case MtpConstants.PROPERTY_OBJECT_FORMAT:
type = MtpConstants.TYPE_UINT16;
break;
case MtpConstants.PROPERTY_PROTECTION_STATUS:
type = MtpConstants.TYPE_UINT16;
break;
case MtpConstants.PROPERTY_OBJECT_SIZE:
type = MtpConstants.TYPE_UINT64;
break;
case MtpConstants.PROPERTY_OBJECT_FILE_NAME:
type = MtpConstants.TYPE_STR;
break;
case MtpConstants.PROPERTY_NAME:
type = MtpConstants.TYPE_STR;
break;
case MtpConstants.PROPERTY_DATE_MODIFIED:
type = MtpConstants.TYPE_STR;
break;
case MtpConstants.PROPERTY_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:
type = MtpConstants.TYPE_UINT32;
break;
case MtpConstants.PROPERTY_PERSISTENT_UID:
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:
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 queryAudio(String path, String column) {
Cursor c = null;
try {
c = mProvider.query(Audio.Media.getContentUri(mVolumeName),
new String [] { column },
PATH_WHERE, new String[] {path}, null, null);
if (c != null && c.moveToNext()) {
return c.getString(0);
} else {
return "";
}
} catch (Exception e) {
return "";
} finally {
if (c != null) {
c.close();
}
}
}
private String queryGenre(String path) {
Cursor c = null;
try {
c = mProvider.query(Audio.Genres.getContentUri(mVolumeName),
new String [] { Audio.GenresColumns.NAME },
PATH_WHERE, new String[] {path}, null, null);
if (c != null && c.moveToNext()) {
return c.getString(0);
} else {
return "";
}
} catch (Exception e) {
return "";
} finally {
if (c != null) {
c.close();
}
}
}
/**
* Gets the values of the properties represented by this property group for the given
* object and adds them to the given property list.
* @return Response_OK if the operation succeeded.
*/
public int getPropertyList(MtpStorageManager.MtpObject object, MtpPropertyList list) {
Cursor c = null;
int id = object.getId();
String path = object.getPath().toString();
for (Property property : mProperties) {
if (property.column != -1 && c == null) {
try {
// Look up the entry in MediaProvider only if one of those properties is needed.
c = mProvider.query(mUri, mColumns,
PATH_WHERE, new String[] {path}, null, null);
if (c != null && !c.moveToNext()) {
c.close();
c = null;
}
} catch (RemoteException e) {
Log.e(TAG, "Mediaprovider lookup failed");
}
}
switch (property.code) {
case MtpConstants.PROPERTY_PROTECTION_STATUS:
// protection status is always 0
list.append(id, property.code, property.type, 0);
break;
case MtpConstants.PROPERTY_NAME:
case MtpConstants.PROPERTY_OBJECT_FILE_NAME:
case MtpConstants.PROPERTY_DISPLAY_NAME:
list.append(id, property.code, object.getName());
break;
case MtpConstants.PROPERTY_DATE_MODIFIED:
case MtpConstants.PROPERTY_DATE_ADDED:
// convert from seconds to DateTime
list.append(id, property.code,
format_date_time(object.getModifiedTime()));
break;
case MtpConstants.PROPERTY_STORAGE_ID:
list.append(id, property.code, property.type, object.getStorageId());
break;
case MtpConstants.PROPERTY_OBJECT_FORMAT:
list.append(id, property.code, property.type, object.getFormat());
break;
case MtpConstants.PROPERTY_OBJECT_SIZE:
list.append(id, property.code, property.type, object.getSize());
break;
case MtpConstants.PROPERTY_PARENT_OBJECT:
list.append(id, property.code, property.type,
object.getParent().isRoot() ? 0 : object.getParent().getId());
break;
case MtpConstants.PROPERTY_PERSISTENT_UID:
// The persistent uid must be unique and never reused among all objects,
// and remain the same between sessions.
long puid = (object.getPath().toString().hashCode() << 32)
+ object.getModifiedTime();
list.append(id, property.code, property.type, puid);
break;
case MtpConstants.PROPERTY_ORIGINAL_RELEASE_DATE:
// release date is stored internally as just the year
int year = 0;
if (c != null)
year = c.getInt(property.column);
String dateTime = Integer.toString(year) + "0101T000000";
list.append(id, property.code, dateTime);
break;
case MtpConstants.PROPERTY_TRACK:
int track = 0;
if (c != null)
track = c.getInt(property.column);
list.append(id, property.code, MtpConstants.TYPE_UINT16,
track % 1000);
break;
case MtpConstants.PROPERTY_ARTIST:
list.append(id, property.code,
queryAudio(path, Audio.AudioColumns.ARTIST));
break;
case MtpConstants.PROPERTY_ALBUM_NAME:
list.append(id, property.code,
queryAudio(path, Audio.AudioColumns.ALBUM));
break;
case MtpConstants.PROPERTY_GENRE:
String genre = queryGenre(path);
if (genre != null) {
list.append(id, property.code, genre);
}
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
list.append(id, property.code, 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
list.append(id, property.code, MtpConstants.TYPE_UINT16, 0);
break;
default:
switch(property.type) {
case MtpConstants.TYPE_UNDEFINED:
list.append(id, property.code, property.type, 0);
break;
case MtpConstants.TYPE_STR:
String value = "";
if (c != null)
value = c.getString(property.column);
list.append(id, property.code, value);
break;
default:
long longValue = 0L;
if (c != null)
longValue = c.getLong(property.column);
list.append(id, property.code, property.type, longValue);
}
}
}
if (c != null)
c.close();
return MtpConstants.RESPONSE_OK;
}
private native String format_date_time(long seconds);
}
|