blob: 17b23264a9fc1714c517b11b042c4f020d89523b [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
Jerry Zhang5f0139d2017-08-22 17:42:54 -070055 private final Context mUserContext;
Mike Lockwood2837eef2010-08-31 16:25:12 -040056 private final Context mContext;
Dianne Hackborn35654b62013-01-14 17:38:02 -080057 private final String mPackageName;
Jeff Sharkey60cfad82016-01-05 17:30:57 -070058 private final ContentProviderClient mMediaProvider;
Mike Lockwoodd21eac92010-07-03 00:44:05 -040059 private final String mVolumeName;
60 private final Uri mObjectsUri;
Jeff Sharkey60cfad82016-01-05 17:30:57 -070061 private final MediaScanner mMediaScanner;
62
63 private final AtomicBoolean mClosed = new AtomicBoolean();
64 private final CloseGuard mCloseGuard = CloseGuard.get();
65
Mike Lockwood73e56d92011-12-01 16:58:41 -050066 // path to primary storage
67 private final String mMediaStoragePath;
68 // if not null, restrict all queries to these subdirectories
69 private final String[] mSubDirectories;
70 // where clause for restricting queries to files in mSubDirectories
71 private String mSubDirectoriesWhere;
72 // where arguments for restricting queries to files in mSubDirectories
73 private String[] mSubDirectoriesWhereArgs;
74
Mike Lockwoodb239b6832011-04-05 10:21:27 -040075 private final HashMap<String, MtpStorage> mStorageMap = new HashMap<String, MtpStorage>();
Mike Lockwoodd21eac92010-07-03 00:44:05 -040076
Mike Lockwood7d7fb632010-12-01 18:46:23 -050077 // cached property groups for single properties
78 private final HashMap<Integer, MtpPropertyGroup> mPropertyGroupsByProperty
79 = new HashMap<Integer, MtpPropertyGroup>();
80
81 // cached property groups for all properties for a given format
82 private final HashMap<Integer, MtpPropertyGroup> mPropertyGroupsByFormat
83 = new HashMap<Integer, MtpPropertyGroup>();
84
Mike Lockwood2837eef2010-08-31 16:25:12 -040085 // true if the database has been modified in the current MTP session
86 private boolean mDatabaseModified;
87
Mike Lockwood775de952011-03-05 17:34:11 -050088 // SharedPreferences for writable MTP device properties
89 private SharedPreferences mDeviceProperties;
Mike Lockwood59e3f0d2010-09-02 14:57:30 -040090 private static final int DEVICE_PROPERTIES_DATABASE_VERSION = 1;
91
Mike Lockwoodd21eac92010-07-03 00:44:05 -040092 private static final String[] ID_PROJECTION = new String[] {
Mike Lockwood3b2a62e2010-09-08 12:47:57 -040093 Files.FileColumns._ID, // 0
Mike Lockwoodd21eac92010-07-03 00:44:05 -040094 };
Mike Lockwood6a6a3af2010-10-12 14:19:51 -040095 private static final String[] PATH_PROJECTION = new String[] {
Mike Lockwood5ebac832010-10-12 11:33:47 -040096 Files.FileColumns._ID, // 0
97 Files.FileColumns.DATA, // 1
Mike Lockwood5ebac832010-10-12 11:33:47 -040098 };
Mike Lockwood71827742015-01-23 10:50:08 -080099 private static final String[] FORMAT_PROJECTION = new String[] {
100 Files.FileColumns._ID, // 0
101 Files.FileColumns.FORMAT, // 1
102 };
Mike Lockwoodf6f16612012-09-12 15:50:59 -0700103 private static final String[] PATH_FORMAT_PROJECTION = new String[] {
Mike Lockwood3b2a62e2010-09-08 12:47:57 -0400104 Files.FileColumns._ID, // 0
105 Files.FileColumns.DATA, // 1
Mike Lockwoodf6f16612012-09-12 15:50:59 -0700106 Files.FileColumns.FORMAT, // 2
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400107 };
108 private static final String[] OBJECT_INFO_PROJECTION = new String[] {
Mike Lockwood3b2a62e2010-09-08 12:47:57 -0400109 Files.FileColumns._ID, // 0
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400110 Files.FileColumns.STORAGE_ID, // 1
Mike Lockwood3b2a62e2010-09-08 12:47:57 -0400111 Files.FileColumns.FORMAT, // 2
112 Files.FileColumns.PARENT, // 3
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400113 Files.FileColumns.DATA, // 4
Mike Lockwood1341f1e2013-04-01 10:52:47 -0700114 Files.FileColumns.DATE_ADDED, // 5
115 Files.FileColumns.DATE_MODIFIED, // 6
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400116 };
Mike Lockwood3b2a62e2010-09-08 12:47:57 -0400117 private static final String ID_WHERE = Files.FileColumns._ID + "=?";
Mike Lockwoodbafca212010-12-13 21:50:09 -0800118 private static final String PATH_WHERE = Files.FileColumns.DATA + "=?";
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400119
120 private static final String STORAGE_WHERE = Files.FileColumns.STORAGE_ID + "=?";
Mike Lockwood58e68312012-09-11 10:49:34 -0700121 private static final String FORMAT_WHERE = Files.FileColumns.FORMAT + "=?";
122 private static final String PARENT_WHERE = Files.FileColumns.PARENT + "=?";
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400123 private static final String STORAGE_FORMAT_WHERE = STORAGE_WHERE + " AND "
Mike Lockwood3b2a62e2010-09-08 12:47:57 -0400124 + Files.FileColumns.FORMAT + "=?";
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400125 private static final String STORAGE_PARENT_WHERE = STORAGE_WHERE + " AND "
126 + Files.FileColumns.PARENT + "=?";
127 private static final String FORMAT_PARENT_WHERE = FORMAT_WHERE + " AND "
128 + Files.FileColumns.PARENT + "=?";
129 private static final String STORAGE_FORMAT_PARENT_WHERE = STORAGE_FORMAT_WHERE + " AND "
130 + Files.FileColumns.PARENT + "=?";
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400131
Mike Lockwood56c85242014-03-07 13:29:08 -0800132 private MtpServer mServer;
133
134 // read from native code
135 private int mBatteryLevel;
136 private int mBatteryScale;
Mike Lockwoodd815f792010-07-12 08:49:01 -0400137
Jerry Zhang13bb2f42016-12-14 15:39:29 -0800138 private int mDeviceType;
139
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400140 static {
141 System.loadLibrary("media_jni");
142 }
143
Mike Lockwood56c85242014-03-07 13:29:08 -0800144 private BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
145 @Override
146 public void onReceive(Context context, Intent intent) {
147 String action = intent.getAction();
148 if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
149 mBatteryScale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
150 int newLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
151 if (newLevel != mBatteryLevel) {
152 mBatteryLevel = newLevel;
153 if (mServer != null) {
154 // send device property changed event
155 mServer.sendDevicePropertyChanged(
156 MtpConstants.DEVICE_PROPERTY_BATTERY_LEVEL);
157 }
158 }
159 }
160 }
161 };
162
Jerry Zhang5f0139d2017-08-22 17:42:54 -0700163 public MtpDatabase(Context context, Context userContext, String volumeName, String storagePath,
Mike Lockwood73e56d92011-12-01 16:58:41 -0500164 String[] subDirectories) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400165 native_setup();
166
Mike Lockwood2837eef2010-08-31 16:25:12 -0400167 mContext = context;
Jerry Zhang5f0139d2017-08-22 17:42:54 -0700168 mUserContext = userContext;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800169 mPackageName = context.getPackageName();
Jerry Zhang5f0139d2017-08-22 17:42:54 -0700170 mMediaProvider = userContext.getContentResolver()
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700171 .acquireContentProviderClient(MediaStore.AUTHORITY);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400172 mVolumeName = volumeName;
Mike Lockwood01788562010-10-11 11:22:19 -0400173 mMediaStoragePath = storagePath;
Mike Lockwood8490e662010-09-09 14:16:22 -0400174 mObjectsUri = Files.getMtpObjectsUri(volumeName);
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700175 mMediaScanner = new MediaScanner(context, mVolumeName);
dujin.chafe464a72011-11-22 12:13:33 +0900176
Mike Lockwood73e56d92011-12-01 16:58:41 -0500177 mSubDirectories = subDirectories;
178 if (subDirectories != null) {
179 // Compute "where" string for restricting queries to subdirectories
180 StringBuilder builder = new StringBuilder();
181 builder.append("(");
182 int count = subDirectories.length;
183 for (int i = 0; i < count; i++) {
184 builder.append(Files.FileColumns.DATA + "=? OR "
185 + Files.FileColumns.DATA + " LIKE ?");
186 if (i != count - 1) {
187 builder.append(" OR ");
188 }
189 }
190 builder.append(")");
191 mSubDirectoriesWhere = builder.toString();
192
193 // Compute "where" arguments for restricting queries to subdirectories
194 mSubDirectoriesWhereArgs = new String[count * 2];
195 for (int i = 0, j = 0; i < count; i++) {
196 String path = subDirectories[i];
197 mSubDirectoriesWhereArgs[j++] = path;
198 mSubDirectoriesWhereArgs[j++] = path + "/%";
199 }
200 }
201
Mike Lockwood775de952011-03-05 17:34:11 -0500202 initDeviceProperties(context);
Jerry Zhang13bb2f42016-12-14 15:39:29 -0800203 mDeviceType = SystemProperties.getInt("sys.usb.mtp.device_type", 0);
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700204
205 mCloseGuard.open("close");
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400206 }
207
Mike Lockwood56c85242014-03-07 13:29:08 -0800208 public void setServer(MtpServer server) {
209 mServer = server;
210
Marco Nelissen1632fae2014-03-27 13:25:14 -0700211 // always unregister before registering
212 try {
213 mContext.unregisterReceiver(mBatteryReceiver);
214 } catch (IllegalArgumentException e) {
215 // wasn't previously registered, ignore
216 }
217
Mike Lockwood56c85242014-03-07 13:29:08 -0800218 // register for battery notifications when we are connected
219 if (server != null) {
220 mContext.registerReceiver(mBatteryReceiver,
221 new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Mike Lockwood56c85242014-03-07 13:29:08 -0800222 }
223 }
224
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400225 @Override
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700226 public void close() {
227 mCloseGuard.close();
228 if (mClosed.compareAndSet(false, true)) {
229 mMediaScanner.close();
230 mMediaProvider.close();
231 native_finalize();
232 }
233 }
234
235 @Override
Mike Lockwooddbead322010-08-30 09:27:55 -0400236 protected void finalize() throws Throwable {
237 try {
Narayan Kamath492e9e82017-03-22 14:28:08 +0000238 if (mCloseGuard != null) {
239 mCloseGuard.warnIfOpen();
240 }
241
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700242 close();
Mike Lockwooddbead322010-08-30 09:27:55 -0400243 } finally {
244 super.finalize();
245 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400246 }
247
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400248 public void addStorage(MtpStorage storage) {
249 mStorageMap.put(storage.getPath(), storage);
250 }
251
252 public void removeStorage(MtpStorage storage) {
253 mStorageMap.remove(storage.getPath());
254 }
255
Mike Lockwood775de952011-03-05 17:34:11 -0500256 private void initDeviceProperties(Context context) {
257 final String devicePropertiesName = "device-properties";
258 mDeviceProperties = context.getSharedPreferences(devicePropertiesName, Context.MODE_PRIVATE);
259 File databaseFile = context.getDatabasePath(devicePropertiesName);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400260
Mike Lockwood775de952011-03-05 17:34:11 -0500261 if (databaseFile.exists()) {
262 // for backward compatibility - read device properties from sqlite database
263 // and migrate them to shared prefs
264 SQLiteDatabase db = null;
265 Cursor c = null;
266 try {
267 db = context.openOrCreateDatabase("device-properties", Context.MODE_PRIVATE, null);
268 if (db != null) {
269 c = db.query("properties", new String[] { "_id", "code", "value" },
270 null, null, null, null, null);
271 if (c != null) {
272 SharedPreferences.Editor e = mDeviceProperties.edit();
273 while (c.moveToNext()) {
274 String name = c.getString(1);
275 String value = c.getString(2);
276 e.putString(name, value);
277 }
278 e.commit();
279 }
280 }
281 } catch (Exception e) {
282 Log.e(TAG, "failed to migrate device properties", e);
283 } finally {
284 if (c != null) c.close();
285 if (db != null) db.close();
286 }
jangwon.lee3ed02532013-07-22 19:52:48 +0900287 context.deleteDatabase(devicePropertiesName);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400288 }
289 }
290
Mike Lockwood73e56d92011-12-01 16:58:41 -0500291 // check to see if the path is contained in one of our storage subdirectories
292 // returns true if we have no special subdirectories
293 private boolean inStorageSubDirectory(String path) {
294 if (mSubDirectories == null) return true;
295 if (path == null) return false;
296
297 boolean allowed = false;
298 int pathLength = path.length();
299 for (int i = 0; i < mSubDirectories.length && !allowed; i++) {
300 String subdir = mSubDirectories[i];
301 int subdirLength = subdir.length();
302 if (subdirLength < pathLength &&
303 path.charAt(subdirLength) == '/' &&
304 path.startsWith(subdir)) {
305 allowed = true;
306 }
307 }
308 return allowed;
309 }
310
311 // check to see if the path matches one of our storage subdirectories
312 // returns true if we have no special subdirectories
313 private boolean isStorageSubDirectory(String path) {
314 if (mSubDirectories == null) return false;
315 for (int i = 0; i < mSubDirectories.length; i++) {
316 if (path.equals(mSubDirectories[i])) {
317 return true;
318 }
319 }
320 return false;
321 }
322
Marco Nelissen5f411692014-09-26 16:03:49 -0700323 // returns true if the path is in the storage root
324 private boolean inStorageRoot(String path) {
325 try {
326 File f = new File(path);
327 String canonical = f.getCanonicalPath();
Marco Nelissenc1fda122014-10-15 14:32:22 -0700328 for (String root: mStorageMap.keySet()) {
329 if (canonical.startsWith(root)) {
330 return true;
331 }
Marco Nelissen5f411692014-09-26 16:03:49 -0700332 }
333 } catch (IOException e) {
334 // ignore
335 }
336 return false;
337 }
338
Mike Lockwoodd815f792010-07-12 08:49:01 -0400339 private int beginSendObject(String path, int format, int parent,
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400340 int storageId, long size, long modified) {
Marco Nelissen5f411692014-09-26 16:03:49 -0700341 // if the path is outside of the storage root, do not allow access
342 if (!inStorageRoot(path)) {
343 Log.e(TAG, "attempt to put file outside of storage area: " + path);
344 return -1;
345 }
Mike Lockwood73e56d92011-12-01 16:58:41 -0500346 // if mSubDirectories is not null, do not allow copying files to any other locations
347 if (!inStorageSubDirectory(path)) return -1;
348
349 // make sure the object does not exist
Mike Lockwoodbafca212010-12-13 21:50:09 -0800350 if (path != null) {
351 Cursor c = null;
352 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700353 c = mMediaProvider.query(mObjectsUri, ID_PROJECTION, PATH_WHERE,
Jeff Brown75ea64f2012-01-25 19:37:13 -0800354 new String[] { path }, null, null);
Mike Lockwoodbafca212010-12-13 21:50:09 -0800355 if (c != null && c.getCount() > 0) {
356 Log.w(TAG, "file already exists in beginSendObject: " + path);
357 return -1;
358 }
359 } catch (RemoteException e) {
360 Log.e(TAG, "RemoteException in beginSendObject", e);
361 } finally {
362 if (c != null) {
363 c.close();
364 }
365 }
366 }
367
Mike Lockwood2837eef2010-08-31 16:25:12 -0400368 mDatabaseModified = true;
Mike Lockwoodd815f792010-07-12 08:49:01 -0400369 ContentValues values = new ContentValues();
Mike Lockwood3b2a62e2010-09-08 12:47:57 -0400370 values.put(Files.FileColumns.DATA, path);
371 values.put(Files.FileColumns.FORMAT, format);
372 values.put(Files.FileColumns.PARENT, parent);
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400373 values.put(Files.FileColumns.STORAGE_ID, storageId);
Mike Lockwood3b2a62e2010-09-08 12:47:57 -0400374 values.put(Files.FileColumns.SIZE, size);
375 values.put(Files.FileColumns.DATE_MODIFIED, modified);
Mike Lockwoodd815f792010-07-12 08:49:01 -0400376
377 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700378 Uri uri = mMediaProvider.insert(mObjectsUri, values);
Mike Lockwoodd815f792010-07-12 08:49:01 -0400379 if (uri != null) {
380 return Integer.parseInt(uri.getPathSegments().get(2));
381 } else {
382 return -1;
383 }
384 } catch (RemoteException e) {
385 Log.e(TAG, "RemoteException in beginSendObject", e);
386 return -1;
387 }
388 }
389
Mike Lockwood7a0bd172011-01-18 11:06:19 -0800390 private void endSendObject(String path, int handle, int format, boolean succeeded) {
Mike Lockwoodd815f792010-07-12 08:49:01 -0400391 if (succeeded) {
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400392 // handle abstract playlists separately
393 // they do not exist in the file system so don't use the media scanner here
Mike Lockwood5367ab62010-08-30 13:23:02 -0400394 if (format == MtpConstants.FORMAT_ABSTRACT_AV_PLAYLIST) {
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400395 // extract name from path
396 String name = path;
397 int lastSlash = name.lastIndexOf('/');
398 if (lastSlash >= 0) {
399 name = name.substring(lastSlash + 1);
400 }
Mike Lockwood8cc6eb12011-01-18 13:13:05 -0800401 // strip trailing ".pla" from the name
402 if (name.endsWith(".pla")) {
403 name = name.substring(0, name.length() - 4);
404 }
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400405
406 ContentValues values = new ContentValues(1);
407 values.put(Audio.Playlists.DATA, path);
408 values.put(Audio.Playlists.NAME, name);
Mike Lockwood0b58c192010-11-17 15:42:09 -0500409 values.put(Files.FileColumns.FORMAT, format);
Mike Lockwood8ed67ac2011-01-18 13:27:25 -0800410 values.put(Files.FileColumns.DATE_MODIFIED, System.currentTimeMillis() / 1000);
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400411 values.put(MediaColumns.MEDIA_SCANNER_NEW_OBJECT_ID, handle);
412 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700413 Uri uri = mMediaProvider.insert(
Dianne Hackborn35654b62013-01-14 17:38:02 -0800414 Audio.Playlists.EXTERNAL_CONTENT_URI, values);
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400415 } catch (RemoteException e) {
416 Log.e(TAG, "RemoteException in endSendObject", e);
417 }
418 } else {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700419 mMediaScanner.scanMtpFile(path, handle, format);
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400420 }
Mike Lockwoodd815f792010-07-12 08:49:01 -0400421 } else {
422 deleteFile(handle);
423 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400424 }
425
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400426 private Cursor createObjectQuery(int storageID, int format, int parent) throws RemoteException {
Mike Lockwood73e56d92011-12-01 16:58:41 -0500427 String where;
428 String[] whereArgs;
429
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400430 if (storageID == 0xFFFFFFFF) {
431 // query all stores
432 if (format == 0) {
433 // query all formats
434 if (parent == 0) {
435 // query all objects
Mike Lockwood73e56d92011-12-01 16:58:41 -0500436 where = null;
437 whereArgs = null;
438 } else {
439 if (parent == 0xFFFFFFFF) {
440 // all objects in root of store
441 parent = 0;
442 }
443 where = PARENT_WHERE;
444 whereArgs = new String[] { Integer.toString(parent) };
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400445 }
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400446 } else {
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400447 // query specific format
448 if (parent == 0) {
449 // query all objects
Mike Lockwood73e56d92011-12-01 16:58:41 -0500450 where = FORMAT_WHERE;
451 whereArgs = new String[] { Integer.toString(format) };
452 } else {
453 if (parent == 0xFFFFFFFF) {
454 // all objects in root of store
455 parent = 0;
456 }
457 where = FORMAT_PARENT_WHERE;
458 whereArgs = new String[] { Integer.toString(format),
459 Integer.toString(parent) };
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400460 }
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400461 }
462 } else {
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400463 // query specific store
464 if (format == 0) {
465 // query all formats
466 if (parent == 0) {
467 // query all objects
Mike Lockwood73e56d92011-12-01 16:58:41 -0500468 where = STORAGE_WHERE;
469 whereArgs = new String[] { Integer.toString(storageID) };
470 } else {
471 if (parent == 0xFFFFFFFF) {
472 // all objects in root of store
473 parent = 0;
474 }
475 where = STORAGE_PARENT_WHERE;
476 whereArgs = new String[] { Integer.toString(storageID),
477 Integer.toString(parent) };
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400478 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400479 } else {
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400480 // query specific format
481 if (parent == 0) {
482 // query all objects
Mike Lockwood73e56d92011-12-01 16:58:41 -0500483 where = STORAGE_FORMAT_WHERE;
484 whereArgs = new String[] { Integer.toString(storageID),
485 Integer.toString(format) };
486 } else {
487 if (parent == 0xFFFFFFFF) {
488 // all objects in root of store
489 parent = 0;
490 }
491 where = STORAGE_FORMAT_PARENT_WHERE;
492 whereArgs = new String[] { Integer.toString(storageID),
493 Integer.toString(format),
494 Integer.toString(parent) };
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400495 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400496 }
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400497 }
Mike Lockwood73e56d92011-12-01 16:58:41 -0500498
499 // if we are restricting queries to mSubDirectories, we need to add the restriction
500 // onto our "where" arguments
501 if (mSubDirectoriesWhere != null) {
502 if (where == null) {
503 where = mSubDirectoriesWhere;
504 whereArgs = mSubDirectoriesWhereArgs;
505 } else {
506 where = where + " AND " + mSubDirectoriesWhere;
507
508 // create new array to hold whereArgs and mSubDirectoriesWhereArgs
509 String[] newWhereArgs =
510 new String[whereArgs.length + mSubDirectoriesWhereArgs.length];
511 int i, j;
512 for (i = 0; i < whereArgs.length; i++) {
513 newWhereArgs[i] = whereArgs[i];
514 }
515 for (j = 0; j < mSubDirectoriesWhereArgs.length; i++, j++) {
516 newWhereArgs[i] = mSubDirectoriesWhereArgs[j];
517 }
518 whereArgs = newWhereArgs;
519 }
520 }
521
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700522 return mMediaProvider.query(mObjectsUri, ID_PROJECTION, where,
Dianne Hackborn35654b62013-01-14 17:38:02 -0800523 whereArgs, null, null);
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400524 }
525
526 private int[] getObjectList(int storageID, int format, int parent) {
527 Cursor c = null;
528 try {
529 c = createObjectQuery(storageID, format, parent);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400530 if (c == null) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400531 return null;
532 }
533 int count = c.getCount();
534 if (count > 0) {
535 int[] result = new int[count];
536 for (int i = 0; i < count; i++) {
537 c.moveToNext();
538 result[i] = c.getInt(0);
539 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400540 return result;
541 }
542 } catch (RemoteException e) {
543 Log.e(TAG, "RemoteException in getObjectList", e);
544 } finally {
545 if (c != null) {
546 c.close();
547 }
548 }
549 return null;
550 }
551
Mike Lockwood7a047c82010-08-02 10:52:20 -0400552 private int getNumObjects(int storageID, int format, int parent) {
Mike Lockwood7a047c82010-08-02 10:52:20 -0400553 Cursor c = null;
554 try {
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400555 c = createObjectQuery(storageID, format, parent);
Mike Lockwood7a047c82010-08-02 10:52:20 -0400556 if (c != null) {
557 return c.getCount();
558 }
559 } catch (RemoteException e) {
560 Log.e(TAG, "RemoteException in getNumObjects", e);
561 } finally {
562 if (c != null) {
563 c.close();
564 }
565 }
566 return -1;
567 }
568
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400569 private int[] getSupportedPlaybackFormats() {
570 return new int[] {
Mike Lockwoode5211692010-09-08 13:50:45 -0400571 // allow transfering arbitrary files
572 MtpConstants.FORMAT_UNDEFINED,
Mike Lockwood12b8a992010-09-23 21:33:29 -0400573
Mike Lockwood792ec842010-09-09 15:30:10 -0400574 MtpConstants.FORMAT_ASSOCIATION,
Mike Lockwood12b8a992010-09-23 21:33:29 -0400575 MtpConstants.FORMAT_TEXT,
576 MtpConstants.FORMAT_HTML,
577 MtpConstants.FORMAT_WAV,
578 MtpConstants.FORMAT_MP3,
579 MtpConstants.FORMAT_MPEG,
580 MtpConstants.FORMAT_EXIF_JPEG,
581 MtpConstants.FORMAT_TIFF_EP,
bo huang240582e2012-02-27 16:27:00 +0800582 MtpConstants.FORMAT_BMP,
Mike Lockwood12b8a992010-09-23 21:33:29 -0400583 MtpConstants.FORMAT_GIF,
584 MtpConstants.FORMAT_JFIF,
585 MtpConstants.FORMAT_PNG,
586 MtpConstants.FORMAT_TIFF,
587 MtpConstants.FORMAT_WMA,
588 MtpConstants.FORMAT_OGG,
589 MtpConstants.FORMAT_AAC,
590 MtpConstants.FORMAT_MP4_CONTAINER,
591 MtpConstants.FORMAT_MP2,
592 MtpConstants.FORMAT_3GP_CONTAINER,
Mike Lockwood792ec842010-09-09 15:30:10 -0400593 MtpConstants.FORMAT_ABSTRACT_AV_PLAYLIST,
Mike Lockwood12b8a992010-09-23 21:33:29 -0400594 MtpConstants.FORMAT_WPL_PLAYLIST,
595 MtpConstants.FORMAT_M3U_PLAYLIST,
596 MtpConstants.FORMAT_PLS_PLAYLIST,
597 MtpConstants.FORMAT_XML_DOCUMENT,
Glenn Kastenf9f223e2011-01-13 11:17:00 -0800598 MtpConstants.FORMAT_FLAC,
Jaesung Chung5a8b9622015-12-18 05:50:21 +0100599 MtpConstants.FORMAT_DNG,
Chong Zhang6e18cce2017-08-16 11:57:02 -0700600 MtpConstants.FORMAT_HEIF,
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400601 };
602 }
603
604 private int[] getSupportedCaptureFormats() {
605 // no capture formats yet
606 return null;
607 }
608
Mike Lockwoodae078f72010-09-26 12:35:51 -0400609 static final int[] FILE_PROPERTIES = {
610 // NOTE must match beginning of AUDIO_PROPERTIES, VIDEO_PROPERTIES
611 // and IMAGE_PROPERTIES below
Mike Lockwood5367ab62010-08-30 13:23:02 -0400612 MtpConstants.PROPERTY_STORAGE_ID,
613 MtpConstants.PROPERTY_OBJECT_FORMAT,
Mike Lockwoodd3bfecb2010-09-23 23:04:28 -0400614 MtpConstants.PROPERTY_PROTECTION_STATUS,
Mike Lockwood5367ab62010-08-30 13:23:02 -0400615 MtpConstants.PROPERTY_OBJECT_SIZE,
616 MtpConstants.PROPERTY_OBJECT_FILE_NAME,
Mike Lockwoodd3bfecb2010-09-23 23:04:28 -0400617 MtpConstants.PROPERTY_DATE_MODIFIED,
Mike Lockwood5367ab62010-08-30 13:23:02 -0400618 MtpConstants.PROPERTY_PARENT_OBJECT,
Mike Lockwoodd3bfecb2010-09-23 23:04:28 -0400619 MtpConstants.PROPERTY_PERSISTENT_UID,
620 MtpConstants.PROPERTY_NAME,
Mike Lockwood71827742015-01-23 10:50:08 -0800621 MtpConstants.PROPERTY_DISPLAY_NAME,
Mike Lockwoodae078f72010-09-26 12:35:51 -0400622 MtpConstants.PROPERTY_DATE_ADDED,
623 };
624
625 static final int[] AUDIO_PROPERTIES = {
626 // NOTE must match FILE_PROPERTIES above
627 MtpConstants.PROPERTY_STORAGE_ID,
628 MtpConstants.PROPERTY_OBJECT_FORMAT,
629 MtpConstants.PROPERTY_PROTECTION_STATUS,
630 MtpConstants.PROPERTY_OBJECT_SIZE,
631 MtpConstants.PROPERTY_OBJECT_FILE_NAME,
632 MtpConstants.PROPERTY_DATE_MODIFIED,
633 MtpConstants.PROPERTY_PARENT_OBJECT,
634 MtpConstants.PROPERTY_PERSISTENT_UID,
635 MtpConstants.PROPERTY_NAME,
636 MtpConstants.PROPERTY_DISPLAY_NAME,
637 MtpConstants.PROPERTY_DATE_ADDED,
638
639 // audio specific properties
640 MtpConstants.PROPERTY_ARTIST,
641 MtpConstants.PROPERTY_ALBUM_NAME,
642 MtpConstants.PROPERTY_ALBUM_ARTIST,
643 MtpConstants.PROPERTY_TRACK,
644 MtpConstants.PROPERTY_ORIGINAL_RELEASE_DATE,
645 MtpConstants.PROPERTY_DURATION,
646 MtpConstants.PROPERTY_GENRE,
647 MtpConstants.PROPERTY_COMPOSER,
Mike Lockwood92b53bc2014-03-13 14:51:29 -0700648 MtpConstants.PROPERTY_AUDIO_WAVE_CODEC,
649 MtpConstants.PROPERTY_BITRATE_TYPE,
650 MtpConstants.PROPERTY_AUDIO_BITRATE,
651 MtpConstants.PROPERTY_NUMBER_OF_CHANNELS,
652 MtpConstants.PROPERTY_SAMPLE_RATE,
Mike Lockwoodae078f72010-09-26 12:35:51 -0400653 };
654
655 static final int[] VIDEO_PROPERTIES = {
656 // NOTE must match FILE_PROPERTIES above
657 MtpConstants.PROPERTY_STORAGE_ID,
658 MtpConstants.PROPERTY_OBJECT_FORMAT,
659 MtpConstants.PROPERTY_PROTECTION_STATUS,
660 MtpConstants.PROPERTY_OBJECT_SIZE,
661 MtpConstants.PROPERTY_OBJECT_FILE_NAME,
662 MtpConstants.PROPERTY_DATE_MODIFIED,
663 MtpConstants.PROPERTY_PARENT_OBJECT,
664 MtpConstants.PROPERTY_PERSISTENT_UID,
665 MtpConstants.PROPERTY_NAME,
666 MtpConstants.PROPERTY_DISPLAY_NAME,
667 MtpConstants.PROPERTY_DATE_ADDED,
668
669 // video specific properties
670 MtpConstants.PROPERTY_ARTIST,
671 MtpConstants.PROPERTY_ALBUM_NAME,
672 MtpConstants.PROPERTY_DURATION,
673 MtpConstants.PROPERTY_DESCRIPTION,
674 };
675
676 static final int[] IMAGE_PROPERTIES = {
677 // NOTE must match FILE_PROPERTIES above
678 MtpConstants.PROPERTY_STORAGE_ID,
679 MtpConstants.PROPERTY_OBJECT_FORMAT,
680 MtpConstants.PROPERTY_PROTECTION_STATUS,
681 MtpConstants.PROPERTY_OBJECT_SIZE,
682 MtpConstants.PROPERTY_OBJECT_FILE_NAME,
683 MtpConstants.PROPERTY_DATE_MODIFIED,
684 MtpConstants.PROPERTY_PARENT_OBJECT,
685 MtpConstants.PROPERTY_PERSISTENT_UID,
686 MtpConstants.PROPERTY_NAME,
687 MtpConstants.PROPERTY_DISPLAY_NAME,
688 MtpConstants.PROPERTY_DATE_ADDED,
689
690 // image specific properties
691 MtpConstants.PROPERTY_DESCRIPTION,
692 };
693
694 private int[] getSupportedObjectProperties(int format) {
695 switch (format) {
696 case MtpConstants.FORMAT_MP3:
697 case MtpConstants.FORMAT_WAV:
698 case MtpConstants.FORMAT_WMA:
699 case MtpConstants.FORMAT_OGG:
700 case MtpConstants.FORMAT_AAC:
701 return AUDIO_PROPERTIES;
702 case MtpConstants.FORMAT_MPEG:
703 case MtpConstants.FORMAT_3GP_CONTAINER:
704 case MtpConstants.FORMAT_WMV:
705 return VIDEO_PROPERTIES;
706 case MtpConstants.FORMAT_EXIF_JPEG:
707 case MtpConstants.FORMAT_GIF:
708 case MtpConstants.FORMAT_PNG:
709 case MtpConstants.FORMAT_BMP:
Jaesung Chung5a8b9622015-12-18 05:50:21 +0100710 case MtpConstants.FORMAT_DNG:
Chong Zhang6e18cce2017-08-16 11:57:02 -0700711 case MtpConstants.FORMAT_HEIF:
Mike Lockwoodae078f72010-09-26 12:35:51 -0400712 return IMAGE_PROPERTIES;
713 default:
714 return FILE_PROPERTIES;
715 }
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400716 }
717
718 private int[] getSupportedDeviceProperties() {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400719 return new int[] {
720 MtpConstants.DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER,
721 MtpConstants.DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME,
Mike Lockwoodea93fa12010-12-07 10:41:35 -0800722 MtpConstants.DEVICE_PROPERTY_IMAGE_SIZE,
Mike Lockwood56c85242014-03-07 13:29:08 -0800723 MtpConstants.DEVICE_PROPERTY_BATTERY_LEVEL,
Jerry Zhang13bb2f42016-12-14 15:39:29 -0800724 MtpConstants.DEVICE_PROPERTY_PERCEIVED_DEVICE_TYPE,
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400725 };
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400726 }
727
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900728 private MtpPropertyList getObjectPropertyList(int handle, int format, int property,
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400729 int groupCode, int depth) {
730 // FIXME - implement group support
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400731 if (groupCode != 0) {
732 return new MtpPropertyList(0, MtpConstants.RESPONSE_SPECIFICATION_BY_GROUP_UNSUPPORTED);
733 }
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400734
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500735 MtpPropertyGroup propertyGroup;
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900736 if (property == 0xffffffff) {
737 if (format == 0 && handle != 0 && handle != 0xffffffff) {
Mike Lockwood71827742015-01-23 10:50:08 -0800738 // return properties based on the object's format
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900739 format = getObjectFormat(handle);
Mike Lockwood71827742015-01-23 10:50:08 -0800740 }
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900741 propertyGroup = mPropertyGroupsByFormat.get(format);
742 if (propertyGroup == null) {
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500743 int[] propertyList = getSupportedObjectProperties(format);
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700744 propertyGroup = new MtpPropertyGroup(this, mMediaProvider,
Dianne Hackborn35654b62013-01-14 17:38:02 -0800745 mVolumeName, propertyList);
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900746 mPropertyGroupsByFormat.put(format, propertyGroup);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400747 }
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500748 } else {
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900749 propertyGroup = mPropertyGroupsByProperty.get(property);
750 if (propertyGroup == null) {
751 final int[] propertyList = new int[] { property };
752 propertyGroup = new MtpPropertyGroup(
753 this, mMediaProvider, mVolumeName, propertyList);
754 mPropertyGroupsByProperty.put(property, propertyGroup);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400755 }
756 }
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500757
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900758 return propertyGroup.getPropertyList(handle, format, depth);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400759 }
760
Mike Lockwood5ebac832010-10-12 11:33:47 -0400761 private int renameFile(int handle, String newName) {
762 Cursor c = null;
763
764 // first compute current path
765 String path = null;
Mike Lockwood5ebac832010-10-12 11:33:47 -0400766 String[] whereArgs = new String[] { Integer.toString(handle) };
767 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700768 c = mMediaProvider.query(mObjectsUri, PATH_PROJECTION, ID_WHERE,
Dianne Hackborn35654b62013-01-14 17:38:02 -0800769 whereArgs, null, null);
Mike Lockwood5ebac832010-10-12 11:33:47 -0400770 if (c != null && c.moveToNext()) {
Mike Lockwood1c4e88d2011-01-12 12:38:41 -0500771 path = c.getString(1);
Mike Lockwood5ebac832010-10-12 11:33:47 -0400772 }
773 } catch (RemoteException e) {
774 Log.e(TAG, "RemoteException in getObjectFilePath", e);
775 return MtpConstants.RESPONSE_GENERAL_ERROR;
776 } finally {
777 if (c != null) {
778 c.close();
779 }
780 }
781 if (path == null) {
782 return MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE;
783 }
Mike Lockwood5ebac832010-10-12 11:33:47 -0400784
Mike Lockwood73e56d92011-12-01 16:58:41 -0500785 // do not allow renaming any of the special subdirectories
786 if (isStorageSubDirectory(path)) {
787 return MtpConstants.RESPONSE_OBJECT_WRITE_PROTECTED;
788 }
789
Mike Lockwood5ebac832010-10-12 11:33:47 -0400790 // now rename the file. make sure this succeeds before updating database
791 File oldFile = new File(path);
792 int lastSlash = path.lastIndexOf('/');
793 if (lastSlash <= 1) {
794 return MtpConstants.RESPONSE_GENERAL_ERROR;
795 }
796 String newPath = path.substring(0, lastSlash + 1) + newName;
797 File newFile = new File(newPath);
798 boolean success = oldFile.renameTo(newFile);
Mike Lockwood5ebac832010-10-12 11:33:47 -0400799 if (!success) {
Mike Lockwoodf26a5862011-01-21 21:00:54 -0800800 Log.w(TAG, "renaming "+ path + " to " + newPath + " failed");
Mike Lockwood5ebac832010-10-12 11:33:47 -0400801 return MtpConstants.RESPONSE_GENERAL_ERROR;
802 }
803
804 // finally update database
805 ContentValues values = new ContentValues();
806 values.put(Files.FileColumns.DATA, newPath);
807 int updated = 0;
808 try {
Mike Lockwood6a6a3af2010-10-12 14:19:51 -0400809 // note - we are relying on a special case in MediaProvider.update() to update
810 // the paths for all children in the case where this is a directory.
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700811 updated = mMediaProvider.update(mObjectsUri, values, ID_WHERE, whereArgs);
Mike Lockwood5ebac832010-10-12 11:33:47 -0400812 } catch (RemoteException e) {
813 Log.e(TAG, "RemoteException in mMediaProvider.update", e);
814 }
Mike Lockwood6a6a3af2010-10-12 14:19:51 -0400815 if (updated == 0) {
Mike Lockwood5ebac832010-10-12 11:33:47 -0400816 Log.e(TAG, "Unable to update path for " + path + " to " + newPath);
817 // this shouldn't happen, but if it does we need to rename the file to its original name
818 newFile.renameTo(oldFile);
819 return MtpConstants.RESPONSE_GENERAL_ERROR;
820 }
821
Marco Nelissenca78f3d2012-01-27 09:43:20 -0800822 // check if nomedia status changed
823 if (newFile.isDirectory()) {
824 // for directories, check if renamed from something hidden to something non-hidden
825 if (oldFile.getName().startsWith(".") && !newPath.startsWith(".")) {
826 // directory was unhidden
827 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700828 mMediaProvider.call(MediaStore.UNHIDE_CALL, newPath, null);
Marco Nelissenca78f3d2012-01-27 09:43:20 -0800829 } catch (RemoteException e) {
830 Log.e(TAG, "failed to unhide/rescan for " + newPath);
831 }
832 }
833 } else {
834 // for files, check if renamed from .nomedia to something else
835 if (oldFile.getName().toLowerCase(Locale.US).equals(".nomedia")
836 && !newPath.toLowerCase(Locale.US).equals(".nomedia")) {
837 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700838 mMediaProvider.call(MediaStore.UNHIDE_CALL, oldFile.getParent(), null);
Marco Nelissenca78f3d2012-01-27 09:43:20 -0800839 } catch (RemoteException e) {
840 Log.e(TAG, "failed to unhide/rescan for " + newPath);
841 }
842 }
843 }
844
Mike Lockwood5ebac832010-10-12 11:33:47 -0400845 return MtpConstants.RESPONSE_OK;
846 }
847
Jerry Zhang952558d2017-09-26 17:49:52 -0700848 private int moveObject(int handle, int newParent, String newPath) {
849 String[] whereArgs = new String[] { Integer.toString(handle) };
850
851 // do not allow renaming any of the special subdirectories
852 if (isStorageSubDirectory(newPath)) {
853 return MtpConstants.RESPONSE_OBJECT_WRITE_PROTECTED;
854 }
855
856 // update database
857 ContentValues values = new ContentValues();
858 values.put(Files.FileColumns.DATA, newPath);
859 values.put(Files.FileColumns.PARENT, newParent);
860 int updated = 0;
861 try {
862 // note - we are relying on a special case in MediaProvider.update() to update
863 // the paths for all children in the case where this is a directory.
864 updated = mMediaProvider.update(mObjectsUri, values, ID_WHERE, whereArgs);
865 } catch (RemoteException e) {
866 Log.e(TAG, "RemoteException in mMediaProvider.update", e);
867 }
868 if (updated == 0) {
869 Log.e(TAG, "Unable to update path for " + handle + " to " + newPath);
870 return MtpConstants.RESPONSE_GENERAL_ERROR;
871 }
872 return MtpConstants.RESPONSE_OK;
873 }
874
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400875 private int setObjectProperty(int handle, int property,
876 long intValue, String stringValue) {
Mike Lockwood5ebac832010-10-12 11:33:47 -0400877 switch (property) {
878 case MtpConstants.PROPERTY_OBJECT_FILE_NAME:
879 return renameFile(handle, stringValue);
880
881 default:
882 return MtpConstants.RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
883 }
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400884 }
885
886 private int getDeviceProperty(int property, long[] outIntValue, char[] outStringValue) {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400887 switch (property) {
888 case MtpConstants.DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER:
889 case MtpConstants.DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME:
Mike Lockwood775de952011-03-05 17:34:11 -0500890 // writable string properties kept in shared preferences
891 String value = mDeviceProperties.getString(Integer.toString(property), "");
892 int length = value.length();
893 if (length > 255) {
894 length = 255;
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400895 }
Mike Lockwood775de952011-03-05 17:34:11 -0500896 value.getChars(0, length, outStringValue, 0);
897 outStringValue[length] = 0;
898 return MtpConstants.RESPONSE_OK;
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400899
Mike Lockwoodea93fa12010-12-07 10:41:35 -0800900 case MtpConstants.DEVICE_PROPERTY_IMAGE_SIZE:
901 // use screen size as max image size
902 Display display = ((WindowManager)mContext.getSystemService(
903 Context.WINDOW_SERVICE)).getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -0700904 int width = display.getMaximumSizeDimension();
905 int height = display.getMaximumSizeDimension();
Mike Lockwoodea93fa12010-12-07 10:41:35 -0800906 String imageSize = Integer.toString(width) + "x" + Integer.toString(height);
907 imageSize.getChars(0, imageSize.length(), outStringValue, 0);
908 outStringValue[imageSize.length()] = 0;
909 return MtpConstants.RESPONSE_OK;
910
Jerry Zhang13bb2f42016-12-14 15:39:29 -0800911 case MtpConstants.DEVICE_PROPERTY_PERCEIVED_DEVICE_TYPE:
912 outIntValue[0] = mDeviceType;
913 return MtpConstants.RESPONSE_OK;
914
Mike Lockwood56c85242014-03-07 13:29:08 -0800915 // DEVICE_PROPERTY_BATTERY_LEVEL is implemented in the JNI code
916
Mike Lockwoodea93fa12010-12-07 10:41:35 -0800917 default:
918 return MtpConstants.RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
919 }
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400920 }
921
922 private int setDeviceProperty(int property, long intValue, String stringValue) {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400923 switch (property) {
924 case MtpConstants.DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER:
925 case MtpConstants.DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME:
Mike Lockwood775de952011-03-05 17:34:11 -0500926 // writable string properties kept in shared prefs
927 SharedPreferences.Editor e = mDeviceProperties.edit();
928 e.putString(Integer.toString(property), stringValue);
929 return (e.commit() ? MtpConstants.RESPONSE_OK
930 : MtpConstants.RESPONSE_GENERAL_ERROR);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400931 }
932
933 return MtpConstants.RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
934 }
935
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400936 private boolean getObjectInfo(int handle, int[] outStorageFormatParent,
Mike Lockwood1341f1e2013-04-01 10:52:47 -0700937 char[] outName, long[] outCreatedModified) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400938 Cursor c = null;
939 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700940 c = mMediaProvider.query(mObjectsUri, OBJECT_INFO_PROJECTION,
Jeff Brown75ea64f2012-01-25 19:37:13 -0800941 ID_WHERE, new String[] { Integer.toString(handle) }, null, null);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400942 if (c != null && c.moveToNext()) {
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400943 outStorageFormatParent[0] = c.getInt(1);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400944 outStorageFormatParent[1] = c.getInt(2);
945 outStorageFormatParent[2] = c.getInt(3);
946
947 // extract name from path
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400948 String path = c.getString(4);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400949 int lastSlash = path.lastIndexOf('/');
950 int start = (lastSlash >= 0 ? lastSlash + 1 : 0);
951 int end = path.length();
952 if (end - start > 255) {
953 end = start + 255;
954 }
955 path.getChars(start, end, outName, 0);
956 outName[end - start] = 0;
957
Mike Lockwood1341f1e2013-04-01 10:52:47 -0700958 outCreatedModified[0] = c.getLong(5);
959 outCreatedModified[1] = c.getLong(6);
960 // use modification date as creation date if date added is not set
961 if (outCreatedModified[0] == 0) {
962 outCreatedModified[0] = outCreatedModified[1];
963 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400964 return true;
965 }
966 } catch (RemoteException e) {
Mike Lockwood2b5f9ad2010-10-29 19:16:27 -0400967 Log.e(TAG, "RemoteException in getObjectInfo", e);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400968 } finally {
969 if (c != null) {
970 c.close();
971 }
972 }
973 return false;
974 }
975
Mike Lockwood365e03e2010-12-08 16:08:01 -0800976 private int getObjectFilePath(int handle, char[] outFilePath, long[] outFileLengthFormat) {
Mike Lockwood01788562010-10-11 11:22:19 -0400977 if (handle == 0) {
978 // special case root directory
979 mMediaStoragePath.getChars(0, mMediaStoragePath.length(), outFilePath, 0);
980 outFilePath[mMediaStoragePath.length()] = 0;
Mike Lockwood365e03e2010-12-08 16:08:01 -0800981 outFileLengthFormat[0] = 0;
982 outFileLengthFormat[1] = MtpConstants.FORMAT_ASSOCIATION;
Mike Lockwood01788562010-10-11 11:22:19 -0400983 return MtpConstants.RESPONSE_OK;
984 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400985 Cursor c = null;
986 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700987 c = mMediaProvider.query(mObjectsUri, PATH_FORMAT_PROJECTION,
Jeff Brown75ea64f2012-01-25 19:37:13 -0800988 ID_WHERE, new String[] { Integer.toString(handle) }, null, null);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400989 if (c != null && c.moveToNext()) {
Mike Lockwood1c4e88d2011-01-12 12:38:41 -0500990 String path = c.getString(1);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400991 path.getChars(0, path.length(), outFilePath, 0);
992 outFilePath[path.length()] = 0;
Mike Lockwoodf6f16612012-09-12 15:50:59 -0700993 // File transfers from device to host will likely fail if the size is incorrect.
994 // So to be safe, use the actual file size here.
995 outFileLengthFormat[0] = new File(path).length();
996 outFileLengthFormat[1] = c.getLong(2);
Mike Lockwood5367ab62010-08-30 13:23:02 -0400997 return MtpConstants.RESPONSE_OK;
Mike Lockwood59c777a2010-08-02 10:37:41 -0400998 } else {
Mike Lockwood5367ab62010-08-30 13:23:02 -0400999 return MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001000 }
1001 } catch (RemoteException e) {
1002 Log.e(TAG, "RemoteException in getObjectFilePath", e);
Mike Lockwood5367ab62010-08-30 13:23:02 -04001003 return MtpConstants.RESPONSE_GENERAL_ERROR;
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001004 } finally {
1005 if (c != null) {
1006 c.close();
1007 }
1008 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001009 }
1010
Mike Lockwood71827742015-01-23 10:50:08 -08001011 private int getObjectFormat(int handle) {
1012 Cursor c = null;
1013 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001014 c = mMediaProvider.query(mObjectsUri, FORMAT_PROJECTION,
Daichi Hirono486ad2e2016-02-29 17:28:47 +09001015 ID_WHERE, new String[] { Integer.toString(handle) }, null, null);
Mike Lockwood71827742015-01-23 10:50:08 -08001016 if (c != null && c.moveToNext()) {
1017 return c.getInt(1);
1018 } else {
1019 return -1;
1020 }
1021 } catch (RemoteException e) {
1022 Log.e(TAG, "RemoteException in getObjectFilePath", e);
1023 return -1;
1024 } finally {
1025 if (c != null) {
1026 c.close();
1027 }
1028 }
1029 }
1030
Mike Lockwood59c777a2010-08-02 10:37:41 -04001031 private int deleteFile(int handle) {
Mike Lockwood2837eef2010-08-31 16:25:12 -04001032 mDatabaseModified = true;
Mike Lockwood55f808c2010-12-14 13:14:29 -08001033 String path = null;
1034 int format = 0;
1035
1036 Cursor c = null;
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001037 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001038 c = mMediaProvider.query(mObjectsUri, PATH_FORMAT_PROJECTION,
Jeff Brown75ea64f2012-01-25 19:37:13 -08001039 ID_WHERE, new String[] { Integer.toString(handle) }, null, null);
Mike Lockwood55f808c2010-12-14 13:14:29 -08001040 if (c != null && c.moveToNext()) {
1041 // don't convert to media path here, since we will be matching
1042 // against paths in the database matching /data/media
1043 path = c.getString(1);
Mike Lockwoodf6f16612012-09-12 15:50:59 -07001044 format = c.getInt(2);
Mike Lockwood55f808c2010-12-14 13:14:29 -08001045 } else {
1046 return MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE;
1047 }
1048
1049 if (path == null || format == 0) {
1050 return MtpConstants.RESPONSE_GENERAL_ERROR;
1051 }
1052
Mike Lockwood73e56d92011-12-01 16:58:41 -05001053 // do not allow deleting any of the special subdirectories
1054 if (isStorageSubDirectory(path)) {
1055 return MtpConstants.RESPONSE_OBJECT_WRITE_PROTECTED;
1056 }
1057
Mike Lockwood55f808c2010-12-14 13:14:29 -08001058 if (format == MtpConstants.FORMAT_ASSOCIATION) {
1059 // recursive case - delete all children first
1060 Uri uri = Files.getMtpObjectsUri(mVolumeName);
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001061 int count = mMediaProvider.delete(uri,
Mike Lockwood1e855d92012-06-26 16:31:41 -07001062 // the 'like' makes it use the index, the 'lower()' makes it correct
1063 // when the path contains sqlite wildcard characters
1064 "_data LIKE ?1 AND lower(substr(_data,1,?2))=lower(?3)",
1065 new String[] { path + "/%",Integer.toString(path.length() + 1), path + "/"});
Mike Lockwood55f808c2010-12-14 13:14:29 -08001066 }
1067
1068 Uri uri = Files.getMtpObjectsUri(mVolumeName, handle);
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001069 if (mMediaProvider.delete(uri, null, null) > 0) {
Marco Nelissenca78f3d2012-01-27 09:43:20 -08001070 if (format != MtpConstants.FORMAT_ASSOCIATION
1071 && path.toLowerCase(Locale.US).endsWith("/.nomedia")) {
1072 try {
1073 String parentPath = path.substring(0, path.lastIndexOf("/"));
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001074 mMediaProvider.call(MediaStore.UNHIDE_CALL, parentPath, null);
Marco Nelissenca78f3d2012-01-27 09:43:20 -08001075 } catch (RemoteException e) {
1076 Log.e(TAG, "failed to unhide/rescan for " + path);
1077 }
1078 }
Mike Lockwood5367ab62010-08-30 13:23:02 -04001079 return MtpConstants.RESPONSE_OK;
Mike Lockwood59c777a2010-08-02 10:37:41 -04001080 } else {
Mike Lockwood5367ab62010-08-30 13:23:02 -04001081 return MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood59c777a2010-08-02 10:37:41 -04001082 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001083 } catch (RemoteException e) {
1084 Log.e(TAG, "RemoteException in deleteFile", e);
Mike Lockwood5367ab62010-08-30 13:23:02 -04001085 return MtpConstants.RESPONSE_GENERAL_ERROR;
Mike Lockwood55f808c2010-12-14 13:14:29 -08001086 } finally {
1087 if (c != null) {
1088 c.close();
1089 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001090 }
1091 }
1092
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001093 private int[] getObjectReferences(int handle) {
Mike Lockwood8490e662010-09-09 14:16:22 -04001094 Uri uri = Files.getMtpReferencesUri(mVolumeName, handle);
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001095 Cursor c = null;
1096 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001097 c = mMediaProvider.query(uri, ID_PROJECTION, null, null, null, null);
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001098 if (c == null) {
1099 return null;
1100 }
1101 int count = c.getCount();
1102 if (count > 0) {
1103 int[] result = new int[count];
1104 for (int i = 0; i < count; i++) {
1105 c.moveToNext();
1106 result[i] = c.getInt(0);
1107 }
1108 return result;
1109 }
1110 } catch (RemoteException e) {
1111 Log.e(TAG, "RemoteException in getObjectList", e);
1112 } finally {
1113 if (c != null) {
1114 c.close();
1115 }
1116 }
1117 return null;
1118 }
1119
1120 private int setObjectReferences(int handle, int[] references) {
Mike Lockwood2837eef2010-08-31 16:25:12 -04001121 mDatabaseModified = true;
Mike Lockwood8490e662010-09-09 14:16:22 -04001122 Uri uri = Files.getMtpReferencesUri(mVolumeName, handle);
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001123 int count = references.length;
1124 ContentValues[] valuesList = new ContentValues[count];
1125 for (int i = 0; i < count; i++) {
1126 ContentValues values = new ContentValues();
Mike Lockwood3b2a62e2010-09-08 12:47:57 -04001127 values.put(Files.FileColumns._ID, references[i]);
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001128 valuesList[i] = values;
1129 }
1130 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001131 if (mMediaProvider.bulkInsert(uri, valuesList) > 0) {
Mike Lockwood5367ab62010-08-30 13:23:02 -04001132 return MtpConstants.RESPONSE_OK;
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001133 }
1134 } catch (RemoteException e) {
1135 Log.e(TAG, "RemoteException in setObjectReferences", e);
1136 }
Mike Lockwood5367ab62010-08-30 13:23:02 -04001137 return MtpConstants.RESPONSE_GENERAL_ERROR;
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001138 }
1139
Mike Lockwood2837eef2010-08-31 16:25:12 -04001140 private void sessionStarted() {
Mike Lockwood2837eef2010-08-31 16:25:12 -04001141 mDatabaseModified = false;
1142 }
1143
1144 private void sessionEnded() {
Mike Lockwood2837eef2010-08-31 16:25:12 -04001145 if (mDatabaseModified) {
Jerry Zhang5f0139d2017-08-22 17:42:54 -07001146 mUserContext.sendBroadcast(new Intent(MediaStore.ACTION_MTP_SESSION_END));
Mike Lockwood2837eef2010-08-31 16:25:12 -04001147 mDatabaseModified = false;
1148 }
1149 }
1150
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001151 // used by the JNI code
Ashok Bhate2e59322013-12-17 19:04:19 +00001152 private long mNativeContext;
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001153
1154 private native final void native_setup();
1155 private native final void native_finalize();
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001156}