blob: fce3fd0f0cbf2cd27a3316003b616fe603cf1c3d [file] [log] [blame]
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001/*
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 Lockwoodd21eac92010-07-03 00:44:05 -040018
Mike Lockwood56c85242014-03-07 13:29:08 -080019import android.content.BroadcastReceiver;
Mike Lockwoodd21eac92010-07-03 00:44:05 -040020import android.content.Context;
Mike Lockwoodd815f792010-07-12 08:49:01 -040021import android.content.ContentValues;
Mike Lockwoodd21eac92010-07-03 00:44:05 -040022import android.content.IContentProvider;
Mike Lockwood2837eef2010-08-31 16:25:12 -040023import android.content.Intent;
Mike Lockwood56c85242014-03-07 13:29:08 -080024import android.content.IntentFilter;
Mike Lockwood775de952011-03-05 17:34:11 -050025import android.content.SharedPreferences;
Mike Lockwoodd21eac92010-07-03 00:44:05 -040026import android.database.Cursor;
Mike Lockwood59e3f0d2010-09-02 14:57:30 -040027import android.database.sqlite.SQLiteDatabase;
Mike Lockwood0cd01362010-12-30 11:54:33 -050028import android.media.MediaScanner;
Mike Lockwoodd21eac92010-07-03 00:44:05 -040029import android.net.Uri;
Mike Lockwood56c85242014-03-07 13:29:08 -080030import android.os.BatteryManager;
31import android.os.BatteryStats;
Mike Lockwoodd21eac92010-07-03 00:44:05 -040032import android.os.RemoteException;
Mike Lockwooda3156052010-11-20 12:28:27 -050033import android.provider.MediaStore;
Mike Lockwood9a2046f2010-08-03 15:30:09 -040034import android.provider.MediaStore.Audio;
Mike Lockwood3b2a62e2010-09-08 12:47:57 -040035import android.provider.MediaStore.Files;
Mike Lockwoodae078f72010-09-26 12:35:51 -040036import android.provider.MediaStore.MediaColumns;
Mike Lockwoodd21eac92010-07-03 00:44:05 -040037import android.util.Log;
Mike Lockwoodea93fa12010-12-07 10:41:35 -080038import android.view.Display;
39import android.view.WindowManager;
Mike Lockwoodd21eac92010-07-03 00:44:05 -040040
Mike Lockwood5ebac832010-10-12 11:33:47 -040041import java.io.File;
Mike Lockwood7d7fb632010-12-01 18:46:23 -050042import java.util.HashMap;
dujin.chafe464a72011-11-22 12:13:33 +090043import java.util.Locale;
Mike Lockwood5ebac832010-10-12 11:33:47 -040044
Mike Lockwoodd21eac92010-07-03 00:44:05 -040045/**
46 * {@hide}
47 */
48public class MtpDatabase {
49
50 private static final String TAG = "MtpDatabase";
51
Mike Lockwood2837eef2010-08-31 16:25:12 -040052 private final Context mContext;
Dianne Hackborn35654b62013-01-14 17:38:02 -080053 private final String mPackageName;
Mike Lockwoodd21eac92010-07-03 00:44:05 -040054 private final IContentProvider mMediaProvider;
55 private final String mVolumeName;
56 private final Uri mObjectsUri;
Mike Lockwood73e56d92011-12-01 16:58:41 -050057 // path to primary storage
58 private final String mMediaStoragePath;
59 // if not null, restrict all queries to these subdirectories
60 private final String[] mSubDirectories;
61 // where clause for restricting queries to files in mSubDirectories
62 private String mSubDirectoriesWhere;
63 // where arguments for restricting queries to files in mSubDirectories
64 private String[] mSubDirectoriesWhereArgs;
65
Mike Lockwoodb239b6832011-04-05 10:21:27 -040066 private final HashMap<String, MtpStorage> mStorageMap = new HashMap<String, MtpStorage>();
Mike Lockwoodd21eac92010-07-03 00:44:05 -040067
Mike Lockwood7d7fb632010-12-01 18:46:23 -050068 // cached property groups for single properties
69 private final HashMap<Integer, MtpPropertyGroup> mPropertyGroupsByProperty
70 = new HashMap<Integer, MtpPropertyGroup>();
71
72 // cached property groups for all properties for a given format
73 private final HashMap<Integer, MtpPropertyGroup> mPropertyGroupsByFormat
74 = new HashMap<Integer, MtpPropertyGroup>();
75
Mike Lockwood2837eef2010-08-31 16:25:12 -040076 // true if the database has been modified in the current MTP session
77 private boolean mDatabaseModified;
78
Mike Lockwood775de952011-03-05 17:34:11 -050079 // SharedPreferences for writable MTP device properties
80 private SharedPreferences mDeviceProperties;
Mike Lockwood59e3f0d2010-09-02 14:57:30 -040081 private static final int DEVICE_PROPERTIES_DATABASE_VERSION = 1;
82
Mike Lockwoodd21eac92010-07-03 00:44:05 -040083 private static final String[] ID_PROJECTION = new String[] {
Mike Lockwood3b2a62e2010-09-08 12:47:57 -040084 Files.FileColumns._ID, // 0
Mike Lockwoodd21eac92010-07-03 00:44:05 -040085 };
Mike Lockwood6a6a3af2010-10-12 14:19:51 -040086 private static final String[] PATH_PROJECTION = new String[] {
Mike Lockwood5ebac832010-10-12 11:33:47 -040087 Files.FileColumns._ID, // 0
88 Files.FileColumns.DATA, // 1
Mike Lockwood5ebac832010-10-12 11:33:47 -040089 };
Mike Lockwoodf6f16612012-09-12 15:50:59 -070090 private static final String[] PATH_FORMAT_PROJECTION = new String[] {
Mike Lockwood3b2a62e2010-09-08 12:47:57 -040091 Files.FileColumns._ID, // 0
92 Files.FileColumns.DATA, // 1
Mike Lockwoodf6f16612012-09-12 15:50:59 -070093 Files.FileColumns.FORMAT, // 2
Mike Lockwoodd21eac92010-07-03 00:44:05 -040094 };
95 private static final String[] OBJECT_INFO_PROJECTION = new String[] {
Mike Lockwood3b2a62e2010-09-08 12:47:57 -040096 Files.FileColumns._ID, // 0
Mike Lockwoodb239b6832011-04-05 10:21:27 -040097 Files.FileColumns.STORAGE_ID, // 1
Mike Lockwood3b2a62e2010-09-08 12:47:57 -040098 Files.FileColumns.FORMAT, // 2
99 Files.FileColumns.PARENT, // 3
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400100 Files.FileColumns.DATA, // 4
Mike Lockwood1341f1e2013-04-01 10:52:47 -0700101 Files.FileColumns.DATE_ADDED, // 5
102 Files.FileColumns.DATE_MODIFIED, // 6
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400103 };
Mike Lockwood3b2a62e2010-09-08 12:47:57 -0400104 private static final String ID_WHERE = Files.FileColumns._ID + "=?";
Mike Lockwoodbafca212010-12-13 21:50:09 -0800105 private static final String PATH_WHERE = Files.FileColumns.DATA + "=?";
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400106
107 private static final String STORAGE_WHERE = Files.FileColumns.STORAGE_ID + "=?";
Mike Lockwood58e6831c2012-09-11 10:49:34 -0700108 private static final String FORMAT_WHERE = Files.FileColumns.FORMAT + "=?";
109 private static final String PARENT_WHERE = Files.FileColumns.PARENT + "=?";
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400110 private static final String STORAGE_FORMAT_WHERE = STORAGE_WHERE + " AND "
Mike Lockwood3b2a62e2010-09-08 12:47:57 -0400111 + Files.FileColumns.FORMAT + "=?";
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400112 private static final String STORAGE_PARENT_WHERE = STORAGE_WHERE + " AND "
113 + Files.FileColumns.PARENT + "=?";
114 private static final String FORMAT_PARENT_WHERE = FORMAT_WHERE + " AND "
115 + Files.FileColumns.PARENT + "=?";
116 private static final String STORAGE_FORMAT_PARENT_WHERE = STORAGE_FORMAT_WHERE + " AND "
117 + Files.FileColumns.PARENT + "=?";
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400118
Mike Lockwoodd815f792010-07-12 08:49:01 -0400119 private final MediaScanner mMediaScanner;
Mike Lockwood56c85242014-03-07 13:29:08 -0800120 private MtpServer mServer;
121
122 // read from native code
123 private int mBatteryLevel;
124 private int mBatteryScale;
Mike Lockwoodd815f792010-07-12 08:49:01 -0400125
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400126 static {
127 System.loadLibrary("media_jni");
128 }
129
Mike Lockwood56c85242014-03-07 13:29:08 -0800130 private BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
131 @Override
132 public void onReceive(Context context, Intent intent) {
133 String action = intent.getAction();
134 if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
135 mBatteryScale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
136 int newLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
137 if (newLevel != mBatteryLevel) {
138 mBatteryLevel = newLevel;
139 if (mServer != null) {
140 // send device property changed event
141 mServer.sendDevicePropertyChanged(
142 MtpConstants.DEVICE_PROPERTY_BATTERY_LEVEL);
143 }
144 }
145 }
146 }
147 };
148
Mike Lockwood73e56d92011-12-01 16:58:41 -0500149 public MtpDatabase(Context context, String volumeName, String storagePath,
150 String[] subDirectories) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400151 native_setup();
152
Mike Lockwood2837eef2010-08-31 16:25:12 -0400153 mContext = context;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800154 mPackageName = context.getPackageName();
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400155 mMediaProvider = context.getContentResolver().acquireProvider("media");
156 mVolumeName = volumeName;
Mike Lockwood01788562010-10-11 11:22:19 -0400157 mMediaStoragePath = storagePath;
Mike Lockwood8490e662010-09-09 14:16:22 -0400158 mObjectsUri = Files.getMtpObjectsUri(volumeName);
Mike Lockwoodd815f792010-07-12 08:49:01 -0400159 mMediaScanner = new MediaScanner(context);
dujin.chafe464a72011-11-22 12:13:33 +0900160
Mike Lockwood73e56d92011-12-01 16:58:41 -0500161 mSubDirectories = subDirectories;
162 if (subDirectories != null) {
163 // Compute "where" string for restricting queries to subdirectories
164 StringBuilder builder = new StringBuilder();
165 builder.append("(");
166 int count = subDirectories.length;
167 for (int i = 0; i < count; i++) {
168 builder.append(Files.FileColumns.DATA + "=? OR "
169 + Files.FileColumns.DATA + " LIKE ?");
170 if (i != count - 1) {
171 builder.append(" OR ");
172 }
173 }
174 builder.append(")");
175 mSubDirectoriesWhere = builder.toString();
176
177 // Compute "where" arguments for restricting queries to subdirectories
178 mSubDirectoriesWhereArgs = new String[count * 2];
179 for (int i = 0, j = 0; i < count; i++) {
180 String path = subDirectories[i];
181 mSubDirectoriesWhereArgs[j++] = path;
182 mSubDirectoriesWhereArgs[j++] = path + "/%";
183 }
184 }
185
dujin.chafe464a72011-11-22 12:13:33 +0900186 // Set locale to MediaScanner.
187 Locale locale = context.getResources().getConfiguration().locale;
188 if (locale != null) {
189 String language = locale.getLanguage();
190 String country = locale.getCountry();
191 if (language != null) {
192 if (country != null) {
193 mMediaScanner.setLocale(language + "_" + country);
194 } else {
195 mMediaScanner.setLocale(language);
196 }
197 }
198 }
Mike Lockwood775de952011-03-05 17:34:11 -0500199 initDeviceProperties(context);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400200 }
201
Mike Lockwood56c85242014-03-07 13:29:08 -0800202 public void setServer(MtpServer server) {
203 mServer = server;
204
Marco Nelissen1632fae2014-03-27 13:25:14 -0700205 // always unregister before registering
206 try {
207 mContext.unregisterReceiver(mBatteryReceiver);
208 } catch (IllegalArgumentException e) {
209 // wasn't previously registered, ignore
210 }
211
Mike Lockwood56c85242014-03-07 13:29:08 -0800212 // register for battery notifications when we are connected
213 if (server != null) {
214 mContext.registerReceiver(mBatteryReceiver,
215 new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Mike Lockwood56c85242014-03-07 13:29:08 -0800216 }
217 }
218
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400219 @Override
Mike Lockwooddbead322010-08-30 09:27:55 -0400220 protected void finalize() throws Throwable {
221 try {
222 native_finalize();
Mike Lockwooddbead322010-08-30 09:27:55 -0400223 } finally {
224 super.finalize();
225 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400226 }
227
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400228 public void addStorage(MtpStorage storage) {
229 mStorageMap.put(storage.getPath(), storage);
230 }
231
232 public void removeStorage(MtpStorage storage) {
233 mStorageMap.remove(storage.getPath());
234 }
235
Mike Lockwood775de952011-03-05 17:34:11 -0500236 private void initDeviceProperties(Context context) {
237 final String devicePropertiesName = "device-properties";
238 mDeviceProperties = context.getSharedPreferences(devicePropertiesName, Context.MODE_PRIVATE);
239 File databaseFile = context.getDatabasePath(devicePropertiesName);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400240
Mike Lockwood775de952011-03-05 17:34:11 -0500241 if (databaseFile.exists()) {
242 // for backward compatibility - read device properties from sqlite database
243 // and migrate them to shared prefs
244 SQLiteDatabase db = null;
245 Cursor c = null;
246 try {
247 db = context.openOrCreateDatabase("device-properties", Context.MODE_PRIVATE, null);
248 if (db != null) {
249 c = db.query("properties", new String[] { "_id", "code", "value" },
250 null, null, null, null, null);
251 if (c != null) {
252 SharedPreferences.Editor e = mDeviceProperties.edit();
253 while (c.moveToNext()) {
254 String name = c.getString(1);
255 String value = c.getString(2);
256 e.putString(name, value);
257 }
258 e.commit();
259 }
260 }
261 } catch (Exception e) {
262 Log.e(TAG, "failed to migrate device properties", e);
263 } finally {
264 if (c != null) c.close();
265 if (db != null) db.close();
266 }
jangwon.lee3ed02532013-07-22 19:52:48 +0900267 context.deleteDatabase(devicePropertiesName);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400268 }
269 }
270
Mike Lockwood73e56d92011-12-01 16:58:41 -0500271 // check to see if the path is contained in one of our storage subdirectories
272 // returns true if we have no special subdirectories
273 private boolean inStorageSubDirectory(String path) {
274 if (mSubDirectories == null) return true;
275 if (path == null) return false;
276
277 boolean allowed = false;
278 int pathLength = path.length();
279 for (int i = 0; i < mSubDirectories.length && !allowed; i++) {
280 String subdir = mSubDirectories[i];
281 int subdirLength = subdir.length();
282 if (subdirLength < pathLength &&
283 path.charAt(subdirLength) == '/' &&
284 path.startsWith(subdir)) {
285 allowed = true;
286 }
287 }
288 return allowed;
289 }
290
291 // check to see if the path matches one of our storage subdirectories
292 // returns true if we have no special subdirectories
293 private boolean isStorageSubDirectory(String path) {
294 if (mSubDirectories == null) return false;
295 for (int i = 0; i < mSubDirectories.length; i++) {
296 if (path.equals(mSubDirectories[i])) {
297 return true;
298 }
299 }
300 return false;
301 }
302
Mike Lockwoodd815f792010-07-12 08:49:01 -0400303 private int beginSendObject(String path, int format, int parent,
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400304 int storageId, long size, long modified) {
Mike Lockwood73e56d92011-12-01 16:58:41 -0500305 // if mSubDirectories is not null, do not allow copying files to any other locations
306 if (!inStorageSubDirectory(path)) return -1;
307
308 // make sure the object does not exist
Mike Lockwoodbafca212010-12-13 21:50:09 -0800309 if (path != null) {
310 Cursor c = null;
311 try {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800312 c = mMediaProvider.query(mPackageName, mObjectsUri, ID_PROJECTION, PATH_WHERE,
Jeff Brown75ea64f2012-01-25 19:37:13 -0800313 new String[] { path }, null, null);
Mike Lockwoodbafca212010-12-13 21:50:09 -0800314 if (c != null && c.getCount() > 0) {
315 Log.w(TAG, "file already exists in beginSendObject: " + path);
316 return -1;
317 }
318 } catch (RemoteException e) {
319 Log.e(TAG, "RemoteException in beginSendObject", e);
320 } finally {
321 if (c != null) {
322 c.close();
323 }
324 }
325 }
326
Mike Lockwood2837eef2010-08-31 16:25:12 -0400327 mDatabaseModified = true;
Mike Lockwoodd815f792010-07-12 08:49:01 -0400328 ContentValues values = new ContentValues();
Mike Lockwood3b2a62e2010-09-08 12:47:57 -0400329 values.put(Files.FileColumns.DATA, path);
330 values.put(Files.FileColumns.FORMAT, format);
331 values.put(Files.FileColumns.PARENT, parent);
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400332 values.put(Files.FileColumns.STORAGE_ID, storageId);
Mike Lockwood3b2a62e2010-09-08 12:47:57 -0400333 values.put(Files.FileColumns.SIZE, size);
334 values.put(Files.FileColumns.DATE_MODIFIED, modified);
Mike Lockwoodd815f792010-07-12 08:49:01 -0400335
336 try {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800337 Uri uri = mMediaProvider.insert(mPackageName, mObjectsUri, values);
Mike Lockwoodd815f792010-07-12 08:49:01 -0400338 if (uri != null) {
339 return Integer.parseInt(uri.getPathSegments().get(2));
340 } else {
341 return -1;
342 }
343 } catch (RemoteException e) {
344 Log.e(TAG, "RemoteException in beginSendObject", e);
345 return -1;
346 }
347 }
348
Mike Lockwood7a0bd172011-01-18 11:06:19 -0800349 private void endSendObject(String path, int handle, int format, boolean succeeded) {
Mike Lockwoodd815f792010-07-12 08:49:01 -0400350 if (succeeded) {
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400351 // handle abstract playlists separately
352 // they do not exist in the file system so don't use the media scanner here
Mike Lockwood5367ab62010-08-30 13:23:02 -0400353 if (format == MtpConstants.FORMAT_ABSTRACT_AV_PLAYLIST) {
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400354 // extract name from path
355 String name = path;
356 int lastSlash = name.lastIndexOf('/');
357 if (lastSlash >= 0) {
358 name = name.substring(lastSlash + 1);
359 }
Mike Lockwood8cc6eb12011-01-18 13:13:05 -0800360 // strip trailing ".pla" from the name
361 if (name.endsWith(".pla")) {
362 name = name.substring(0, name.length() - 4);
363 }
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400364
365 ContentValues values = new ContentValues(1);
366 values.put(Audio.Playlists.DATA, path);
367 values.put(Audio.Playlists.NAME, name);
Mike Lockwood0b58c192010-11-17 15:42:09 -0500368 values.put(Files.FileColumns.FORMAT, format);
Mike Lockwood8ed67ac2011-01-18 13:27:25 -0800369 values.put(Files.FileColumns.DATE_MODIFIED, System.currentTimeMillis() / 1000);
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400370 values.put(MediaColumns.MEDIA_SCANNER_NEW_OBJECT_ID, handle);
371 try {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800372 Uri uri = mMediaProvider.insert(mPackageName,
373 Audio.Playlists.EXTERNAL_CONTENT_URI, values);
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400374 } catch (RemoteException e) {
375 Log.e(TAG, "RemoteException in endSendObject", e);
376 }
377 } else {
Mike Lockwoodc37255d2010-09-10 14:47:36 -0400378 mMediaScanner.scanMtpFile(path, mVolumeName, handle, format);
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400379 }
Mike Lockwoodd815f792010-07-12 08:49:01 -0400380 } else {
381 deleteFile(handle);
382 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400383 }
384
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400385 private Cursor createObjectQuery(int storageID, int format, int parent) throws RemoteException {
Mike Lockwood73e56d92011-12-01 16:58:41 -0500386 String where;
387 String[] whereArgs;
388
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400389 if (storageID == 0xFFFFFFFF) {
390 // query all stores
391 if (format == 0) {
392 // query all formats
393 if (parent == 0) {
394 // query all objects
Mike Lockwood73e56d92011-12-01 16:58:41 -0500395 where = null;
396 whereArgs = null;
397 } else {
398 if (parent == 0xFFFFFFFF) {
399 // all objects in root of store
400 parent = 0;
401 }
402 where = PARENT_WHERE;
403 whereArgs = new String[] { Integer.toString(parent) };
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400404 }
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400405 } else {
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400406 // query specific format
407 if (parent == 0) {
408 // query all objects
Mike Lockwood73e56d92011-12-01 16:58:41 -0500409 where = FORMAT_WHERE;
410 whereArgs = new String[] { Integer.toString(format) };
411 } else {
412 if (parent == 0xFFFFFFFF) {
413 // all objects in root of store
414 parent = 0;
415 }
416 where = FORMAT_PARENT_WHERE;
417 whereArgs = new String[] { Integer.toString(format),
418 Integer.toString(parent) };
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400419 }
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400420 }
421 } else {
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400422 // query specific store
423 if (format == 0) {
424 // query all formats
425 if (parent == 0) {
426 // query all objects
Mike Lockwood73e56d92011-12-01 16:58:41 -0500427 where = STORAGE_WHERE;
428 whereArgs = new String[] { Integer.toString(storageID) };
429 } else {
430 if (parent == 0xFFFFFFFF) {
431 // all objects in root of store
432 parent = 0;
433 }
434 where = STORAGE_PARENT_WHERE;
435 whereArgs = new String[] { Integer.toString(storageID),
436 Integer.toString(parent) };
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400437 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400438 } else {
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400439 // query specific format
440 if (parent == 0) {
441 // query all objects
Mike Lockwood73e56d92011-12-01 16:58:41 -0500442 where = STORAGE_FORMAT_WHERE;
443 whereArgs = new String[] { Integer.toString(storageID),
444 Integer.toString(format) };
445 } else {
446 if (parent == 0xFFFFFFFF) {
447 // all objects in root of store
448 parent = 0;
449 }
450 where = STORAGE_FORMAT_PARENT_WHERE;
451 whereArgs = new String[] { Integer.toString(storageID),
452 Integer.toString(format),
453 Integer.toString(parent) };
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400454 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400455 }
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400456 }
Mike Lockwood73e56d92011-12-01 16:58:41 -0500457
458 // if we are restricting queries to mSubDirectories, we need to add the restriction
459 // onto our "where" arguments
460 if (mSubDirectoriesWhere != null) {
461 if (where == null) {
462 where = mSubDirectoriesWhere;
463 whereArgs = mSubDirectoriesWhereArgs;
464 } else {
465 where = where + " AND " + mSubDirectoriesWhere;
466
467 // create new array to hold whereArgs and mSubDirectoriesWhereArgs
468 String[] newWhereArgs =
469 new String[whereArgs.length + mSubDirectoriesWhereArgs.length];
470 int i, j;
471 for (i = 0; i < whereArgs.length; i++) {
472 newWhereArgs[i] = whereArgs[i];
473 }
474 for (j = 0; j < mSubDirectoriesWhereArgs.length; i++, j++) {
475 newWhereArgs[i] = mSubDirectoriesWhereArgs[j];
476 }
477 whereArgs = newWhereArgs;
478 }
479 }
480
Dianne Hackborn35654b62013-01-14 17:38:02 -0800481 return mMediaProvider.query(mPackageName, mObjectsUri, ID_PROJECTION, where,
482 whereArgs, null, null);
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400483 }
484
485 private int[] getObjectList(int storageID, int format, int parent) {
486 Cursor c = null;
487 try {
488 c = createObjectQuery(storageID, format, parent);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400489 if (c == null) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400490 return null;
491 }
492 int count = c.getCount();
493 if (count > 0) {
494 int[] result = new int[count];
495 for (int i = 0; i < count; i++) {
496 c.moveToNext();
497 result[i] = c.getInt(0);
498 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400499 return result;
500 }
501 } catch (RemoteException e) {
502 Log.e(TAG, "RemoteException in getObjectList", e);
503 } finally {
504 if (c != null) {
505 c.close();
506 }
507 }
508 return null;
509 }
510
Mike Lockwood7a047c82010-08-02 10:52:20 -0400511 private int getNumObjects(int storageID, int format, int parent) {
Mike Lockwood7a047c82010-08-02 10:52:20 -0400512 Cursor c = null;
513 try {
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400514 c = createObjectQuery(storageID, format, parent);
Mike Lockwood7a047c82010-08-02 10:52:20 -0400515 if (c != null) {
516 return c.getCount();
517 }
518 } catch (RemoteException e) {
519 Log.e(TAG, "RemoteException in getNumObjects", e);
520 } finally {
521 if (c != null) {
522 c.close();
523 }
524 }
525 return -1;
526 }
527
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400528 private int[] getSupportedPlaybackFormats() {
529 return new int[] {
Mike Lockwoode5211692010-09-08 13:50:45 -0400530 // allow transfering arbitrary files
531 MtpConstants.FORMAT_UNDEFINED,
Mike Lockwood12b8a992010-09-23 21:33:29 -0400532
Mike Lockwood792ec842010-09-09 15:30:10 -0400533 MtpConstants.FORMAT_ASSOCIATION,
Mike Lockwood12b8a992010-09-23 21:33:29 -0400534 MtpConstants.FORMAT_TEXT,
535 MtpConstants.FORMAT_HTML,
536 MtpConstants.FORMAT_WAV,
537 MtpConstants.FORMAT_MP3,
538 MtpConstants.FORMAT_MPEG,
539 MtpConstants.FORMAT_EXIF_JPEG,
540 MtpConstants.FORMAT_TIFF_EP,
bo huang240582e2012-02-27 16:27:00 +0800541 MtpConstants.FORMAT_BMP,
Mike Lockwood12b8a992010-09-23 21:33:29 -0400542 MtpConstants.FORMAT_GIF,
543 MtpConstants.FORMAT_JFIF,
544 MtpConstants.FORMAT_PNG,
545 MtpConstants.FORMAT_TIFF,
546 MtpConstants.FORMAT_WMA,
547 MtpConstants.FORMAT_OGG,
548 MtpConstants.FORMAT_AAC,
549 MtpConstants.FORMAT_MP4_CONTAINER,
550 MtpConstants.FORMAT_MP2,
551 MtpConstants.FORMAT_3GP_CONTAINER,
Mike Lockwood792ec842010-09-09 15:30:10 -0400552 MtpConstants.FORMAT_ABSTRACT_AV_PLAYLIST,
Mike Lockwood12b8a992010-09-23 21:33:29 -0400553 MtpConstants.FORMAT_WPL_PLAYLIST,
554 MtpConstants.FORMAT_M3U_PLAYLIST,
555 MtpConstants.FORMAT_PLS_PLAYLIST,
556 MtpConstants.FORMAT_XML_DOCUMENT,
Glenn Kastenf9f223e2011-01-13 11:17:00 -0800557 MtpConstants.FORMAT_FLAC,
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400558 };
559 }
560
561 private int[] getSupportedCaptureFormats() {
562 // no capture formats yet
563 return null;
564 }
565
Mike Lockwoodae078f72010-09-26 12:35:51 -0400566 static final int[] FILE_PROPERTIES = {
567 // NOTE must match beginning of AUDIO_PROPERTIES, VIDEO_PROPERTIES
568 // and IMAGE_PROPERTIES below
Mike Lockwood5367ab62010-08-30 13:23:02 -0400569 MtpConstants.PROPERTY_STORAGE_ID,
570 MtpConstants.PROPERTY_OBJECT_FORMAT,
Mike Lockwoodd3bfecb2010-09-23 23:04:28 -0400571 MtpConstants.PROPERTY_PROTECTION_STATUS,
Mike Lockwood5367ab62010-08-30 13:23:02 -0400572 MtpConstants.PROPERTY_OBJECT_SIZE,
573 MtpConstants.PROPERTY_OBJECT_FILE_NAME,
Mike Lockwoodd3bfecb2010-09-23 23:04:28 -0400574 MtpConstants.PROPERTY_DATE_MODIFIED,
Mike Lockwood5367ab62010-08-30 13:23:02 -0400575 MtpConstants.PROPERTY_PARENT_OBJECT,
Mike Lockwoodd3bfecb2010-09-23 23:04:28 -0400576 MtpConstants.PROPERTY_PERSISTENT_UID,
577 MtpConstants.PROPERTY_NAME,
Mike Lockwoodae078f72010-09-26 12:35:51 -0400578 MtpConstants.PROPERTY_DATE_ADDED,
579 };
580
581 static final int[] AUDIO_PROPERTIES = {
582 // NOTE must match FILE_PROPERTIES above
583 MtpConstants.PROPERTY_STORAGE_ID,
584 MtpConstants.PROPERTY_OBJECT_FORMAT,
585 MtpConstants.PROPERTY_PROTECTION_STATUS,
586 MtpConstants.PROPERTY_OBJECT_SIZE,
587 MtpConstants.PROPERTY_OBJECT_FILE_NAME,
588 MtpConstants.PROPERTY_DATE_MODIFIED,
589 MtpConstants.PROPERTY_PARENT_OBJECT,
590 MtpConstants.PROPERTY_PERSISTENT_UID,
591 MtpConstants.PROPERTY_NAME,
592 MtpConstants.PROPERTY_DISPLAY_NAME,
593 MtpConstants.PROPERTY_DATE_ADDED,
594
595 // audio specific properties
596 MtpConstants.PROPERTY_ARTIST,
597 MtpConstants.PROPERTY_ALBUM_NAME,
598 MtpConstants.PROPERTY_ALBUM_ARTIST,
599 MtpConstants.PROPERTY_TRACK,
600 MtpConstants.PROPERTY_ORIGINAL_RELEASE_DATE,
601 MtpConstants.PROPERTY_DURATION,
602 MtpConstants.PROPERTY_GENRE,
603 MtpConstants.PROPERTY_COMPOSER,
Mike Lockwood92b53bc2014-03-13 14:51:29 -0700604 MtpConstants.PROPERTY_AUDIO_WAVE_CODEC,
605 MtpConstants.PROPERTY_BITRATE_TYPE,
606 MtpConstants.PROPERTY_AUDIO_BITRATE,
607 MtpConstants.PROPERTY_NUMBER_OF_CHANNELS,
608 MtpConstants.PROPERTY_SAMPLE_RATE,
Mike Lockwoodae078f72010-09-26 12:35:51 -0400609 };
610
611 static final int[] VIDEO_PROPERTIES = {
612 // NOTE must match FILE_PROPERTIES above
613 MtpConstants.PROPERTY_STORAGE_ID,
614 MtpConstants.PROPERTY_OBJECT_FORMAT,
615 MtpConstants.PROPERTY_PROTECTION_STATUS,
616 MtpConstants.PROPERTY_OBJECT_SIZE,
617 MtpConstants.PROPERTY_OBJECT_FILE_NAME,
618 MtpConstants.PROPERTY_DATE_MODIFIED,
619 MtpConstants.PROPERTY_PARENT_OBJECT,
620 MtpConstants.PROPERTY_PERSISTENT_UID,
621 MtpConstants.PROPERTY_NAME,
622 MtpConstants.PROPERTY_DISPLAY_NAME,
623 MtpConstants.PROPERTY_DATE_ADDED,
624
625 // video specific properties
626 MtpConstants.PROPERTY_ARTIST,
627 MtpConstants.PROPERTY_ALBUM_NAME,
628 MtpConstants.PROPERTY_DURATION,
629 MtpConstants.PROPERTY_DESCRIPTION,
630 };
631
632 static final int[] IMAGE_PROPERTIES = {
633 // NOTE must match FILE_PROPERTIES above
634 MtpConstants.PROPERTY_STORAGE_ID,
635 MtpConstants.PROPERTY_OBJECT_FORMAT,
636 MtpConstants.PROPERTY_PROTECTION_STATUS,
637 MtpConstants.PROPERTY_OBJECT_SIZE,
638 MtpConstants.PROPERTY_OBJECT_FILE_NAME,
639 MtpConstants.PROPERTY_DATE_MODIFIED,
640 MtpConstants.PROPERTY_PARENT_OBJECT,
641 MtpConstants.PROPERTY_PERSISTENT_UID,
642 MtpConstants.PROPERTY_NAME,
643 MtpConstants.PROPERTY_DISPLAY_NAME,
644 MtpConstants.PROPERTY_DATE_ADDED,
645
646 // image specific properties
647 MtpConstants.PROPERTY_DESCRIPTION,
648 };
649
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500650 static final int[] ALL_PROPERTIES = {
651 // NOTE must match FILE_PROPERTIES above
652 MtpConstants.PROPERTY_STORAGE_ID,
653 MtpConstants.PROPERTY_OBJECT_FORMAT,
654 MtpConstants.PROPERTY_PROTECTION_STATUS,
655 MtpConstants.PROPERTY_OBJECT_SIZE,
656 MtpConstants.PROPERTY_OBJECT_FILE_NAME,
657 MtpConstants.PROPERTY_DATE_MODIFIED,
658 MtpConstants.PROPERTY_PARENT_OBJECT,
659 MtpConstants.PROPERTY_PERSISTENT_UID,
660 MtpConstants.PROPERTY_NAME,
661 MtpConstants.PROPERTY_DISPLAY_NAME,
662 MtpConstants.PROPERTY_DATE_ADDED,
663
664 // image specific properties
665 MtpConstants.PROPERTY_DESCRIPTION,
666
667 // audio specific properties
668 MtpConstants.PROPERTY_ARTIST,
669 MtpConstants.PROPERTY_ALBUM_NAME,
670 MtpConstants.PROPERTY_ALBUM_ARTIST,
671 MtpConstants.PROPERTY_TRACK,
672 MtpConstants.PROPERTY_ORIGINAL_RELEASE_DATE,
673 MtpConstants.PROPERTY_DURATION,
674 MtpConstants.PROPERTY_GENRE,
675 MtpConstants.PROPERTY_COMPOSER,
676
677 // video specific properties
678 MtpConstants.PROPERTY_ARTIST,
679 MtpConstants.PROPERTY_ALBUM_NAME,
680 MtpConstants.PROPERTY_DURATION,
681 MtpConstants.PROPERTY_DESCRIPTION,
682
683 // image specific properties
684 MtpConstants.PROPERTY_DESCRIPTION,
685 };
686
Mike Lockwoodae078f72010-09-26 12:35:51 -0400687 private int[] getSupportedObjectProperties(int format) {
688 switch (format) {
689 case MtpConstants.FORMAT_MP3:
690 case MtpConstants.FORMAT_WAV:
691 case MtpConstants.FORMAT_WMA:
692 case MtpConstants.FORMAT_OGG:
693 case MtpConstants.FORMAT_AAC:
694 return AUDIO_PROPERTIES;
695 case MtpConstants.FORMAT_MPEG:
696 case MtpConstants.FORMAT_3GP_CONTAINER:
697 case MtpConstants.FORMAT_WMV:
698 return VIDEO_PROPERTIES;
699 case MtpConstants.FORMAT_EXIF_JPEG:
700 case MtpConstants.FORMAT_GIF:
701 case MtpConstants.FORMAT_PNG:
702 case MtpConstants.FORMAT_BMP:
703 return IMAGE_PROPERTIES;
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500704 case 0:
705 return ALL_PROPERTIES;
Mike Lockwoodae078f72010-09-26 12:35:51 -0400706 default:
707 return FILE_PROPERTIES;
708 }
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400709 }
710
711 private int[] getSupportedDeviceProperties() {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400712 return new int[] {
713 MtpConstants.DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER,
714 MtpConstants.DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME,
Mike Lockwoodea93fa12010-12-07 10:41:35 -0800715 MtpConstants.DEVICE_PROPERTY_IMAGE_SIZE,
Mike Lockwood56c85242014-03-07 13:29:08 -0800716 MtpConstants.DEVICE_PROPERTY_BATTERY_LEVEL,
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400717 };
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400718 }
719
Mike Lockwoodae078f72010-09-26 12:35:51 -0400720
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500721 private MtpPropertyList getObjectPropertyList(long handle, int format, long property,
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400722 int groupCode, int depth) {
723 // FIXME - implement group support
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400724 if (groupCode != 0) {
725 return new MtpPropertyList(0, MtpConstants.RESPONSE_SPECIFICATION_BY_GROUP_UNSUPPORTED);
726 }
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400727
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500728 MtpPropertyGroup propertyGroup;
729 if (property == 0xFFFFFFFFL) {
730 propertyGroup = mPropertyGroupsByFormat.get(format);
731 if (propertyGroup == null) {
732 int[] propertyList = getSupportedObjectProperties(format);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800733 propertyGroup = new MtpPropertyGroup(this, mMediaProvider, mPackageName,
734 mVolumeName, propertyList);
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500735 mPropertyGroupsByFormat.put(new Integer(format), propertyGroup);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400736 }
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500737 } else {
738 propertyGroup = mPropertyGroupsByProperty.get(property);
739 if (propertyGroup == null) {
740 int[] propertyList = new int[] { (int)property };
Dianne Hackborn35654b62013-01-14 17:38:02 -0800741 propertyGroup = new MtpPropertyGroup(this, mMediaProvider, mPackageName,
742 mVolumeName, propertyList);
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500743 mPropertyGroupsByProperty.put(new Integer((int)property), propertyGroup);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400744 }
745 }
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500746
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400747 return propertyGroup.getPropertyList((int)handle, format, depth);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400748 }
749
Mike Lockwood5ebac832010-10-12 11:33:47 -0400750 private int renameFile(int handle, String newName) {
751 Cursor c = null;
752
753 // first compute current path
754 String path = null;
Mike Lockwood5ebac832010-10-12 11:33:47 -0400755 String[] whereArgs = new String[] { Integer.toString(handle) };
756 try {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800757 c = mMediaProvider.query(mPackageName, mObjectsUri, PATH_PROJECTION, ID_WHERE,
758 whereArgs, null, null);
Mike Lockwood5ebac832010-10-12 11:33:47 -0400759 if (c != null && c.moveToNext()) {
Mike Lockwood1c4e88d2011-01-12 12:38:41 -0500760 path = c.getString(1);
Mike Lockwood5ebac832010-10-12 11:33:47 -0400761 }
762 } catch (RemoteException e) {
763 Log.e(TAG, "RemoteException in getObjectFilePath", e);
764 return MtpConstants.RESPONSE_GENERAL_ERROR;
765 } finally {
766 if (c != null) {
767 c.close();
768 }
769 }
770 if (path == null) {
771 return MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE;
772 }
Mike Lockwood5ebac832010-10-12 11:33:47 -0400773
Mike Lockwood73e56d92011-12-01 16:58:41 -0500774 // do not allow renaming any of the special subdirectories
775 if (isStorageSubDirectory(path)) {
776 return MtpConstants.RESPONSE_OBJECT_WRITE_PROTECTED;
777 }
778
Mike Lockwood5ebac832010-10-12 11:33:47 -0400779 // now rename the file. make sure this succeeds before updating database
780 File oldFile = new File(path);
781 int lastSlash = path.lastIndexOf('/');
782 if (lastSlash <= 1) {
783 return MtpConstants.RESPONSE_GENERAL_ERROR;
784 }
785 String newPath = path.substring(0, lastSlash + 1) + newName;
786 File newFile = new File(newPath);
787 boolean success = oldFile.renameTo(newFile);
Mike Lockwood5ebac832010-10-12 11:33:47 -0400788 if (!success) {
Mike Lockwoodf26a5862011-01-21 21:00:54 -0800789 Log.w(TAG, "renaming "+ path + " to " + newPath + " failed");
Mike Lockwood5ebac832010-10-12 11:33:47 -0400790 return MtpConstants.RESPONSE_GENERAL_ERROR;
791 }
792
793 // finally update database
794 ContentValues values = new ContentValues();
795 values.put(Files.FileColumns.DATA, newPath);
796 int updated = 0;
797 try {
Mike Lockwood6a6a3af2010-10-12 14:19:51 -0400798 // note - we are relying on a special case in MediaProvider.update() to update
799 // the paths for all children in the case where this is a directory.
Dianne Hackborn35654b62013-01-14 17:38:02 -0800800 updated = mMediaProvider.update(mPackageName, mObjectsUri, values, ID_WHERE, whereArgs);
Mike Lockwood5ebac832010-10-12 11:33:47 -0400801 } catch (RemoteException e) {
802 Log.e(TAG, "RemoteException in mMediaProvider.update", e);
803 }
Mike Lockwood6a6a3af2010-10-12 14:19:51 -0400804 if (updated == 0) {
Mike Lockwood5ebac832010-10-12 11:33:47 -0400805 Log.e(TAG, "Unable to update path for " + path + " to " + newPath);
806 // this shouldn't happen, but if it does we need to rename the file to its original name
807 newFile.renameTo(oldFile);
808 return MtpConstants.RESPONSE_GENERAL_ERROR;
809 }
810
Marco Nelissenca78f3d2012-01-27 09:43:20 -0800811 // check if nomedia status changed
812 if (newFile.isDirectory()) {
813 // for directories, check if renamed from something hidden to something non-hidden
814 if (oldFile.getName().startsWith(".") && !newPath.startsWith(".")) {
815 // directory was unhidden
816 try {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800817 mMediaProvider.call(mPackageName, MediaStore.UNHIDE_CALL, newPath, null);
Marco Nelissenca78f3d2012-01-27 09:43:20 -0800818 } catch (RemoteException e) {
819 Log.e(TAG, "failed to unhide/rescan for " + newPath);
820 }
821 }
822 } else {
823 // for files, check if renamed from .nomedia to something else
824 if (oldFile.getName().toLowerCase(Locale.US).equals(".nomedia")
825 && !newPath.toLowerCase(Locale.US).equals(".nomedia")) {
826 try {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800827 mMediaProvider.call(mPackageName, MediaStore.UNHIDE_CALL, oldFile.getParent(), null);
Marco Nelissenca78f3d2012-01-27 09:43:20 -0800828 } catch (RemoteException e) {
829 Log.e(TAG, "failed to unhide/rescan for " + newPath);
830 }
831 }
832 }
833
Mike Lockwood5ebac832010-10-12 11:33:47 -0400834 return MtpConstants.RESPONSE_OK;
835 }
836
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400837 private int setObjectProperty(int handle, int property,
838 long intValue, String stringValue) {
Mike Lockwood5ebac832010-10-12 11:33:47 -0400839 switch (property) {
840 case MtpConstants.PROPERTY_OBJECT_FILE_NAME:
841 return renameFile(handle, stringValue);
842
843 default:
844 return MtpConstants.RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
845 }
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400846 }
847
848 private int getDeviceProperty(int property, long[] outIntValue, char[] outStringValue) {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400849 switch (property) {
850 case MtpConstants.DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER:
851 case MtpConstants.DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME:
Mike Lockwood775de952011-03-05 17:34:11 -0500852 // writable string properties kept in shared preferences
853 String value = mDeviceProperties.getString(Integer.toString(property), "");
854 int length = value.length();
855 if (length > 255) {
856 length = 255;
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400857 }
Mike Lockwood775de952011-03-05 17:34:11 -0500858 value.getChars(0, length, outStringValue, 0);
859 outStringValue[length] = 0;
860 return MtpConstants.RESPONSE_OK;
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400861
Mike Lockwoodea93fa12010-12-07 10:41:35 -0800862 case MtpConstants.DEVICE_PROPERTY_IMAGE_SIZE:
863 // use screen size as max image size
864 Display display = ((WindowManager)mContext.getSystemService(
865 Context.WINDOW_SERVICE)).getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -0700866 int width = display.getMaximumSizeDimension();
867 int height = display.getMaximumSizeDimension();
Mike Lockwoodea93fa12010-12-07 10:41:35 -0800868 String imageSize = Integer.toString(width) + "x" + Integer.toString(height);
869 imageSize.getChars(0, imageSize.length(), outStringValue, 0);
870 outStringValue[imageSize.length()] = 0;
871 return MtpConstants.RESPONSE_OK;
872
Mike Lockwood56c85242014-03-07 13:29:08 -0800873 // DEVICE_PROPERTY_BATTERY_LEVEL is implemented in the JNI code
874
Mike Lockwoodea93fa12010-12-07 10:41:35 -0800875 default:
876 return MtpConstants.RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
877 }
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400878 }
879
880 private int setDeviceProperty(int property, long intValue, String stringValue) {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400881 switch (property) {
882 case MtpConstants.DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER:
883 case MtpConstants.DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME:
Mike Lockwood775de952011-03-05 17:34:11 -0500884 // writable string properties kept in shared prefs
885 SharedPreferences.Editor e = mDeviceProperties.edit();
886 e.putString(Integer.toString(property), stringValue);
887 return (e.commit() ? MtpConstants.RESPONSE_OK
888 : MtpConstants.RESPONSE_GENERAL_ERROR);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400889 }
890
891 return MtpConstants.RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
892 }
893
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400894 private boolean getObjectInfo(int handle, int[] outStorageFormatParent,
Mike Lockwood1341f1e2013-04-01 10:52:47 -0700895 char[] outName, long[] outCreatedModified) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400896 Cursor c = null;
897 try {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800898 c = mMediaProvider.query(mPackageName, mObjectsUri, OBJECT_INFO_PROJECTION,
Jeff Brown75ea64f2012-01-25 19:37:13 -0800899 ID_WHERE, new String[] { Integer.toString(handle) }, null, null);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400900 if (c != null && c.moveToNext()) {
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400901 outStorageFormatParent[0] = c.getInt(1);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400902 outStorageFormatParent[1] = c.getInt(2);
903 outStorageFormatParent[2] = c.getInt(3);
904
905 // extract name from path
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400906 String path = c.getString(4);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400907 int lastSlash = path.lastIndexOf('/');
908 int start = (lastSlash >= 0 ? lastSlash + 1 : 0);
909 int end = path.length();
910 if (end - start > 255) {
911 end = start + 255;
912 }
913 path.getChars(start, end, outName, 0);
914 outName[end - start] = 0;
915
Mike Lockwood1341f1e2013-04-01 10:52:47 -0700916 outCreatedModified[0] = c.getLong(5);
917 outCreatedModified[1] = c.getLong(6);
918 // use modification date as creation date if date added is not set
919 if (outCreatedModified[0] == 0) {
920 outCreatedModified[0] = outCreatedModified[1];
921 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400922 return true;
923 }
924 } catch (RemoteException e) {
Mike Lockwood2b5f9ad12010-10-29 19:16:27 -0400925 Log.e(TAG, "RemoteException in getObjectInfo", e);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400926 } finally {
927 if (c != null) {
928 c.close();
929 }
930 }
931 return false;
932 }
933
Mike Lockwood365e03e2010-12-08 16:08:01 -0800934 private int getObjectFilePath(int handle, char[] outFilePath, long[] outFileLengthFormat) {
Mike Lockwood01788562010-10-11 11:22:19 -0400935 if (handle == 0) {
936 // special case root directory
937 mMediaStoragePath.getChars(0, mMediaStoragePath.length(), outFilePath, 0);
938 outFilePath[mMediaStoragePath.length()] = 0;
Mike Lockwood365e03e2010-12-08 16:08:01 -0800939 outFileLengthFormat[0] = 0;
940 outFileLengthFormat[1] = MtpConstants.FORMAT_ASSOCIATION;
Mike Lockwood01788562010-10-11 11:22:19 -0400941 return MtpConstants.RESPONSE_OK;
942 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400943 Cursor c = null;
944 try {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800945 c = mMediaProvider.query(mPackageName, mObjectsUri, PATH_FORMAT_PROJECTION,
Jeff Brown75ea64f2012-01-25 19:37:13 -0800946 ID_WHERE, new String[] { Integer.toString(handle) }, null, null);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400947 if (c != null && c.moveToNext()) {
Mike Lockwood1c4e88d2011-01-12 12:38:41 -0500948 String path = c.getString(1);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400949 path.getChars(0, path.length(), outFilePath, 0);
950 outFilePath[path.length()] = 0;
Mike Lockwoodf6f16612012-09-12 15:50:59 -0700951 // File transfers from device to host will likely fail if the size is incorrect.
952 // So to be safe, use the actual file size here.
953 outFileLengthFormat[0] = new File(path).length();
954 outFileLengthFormat[1] = c.getLong(2);
Mike Lockwood5367ab62010-08-30 13:23:02 -0400955 return MtpConstants.RESPONSE_OK;
Mike Lockwood59c777a2010-08-02 10:37:41 -0400956 } else {
Mike Lockwood5367ab62010-08-30 13:23:02 -0400957 return MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400958 }
959 } catch (RemoteException e) {
960 Log.e(TAG, "RemoteException in getObjectFilePath", e);
Mike Lockwood5367ab62010-08-30 13:23:02 -0400961 return MtpConstants.RESPONSE_GENERAL_ERROR;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400962 } finally {
963 if (c != null) {
964 c.close();
965 }
966 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400967 }
968
Mike Lockwood59c777a2010-08-02 10:37:41 -0400969 private int deleteFile(int handle) {
Mike Lockwood2837eef2010-08-31 16:25:12 -0400970 mDatabaseModified = true;
Mike Lockwood55f808c2010-12-14 13:14:29 -0800971 String path = null;
972 int format = 0;
973
974 Cursor c = null;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400975 try {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800976 c = mMediaProvider.query(mPackageName, mObjectsUri, PATH_FORMAT_PROJECTION,
Jeff Brown75ea64f2012-01-25 19:37:13 -0800977 ID_WHERE, new String[] { Integer.toString(handle) }, null, null);
Mike Lockwood55f808c2010-12-14 13:14:29 -0800978 if (c != null && c.moveToNext()) {
979 // don't convert to media path here, since we will be matching
980 // against paths in the database matching /data/media
981 path = c.getString(1);
Mike Lockwoodf6f16612012-09-12 15:50:59 -0700982 format = c.getInt(2);
Mike Lockwood55f808c2010-12-14 13:14:29 -0800983 } else {
984 return MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE;
985 }
986
987 if (path == null || format == 0) {
988 return MtpConstants.RESPONSE_GENERAL_ERROR;
989 }
990
Mike Lockwood73e56d92011-12-01 16:58:41 -0500991 // do not allow deleting any of the special subdirectories
992 if (isStorageSubDirectory(path)) {
993 return MtpConstants.RESPONSE_OBJECT_WRITE_PROTECTED;
994 }
995
Mike Lockwood55f808c2010-12-14 13:14:29 -0800996 if (format == MtpConstants.FORMAT_ASSOCIATION) {
997 // recursive case - delete all children first
998 Uri uri = Files.getMtpObjectsUri(mVolumeName);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800999 int count = mMediaProvider.delete(mPackageName, uri,
Mike Lockwood1e855d92012-06-26 16:31:41 -07001000 // the 'like' makes it use the index, the 'lower()' makes it correct
1001 // when the path contains sqlite wildcard characters
1002 "_data LIKE ?1 AND lower(substr(_data,1,?2))=lower(?3)",
1003 new String[] { path + "/%",Integer.toString(path.length() + 1), path + "/"});
Mike Lockwood55f808c2010-12-14 13:14:29 -08001004 }
1005
1006 Uri uri = Files.getMtpObjectsUri(mVolumeName, handle);
Dianne Hackborn35654b62013-01-14 17:38:02 -08001007 if (mMediaProvider.delete(mPackageName, uri, null, null) > 0) {
Marco Nelissenca78f3d2012-01-27 09:43:20 -08001008 if (format != MtpConstants.FORMAT_ASSOCIATION
1009 && path.toLowerCase(Locale.US).endsWith("/.nomedia")) {
1010 try {
1011 String parentPath = path.substring(0, path.lastIndexOf("/"));
Dianne Hackborn35654b62013-01-14 17:38:02 -08001012 mMediaProvider.call(mPackageName, MediaStore.UNHIDE_CALL, parentPath, null);
Marco Nelissenca78f3d2012-01-27 09:43:20 -08001013 } catch (RemoteException e) {
1014 Log.e(TAG, "failed to unhide/rescan for " + path);
1015 }
1016 }
Mike Lockwood5367ab62010-08-30 13:23:02 -04001017 return MtpConstants.RESPONSE_OK;
Mike Lockwood59c777a2010-08-02 10:37:41 -04001018 } else {
Mike Lockwood5367ab62010-08-30 13:23:02 -04001019 return MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood59c777a2010-08-02 10:37:41 -04001020 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001021 } catch (RemoteException e) {
1022 Log.e(TAG, "RemoteException in deleteFile", e);
Mike Lockwood5367ab62010-08-30 13:23:02 -04001023 return MtpConstants.RESPONSE_GENERAL_ERROR;
Mike Lockwood55f808c2010-12-14 13:14:29 -08001024 } finally {
1025 if (c != null) {
1026 c.close();
1027 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001028 }
1029 }
1030
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001031 private int[] getObjectReferences(int handle) {
Mike Lockwood8490e662010-09-09 14:16:22 -04001032 Uri uri = Files.getMtpReferencesUri(mVolumeName, handle);
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001033 Cursor c = null;
1034 try {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001035 c = mMediaProvider.query(mPackageName, uri, ID_PROJECTION, null, null, null, null);
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001036 if (c == null) {
1037 return null;
1038 }
1039 int count = c.getCount();
1040 if (count > 0) {
1041 int[] result = new int[count];
1042 for (int i = 0; i < count; i++) {
1043 c.moveToNext();
1044 result[i] = c.getInt(0);
1045 }
1046 return result;
1047 }
1048 } catch (RemoteException e) {
1049 Log.e(TAG, "RemoteException in getObjectList", e);
1050 } finally {
1051 if (c != null) {
1052 c.close();
1053 }
1054 }
1055 return null;
1056 }
1057
1058 private int setObjectReferences(int handle, int[] references) {
Mike Lockwood2837eef2010-08-31 16:25:12 -04001059 mDatabaseModified = true;
Mike Lockwood8490e662010-09-09 14:16:22 -04001060 Uri uri = Files.getMtpReferencesUri(mVolumeName, handle);
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001061 int count = references.length;
1062 ContentValues[] valuesList = new ContentValues[count];
1063 for (int i = 0; i < count; i++) {
1064 ContentValues values = new ContentValues();
Mike Lockwood3b2a62e2010-09-08 12:47:57 -04001065 values.put(Files.FileColumns._ID, references[i]);
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001066 valuesList[i] = values;
1067 }
1068 try {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001069 if (mMediaProvider.bulkInsert(mPackageName, uri, valuesList) > 0) {
Mike Lockwood5367ab62010-08-30 13:23:02 -04001070 return MtpConstants.RESPONSE_OK;
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001071 }
1072 } catch (RemoteException e) {
1073 Log.e(TAG, "RemoteException in setObjectReferences", e);
1074 }
Mike Lockwood5367ab62010-08-30 13:23:02 -04001075 return MtpConstants.RESPONSE_GENERAL_ERROR;
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001076 }
1077
Mike Lockwood2837eef2010-08-31 16:25:12 -04001078 private void sessionStarted() {
Mike Lockwood2837eef2010-08-31 16:25:12 -04001079 mDatabaseModified = false;
1080 }
1081
1082 private void sessionEnded() {
Mike Lockwood2837eef2010-08-31 16:25:12 -04001083 if (mDatabaseModified) {
Mike Lockwooda3156052010-11-20 12:28:27 -05001084 mContext.sendBroadcast(new Intent(MediaStore.ACTION_MTP_SESSION_END));
Mike Lockwood2837eef2010-08-31 16:25:12 -04001085 mDatabaseModified = false;
1086 }
1087 }
1088
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001089 // used by the JNI code
Ashok Bhate2e59322013-12-17 19:04:19 +00001090 private long mNativeContext;
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001091
1092 private native final void native_setup();
1093 private native final void native_finalize();
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001094}