blob: adcd79bc8bcfc7afdf567e8be79d5cddbe962459 [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 {
Narayan Kamath492e9e82017-03-22 14:28:08 +0000236 if (mCloseGuard != null) {
237 mCloseGuard.warnIfOpen();
238 }
239
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700240 close();
Mike Lockwooddbead322010-08-30 09:27:55 -0400241 } finally {
242 super.finalize();
243 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400244 }
245
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400246 public void addStorage(MtpStorage storage) {
247 mStorageMap.put(storage.getPath(), storage);
248 }
249
250 public void removeStorage(MtpStorage storage) {
251 mStorageMap.remove(storage.getPath());
252 }
253
Mike Lockwood775de952011-03-05 17:34:11 -0500254 private void initDeviceProperties(Context context) {
255 final String devicePropertiesName = "device-properties";
256 mDeviceProperties = context.getSharedPreferences(devicePropertiesName, Context.MODE_PRIVATE);
257 File databaseFile = context.getDatabasePath(devicePropertiesName);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400258
Mike Lockwood775de952011-03-05 17:34:11 -0500259 if (databaseFile.exists()) {
260 // for backward compatibility - read device properties from sqlite database
261 // and migrate them to shared prefs
262 SQLiteDatabase db = null;
263 Cursor c = null;
264 try {
265 db = context.openOrCreateDatabase("device-properties", Context.MODE_PRIVATE, null);
266 if (db != null) {
267 c = db.query("properties", new String[] { "_id", "code", "value" },
268 null, null, null, null, null);
269 if (c != null) {
270 SharedPreferences.Editor e = mDeviceProperties.edit();
271 while (c.moveToNext()) {
272 String name = c.getString(1);
273 String value = c.getString(2);
274 e.putString(name, value);
275 }
276 e.commit();
277 }
278 }
279 } catch (Exception e) {
280 Log.e(TAG, "failed to migrate device properties", e);
281 } finally {
282 if (c != null) c.close();
283 if (db != null) db.close();
284 }
jangwon.lee3ed02532013-07-22 19:52:48 +0900285 context.deleteDatabase(devicePropertiesName);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400286 }
287 }
288
Mike Lockwood73e56d92011-12-01 16:58:41 -0500289 // check to see if the path is contained in one of our storage subdirectories
290 // returns true if we have no special subdirectories
291 private boolean inStorageSubDirectory(String path) {
292 if (mSubDirectories == null) return true;
293 if (path == null) return false;
294
295 boolean allowed = false;
296 int pathLength = path.length();
297 for (int i = 0; i < mSubDirectories.length && !allowed; i++) {
298 String subdir = mSubDirectories[i];
299 int subdirLength = subdir.length();
300 if (subdirLength < pathLength &&
301 path.charAt(subdirLength) == '/' &&
302 path.startsWith(subdir)) {
303 allowed = true;
304 }
305 }
306 return allowed;
307 }
308
309 // check to see if the path matches one of our storage subdirectories
310 // returns true if we have no special subdirectories
311 private boolean isStorageSubDirectory(String path) {
312 if (mSubDirectories == null) return false;
313 for (int i = 0; i < mSubDirectories.length; i++) {
314 if (path.equals(mSubDirectories[i])) {
315 return true;
316 }
317 }
318 return false;
319 }
320
Marco Nelissen5f411692014-09-26 16:03:49 -0700321 // returns true if the path is in the storage root
322 private boolean inStorageRoot(String path) {
323 try {
324 File f = new File(path);
325 String canonical = f.getCanonicalPath();
Marco Nelissenc1fda122014-10-15 14:32:22 -0700326 for (String root: mStorageMap.keySet()) {
327 if (canonical.startsWith(root)) {
328 return true;
329 }
Marco Nelissen5f411692014-09-26 16:03:49 -0700330 }
331 } catch (IOException e) {
332 // ignore
333 }
334 return false;
335 }
336
Mike Lockwoodd815f792010-07-12 08:49:01 -0400337 private int beginSendObject(String path, int format, int parent,
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400338 int storageId, long size, long modified) {
Marco Nelissen5f411692014-09-26 16:03:49 -0700339 // if the path is outside of the storage root, do not allow access
340 if (!inStorageRoot(path)) {
341 Log.e(TAG, "attempt to put file outside of storage area: " + path);
342 return -1;
343 }
Mike Lockwood73e56d92011-12-01 16:58:41 -0500344 // if mSubDirectories is not null, do not allow copying files to any other locations
345 if (!inStorageSubDirectory(path)) return -1;
346
347 // make sure the object does not exist
Mike Lockwoodbafca212010-12-13 21:50:09 -0800348 if (path != null) {
349 Cursor c = null;
350 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700351 c = mMediaProvider.query(mObjectsUri, ID_PROJECTION, PATH_WHERE,
Jeff Brown75ea64f2012-01-25 19:37:13 -0800352 new String[] { path }, null, null);
Mike Lockwoodbafca212010-12-13 21:50:09 -0800353 if (c != null && c.getCount() > 0) {
354 Log.w(TAG, "file already exists in beginSendObject: " + path);
355 return -1;
356 }
357 } catch (RemoteException e) {
358 Log.e(TAG, "RemoteException in beginSendObject", e);
359 } finally {
360 if (c != null) {
361 c.close();
362 }
363 }
364 }
365
Mike Lockwood2837eef2010-08-31 16:25:12 -0400366 mDatabaseModified = true;
Mike Lockwoodd815f792010-07-12 08:49:01 -0400367 ContentValues values = new ContentValues();
Mike Lockwood3b2a62e2010-09-08 12:47:57 -0400368 values.put(Files.FileColumns.DATA, path);
369 values.put(Files.FileColumns.FORMAT, format);
370 values.put(Files.FileColumns.PARENT, parent);
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400371 values.put(Files.FileColumns.STORAGE_ID, storageId);
Mike Lockwood3b2a62e2010-09-08 12:47:57 -0400372 values.put(Files.FileColumns.SIZE, size);
373 values.put(Files.FileColumns.DATE_MODIFIED, modified);
Mike Lockwoodd815f792010-07-12 08:49:01 -0400374
375 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700376 Uri uri = mMediaProvider.insert(mObjectsUri, values);
Mike Lockwoodd815f792010-07-12 08:49:01 -0400377 if (uri != null) {
378 return Integer.parseInt(uri.getPathSegments().get(2));
379 } else {
380 return -1;
381 }
382 } catch (RemoteException e) {
383 Log.e(TAG, "RemoteException in beginSendObject", e);
384 return -1;
385 }
386 }
387
Mike Lockwood7a0bd172011-01-18 11:06:19 -0800388 private void endSendObject(String path, int handle, int format, boolean succeeded) {
Mike Lockwoodd815f792010-07-12 08:49:01 -0400389 if (succeeded) {
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400390 // handle abstract playlists separately
391 // they do not exist in the file system so don't use the media scanner here
Mike Lockwood5367ab62010-08-30 13:23:02 -0400392 if (format == MtpConstants.FORMAT_ABSTRACT_AV_PLAYLIST) {
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400393 // extract name from path
394 String name = path;
395 int lastSlash = name.lastIndexOf('/');
396 if (lastSlash >= 0) {
397 name = name.substring(lastSlash + 1);
398 }
Mike Lockwood8cc6eb12011-01-18 13:13:05 -0800399 // strip trailing ".pla" from the name
400 if (name.endsWith(".pla")) {
401 name = name.substring(0, name.length() - 4);
402 }
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400403
404 ContentValues values = new ContentValues(1);
405 values.put(Audio.Playlists.DATA, path);
406 values.put(Audio.Playlists.NAME, name);
Mike Lockwood0b58c192010-11-17 15:42:09 -0500407 values.put(Files.FileColumns.FORMAT, format);
Mike Lockwood8ed67ac2011-01-18 13:27:25 -0800408 values.put(Files.FileColumns.DATE_MODIFIED, System.currentTimeMillis() / 1000);
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400409 values.put(MediaColumns.MEDIA_SCANNER_NEW_OBJECT_ID, handle);
410 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700411 Uri uri = mMediaProvider.insert(
Dianne Hackborn35654b62013-01-14 17:38:02 -0800412 Audio.Playlists.EXTERNAL_CONTENT_URI, values);
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400413 } catch (RemoteException e) {
414 Log.e(TAG, "RemoteException in endSendObject", e);
415 }
416 } else {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700417 mMediaScanner.scanMtpFile(path, handle, format);
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400418 }
Mike Lockwoodd815f792010-07-12 08:49:01 -0400419 } else {
420 deleteFile(handle);
421 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400422 }
423
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400424 private Cursor createObjectQuery(int storageID, int format, int parent) throws RemoteException {
Mike Lockwood73e56d92011-12-01 16:58:41 -0500425 String where;
426 String[] whereArgs;
427
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400428 if (storageID == 0xFFFFFFFF) {
429 // query all stores
430 if (format == 0) {
431 // query all formats
432 if (parent == 0) {
433 // query all objects
Mike Lockwood73e56d92011-12-01 16:58:41 -0500434 where = null;
435 whereArgs = null;
436 } else {
437 if (parent == 0xFFFFFFFF) {
438 // all objects in root of store
439 parent = 0;
440 }
441 where = PARENT_WHERE;
442 whereArgs = new String[] { Integer.toString(parent) };
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400443 }
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400444 } else {
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400445 // query specific format
446 if (parent == 0) {
447 // query all objects
Mike Lockwood73e56d92011-12-01 16:58:41 -0500448 where = FORMAT_WHERE;
449 whereArgs = new String[] { Integer.toString(format) };
450 } else {
451 if (parent == 0xFFFFFFFF) {
452 // all objects in root of store
453 parent = 0;
454 }
455 where = FORMAT_PARENT_WHERE;
456 whereArgs = new String[] { Integer.toString(format),
457 Integer.toString(parent) };
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400458 }
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400459 }
460 } else {
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400461 // query specific store
462 if (format == 0) {
463 // query all formats
464 if (parent == 0) {
465 // query all objects
Mike Lockwood73e56d92011-12-01 16:58:41 -0500466 where = STORAGE_WHERE;
467 whereArgs = new String[] { Integer.toString(storageID) };
468 } else {
469 if (parent == 0xFFFFFFFF) {
470 // all objects in root of store
471 parent = 0;
Jerry Zhangdef7b1932017-10-17 13:47:51 -0700472 where = STORAGE_PARENT_WHERE;
473 whereArgs = new String[]{Integer.toString(storageID),
474 Integer.toString(parent)};
475 } else {
476 // If a parent is specified, the storage is redundant
477 where = PARENT_WHERE;
478 whereArgs = new String[]{Integer.toString(parent)};
Mike Lockwood73e56d92011-12-01 16:58:41 -0500479 }
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400480 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400481 } else {
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400482 // query specific format
483 if (parent == 0) {
484 // query all objects
Mike Lockwood73e56d92011-12-01 16:58:41 -0500485 where = STORAGE_FORMAT_WHERE;
486 whereArgs = new String[] { Integer.toString(storageID),
487 Integer.toString(format) };
488 } else {
489 if (parent == 0xFFFFFFFF) {
490 // all objects in root of store
491 parent = 0;
Jerry Zhangdef7b1932017-10-17 13:47:51 -0700492 where = STORAGE_FORMAT_PARENT_WHERE;
493 whereArgs = new String[]{Integer.toString(storageID),
494 Integer.toString(format),
495 Integer.toString(parent)};
496 } else {
497 // If a parent is specified, the storage is redundant
498 where = FORMAT_PARENT_WHERE;
499 whereArgs = new String[]{Integer.toString(format),
500 Integer.toString(parent)};
Mike Lockwood73e56d92011-12-01 16:58:41 -0500501 }
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400502 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400503 }
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400504 }
Mike Lockwood73e56d92011-12-01 16:58:41 -0500505
506 // if we are restricting queries to mSubDirectories, we need to add the restriction
507 // onto our "where" arguments
508 if (mSubDirectoriesWhere != null) {
509 if (where == null) {
510 where = mSubDirectoriesWhere;
511 whereArgs = mSubDirectoriesWhereArgs;
512 } else {
513 where = where + " AND " + mSubDirectoriesWhere;
514
515 // create new array to hold whereArgs and mSubDirectoriesWhereArgs
516 String[] newWhereArgs =
517 new String[whereArgs.length + mSubDirectoriesWhereArgs.length];
518 int i, j;
519 for (i = 0; i < whereArgs.length; i++) {
520 newWhereArgs[i] = whereArgs[i];
521 }
522 for (j = 0; j < mSubDirectoriesWhereArgs.length; i++, j++) {
523 newWhereArgs[i] = mSubDirectoriesWhereArgs[j];
524 }
525 whereArgs = newWhereArgs;
526 }
527 }
528
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700529 return mMediaProvider.query(mObjectsUri, ID_PROJECTION, where,
Dianne Hackborn35654b62013-01-14 17:38:02 -0800530 whereArgs, null, null);
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400531 }
532
533 private int[] getObjectList(int storageID, int format, int parent) {
534 Cursor c = null;
535 try {
536 c = createObjectQuery(storageID, format, parent);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400537 if (c == null) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400538 return null;
539 }
540 int count = c.getCount();
541 if (count > 0) {
542 int[] result = new int[count];
543 for (int i = 0; i < count; i++) {
544 c.moveToNext();
545 result[i] = c.getInt(0);
546 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400547 return result;
548 }
549 } catch (RemoteException e) {
550 Log.e(TAG, "RemoteException in getObjectList", e);
551 } finally {
552 if (c != null) {
553 c.close();
554 }
555 }
556 return null;
557 }
558
Mike Lockwood7a047c82010-08-02 10:52:20 -0400559 private int getNumObjects(int storageID, int format, int parent) {
Mike Lockwood7a047c82010-08-02 10:52:20 -0400560 Cursor c = null;
561 try {
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400562 c = createObjectQuery(storageID, format, parent);
Mike Lockwood7a047c82010-08-02 10:52:20 -0400563 if (c != null) {
564 return c.getCount();
565 }
566 } catch (RemoteException e) {
567 Log.e(TAG, "RemoteException in getNumObjects", e);
568 } finally {
569 if (c != null) {
570 c.close();
571 }
572 }
573 return -1;
574 }
575
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400576 private int[] getSupportedPlaybackFormats() {
577 return new int[] {
Mike Lockwoode5211692010-09-08 13:50:45 -0400578 // allow transfering arbitrary files
579 MtpConstants.FORMAT_UNDEFINED,
Mike Lockwood12b8a992010-09-23 21:33:29 -0400580
Mike Lockwood792ec842010-09-09 15:30:10 -0400581 MtpConstants.FORMAT_ASSOCIATION,
Mike Lockwood12b8a992010-09-23 21:33:29 -0400582 MtpConstants.FORMAT_TEXT,
583 MtpConstants.FORMAT_HTML,
584 MtpConstants.FORMAT_WAV,
585 MtpConstants.FORMAT_MP3,
586 MtpConstants.FORMAT_MPEG,
587 MtpConstants.FORMAT_EXIF_JPEG,
588 MtpConstants.FORMAT_TIFF_EP,
bo huang240582e2012-02-27 16:27:00 +0800589 MtpConstants.FORMAT_BMP,
Mike Lockwood12b8a992010-09-23 21:33:29 -0400590 MtpConstants.FORMAT_GIF,
591 MtpConstants.FORMAT_JFIF,
592 MtpConstants.FORMAT_PNG,
593 MtpConstants.FORMAT_TIFF,
594 MtpConstants.FORMAT_WMA,
595 MtpConstants.FORMAT_OGG,
596 MtpConstants.FORMAT_AAC,
597 MtpConstants.FORMAT_MP4_CONTAINER,
598 MtpConstants.FORMAT_MP2,
599 MtpConstants.FORMAT_3GP_CONTAINER,
Mike Lockwood792ec842010-09-09 15:30:10 -0400600 MtpConstants.FORMAT_ABSTRACT_AV_PLAYLIST,
Mike Lockwood12b8a992010-09-23 21:33:29 -0400601 MtpConstants.FORMAT_WPL_PLAYLIST,
602 MtpConstants.FORMAT_M3U_PLAYLIST,
603 MtpConstants.FORMAT_PLS_PLAYLIST,
604 MtpConstants.FORMAT_XML_DOCUMENT,
Glenn Kastenf9f223e2011-01-13 11:17:00 -0800605 MtpConstants.FORMAT_FLAC,
Jaesung Chung5a8b9622015-12-18 05:50:21 +0100606 MtpConstants.FORMAT_DNG,
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400607 };
608 }
609
610 private int[] getSupportedCaptureFormats() {
611 // no capture formats yet
612 return null;
613 }
614
Mike Lockwoodae078f72010-09-26 12:35:51 -0400615 static final int[] FILE_PROPERTIES = {
616 // NOTE must match beginning of AUDIO_PROPERTIES, VIDEO_PROPERTIES
617 // and IMAGE_PROPERTIES below
Mike Lockwood5367ab62010-08-30 13:23:02 -0400618 MtpConstants.PROPERTY_STORAGE_ID,
619 MtpConstants.PROPERTY_OBJECT_FORMAT,
Mike Lockwoodd3bfecb2010-09-23 23:04:28 -0400620 MtpConstants.PROPERTY_PROTECTION_STATUS,
Mike Lockwood5367ab62010-08-30 13:23:02 -0400621 MtpConstants.PROPERTY_OBJECT_SIZE,
622 MtpConstants.PROPERTY_OBJECT_FILE_NAME,
Mike Lockwoodd3bfecb2010-09-23 23:04:28 -0400623 MtpConstants.PROPERTY_DATE_MODIFIED,
Mike Lockwood5367ab62010-08-30 13:23:02 -0400624 MtpConstants.PROPERTY_PARENT_OBJECT,
Mike Lockwoodd3bfecb2010-09-23 23:04:28 -0400625 MtpConstants.PROPERTY_PERSISTENT_UID,
626 MtpConstants.PROPERTY_NAME,
Mike Lockwood71827742015-01-23 10:50:08 -0800627 MtpConstants.PROPERTY_DISPLAY_NAME,
Mike Lockwoodae078f72010-09-26 12:35:51 -0400628 MtpConstants.PROPERTY_DATE_ADDED,
629 };
630
631 static final int[] AUDIO_PROPERTIES = {
632 // NOTE must match FILE_PROPERTIES above
633 MtpConstants.PROPERTY_STORAGE_ID,
634 MtpConstants.PROPERTY_OBJECT_FORMAT,
635 MtpConstants.PROPERTY_PROTECTION_STATUS,
636 MtpConstants.PROPERTY_OBJECT_SIZE,
637 MtpConstants.PROPERTY_OBJECT_FILE_NAME,
638 MtpConstants.PROPERTY_DATE_MODIFIED,
639 MtpConstants.PROPERTY_PARENT_OBJECT,
640 MtpConstants.PROPERTY_PERSISTENT_UID,
641 MtpConstants.PROPERTY_NAME,
642 MtpConstants.PROPERTY_DISPLAY_NAME,
643 MtpConstants.PROPERTY_DATE_ADDED,
644
645 // audio specific properties
646 MtpConstants.PROPERTY_ARTIST,
647 MtpConstants.PROPERTY_ALBUM_NAME,
648 MtpConstants.PROPERTY_ALBUM_ARTIST,
649 MtpConstants.PROPERTY_TRACK,
650 MtpConstants.PROPERTY_ORIGINAL_RELEASE_DATE,
651 MtpConstants.PROPERTY_DURATION,
652 MtpConstants.PROPERTY_GENRE,
653 MtpConstants.PROPERTY_COMPOSER,
Mike Lockwood92b53bc2014-03-13 14:51:29 -0700654 MtpConstants.PROPERTY_AUDIO_WAVE_CODEC,
655 MtpConstants.PROPERTY_BITRATE_TYPE,
656 MtpConstants.PROPERTY_AUDIO_BITRATE,
657 MtpConstants.PROPERTY_NUMBER_OF_CHANNELS,
658 MtpConstants.PROPERTY_SAMPLE_RATE,
Mike Lockwoodae078f72010-09-26 12:35:51 -0400659 };
660
661 static final int[] VIDEO_PROPERTIES = {
662 // NOTE must match FILE_PROPERTIES above
663 MtpConstants.PROPERTY_STORAGE_ID,
664 MtpConstants.PROPERTY_OBJECT_FORMAT,
665 MtpConstants.PROPERTY_PROTECTION_STATUS,
666 MtpConstants.PROPERTY_OBJECT_SIZE,
667 MtpConstants.PROPERTY_OBJECT_FILE_NAME,
668 MtpConstants.PROPERTY_DATE_MODIFIED,
669 MtpConstants.PROPERTY_PARENT_OBJECT,
670 MtpConstants.PROPERTY_PERSISTENT_UID,
671 MtpConstants.PROPERTY_NAME,
672 MtpConstants.PROPERTY_DISPLAY_NAME,
673 MtpConstants.PROPERTY_DATE_ADDED,
674
675 // video specific properties
676 MtpConstants.PROPERTY_ARTIST,
677 MtpConstants.PROPERTY_ALBUM_NAME,
678 MtpConstants.PROPERTY_DURATION,
679 MtpConstants.PROPERTY_DESCRIPTION,
680 };
681
682 static final int[] IMAGE_PROPERTIES = {
683 // NOTE must match FILE_PROPERTIES above
684 MtpConstants.PROPERTY_STORAGE_ID,
685 MtpConstants.PROPERTY_OBJECT_FORMAT,
686 MtpConstants.PROPERTY_PROTECTION_STATUS,
687 MtpConstants.PROPERTY_OBJECT_SIZE,
688 MtpConstants.PROPERTY_OBJECT_FILE_NAME,
689 MtpConstants.PROPERTY_DATE_MODIFIED,
690 MtpConstants.PROPERTY_PARENT_OBJECT,
691 MtpConstants.PROPERTY_PERSISTENT_UID,
692 MtpConstants.PROPERTY_NAME,
693 MtpConstants.PROPERTY_DISPLAY_NAME,
694 MtpConstants.PROPERTY_DATE_ADDED,
695
696 // image specific properties
697 MtpConstants.PROPERTY_DESCRIPTION,
698 };
699
700 private int[] getSupportedObjectProperties(int format) {
701 switch (format) {
702 case MtpConstants.FORMAT_MP3:
703 case MtpConstants.FORMAT_WAV:
704 case MtpConstants.FORMAT_WMA:
705 case MtpConstants.FORMAT_OGG:
706 case MtpConstants.FORMAT_AAC:
707 return AUDIO_PROPERTIES;
708 case MtpConstants.FORMAT_MPEG:
709 case MtpConstants.FORMAT_3GP_CONTAINER:
710 case MtpConstants.FORMAT_WMV:
711 return VIDEO_PROPERTIES;
712 case MtpConstants.FORMAT_EXIF_JPEG:
713 case MtpConstants.FORMAT_GIF:
714 case MtpConstants.FORMAT_PNG:
715 case MtpConstants.FORMAT_BMP:
Jaesung Chung5a8b9622015-12-18 05:50:21 +0100716 case MtpConstants.FORMAT_DNG:
Mike Lockwoodae078f72010-09-26 12:35:51 -0400717 return IMAGE_PROPERTIES;
718 default:
719 return FILE_PROPERTIES;
720 }
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400721 }
722
723 private int[] getSupportedDeviceProperties() {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400724 return new int[] {
725 MtpConstants.DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER,
726 MtpConstants.DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME,
Mike Lockwoodea93fa12010-12-07 10:41:35 -0800727 MtpConstants.DEVICE_PROPERTY_IMAGE_SIZE,
Mike Lockwood56c85242014-03-07 13:29:08 -0800728 MtpConstants.DEVICE_PROPERTY_BATTERY_LEVEL,
Jerry Zhang13bb2f42016-12-14 15:39:29 -0800729 MtpConstants.DEVICE_PROPERTY_PERCEIVED_DEVICE_TYPE,
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400730 };
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400731 }
732
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900733 private MtpPropertyList getObjectPropertyList(int handle, int format, int property,
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400734 int groupCode, int depth) {
735 // FIXME - implement group support
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400736 if (groupCode != 0) {
737 return new MtpPropertyList(0, MtpConstants.RESPONSE_SPECIFICATION_BY_GROUP_UNSUPPORTED);
738 }
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400739
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500740 MtpPropertyGroup propertyGroup;
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900741 if (property == 0xffffffff) {
742 if (format == 0 && handle != 0 && handle != 0xffffffff) {
Mike Lockwood71827742015-01-23 10:50:08 -0800743 // return properties based on the object's format
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900744 format = getObjectFormat(handle);
Mike Lockwood71827742015-01-23 10:50:08 -0800745 }
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900746 propertyGroup = mPropertyGroupsByFormat.get(format);
747 if (propertyGroup == null) {
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500748 int[] propertyList = getSupportedObjectProperties(format);
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700749 propertyGroup = new MtpPropertyGroup(this, mMediaProvider,
Dianne Hackborn35654b62013-01-14 17:38:02 -0800750 mVolumeName, propertyList);
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900751 mPropertyGroupsByFormat.put(format, propertyGroup);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400752 }
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500753 } else {
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900754 propertyGroup = mPropertyGroupsByProperty.get(property);
755 if (propertyGroup == null) {
756 final int[] propertyList = new int[] { property };
757 propertyGroup = new MtpPropertyGroup(
758 this, mMediaProvider, mVolumeName, propertyList);
759 mPropertyGroupsByProperty.put(property, propertyGroup);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400760 }
761 }
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500762
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900763 return propertyGroup.getPropertyList(handle, format, depth);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400764 }
765
Mike Lockwood5ebac832010-10-12 11:33:47 -0400766 private int renameFile(int handle, String newName) {
767 Cursor c = null;
768
769 // first compute current path
770 String path = null;
Mike Lockwood5ebac832010-10-12 11:33:47 -0400771 String[] whereArgs = new String[] { Integer.toString(handle) };
772 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700773 c = mMediaProvider.query(mObjectsUri, PATH_PROJECTION, ID_WHERE,
Dianne Hackborn35654b62013-01-14 17:38:02 -0800774 whereArgs, null, null);
Mike Lockwood5ebac832010-10-12 11:33:47 -0400775 if (c != null && c.moveToNext()) {
Mike Lockwood1c4e88d2011-01-12 12:38:41 -0500776 path = c.getString(1);
Mike Lockwood5ebac832010-10-12 11:33:47 -0400777 }
778 } catch (RemoteException e) {
779 Log.e(TAG, "RemoteException in getObjectFilePath", e);
780 return MtpConstants.RESPONSE_GENERAL_ERROR;
781 } finally {
782 if (c != null) {
783 c.close();
784 }
785 }
786 if (path == null) {
787 return MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE;
788 }
Mike Lockwood5ebac832010-10-12 11:33:47 -0400789
Mike Lockwood73e56d92011-12-01 16:58:41 -0500790 // do not allow renaming any of the special subdirectories
791 if (isStorageSubDirectory(path)) {
792 return MtpConstants.RESPONSE_OBJECT_WRITE_PROTECTED;
793 }
794
Mike Lockwood5ebac832010-10-12 11:33:47 -0400795 // now rename the file. make sure this succeeds before updating database
796 File oldFile = new File(path);
797 int lastSlash = path.lastIndexOf('/');
798 if (lastSlash <= 1) {
799 return MtpConstants.RESPONSE_GENERAL_ERROR;
800 }
801 String newPath = path.substring(0, lastSlash + 1) + newName;
802 File newFile = new File(newPath);
803 boolean success = oldFile.renameTo(newFile);
Mike Lockwood5ebac832010-10-12 11:33:47 -0400804 if (!success) {
Mike Lockwoodf26a5862011-01-21 21:00:54 -0800805 Log.w(TAG, "renaming "+ path + " to " + newPath + " failed");
Mike Lockwood5ebac832010-10-12 11:33:47 -0400806 return MtpConstants.RESPONSE_GENERAL_ERROR;
807 }
808
809 // finally update database
810 ContentValues values = new ContentValues();
811 values.put(Files.FileColumns.DATA, newPath);
812 int updated = 0;
813 try {
Mike Lockwood6a6a3af2010-10-12 14:19:51 -0400814 // note - we are relying on a special case in MediaProvider.update() to update
815 // the paths for all children in the case where this is a directory.
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700816 updated = mMediaProvider.update(mObjectsUri, values, ID_WHERE, whereArgs);
Mike Lockwood5ebac832010-10-12 11:33:47 -0400817 } catch (RemoteException e) {
818 Log.e(TAG, "RemoteException in mMediaProvider.update", e);
819 }
Mike Lockwood6a6a3af2010-10-12 14:19:51 -0400820 if (updated == 0) {
Mike Lockwood5ebac832010-10-12 11:33:47 -0400821 Log.e(TAG, "Unable to update path for " + path + " to " + newPath);
822 // this shouldn't happen, but if it does we need to rename the file to its original name
823 newFile.renameTo(oldFile);
824 return MtpConstants.RESPONSE_GENERAL_ERROR;
825 }
826
Marco Nelissenca78f3d2012-01-27 09:43:20 -0800827 // check if nomedia status changed
828 if (newFile.isDirectory()) {
829 // for directories, check if renamed from something hidden to something non-hidden
830 if (oldFile.getName().startsWith(".") && !newPath.startsWith(".")) {
831 // directory was unhidden
832 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700833 mMediaProvider.call(MediaStore.UNHIDE_CALL, newPath, null);
Marco Nelissenca78f3d2012-01-27 09:43:20 -0800834 } catch (RemoteException e) {
835 Log.e(TAG, "failed to unhide/rescan for " + newPath);
836 }
837 }
838 } else {
839 // for files, check if renamed from .nomedia to something else
840 if (oldFile.getName().toLowerCase(Locale.US).equals(".nomedia")
841 && !newPath.toLowerCase(Locale.US).equals(".nomedia")) {
842 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700843 mMediaProvider.call(MediaStore.UNHIDE_CALL, oldFile.getParent(), null);
Marco Nelissenca78f3d2012-01-27 09:43:20 -0800844 } catch (RemoteException e) {
845 Log.e(TAG, "failed to unhide/rescan for " + newPath);
846 }
847 }
848 }
849
Mike Lockwood5ebac832010-10-12 11:33:47 -0400850 return MtpConstants.RESPONSE_OK;
851 }
852
Jerry Zhangdef7b1932017-10-17 13:47:51 -0700853 private int moveObject(int handle, int newParent, int newStorage, String newPath) {
Jerry Zhang952558d42017-09-26 17:49:52 -0700854 String[] whereArgs = new String[] { Integer.toString(handle) };
855
856 // do not allow renaming any of the special subdirectories
857 if (isStorageSubDirectory(newPath)) {
858 return MtpConstants.RESPONSE_OBJECT_WRITE_PROTECTED;
859 }
860
861 // update database
862 ContentValues values = new ContentValues();
863 values.put(Files.FileColumns.DATA, newPath);
864 values.put(Files.FileColumns.PARENT, newParent);
Jerry Zhangdef7b1932017-10-17 13:47:51 -0700865 values.put(Files.FileColumns.STORAGE_ID, newStorage);
Jerry Zhang952558d42017-09-26 17:49:52 -0700866 int updated = 0;
867 try {
868 // note - we are relying on a special case in MediaProvider.update() to update
869 // the paths for all children in the case where this is a directory.
870 updated = mMediaProvider.update(mObjectsUri, values, ID_WHERE, whereArgs);
871 } catch (RemoteException e) {
872 Log.e(TAG, "RemoteException in mMediaProvider.update", e);
873 }
874 if (updated == 0) {
875 Log.e(TAG, "Unable to update path for " + handle + " to " + newPath);
876 return MtpConstants.RESPONSE_GENERAL_ERROR;
877 }
878 return MtpConstants.RESPONSE_OK;
879 }
880
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400881 private int setObjectProperty(int handle, int property,
882 long intValue, String stringValue) {
Mike Lockwood5ebac832010-10-12 11:33:47 -0400883 switch (property) {
884 case MtpConstants.PROPERTY_OBJECT_FILE_NAME:
885 return renameFile(handle, stringValue);
886
887 default:
888 return MtpConstants.RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
889 }
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400890 }
891
892 private int getDeviceProperty(int property, long[] outIntValue, char[] outStringValue) {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400893 switch (property) {
894 case MtpConstants.DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER:
895 case MtpConstants.DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME:
Mike Lockwood775de952011-03-05 17:34:11 -0500896 // writable string properties kept in shared preferences
897 String value = mDeviceProperties.getString(Integer.toString(property), "");
898 int length = value.length();
899 if (length > 255) {
900 length = 255;
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400901 }
Mike Lockwood775de952011-03-05 17:34:11 -0500902 value.getChars(0, length, outStringValue, 0);
903 outStringValue[length] = 0;
904 return MtpConstants.RESPONSE_OK;
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400905
Mike Lockwoodea93fa12010-12-07 10:41:35 -0800906 case MtpConstants.DEVICE_PROPERTY_IMAGE_SIZE:
907 // use screen size as max image size
908 Display display = ((WindowManager)mContext.getSystemService(
909 Context.WINDOW_SERVICE)).getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -0700910 int width = display.getMaximumSizeDimension();
911 int height = display.getMaximumSizeDimension();
Mike Lockwoodea93fa12010-12-07 10:41:35 -0800912 String imageSize = Integer.toString(width) + "x" + Integer.toString(height);
913 imageSize.getChars(0, imageSize.length(), outStringValue, 0);
914 outStringValue[imageSize.length()] = 0;
915 return MtpConstants.RESPONSE_OK;
916
Jerry Zhang13bb2f42016-12-14 15:39:29 -0800917 case MtpConstants.DEVICE_PROPERTY_PERCEIVED_DEVICE_TYPE:
918 outIntValue[0] = mDeviceType;
919 return MtpConstants.RESPONSE_OK;
920
Mike Lockwood56c85242014-03-07 13:29:08 -0800921 // DEVICE_PROPERTY_BATTERY_LEVEL is implemented in the JNI code
922
Mike Lockwoodea93fa12010-12-07 10:41:35 -0800923 default:
924 return MtpConstants.RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
925 }
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400926 }
927
928 private int setDeviceProperty(int property, long intValue, String stringValue) {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400929 switch (property) {
930 case MtpConstants.DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER:
931 case MtpConstants.DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME:
Mike Lockwood775de952011-03-05 17:34:11 -0500932 // writable string properties kept in shared prefs
933 SharedPreferences.Editor e = mDeviceProperties.edit();
934 e.putString(Integer.toString(property), stringValue);
935 return (e.commit() ? MtpConstants.RESPONSE_OK
936 : MtpConstants.RESPONSE_GENERAL_ERROR);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400937 }
938
939 return MtpConstants.RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
940 }
941
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400942 private boolean getObjectInfo(int handle, int[] outStorageFormatParent,
Mike Lockwood1341f1e2013-04-01 10:52:47 -0700943 char[] outName, long[] outCreatedModified) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400944 Cursor c = null;
945 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700946 c = mMediaProvider.query(mObjectsUri, OBJECT_INFO_PROJECTION,
Jeff Brown75ea64f2012-01-25 19:37:13 -0800947 ID_WHERE, new String[] { Integer.toString(handle) }, null, null);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400948 if (c != null && c.moveToNext()) {
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400949 outStorageFormatParent[0] = c.getInt(1);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400950 outStorageFormatParent[1] = c.getInt(2);
951 outStorageFormatParent[2] = c.getInt(3);
952
953 // extract name from path
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400954 String path = c.getString(4);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400955 int lastSlash = path.lastIndexOf('/');
956 int start = (lastSlash >= 0 ? lastSlash + 1 : 0);
957 int end = path.length();
958 if (end - start > 255) {
959 end = start + 255;
960 }
961 path.getChars(start, end, outName, 0);
962 outName[end - start] = 0;
963
Mike Lockwood1341f1e2013-04-01 10:52:47 -0700964 outCreatedModified[0] = c.getLong(5);
965 outCreatedModified[1] = c.getLong(6);
966 // use modification date as creation date if date added is not set
967 if (outCreatedModified[0] == 0) {
968 outCreatedModified[0] = outCreatedModified[1];
969 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400970 return true;
971 }
972 } catch (RemoteException e) {
Mike Lockwood2b5f9ad12010-10-29 19:16:27 -0400973 Log.e(TAG, "RemoteException in getObjectInfo", e);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400974 } finally {
975 if (c != null) {
976 c.close();
977 }
978 }
979 return false;
980 }
981
Mike Lockwood365e03e2010-12-08 16:08:01 -0800982 private int getObjectFilePath(int handle, char[] outFilePath, long[] outFileLengthFormat) {
Mike Lockwood01788562010-10-11 11:22:19 -0400983 if (handle == 0) {
984 // special case root directory
985 mMediaStoragePath.getChars(0, mMediaStoragePath.length(), outFilePath, 0);
986 outFilePath[mMediaStoragePath.length()] = 0;
Mike Lockwood365e03e2010-12-08 16:08:01 -0800987 outFileLengthFormat[0] = 0;
988 outFileLengthFormat[1] = MtpConstants.FORMAT_ASSOCIATION;
Mike Lockwood01788562010-10-11 11:22:19 -0400989 return MtpConstants.RESPONSE_OK;
990 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400991 Cursor c = null;
992 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700993 c = mMediaProvider.query(mObjectsUri, PATH_FORMAT_PROJECTION,
Jeff Brown75ea64f2012-01-25 19:37:13 -0800994 ID_WHERE, new String[] { Integer.toString(handle) }, null, null);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400995 if (c != null && c.moveToNext()) {
Mike Lockwood1c4e88d2011-01-12 12:38:41 -0500996 String path = c.getString(1);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400997 path.getChars(0, path.length(), outFilePath, 0);
998 outFilePath[path.length()] = 0;
Mike Lockwoodf6f16612012-09-12 15:50:59 -0700999 // File transfers from device to host will likely fail if the size is incorrect.
1000 // So to be safe, use the actual file size here.
1001 outFileLengthFormat[0] = new File(path).length();
1002 outFileLengthFormat[1] = c.getLong(2);
Mike Lockwood5367ab62010-08-30 13:23:02 -04001003 return MtpConstants.RESPONSE_OK;
Mike Lockwood59c777a2010-08-02 10:37:41 -04001004 } else {
Mike Lockwood5367ab62010-08-30 13:23:02 -04001005 return MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001006 }
1007 } catch (RemoteException e) {
1008 Log.e(TAG, "RemoteException in getObjectFilePath", e);
Mike Lockwood5367ab62010-08-30 13:23:02 -04001009 return MtpConstants.RESPONSE_GENERAL_ERROR;
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001010 } finally {
1011 if (c != null) {
1012 c.close();
1013 }
1014 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001015 }
1016
Mike Lockwood71827742015-01-23 10:50:08 -08001017 private int getObjectFormat(int handle) {
1018 Cursor c = null;
1019 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001020 c = mMediaProvider.query(mObjectsUri, FORMAT_PROJECTION,
Daichi Hirono486ad2e2016-02-29 17:28:47 +09001021 ID_WHERE, new String[] { Integer.toString(handle) }, null, null);
Mike Lockwood71827742015-01-23 10:50:08 -08001022 if (c != null && c.moveToNext()) {
1023 return c.getInt(1);
1024 } else {
1025 return -1;
1026 }
1027 } catch (RemoteException e) {
1028 Log.e(TAG, "RemoteException in getObjectFilePath", e);
1029 return -1;
1030 } finally {
1031 if (c != null) {
1032 c.close();
1033 }
1034 }
1035 }
1036
Mike Lockwood59c777a2010-08-02 10:37:41 -04001037 private int deleteFile(int handle) {
Mike Lockwood2837eef2010-08-31 16:25:12 -04001038 mDatabaseModified = true;
Mike Lockwood55f808c2010-12-14 13:14:29 -08001039 String path = null;
1040 int format = 0;
1041
1042 Cursor c = null;
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001043 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001044 c = mMediaProvider.query(mObjectsUri, PATH_FORMAT_PROJECTION,
Jeff Brown75ea64f2012-01-25 19:37:13 -08001045 ID_WHERE, new String[] { Integer.toString(handle) }, null, null);
Mike Lockwood55f808c2010-12-14 13:14:29 -08001046 if (c != null && c.moveToNext()) {
1047 // don't convert to media path here, since we will be matching
1048 // against paths in the database matching /data/media
1049 path = c.getString(1);
Mike Lockwoodf6f16612012-09-12 15:50:59 -07001050 format = c.getInt(2);
Mike Lockwood55f808c2010-12-14 13:14:29 -08001051 } else {
1052 return MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE;
1053 }
1054
1055 if (path == null || format == 0) {
1056 return MtpConstants.RESPONSE_GENERAL_ERROR;
1057 }
1058
Mike Lockwood73e56d92011-12-01 16:58:41 -05001059 // do not allow deleting any of the special subdirectories
1060 if (isStorageSubDirectory(path)) {
1061 return MtpConstants.RESPONSE_OBJECT_WRITE_PROTECTED;
1062 }
1063
Mike Lockwood55f808c2010-12-14 13:14:29 -08001064 if (format == MtpConstants.FORMAT_ASSOCIATION) {
1065 // recursive case - delete all children first
1066 Uri uri = Files.getMtpObjectsUri(mVolumeName);
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001067 int count = mMediaProvider.delete(uri,
Mike Lockwood1e855d92012-06-26 16:31:41 -07001068 // the 'like' makes it use the index, the 'lower()' makes it correct
1069 // when the path contains sqlite wildcard characters
1070 "_data LIKE ?1 AND lower(substr(_data,1,?2))=lower(?3)",
1071 new String[] { path + "/%",Integer.toString(path.length() + 1), path + "/"});
Mike Lockwood55f808c2010-12-14 13:14:29 -08001072 }
1073
1074 Uri uri = Files.getMtpObjectsUri(mVolumeName, handle);
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001075 if (mMediaProvider.delete(uri, null, null) > 0) {
Marco Nelissenca78f3d2012-01-27 09:43:20 -08001076 if (format != MtpConstants.FORMAT_ASSOCIATION
1077 && path.toLowerCase(Locale.US).endsWith("/.nomedia")) {
1078 try {
1079 String parentPath = path.substring(0, path.lastIndexOf("/"));
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001080 mMediaProvider.call(MediaStore.UNHIDE_CALL, parentPath, null);
Marco Nelissenca78f3d2012-01-27 09:43:20 -08001081 } catch (RemoteException e) {
1082 Log.e(TAG, "failed to unhide/rescan for " + path);
1083 }
1084 }
Mike Lockwood5367ab62010-08-30 13:23:02 -04001085 return MtpConstants.RESPONSE_OK;
Mike Lockwood59c777a2010-08-02 10:37:41 -04001086 } else {
Mike Lockwood5367ab62010-08-30 13:23:02 -04001087 return MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood59c777a2010-08-02 10:37:41 -04001088 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001089 } catch (RemoteException e) {
1090 Log.e(TAG, "RemoteException in deleteFile", e);
Mike Lockwood5367ab62010-08-30 13:23:02 -04001091 return MtpConstants.RESPONSE_GENERAL_ERROR;
Mike Lockwood55f808c2010-12-14 13:14:29 -08001092 } finally {
1093 if (c != null) {
1094 c.close();
1095 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001096 }
1097 }
1098
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001099 private int[] getObjectReferences(int handle) {
Mike Lockwood8490e662010-09-09 14:16:22 -04001100 Uri uri = Files.getMtpReferencesUri(mVolumeName, handle);
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001101 Cursor c = null;
1102 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001103 c = mMediaProvider.query(uri, ID_PROJECTION, null, null, null, null);
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001104 if (c == null) {
1105 return null;
1106 }
1107 int count = c.getCount();
1108 if (count > 0) {
1109 int[] result = new int[count];
1110 for (int i = 0; i < count; i++) {
1111 c.moveToNext();
1112 result[i] = c.getInt(0);
1113 }
1114 return result;
1115 }
1116 } catch (RemoteException e) {
1117 Log.e(TAG, "RemoteException in getObjectList", e);
1118 } finally {
1119 if (c != null) {
1120 c.close();
1121 }
1122 }
1123 return null;
1124 }
1125
1126 private int setObjectReferences(int handle, int[] references) {
Mike Lockwood2837eef2010-08-31 16:25:12 -04001127 mDatabaseModified = true;
Mike Lockwood8490e662010-09-09 14:16:22 -04001128 Uri uri = Files.getMtpReferencesUri(mVolumeName, handle);
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001129 int count = references.length;
1130 ContentValues[] valuesList = new ContentValues[count];
1131 for (int i = 0; i < count; i++) {
1132 ContentValues values = new ContentValues();
Mike Lockwood3b2a62e2010-09-08 12:47:57 -04001133 values.put(Files.FileColumns._ID, references[i]);
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001134 valuesList[i] = values;
1135 }
1136 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001137 if (mMediaProvider.bulkInsert(uri, valuesList) > 0) {
Mike Lockwood5367ab62010-08-30 13:23:02 -04001138 return MtpConstants.RESPONSE_OK;
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001139 }
1140 } catch (RemoteException e) {
1141 Log.e(TAG, "RemoteException in setObjectReferences", e);
1142 }
Mike Lockwood5367ab62010-08-30 13:23:02 -04001143 return MtpConstants.RESPONSE_GENERAL_ERROR;
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001144 }
1145
Mike Lockwood2837eef2010-08-31 16:25:12 -04001146 private void sessionStarted() {
Mike Lockwood2837eef2010-08-31 16:25:12 -04001147 mDatabaseModified = false;
1148 }
1149
1150 private void sessionEnded() {
Mike Lockwood2837eef2010-08-31 16:25:12 -04001151 if (mDatabaseModified) {
Mike Lockwooda3156052010-11-20 12:28:27 -05001152 mContext.sendBroadcast(new Intent(MediaStore.ACTION_MTP_SESSION_END));
Mike Lockwood2837eef2010-08-31 16:25:12 -04001153 mDatabaseModified = false;
1154 }
1155 }
1156
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001157 // used by the JNI code
Ashok Bhate2e59322013-12-17 19:04:19 +00001158 private long mNativeContext;
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001159
1160 private native final void native_setup();
1161 private native final void native_finalize();
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001162}