blob: dd99a1594f5f5ec8bde33c035f131749bf582656 [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;
Jeff Sharkey60cfad82016-01-05 17:30:57 -070020import android.content.ContentProviderClient;
Mike Lockwoodd815f792010-07-12 08:49:01 -040021import android.content.ContentValues;
Jeff Sharkey60cfad82016-01-05 17:30:57 -070022import android.content.Context;
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;
Mike Lockwoodd21eac92010-07-03 00:44:05 -040031import android.os.RemoteException;
Jerry Zhang13bb2f42016-12-14 15:39:29 -080032import android.os.SystemProperties;
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
Jeff Sharkey60cfad82016-01-05 17:30:57 -070041import dalvik.system.CloseGuard;
42
Mike Lockwood5ebac832010-10-12 11:33:47 -040043import java.io.File;
Marco Nelissen5f411692014-09-26 16:03:49 -070044import java.io.IOException;
Mike Lockwood7d7fb632010-12-01 18:46:23 -050045import java.util.HashMap;
dujin.chafe464a72011-11-22 12:13:33 +090046import java.util.Locale;
Jeff Sharkey60cfad82016-01-05 17:30:57 -070047import java.util.concurrent.atomic.AtomicBoolean;
Mike Lockwood5ebac832010-10-12 11:33:47 -040048
Mike Lockwoodd21eac92010-07-03 00:44:05 -040049/**
50 * {@hide}
51 */
Jeff Sharkey60cfad82016-01-05 17:30:57 -070052public class MtpDatabase implements AutoCloseable {
Mike Lockwoodd21eac92010-07-03 00:44:05 -040053 private static final String TAG = "MtpDatabase";
54
Mike Lockwood2837eef2010-08-31 16:25:12 -040055 private final Context mContext;
Dianne Hackborn35654b62013-01-14 17:38:02 -080056 private final String mPackageName;
Jeff Sharkey60cfad82016-01-05 17:30:57 -070057 private final ContentProviderClient mMediaProvider;
Mike Lockwoodd21eac92010-07-03 00:44:05 -040058 private final String mVolumeName;
59 private final Uri mObjectsUri;
Jeff Sharkey60cfad82016-01-05 17:30:57 -070060 private final MediaScanner mMediaScanner;
61
62 private final AtomicBoolean mClosed = new AtomicBoolean();
63 private final CloseGuard mCloseGuard = CloseGuard.get();
64
Mike Lockwood73e56d92011-12-01 16:58:41 -050065 // path to primary storage
66 private final String mMediaStoragePath;
67 // if not null, restrict all queries to these subdirectories
68 private final String[] mSubDirectories;
69 // where clause for restricting queries to files in mSubDirectories
70 private String mSubDirectoriesWhere;
71 // where arguments for restricting queries to files in mSubDirectories
72 private String[] mSubDirectoriesWhereArgs;
73
Mike Lockwoodb239b6832011-04-05 10:21:27 -040074 private final HashMap<String, MtpStorage> mStorageMap = new HashMap<String, MtpStorage>();
Mike Lockwoodd21eac92010-07-03 00:44:05 -040075
Mike Lockwood7d7fb632010-12-01 18:46:23 -050076 // cached property groups for single properties
77 private final HashMap<Integer, MtpPropertyGroup> mPropertyGroupsByProperty
78 = new HashMap<Integer, MtpPropertyGroup>();
79
80 // cached property groups for all properties for a given format
81 private final HashMap<Integer, MtpPropertyGroup> mPropertyGroupsByFormat
82 = new HashMap<Integer, MtpPropertyGroup>();
83
Mike Lockwood2837eef2010-08-31 16:25:12 -040084 // true if the database has been modified in the current MTP session
85 private boolean mDatabaseModified;
86
Mike Lockwood775de952011-03-05 17:34:11 -050087 // SharedPreferences for writable MTP device properties
88 private SharedPreferences mDeviceProperties;
Mike Lockwood59e3f0d2010-09-02 14:57:30 -040089 private static final int DEVICE_PROPERTIES_DATABASE_VERSION = 1;
90
Mike Lockwoodd21eac92010-07-03 00:44:05 -040091 private static final String[] ID_PROJECTION = new String[] {
Mike Lockwood3b2a62e2010-09-08 12:47:57 -040092 Files.FileColumns._ID, // 0
Mike Lockwoodd21eac92010-07-03 00:44:05 -040093 };
Mike Lockwood6a6a3af2010-10-12 14:19:51 -040094 private static final String[] PATH_PROJECTION = new String[] {
Mike Lockwood5ebac832010-10-12 11:33:47 -040095 Files.FileColumns._ID, // 0
96 Files.FileColumns.DATA, // 1
Mike Lockwood5ebac832010-10-12 11:33:47 -040097 };
Mike Lockwood71827742015-01-23 10:50:08 -080098 private static final String[] FORMAT_PROJECTION = new String[] {
99 Files.FileColumns._ID, // 0
100 Files.FileColumns.FORMAT, // 1
101 };
Mike Lockwoodf6f16612012-09-12 15:50:59 -0700102 private static final String[] PATH_FORMAT_PROJECTION = new String[] {
Mike Lockwood3b2a62e2010-09-08 12:47:57 -0400103 Files.FileColumns._ID, // 0
104 Files.FileColumns.DATA, // 1
Mike Lockwoodf6f16612012-09-12 15:50:59 -0700105 Files.FileColumns.FORMAT, // 2
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400106 };
107 private static final String[] OBJECT_INFO_PROJECTION = new String[] {
Mike Lockwood3b2a62e2010-09-08 12:47:57 -0400108 Files.FileColumns._ID, // 0
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400109 Files.FileColumns.STORAGE_ID, // 1
Mike Lockwood3b2a62e2010-09-08 12:47:57 -0400110 Files.FileColumns.FORMAT, // 2
111 Files.FileColumns.PARENT, // 3
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400112 Files.FileColumns.DATA, // 4
Mike Lockwood1341f1e2013-04-01 10:52:47 -0700113 Files.FileColumns.DATE_ADDED, // 5
114 Files.FileColumns.DATE_MODIFIED, // 6
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400115 };
Mike Lockwood3b2a62e2010-09-08 12:47:57 -0400116 private static final String ID_WHERE = Files.FileColumns._ID + "=?";
Mike Lockwoodbafca212010-12-13 21:50:09 -0800117 private static final String PATH_WHERE = Files.FileColumns.DATA + "=?";
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400118
119 private static final String STORAGE_WHERE = Files.FileColumns.STORAGE_ID + "=?";
Mike Lockwood58e6831c2012-09-11 10:49:34 -0700120 private static final String FORMAT_WHERE = Files.FileColumns.FORMAT + "=?";
121 private static final String PARENT_WHERE = Files.FileColumns.PARENT + "=?";
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400122 private static final String STORAGE_FORMAT_WHERE = STORAGE_WHERE + " AND "
Mike Lockwood3b2a62e2010-09-08 12:47:57 -0400123 + Files.FileColumns.FORMAT + "=?";
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400124 private static final String STORAGE_PARENT_WHERE = STORAGE_WHERE + " AND "
125 + Files.FileColumns.PARENT + "=?";
126 private static final String FORMAT_PARENT_WHERE = FORMAT_WHERE + " AND "
127 + Files.FileColumns.PARENT + "=?";
128 private static final String STORAGE_FORMAT_PARENT_WHERE = STORAGE_FORMAT_WHERE + " AND "
129 + Files.FileColumns.PARENT + "=?";
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400130
Mike Lockwood56c85242014-03-07 13:29:08 -0800131 private MtpServer mServer;
132
133 // read from native code
134 private int mBatteryLevel;
135 private int mBatteryScale;
Mike Lockwoodd815f792010-07-12 08:49:01 -0400136
Jerry Zhang13bb2f42016-12-14 15:39:29 -0800137 private int mDeviceType;
138
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400139 static {
140 System.loadLibrary("media_jni");
141 }
142
Mike Lockwood56c85242014-03-07 13:29:08 -0800143 private BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
144 @Override
145 public void onReceive(Context context, Intent intent) {
146 String action = intent.getAction();
147 if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
148 mBatteryScale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
149 int newLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
150 if (newLevel != mBatteryLevel) {
151 mBatteryLevel = newLevel;
152 if (mServer != null) {
153 // send device property changed event
154 mServer.sendDevicePropertyChanged(
155 MtpConstants.DEVICE_PROPERTY_BATTERY_LEVEL);
156 }
157 }
158 }
159 }
160 };
161
Mike Lockwood73e56d92011-12-01 16:58:41 -0500162 public MtpDatabase(Context context, String volumeName, String storagePath,
163 String[] subDirectories) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400164 native_setup();
165
Mike Lockwood2837eef2010-08-31 16:25:12 -0400166 mContext = context;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800167 mPackageName = context.getPackageName();
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700168 mMediaProvider = context.getContentResolver()
169 .acquireContentProviderClient(MediaStore.AUTHORITY);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400170 mVolumeName = volumeName;
Mike Lockwood01788562010-10-11 11:22:19 -0400171 mMediaStoragePath = storagePath;
Mike Lockwood8490e662010-09-09 14:16:22 -0400172 mObjectsUri = Files.getMtpObjectsUri(volumeName);
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700173 mMediaScanner = new MediaScanner(context, mVolumeName);
dujin.chafe464a72011-11-22 12:13:33 +0900174
Mike Lockwood73e56d92011-12-01 16:58:41 -0500175 mSubDirectories = subDirectories;
176 if (subDirectories != null) {
177 // Compute "where" string for restricting queries to subdirectories
178 StringBuilder builder = new StringBuilder();
179 builder.append("(");
180 int count = subDirectories.length;
181 for (int i = 0; i < count; i++) {
182 builder.append(Files.FileColumns.DATA + "=? OR "
183 + Files.FileColumns.DATA + " LIKE ?");
184 if (i != count - 1) {
185 builder.append(" OR ");
186 }
187 }
188 builder.append(")");
189 mSubDirectoriesWhere = builder.toString();
190
191 // Compute "where" arguments for restricting queries to subdirectories
192 mSubDirectoriesWhereArgs = new String[count * 2];
193 for (int i = 0, j = 0; i < count; i++) {
194 String path = subDirectories[i];
195 mSubDirectoriesWhereArgs[j++] = path;
196 mSubDirectoriesWhereArgs[j++] = path + "/%";
197 }
198 }
199
Mike Lockwood775de952011-03-05 17:34:11 -0500200 initDeviceProperties(context);
Jerry Zhang13bb2f42016-12-14 15:39:29 -0800201 mDeviceType = SystemProperties.getInt("sys.usb.mtp.device_type", 0);
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700202
203 mCloseGuard.open("close");
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400204 }
205
Mike Lockwood56c85242014-03-07 13:29:08 -0800206 public void setServer(MtpServer server) {
207 mServer = server;
208
Marco Nelissen1632fae2014-03-27 13:25:14 -0700209 // always unregister before registering
210 try {
211 mContext.unregisterReceiver(mBatteryReceiver);
212 } catch (IllegalArgumentException e) {
213 // wasn't previously registered, ignore
214 }
215
Mike Lockwood56c85242014-03-07 13:29:08 -0800216 // register for battery notifications when we are connected
217 if (server != null) {
218 mContext.registerReceiver(mBatteryReceiver,
219 new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Mike Lockwood56c85242014-03-07 13:29:08 -0800220 }
221 }
222
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400223 @Override
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700224 public void close() {
225 mCloseGuard.close();
226 if (mClosed.compareAndSet(false, true)) {
227 mMediaScanner.close();
228 mMediaProvider.close();
229 native_finalize();
230 }
231 }
232
233 @Override
Mike Lockwooddbead322010-08-30 09:27:55 -0400234 protected void finalize() throws Throwable {
235 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700236 mCloseGuard.warnIfOpen();
237 close();
Mike Lockwooddbead322010-08-30 09:27:55 -0400238 } finally {
239 super.finalize();
240 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400241 }
242
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400243 public void addStorage(MtpStorage storage) {
244 mStorageMap.put(storage.getPath(), storage);
245 }
246
247 public void removeStorage(MtpStorage storage) {
248 mStorageMap.remove(storage.getPath());
249 }
250
Mike Lockwood775de952011-03-05 17:34:11 -0500251 private void initDeviceProperties(Context context) {
252 final String devicePropertiesName = "device-properties";
253 mDeviceProperties = context.getSharedPreferences(devicePropertiesName, Context.MODE_PRIVATE);
254 File databaseFile = context.getDatabasePath(devicePropertiesName);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400255
Mike Lockwood775de952011-03-05 17:34:11 -0500256 if (databaseFile.exists()) {
257 // for backward compatibility - read device properties from sqlite database
258 // and migrate them to shared prefs
259 SQLiteDatabase db = null;
260 Cursor c = null;
261 try {
262 db = context.openOrCreateDatabase("device-properties", Context.MODE_PRIVATE, null);
263 if (db != null) {
264 c = db.query("properties", new String[] { "_id", "code", "value" },
265 null, null, null, null, null);
266 if (c != null) {
267 SharedPreferences.Editor e = mDeviceProperties.edit();
268 while (c.moveToNext()) {
269 String name = c.getString(1);
270 String value = c.getString(2);
271 e.putString(name, value);
272 }
273 e.commit();
274 }
275 }
276 } catch (Exception e) {
277 Log.e(TAG, "failed to migrate device properties", e);
278 } finally {
279 if (c != null) c.close();
280 if (db != null) db.close();
281 }
jangwon.lee3ed02532013-07-22 19:52:48 +0900282 context.deleteDatabase(devicePropertiesName);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400283 }
284 }
285
Mike Lockwood73e56d92011-12-01 16:58:41 -0500286 // check to see if the path is contained in one of our storage subdirectories
287 // returns true if we have no special subdirectories
288 private boolean inStorageSubDirectory(String path) {
289 if (mSubDirectories == null) return true;
290 if (path == null) return false;
291
292 boolean allowed = false;
293 int pathLength = path.length();
294 for (int i = 0; i < mSubDirectories.length && !allowed; i++) {
295 String subdir = mSubDirectories[i];
296 int subdirLength = subdir.length();
297 if (subdirLength < pathLength &&
298 path.charAt(subdirLength) == '/' &&
299 path.startsWith(subdir)) {
300 allowed = true;
301 }
302 }
303 return allowed;
304 }
305
306 // check to see if the path matches one of our storage subdirectories
307 // returns true if we have no special subdirectories
308 private boolean isStorageSubDirectory(String path) {
309 if (mSubDirectories == null) return false;
310 for (int i = 0; i < mSubDirectories.length; i++) {
311 if (path.equals(mSubDirectories[i])) {
312 return true;
313 }
314 }
315 return false;
316 }
317
Marco Nelissen5f411692014-09-26 16:03:49 -0700318 // returns true if the path is in the storage root
319 private boolean inStorageRoot(String path) {
320 try {
321 File f = new File(path);
322 String canonical = f.getCanonicalPath();
Marco Nelissenc1fda122014-10-15 14:32:22 -0700323 for (String root: mStorageMap.keySet()) {
324 if (canonical.startsWith(root)) {
325 return true;
326 }
Marco Nelissen5f411692014-09-26 16:03:49 -0700327 }
328 } catch (IOException e) {
329 // ignore
330 }
331 return false;
332 }
333
Mike Lockwoodd815f792010-07-12 08:49:01 -0400334 private int beginSendObject(String path, int format, int parent,
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400335 int storageId, long size, long modified) {
Marco Nelissen5f411692014-09-26 16:03:49 -0700336 // if the path is outside of the storage root, do not allow access
337 if (!inStorageRoot(path)) {
338 Log.e(TAG, "attempt to put file outside of storage area: " + path);
339 return -1;
340 }
Mike Lockwood73e56d92011-12-01 16:58:41 -0500341 // if mSubDirectories is not null, do not allow copying files to any other locations
342 if (!inStorageSubDirectory(path)) return -1;
343
344 // make sure the object does not exist
Mike Lockwoodbafca212010-12-13 21:50:09 -0800345 if (path != null) {
346 Cursor c = null;
347 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700348 c = mMediaProvider.query(mObjectsUri, ID_PROJECTION, PATH_WHERE,
Jeff Brown75ea64f2012-01-25 19:37:13 -0800349 new String[] { path }, null, null);
Mike Lockwoodbafca212010-12-13 21:50:09 -0800350 if (c != null && c.getCount() > 0) {
351 Log.w(TAG, "file already exists in beginSendObject: " + path);
352 return -1;
353 }
354 } catch (RemoteException e) {
355 Log.e(TAG, "RemoteException in beginSendObject", e);
356 } finally {
357 if (c != null) {
358 c.close();
359 }
360 }
361 }
362
Mike Lockwood2837eef2010-08-31 16:25:12 -0400363 mDatabaseModified = true;
Mike Lockwoodd815f792010-07-12 08:49:01 -0400364 ContentValues values = new ContentValues();
Mike Lockwood3b2a62e2010-09-08 12:47:57 -0400365 values.put(Files.FileColumns.DATA, path);
366 values.put(Files.FileColumns.FORMAT, format);
367 values.put(Files.FileColumns.PARENT, parent);
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400368 values.put(Files.FileColumns.STORAGE_ID, storageId);
Mike Lockwood3b2a62e2010-09-08 12:47:57 -0400369 values.put(Files.FileColumns.SIZE, size);
370 values.put(Files.FileColumns.DATE_MODIFIED, modified);
Mike Lockwoodd815f792010-07-12 08:49:01 -0400371
372 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700373 Uri uri = mMediaProvider.insert(mObjectsUri, values);
Mike Lockwoodd815f792010-07-12 08:49:01 -0400374 if (uri != null) {
375 return Integer.parseInt(uri.getPathSegments().get(2));
376 } else {
377 return -1;
378 }
379 } catch (RemoteException e) {
380 Log.e(TAG, "RemoteException in beginSendObject", e);
381 return -1;
382 }
383 }
384
Mike Lockwood7a0bd172011-01-18 11:06:19 -0800385 private void endSendObject(String path, int handle, int format, boolean succeeded) {
Mike Lockwoodd815f792010-07-12 08:49:01 -0400386 if (succeeded) {
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400387 // handle abstract playlists separately
388 // they do not exist in the file system so don't use the media scanner here
Mike Lockwood5367ab62010-08-30 13:23:02 -0400389 if (format == MtpConstants.FORMAT_ABSTRACT_AV_PLAYLIST) {
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400390 // extract name from path
391 String name = path;
392 int lastSlash = name.lastIndexOf('/');
393 if (lastSlash >= 0) {
394 name = name.substring(lastSlash + 1);
395 }
Mike Lockwood8cc6eb12011-01-18 13:13:05 -0800396 // strip trailing ".pla" from the name
397 if (name.endsWith(".pla")) {
398 name = name.substring(0, name.length() - 4);
399 }
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400400
401 ContentValues values = new ContentValues(1);
402 values.put(Audio.Playlists.DATA, path);
403 values.put(Audio.Playlists.NAME, name);
Mike Lockwood0b58c192010-11-17 15:42:09 -0500404 values.put(Files.FileColumns.FORMAT, format);
Mike Lockwood8ed67ac2011-01-18 13:27:25 -0800405 values.put(Files.FileColumns.DATE_MODIFIED, System.currentTimeMillis() / 1000);
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400406 values.put(MediaColumns.MEDIA_SCANNER_NEW_OBJECT_ID, handle);
407 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700408 Uri uri = mMediaProvider.insert(
Dianne Hackborn35654b62013-01-14 17:38:02 -0800409 Audio.Playlists.EXTERNAL_CONTENT_URI, values);
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400410 } catch (RemoteException e) {
411 Log.e(TAG, "RemoteException in endSendObject", e);
412 }
413 } else {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700414 mMediaScanner.scanMtpFile(path, handle, format);
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400415 }
Mike Lockwoodd815f792010-07-12 08:49:01 -0400416 } else {
417 deleteFile(handle);
418 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400419 }
420
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400421 private Cursor createObjectQuery(int storageID, int format, int parent) throws RemoteException {
Mike Lockwood73e56d92011-12-01 16:58:41 -0500422 String where;
423 String[] whereArgs;
424
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400425 if (storageID == 0xFFFFFFFF) {
426 // query all stores
427 if (format == 0) {
428 // query all formats
429 if (parent == 0) {
430 // query all objects
Mike Lockwood73e56d92011-12-01 16:58:41 -0500431 where = null;
432 whereArgs = null;
433 } else {
434 if (parent == 0xFFFFFFFF) {
435 // all objects in root of store
436 parent = 0;
437 }
438 where = PARENT_WHERE;
439 whereArgs = new String[] { Integer.toString(parent) };
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400440 }
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400441 } else {
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400442 // query specific format
443 if (parent == 0) {
444 // query all objects
Mike Lockwood73e56d92011-12-01 16:58:41 -0500445 where = FORMAT_WHERE;
446 whereArgs = new String[] { Integer.toString(format) };
447 } else {
448 if (parent == 0xFFFFFFFF) {
449 // all objects in root of store
450 parent = 0;
451 }
452 where = FORMAT_PARENT_WHERE;
453 whereArgs = new String[] { Integer.toString(format),
454 Integer.toString(parent) };
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400455 }
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400456 }
457 } else {
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400458 // query specific store
459 if (format == 0) {
460 // query all formats
461 if (parent == 0) {
462 // query all objects
Mike Lockwood73e56d92011-12-01 16:58:41 -0500463 where = STORAGE_WHERE;
464 whereArgs = new String[] { Integer.toString(storageID) };
465 } else {
466 if (parent == 0xFFFFFFFF) {
467 // all objects in root of store
468 parent = 0;
Jerry Zhangdef7b1932017-10-17 13:47:51 -0700469 where = STORAGE_PARENT_WHERE;
470 whereArgs = new String[]{Integer.toString(storageID),
471 Integer.toString(parent)};
472 } else {
473 // If a parent is specified, the storage is redundant
474 where = PARENT_WHERE;
475 whereArgs = new String[]{Integer.toString(parent)};
Mike Lockwood73e56d92011-12-01 16:58:41 -0500476 }
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400477 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400478 } else {
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400479 // query specific format
480 if (parent == 0) {
481 // query all objects
Mike Lockwood73e56d92011-12-01 16:58:41 -0500482 where = STORAGE_FORMAT_WHERE;
483 whereArgs = new String[] { Integer.toString(storageID),
484 Integer.toString(format) };
485 } else {
486 if (parent == 0xFFFFFFFF) {
487 // all objects in root of store
488 parent = 0;
Jerry Zhangdef7b1932017-10-17 13:47:51 -0700489 where = STORAGE_FORMAT_PARENT_WHERE;
490 whereArgs = new String[]{Integer.toString(storageID),
491 Integer.toString(format),
492 Integer.toString(parent)};
493 } else {
494 // If a parent is specified, the storage is redundant
495 where = FORMAT_PARENT_WHERE;
496 whereArgs = new String[]{Integer.toString(format),
497 Integer.toString(parent)};
Mike Lockwood73e56d92011-12-01 16:58:41 -0500498 }
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400499 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400500 }
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400501 }
Mike Lockwood73e56d92011-12-01 16:58:41 -0500502
503 // if we are restricting queries to mSubDirectories, we need to add the restriction
504 // onto our "where" arguments
505 if (mSubDirectoriesWhere != null) {
506 if (where == null) {
507 where = mSubDirectoriesWhere;
508 whereArgs = mSubDirectoriesWhereArgs;
509 } else {
510 where = where + " AND " + mSubDirectoriesWhere;
511
512 // create new array to hold whereArgs and mSubDirectoriesWhereArgs
513 String[] newWhereArgs =
514 new String[whereArgs.length + mSubDirectoriesWhereArgs.length];
515 int i, j;
516 for (i = 0; i < whereArgs.length; i++) {
517 newWhereArgs[i] = whereArgs[i];
518 }
519 for (j = 0; j < mSubDirectoriesWhereArgs.length; i++, j++) {
520 newWhereArgs[i] = mSubDirectoriesWhereArgs[j];
521 }
522 whereArgs = newWhereArgs;
523 }
524 }
525
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700526 return mMediaProvider.query(mObjectsUri, ID_PROJECTION, where,
Dianne Hackborn35654b62013-01-14 17:38:02 -0800527 whereArgs, null, null);
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400528 }
529
530 private int[] getObjectList(int storageID, int format, int parent) {
531 Cursor c = null;
532 try {
533 c = createObjectQuery(storageID, format, parent);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400534 if (c == null) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400535 return null;
536 }
537 int count = c.getCount();
538 if (count > 0) {
539 int[] result = new int[count];
540 for (int i = 0; i < count; i++) {
541 c.moveToNext();
542 result[i] = c.getInt(0);
543 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400544 return result;
545 }
546 } catch (RemoteException e) {
547 Log.e(TAG, "RemoteException in getObjectList", e);
548 } finally {
549 if (c != null) {
550 c.close();
551 }
552 }
553 return null;
554 }
555
Mike Lockwood7a047c82010-08-02 10:52:20 -0400556 private int getNumObjects(int storageID, int format, int parent) {
Mike Lockwood7a047c82010-08-02 10:52:20 -0400557 Cursor c = null;
558 try {
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400559 c = createObjectQuery(storageID, format, parent);
Mike Lockwood7a047c82010-08-02 10:52:20 -0400560 if (c != null) {
561 return c.getCount();
562 }
563 } catch (RemoteException e) {
564 Log.e(TAG, "RemoteException in getNumObjects", e);
565 } finally {
566 if (c != null) {
567 c.close();
568 }
569 }
570 return -1;
571 }
572
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400573 private int[] getSupportedPlaybackFormats() {
574 return new int[] {
Mike Lockwoode5211692010-09-08 13:50:45 -0400575 // allow transfering arbitrary files
576 MtpConstants.FORMAT_UNDEFINED,
Mike Lockwood12b8a992010-09-23 21:33:29 -0400577
Mike Lockwood792ec842010-09-09 15:30:10 -0400578 MtpConstants.FORMAT_ASSOCIATION,
Mike Lockwood12b8a992010-09-23 21:33:29 -0400579 MtpConstants.FORMAT_TEXT,
580 MtpConstants.FORMAT_HTML,
581 MtpConstants.FORMAT_WAV,
582 MtpConstants.FORMAT_MP3,
583 MtpConstants.FORMAT_MPEG,
584 MtpConstants.FORMAT_EXIF_JPEG,
585 MtpConstants.FORMAT_TIFF_EP,
bo huang240582e2012-02-27 16:27:00 +0800586 MtpConstants.FORMAT_BMP,
Mike Lockwood12b8a992010-09-23 21:33:29 -0400587 MtpConstants.FORMAT_GIF,
588 MtpConstants.FORMAT_JFIF,
589 MtpConstants.FORMAT_PNG,
590 MtpConstants.FORMAT_TIFF,
591 MtpConstants.FORMAT_WMA,
592 MtpConstants.FORMAT_OGG,
593 MtpConstants.FORMAT_AAC,
594 MtpConstants.FORMAT_MP4_CONTAINER,
595 MtpConstants.FORMAT_MP2,
596 MtpConstants.FORMAT_3GP_CONTAINER,
Mike Lockwood792ec842010-09-09 15:30:10 -0400597 MtpConstants.FORMAT_ABSTRACT_AV_PLAYLIST,
Mike Lockwood12b8a992010-09-23 21:33:29 -0400598 MtpConstants.FORMAT_WPL_PLAYLIST,
599 MtpConstants.FORMAT_M3U_PLAYLIST,
600 MtpConstants.FORMAT_PLS_PLAYLIST,
601 MtpConstants.FORMAT_XML_DOCUMENT,
Glenn Kastenf9f223e2011-01-13 11:17:00 -0800602 MtpConstants.FORMAT_FLAC,
Jaesung Chung5a8b9622015-12-18 05:50:21 +0100603 MtpConstants.FORMAT_DNG,
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400604 };
605 }
606
607 private int[] getSupportedCaptureFormats() {
608 // no capture formats yet
609 return null;
610 }
611
Mike Lockwoodae078f72010-09-26 12:35:51 -0400612 static final int[] FILE_PROPERTIES = {
613 // NOTE must match beginning of AUDIO_PROPERTIES, VIDEO_PROPERTIES
614 // and IMAGE_PROPERTIES below
Mike Lockwood5367ab62010-08-30 13:23:02 -0400615 MtpConstants.PROPERTY_STORAGE_ID,
616 MtpConstants.PROPERTY_OBJECT_FORMAT,
Mike Lockwoodd3bfecb2010-09-23 23:04:28 -0400617 MtpConstants.PROPERTY_PROTECTION_STATUS,
Mike Lockwood5367ab62010-08-30 13:23:02 -0400618 MtpConstants.PROPERTY_OBJECT_SIZE,
619 MtpConstants.PROPERTY_OBJECT_FILE_NAME,
Mike Lockwoodd3bfecb2010-09-23 23:04:28 -0400620 MtpConstants.PROPERTY_DATE_MODIFIED,
Mike Lockwood5367ab62010-08-30 13:23:02 -0400621 MtpConstants.PROPERTY_PARENT_OBJECT,
Mike Lockwoodd3bfecb2010-09-23 23:04:28 -0400622 MtpConstants.PROPERTY_PERSISTENT_UID,
623 MtpConstants.PROPERTY_NAME,
Mike Lockwood71827742015-01-23 10:50:08 -0800624 MtpConstants.PROPERTY_DISPLAY_NAME,
Mike Lockwoodae078f72010-09-26 12:35:51 -0400625 MtpConstants.PROPERTY_DATE_ADDED,
626 };
627
628 static final int[] AUDIO_PROPERTIES = {
629 // NOTE must match FILE_PROPERTIES above
630 MtpConstants.PROPERTY_STORAGE_ID,
631 MtpConstants.PROPERTY_OBJECT_FORMAT,
632 MtpConstants.PROPERTY_PROTECTION_STATUS,
633 MtpConstants.PROPERTY_OBJECT_SIZE,
634 MtpConstants.PROPERTY_OBJECT_FILE_NAME,
635 MtpConstants.PROPERTY_DATE_MODIFIED,
636 MtpConstants.PROPERTY_PARENT_OBJECT,
637 MtpConstants.PROPERTY_PERSISTENT_UID,
638 MtpConstants.PROPERTY_NAME,
639 MtpConstants.PROPERTY_DISPLAY_NAME,
640 MtpConstants.PROPERTY_DATE_ADDED,
641
642 // audio specific properties
643 MtpConstants.PROPERTY_ARTIST,
644 MtpConstants.PROPERTY_ALBUM_NAME,
645 MtpConstants.PROPERTY_ALBUM_ARTIST,
646 MtpConstants.PROPERTY_TRACK,
647 MtpConstants.PROPERTY_ORIGINAL_RELEASE_DATE,
648 MtpConstants.PROPERTY_DURATION,
649 MtpConstants.PROPERTY_GENRE,
650 MtpConstants.PROPERTY_COMPOSER,
Mike Lockwood92b53bc2014-03-13 14:51:29 -0700651 MtpConstants.PROPERTY_AUDIO_WAVE_CODEC,
652 MtpConstants.PROPERTY_BITRATE_TYPE,
653 MtpConstants.PROPERTY_AUDIO_BITRATE,
654 MtpConstants.PROPERTY_NUMBER_OF_CHANNELS,
655 MtpConstants.PROPERTY_SAMPLE_RATE,
Mike Lockwoodae078f72010-09-26 12:35:51 -0400656 };
657
658 static final int[] VIDEO_PROPERTIES = {
659 // NOTE must match FILE_PROPERTIES above
660 MtpConstants.PROPERTY_STORAGE_ID,
661 MtpConstants.PROPERTY_OBJECT_FORMAT,
662 MtpConstants.PROPERTY_PROTECTION_STATUS,
663 MtpConstants.PROPERTY_OBJECT_SIZE,
664 MtpConstants.PROPERTY_OBJECT_FILE_NAME,
665 MtpConstants.PROPERTY_DATE_MODIFIED,
666 MtpConstants.PROPERTY_PARENT_OBJECT,
667 MtpConstants.PROPERTY_PERSISTENT_UID,
668 MtpConstants.PROPERTY_NAME,
669 MtpConstants.PROPERTY_DISPLAY_NAME,
670 MtpConstants.PROPERTY_DATE_ADDED,
671
672 // video specific properties
673 MtpConstants.PROPERTY_ARTIST,
674 MtpConstants.PROPERTY_ALBUM_NAME,
675 MtpConstants.PROPERTY_DURATION,
676 MtpConstants.PROPERTY_DESCRIPTION,
677 };
678
679 static final int[] IMAGE_PROPERTIES = {
680 // NOTE must match FILE_PROPERTIES above
681 MtpConstants.PROPERTY_STORAGE_ID,
682 MtpConstants.PROPERTY_OBJECT_FORMAT,
683 MtpConstants.PROPERTY_PROTECTION_STATUS,
684 MtpConstants.PROPERTY_OBJECT_SIZE,
685 MtpConstants.PROPERTY_OBJECT_FILE_NAME,
686 MtpConstants.PROPERTY_DATE_MODIFIED,
687 MtpConstants.PROPERTY_PARENT_OBJECT,
688 MtpConstants.PROPERTY_PERSISTENT_UID,
689 MtpConstants.PROPERTY_NAME,
690 MtpConstants.PROPERTY_DISPLAY_NAME,
691 MtpConstants.PROPERTY_DATE_ADDED,
692
693 // image specific properties
694 MtpConstants.PROPERTY_DESCRIPTION,
695 };
696
697 private int[] getSupportedObjectProperties(int format) {
698 switch (format) {
699 case MtpConstants.FORMAT_MP3:
700 case MtpConstants.FORMAT_WAV:
701 case MtpConstants.FORMAT_WMA:
702 case MtpConstants.FORMAT_OGG:
703 case MtpConstants.FORMAT_AAC:
704 return AUDIO_PROPERTIES;
705 case MtpConstants.FORMAT_MPEG:
706 case MtpConstants.FORMAT_3GP_CONTAINER:
707 case MtpConstants.FORMAT_WMV:
708 return VIDEO_PROPERTIES;
709 case MtpConstants.FORMAT_EXIF_JPEG:
710 case MtpConstants.FORMAT_GIF:
711 case MtpConstants.FORMAT_PNG:
712 case MtpConstants.FORMAT_BMP:
Jaesung Chung5a8b9622015-12-18 05:50:21 +0100713 case MtpConstants.FORMAT_DNG:
Mike Lockwoodae078f72010-09-26 12:35:51 -0400714 return IMAGE_PROPERTIES;
715 default:
716 return FILE_PROPERTIES;
717 }
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400718 }
719
720 private int[] getSupportedDeviceProperties() {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400721 return new int[] {
722 MtpConstants.DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER,
723 MtpConstants.DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME,
Mike Lockwoodea93fa12010-12-07 10:41:35 -0800724 MtpConstants.DEVICE_PROPERTY_IMAGE_SIZE,
Mike Lockwood56c85242014-03-07 13:29:08 -0800725 MtpConstants.DEVICE_PROPERTY_BATTERY_LEVEL,
Jerry Zhang13bb2f42016-12-14 15:39:29 -0800726 MtpConstants.DEVICE_PROPERTY_PERCEIVED_DEVICE_TYPE,
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400727 };
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400728 }
729
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900730 private MtpPropertyList getObjectPropertyList(int handle, int format, int property,
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400731 int groupCode, int depth) {
732 // FIXME - implement group support
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400733 if (groupCode != 0) {
734 return new MtpPropertyList(0, MtpConstants.RESPONSE_SPECIFICATION_BY_GROUP_UNSUPPORTED);
735 }
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400736
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500737 MtpPropertyGroup propertyGroup;
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900738 if (property == 0xffffffff) {
739 if (format == 0 && handle != 0 && handle != 0xffffffff) {
Mike Lockwood71827742015-01-23 10:50:08 -0800740 // return properties based on the object's format
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900741 format = getObjectFormat(handle);
Mike Lockwood71827742015-01-23 10:50:08 -0800742 }
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900743 propertyGroup = mPropertyGroupsByFormat.get(format);
744 if (propertyGroup == null) {
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500745 int[] propertyList = getSupportedObjectProperties(format);
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700746 propertyGroup = new MtpPropertyGroup(this, mMediaProvider,
Dianne Hackborn35654b62013-01-14 17:38:02 -0800747 mVolumeName, propertyList);
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900748 mPropertyGroupsByFormat.put(format, propertyGroup);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400749 }
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500750 } else {
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900751 propertyGroup = mPropertyGroupsByProperty.get(property);
752 if (propertyGroup == null) {
753 final int[] propertyList = new int[] { property };
754 propertyGroup = new MtpPropertyGroup(
755 this, mMediaProvider, mVolumeName, propertyList);
756 mPropertyGroupsByProperty.put(property, propertyGroup);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400757 }
758 }
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500759
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900760 return propertyGroup.getPropertyList(handle, format, depth);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400761 }
762
Mike Lockwood5ebac832010-10-12 11:33:47 -0400763 private int renameFile(int handle, String newName) {
764 Cursor c = null;
765
766 // first compute current path
767 String path = null;
Mike Lockwood5ebac832010-10-12 11:33:47 -0400768 String[] whereArgs = new String[] { Integer.toString(handle) };
769 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700770 c = mMediaProvider.query(mObjectsUri, PATH_PROJECTION, ID_WHERE,
Dianne Hackborn35654b62013-01-14 17:38:02 -0800771 whereArgs, null, null);
Mike Lockwood5ebac832010-10-12 11:33:47 -0400772 if (c != null && c.moveToNext()) {
Mike Lockwood1c4e88d2011-01-12 12:38:41 -0500773 path = c.getString(1);
Mike Lockwood5ebac832010-10-12 11:33:47 -0400774 }
775 } catch (RemoteException e) {
776 Log.e(TAG, "RemoteException in getObjectFilePath", e);
777 return MtpConstants.RESPONSE_GENERAL_ERROR;
778 } finally {
779 if (c != null) {
780 c.close();
781 }
782 }
783 if (path == null) {
784 return MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE;
785 }
Mike Lockwood5ebac832010-10-12 11:33:47 -0400786
Mike Lockwood73e56d92011-12-01 16:58:41 -0500787 // do not allow renaming any of the special subdirectories
788 if (isStorageSubDirectory(path)) {
789 return MtpConstants.RESPONSE_OBJECT_WRITE_PROTECTED;
790 }
791
Mike Lockwood5ebac832010-10-12 11:33:47 -0400792 // now rename the file. make sure this succeeds before updating database
793 File oldFile = new File(path);
794 int lastSlash = path.lastIndexOf('/');
795 if (lastSlash <= 1) {
796 return MtpConstants.RESPONSE_GENERAL_ERROR;
797 }
798 String newPath = path.substring(0, lastSlash + 1) + newName;
799 File newFile = new File(newPath);
800 boolean success = oldFile.renameTo(newFile);
Mike Lockwood5ebac832010-10-12 11:33:47 -0400801 if (!success) {
Mike Lockwoodf26a5862011-01-21 21:00:54 -0800802 Log.w(TAG, "renaming "+ path + " to " + newPath + " failed");
Mike Lockwood5ebac832010-10-12 11:33:47 -0400803 return MtpConstants.RESPONSE_GENERAL_ERROR;
804 }
805
806 // finally update database
807 ContentValues values = new ContentValues();
808 values.put(Files.FileColumns.DATA, newPath);
809 int updated = 0;
810 try {
Mike Lockwood6a6a3af2010-10-12 14:19:51 -0400811 // note - we are relying on a special case in MediaProvider.update() to update
812 // the paths for all children in the case where this is a directory.
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700813 updated = mMediaProvider.update(mObjectsUri, values, ID_WHERE, whereArgs);
Mike Lockwood5ebac832010-10-12 11:33:47 -0400814 } catch (RemoteException e) {
815 Log.e(TAG, "RemoteException in mMediaProvider.update", e);
816 }
Mike Lockwood6a6a3af2010-10-12 14:19:51 -0400817 if (updated == 0) {
Mike Lockwood5ebac832010-10-12 11:33:47 -0400818 Log.e(TAG, "Unable to update path for " + path + " to " + newPath);
819 // this shouldn't happen, but if it does we need to rename the file to its original name
820 newFile.renameTo(oldFile);
821 return MtpConstants.RESPONSE_GENERAL_ERROR;
822 }
823
Marco Nelissenca78f3d2012-01-27 09:43:20 -0800824 // check if nomedia status changed
825 if (newFile.isDirectory()) {
826 // for directories, check if renamed from something hidden to something non-hidden
827 if (oldFile.getName().startsWith(".") && !newPath.startsWith(".")) {
828 // directory was unhidden
829 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700830 mMediaProvider.call(MediaStore.UNHIDE_CALL, newPath, null);
Marco Nelissenca78f3d2012-01-27 09:43:20 -0800831 } catch (RemoteException e) {
832 Log.e(TAG, "failed to unhide/rescan for " + newPath);
833 }
834 }
835 } else {
836 // for files, check if renamed from .nomedia to something else
837 if (oldFile.getName().toLowerCase(Locale.US).equals(".nomedia")
838 && !newPath.toLowerCase(Locale.US).equals(".nomedia")) {
839 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700840 mMediaProvider.call(MediaStore.UNHIDE_CALL, oldFile.getParent(), null);
Marco Nelissenca78f3d2012-01-27 09:43:20 -0800841 } catch (RemoteException e) {
842 Log.e(TAG, "failed to unhide/rescan for " + newPath);
843 }
844 }
845 }
846
Mike Lockwood5ebac832010-10-12 11:33:47 -0400847 return MtpConstants.RESPONSE_OK;
848 }
849
Jerry Zhangdef7b1932017-10-17 13:47:51 -0700850 private int moveObject(int handle, int newParent, int newStorage, String newPath) {
Jerry Zhang952558d42017-09-26 17:49:52 -0700851 String[] whereArgs = new String[] { Integer.toString(handle) };
852
853 // do not allow renaming any of the special subdirectories
854 if (isStorageSubDirectory(newPath)) {
855 return MtpConstants.RESPONSE_OBJECT_WRITE_PROTECTED;
856 }
857
858 // update database
859 ContentValues values = new ContentValues();
860 values.put(Files.FileColumns.DATA, newPath);
861 values.put(Files.FileColumns.PARENT, newParent);
Jerry Zhangdef7b1932017-10-17 13:47:51 -0700862 values.put(Files.FileColumns.STORAGE_ID, newStorage);
Jerry Zhang952558d42017-09-26 17:49:52 -0700863 int updated = 0;
864 try {
865 // note - we are relying on a special case in MediaProvider.update() to update
866 // the paths for all children in the case where this is a directory.
867 updated = mMediaProvider.update(mObjectsUri, values, ID_WHERE, whereArgs);
868 } catch (RemoteException e) {
869 Log.e(TAG, "RemoteException in mMediaProvider.update", e);
870 }
871 if (updated == 0) {
872 Log.e(TAG, "Unable to update path for " + handle + " to " + newPath);
873 return MtpConstants.RESPONSE_GENERAL_ERROR;
874 }
875 return MtpConstants.RESPONSE_OK;
876 }
877
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400878 private int setObjectProperty(int handle, int property,
879 long intValue, String stringValue) {
Mike Lockwood5ebac832010-10-12 11:33:47 -0400880 switch (property) {
881 case MtpConstants.PROPERTY_OBJECT_FILE_NAME:
882 return renameFile(handle, stringValue);
883
884 default:
885 return MtpConstants.RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
886 }
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400887 }
888
889 private int getDeviceProperty(int property, long[] outIntValue, char[] outStringValue) {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400890 switch (property) {
891 case MtpConstants.DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER:
892 case MtpConstants.DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME:
Mike Lockwood775de952011-03-05 17:34:11 -0500893 // writable string properties kept in shared preferences
894 String value = mDeviceProperties.getString(Integer.toString(property), "");
895 int length = value.length();
896 if (length > 255) {
897 length = 255;
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400898 }
Mike Lockwood775de952011-03-05 17:34:11 -0500899 value.getChars(0, length, outStringValue, 0);
900 outStringValue[length] = 0;
901 return MtpConstants.RESPONSE_OK;
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400902
Mike Lockwoodea93fa12010-12-07 10:41:35 -0800903 case MtpConstants.DEVICE_PROPERTY_IMAGE_SIZE:
904 // use screen size as max image size
905 Display display = ((WindowManager)mContext.getSystemService(
906 Context.WINDOW_SERVICE)).getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -0700907 int width = display.getMaximumSizeDimension();
908 int height = display.getMaximumSizeDimension();
Mike Lockwoodea93fa12010-12-07 10:41:35 -0800909 String imageSize = Integer.toString(width) + "x" + Integer.toString(height);
910 imageSize.getChars(0, imageSize.length(), outStringValue, 0);
911 outStringValue[imageSize.length()] = 0;
912 return MtpConstants.RESPONSE_OK;
913
Jerry Zhang13bb2f42016-12-14 15:39:29 -0800914 case MtpConstants.DEVICE_PROPERTY_PERCEIVED_DEVICE_TYPE:
915 outIntValue[0] = mDeviceType;
916 return MtpConstants.RESPONSE_OK;
917
Mike Lockwood56c85242014-03-07 13:29:08 -0800918 // DEVICE_PROPERTY_BATTERY_LEVEL is implemented in the JNI code
919
Mike Lockwoodea93fa12010-12-07 10:41:35 -0800920 default:
921 return MtpConstants.RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
922 }
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400923 }
924
925 private int setDeviceProperty(int property, long intValue, String stringValue) {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400926 switch (property) {
927 case MtpConstants.DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER:
928 case MtpConstants.DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME:
Mike Lockwood775de952011-03-05 17:34:11 -0500929 // writable string properties kept in shared prefs
930 SharedPreferences.Editor e = mDeviceProperties.edit();
931 e.putString(Integer.toString(property), stringValue);
932 return (e.commit() ? MtpConstants.RESPONSE_OK
933 : MtpConstants.RESPONSE_GENERAL_ERROR);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400934 }
935
936 return MtpConstants.RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
937 }
938
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400939 private boolean getObjectInfo(int handle, int[] outStorageFormatParent,
Mike Lockwood1341f1e2013-04-01 10:52:47 -0700940 char[] outName, long[] outCreatedModified) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400941 Cursor c = null;
942 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700943 c = mMediaProvider.query(mObjectsUri, OBJECT_INFO_PROJECTION,
Jeff Brown75ea64f2012-01-25 19:37:13 -0800944 ID_WHERE, new String[] { Integer.toString(handle) }, null, null);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400945 if (c != null && c.moveToNext()) {
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400946 outStorageFormatParent[0] = c.getInt(1);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400947 outStorageFormatParent[1] = c.getInt(2);
948 outStorageFormatParent[2] = c.getInt(3);
949
950 // extract name from path
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400951 String path = c.getString(4);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400952 int lastSlash = path.lastIndexOf('/');
953 int start = (lastSlash >= 0 ? lastSlash + 1 : 0);
954 int end = path.length();
955 if (end - start > 255) {
956 end = start + 255;
957 }
958 path.getChars(start, end, outName, 0);
959 outName[end - start] = 0;
960
Mike Lockwood1341f1e2013-04-01 10:52:47 -0700961 outCreatedModified[0] = c.getLong(5);
962 outCreatedModified[1] = c.getLong(6);
963 // use modification date as creation date if date added is not set
964 if (outCreatedModified[0] == 0) {
965 outCreatedModified[0] = outCreatedModified[1];
966 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400967 return true;
968 }
969 } catch (RemoteException e) {
Mike Lockwood2b5f9ad12010-10-29 19:16:27 -0400970 Log.e(TAG, "RemoteException in getObjectInfo", e);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400971 } finally {
972 if (c != null) {
973 c.close();
974 }
975 }
976 return false;
977 }
978
Mike Lockwood365e03e2010-12-08 16:08:01 -0800979 private int getObjectFilePath(int handle, char[] outFilePath, long[] outFileLengthFormat) {
Mike Lockwood01788562010-10-11 11:22:19 -0400980 if (handle == 0) {
981 // special case root directory
982 mMediaStoragePath.getChars(0, mMediaStoragePath.length(), outFilePath, 0);
983 outFilePath[mMediaStoragePath.length()] = 0;
Mike Lockwood365e03e2010-12-08 16:08:01 -0800984 outFileLengthFormat[0] = 0;
985 outFileLengthFormat[1] = MtpConstants.FORMAT_ASSOCIATION;
Mike Lockwood01788562010-10-11 11:22:19 -0400986 return MtpConstants.RESPONSE_OK;
987 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400988 Cursor c = null;
989 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700990 c = mMediaProvider.query(mObjectsUri, PATH_FORMAT_PROJECTION,
Jeff Brown75ea64f2012-01-25 19:37:13 -0800991 ID_WHERE, new String[] { Integer.toString(handle) }, null, null);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400992 if (c != null && c.moveToNext()) {
Mike Lockwood1c4e88d2011-01-12 12:38:41 -0500993 String path = c.getString(1);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400994 path.getChars(0, path.length(), outFilePath, 0);
995 outFilePath[path.length()] = 0;
Mike Lockwoodf6f16612012-09-12 15:50:59 -0700996 // File transfers from device to host will likely fail if the size is incorrect.
997 // So to be safe, use the actual file size here.
998 outFileLengthFormat[0] = new File(path).length();
999 outFileLengthFormat[1] = c.getLong(2);
Mike Lockwood5367ab62010-08-30 13:23:02 -04001000 return MtpConstants.RESPONSE_OK;
Mike Lockwood59c777a2010-08-02 10:37:41 -04001001 } else {
Mike Lockwood5367ab62010-08-30 13:23:02 -04001002 return MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001003 }
1004 } catch (RemoteException e) {
1005 Log.e(TAG, "RemoteException in getObjectFilePath", e);
Mike Lockwood5367ab62010-08-30 13:23:02 -04001006 return MtpConstants.RESPONSE_GENERAL_ERROR;
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001007 } finally {
1008 if (c != null) {
1009 c.close();
1010 }
1011 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001012 }
1013
Mike Lockwood71827742015-01-23 10:50:08 -08001014 private int getObjectFormat(int handle) {
1015 Cursor c = null;
1016 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001017 c = mMediaProvider.query(mObjectsUri, FORMAT_PROJECTION,
Daichi Hirono486ad2e2016-02-29 17:28:47 +09001018 ID_WHERE, new String[] { Integer.toString(handle) }, null, null);
Mike Lockwood71827742015-01-23 10:50:08 -08001019 if (c != null && c.moveToNext()) {
1020 return c.getInt(1);
1021 } else {
1022 return -1;
1023 }
1024 } catch (RemoteException e) {
1025 Log.e(TAG, "RemoteException in getObjectFilePath", e);
1026 return -1;
1027 } finally {
1028 if (c != null) {
1029 c.close();
1030 }
1031 }
1032 }
1033
Mike Lockwood59c777a2010-08-02 10:37:41 -04001034 private int deleteFile(int handle) {
Mike Lockwood2837eef2010-08-31 16:25:12 -04001035 mDatabaseModified = true;
Mike Lockwood55f808c2010-12-14 13:14:29 -08001036 String path = null;
1037 int format = 0;
1038
1039 Cursor c = null;
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001040 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001041 c = mMediaProvider.query(mObjectsUri, PATH_FORMAT_PROJECTION,
Jeff Brown75ea64f2012-01-25 19:37:13 -08001042 ID_WHERE, new String[] { Integer.toString(handle) }, null, null);
Mike Lockwood55f808c2010-12-14 13:14:29 -08001043 if (c != null && c.moveToNext()) {
1044 // don't convert to media path here, since we will be matching
1045 // against paths in the database matching /data/media
1046 path = c.getString(1);
Mike Lockwoodf6f16612012-09-12 15:50:59 -07001047 format = c.getInt(2);
Mike Lockwood55f808c2010-12-14 13:14:29 -08001048 } else {
1049 return MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE;
1050 }
1051
1052 if (path == null || format == 0) {
1053 return MtpConstants.RESPONSE_GENERAL_ERROR;
1054 }
1055
Mike Lockwood73e56d92011-12-01 16:58:41 -05001056 // do not allow deleting any of the special subdirectories
1057 if (isStorageSubDirectory(path)) {
1058 return MtpConstants.RESPONSE_OBJECT_WRITE_PROTECTED;
1059 }
1060
Mike Lockwood55f808c2010-12-14 13:14:29 -08001061 if (format == MtpConstants.FORMAT_ASSOCIATION) {
1062 // recursive case - delete all children first
1063 Uri uri = Files.getMtpObjectsUri(mVolumeName);
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001064 int count = mMediaProvider.delete(uri,
Mike Lockwood1e855d92012-06-26 16:31:41 -07001065 // the 'like' makes it use the index, the 'lower()' makes it correct
1066 // when the path contains sqlite wildcard characters
1067 "_data LIKE ?1 AND lower(substr(_data,1,?2))=lower(?3)",
1068 new String[] { path + "/%",Integer.toString(path.length() + 1), path + "/"});
Mike Lockwood55f808c2010-12-14 13:14:29 -08001069 }
1070
1071 Uri uri = Files.getMtpObjectsUri(mVolumeName, handle);
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001072 if (mMediaProvider.delete(uri, null, null) > 0) {
Marco Nelissenca78f3d2012-01-27 09:43:20 -08001073 if (format != MtpConstants.FORMAT_ASSOCIATION
1074 && path.toLowerCase(Locale.US).endsWith("/.nomedia")) {
1075 try {
1076 String parentPath = path.substring(0, path.lastIndexOf("/"));
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001077 mMediaProvider.call(MediaStore.UNHIDE_CALL, parentPath, null);
Marco Nelissenca78f3d2012-01-27 09:43:20 -08001078 } catch (RemoteException e) {
1079 Log.e(TAG, "failed to unhide/rescan for " + path);
1080 }
1081 }
Mike Lockwood5367ab62010-08-30 13:23:02 -04001082 return MtpConstants.RESPONSE_OK;
Mike Lockwood59c777a2010-08-02 10:37:41 -04001083 } else {
Mike Lockwood5367ab62010-08-30 13:23:02 -04001084 return MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood59c777a2010-08-02 10:37:41 -04001085 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001086 } catch (RemoteException e) {
1087 Log.e(TAG, "RemoteException in deleteFile", e);
Mike Lockwood5367ab62010-08-30 13:23:02 -04001088 return MtpConstants.RESPONSE_GENERAL_ERROR;
Mike Lockwood55f808c2010-12-14 13:14:29 -08001089 } finally {
1090 if (c != null) {
1091 c.close();
1092 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001093 }
1094 }
1095
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001096 private int[] getObjectReferences(int handle) {
Mike Lockwood8490e662010-09-09 14:16:22 -04001097 Uri uri = Files.getMtpReferencesUri(mVolumeName, handle);
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001098 Cursor c = null;
1099 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001100 c = mMediaProvider.query(uri, ID_PROJECTION, null, null, null, null);
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001101 if (c == null) {
1102 return null;
1103 }
1104 int count = c.getCount();
1105 if (count > 0) {
1106 int[] result = new int[count];
1107 for (int i = 0; i < count; i++) {
1108 c.moveToNext();
1109 result[i] = c.getInt(0);
1110 }
1111 return result;
1112 }
1113 } catch (RemoteException e) {
1114 Log.e(TAG, "RemoteException in getObjectList", e);
1115 } finally {
1116 if (c != null) {
1117 c.close();
1118 }
1119 }
1120 return null;
1121 }
1122
1123 private int setObjectReferences(int handle, int[] references) {
Mike Lockwood2837eef2010-08-31 16:25:12 -04001124 mDatabaseModified = true;
Mike Lockwood8490e662010-09-09 14:16:22 -04001125 Uri uri = Files.getMtpReferencesUri(mVolumeName, handle);
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001126 int count = references.length;
1127 ContentValues[] valuesList = new ContentValues[count];
1128 for (int i = 0; i < count; i++) {
1129 ContentValues values = new ContentValues();
Mike Lockwood3b2a62e2010-09-08 12:47:57 -04001130 values.put(Files.FileColumns._ID, references[i]);
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001131 valuesList[i] = values;
1132 }
1133 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001134 if (mMediaProvider.bulkInsert(uri, valuesList) > 0) {
Mike Lockwood5367ab62010-08-30 13:23:02 -04001135 return MtpConstants.RESPONSE_OK;
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001136 }
1137 } catch (RemoteException e) {
1138 Log.e(TAG, "RemoteException in setObjectReferences", e);
1139 }
Mike Lockwood5367ab62010-08-30 13:23:02 -04001140 return MtpConstants.RESPONSE_GENERAL_ERROR;
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001141 }
1142
Mike Lockwood2837eef2010-08-31 16:25:12 -04001143 private void sessionStarted() {
Mike Lockwood2837eef2010-08-31 16:25:12 -04001144 mDatabaseModified = false;
1145 }
1146
1147 private void sessionEnded() {
Mike Lockwood2837eef2010-08-31 16:25:12 -04001148 if (mDatabaseModified) {
Mike Lockwooda3156052010-11-20 12:28:27 -05001149 mContext.sendBroadcast(new Intent(MediaStore.ACTION_MTP_SESSION_END));
Mike Lockwood2837eef2010-08-31 16:25:12 -04001150 mDatabaseModified = false;
1151 }
1152 }
1153
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001154 // used by the JNI code
Ashok Bhate2e59322013-12-17 19:04:19 +00001155 private long mNativeContext;
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001156
1157 private native final void native_setup();
1158 private native final void native_finalize();
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001159}