blob: 698c9c96fe01e0de3fb5180272c074b1f593edda [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;
472 }
473 where = STORAGE_PARENT_WHERE;
474 whereArgs = new String[] { Integer.toString(storageID),
475 Integer.toString(parent) };
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400476 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400477 } else {
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400478 // query specific format
479 if (parent == 0) {
480 // query all objects
Mike Lockwood73e56d92011-12-01 16:58:41 -0500481 where = STORAGE_FORMAT_WHERE;
482 whereArgs = new String[] { Integer.toString(storageID),
483 Integer.toString(format) };
484 } else {
485 if (parent == 0xFFFFFFFF) {
486 // all objects in root of store
487 parent = 0;
488 }
489 where = STORAGE_FORMAT_PARENT_WHERE;
490 whereArgs = new String[] { Integer.toString(storageID),
491 Integer.toString(format),
492 Integer.toString(parent) };
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400493 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400494 }
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400495 }
Mike Lockwood73e56d92011-12-01 16:58:41 -0500496
497 // if we are restricting queries to mSubDirectories, we need to add the restriction
498 // onto our "where" arguments
499 if (mSubDirectoriesWhere != null) {
500 if (where == null) {
501 where = mSubDirectoriesWhere;
502 whereArgs = mSubDirectoriesWhereArgs;
503 } else {
504 where = where + " AND " + mSubDirectoriesWhere;
505
506 // create new array to hold whereArgs and mSubDirectoriesWhereArgs
507 String[] newWhereArgs =
508 new String[whereArgs.length + mSubDirectoriesWhereArgs.length];
509 int i, j;
510 for (i = 0; i < whereArgs.length; i++) {
511 newWhereArgs[i] = whereArgs[i];
512 }
513 for (j = 0; j < mSubDirectoriesWhereArgs.length; i++, j++) {
514 newWhereArgs[i] = mSubDirectoriesWhereArgs[j];
515 }
516 whereArgs = newWhereArgs;
517 }
518 }
519
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700520 return mMediaProvider.query(mObjectsUri, ID_PROJECTION, where,
Dianne Hackborn35654b62013-01-14 17:38:02 -0800521 whereArgs, null, null);
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400522 }
523
524 private int[] getObjectList(int storageID, int format, int parent) {
525 Cursor c = null;
526 try {
527 c = createObjectQuery(storageID, format, parent);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400528 if (c == null) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400529 return null;
530 }
531 int count = c.getCount();
532 if (count > 0) {
533 int[] result = new int[count];
534 for (int i = 0; i < count; i++) {
535 c.moveToNext();
536 result[i] = c.getInt(0);
537 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400538 return result;
539 }
540 } catch (RemoteException e) {
541 Log.e(TAG, "RemoteException in getObjectList", e);
542 } finally {
543 if (c != null) {
544 c.close();
545 }
546 }
547 return null;
548 }
549
Mike Lockwood7a047c82010-08-02 10:52:20 -0400550 private int getNumObjects(int storageID, int format, int parent) {
Mike Lockwood7a047c82010-08-02 10:52:20 -0400551 Cursor c = null;
552 try {
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400553 c = createObjectQuery(storageID, format, parent);
Mike Lockwood7a047c82010-08-02 10:52:20 -0400554 if (c != null) {
555 return c.getCount();
556 }
557 } catch (RemoteException e) {
558 Log.e(TAG, "RemoteException in getNumObjects", e);
559 } finally {
560 if (c != null) {
561 c.close();
562 }
563 }
564 return -1;
565 }
566
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400567 private int[] getSupportedPlaybackFormats() {
568 return new int[] {
Mike Lockwoode5211692010-09-08 13:50:45 -0400569 // allow transfering arbitrary files
570 MtpConstants.FORMAT_UNDEFINED,
Mike Lockwood12b8a992010-09-23 21:33:29 -0400571
Mike Lockwood792ec842010-09-09 15:30:10 -0400572 MtpConstants.FORMAT_ASSOCIATION,
Mike Lockwood12b8a992010-09-23 21:33:29 -0400573 MtpConstants.FORMAT_TEXT,
574 MtpConstants.FORMAT_HTML,
575 MtpConstants.FORMAT_WAV,
576 MtpConstants.FORMAT_MP3,
577 MtpConstants.FORMAT_MPEG,
578 MtpConstants.FORMAT_EXIF_JPEG,
579 MtpConstants.FORMAT_TIFF_EP,
bo huang240582e2012-02-27 16:27:00 +0800580 MtpConstants.FORMAT_BMP,
Mike Lockwood12b8a992010-09-23 21:33:29 -0400581 MtpConstants.FORMAT_GIF,
582 MtpConstants.FORMAT_JFIF,
583 MtpConstants.FORMAT_PNG,
584 MtpConstants.FORMAT_TIFF,
585 MtpConstants.FORMAT_WMA,
586 MtpConstants.FORMAT_OGG,
587 MtpConstants.FORMAT_AAC,
588 MtpConstants.FORMAT_MP4_CONTAINER,
589 MtpConstants.FORMAT_MP2,
590 MtpConstants.FORMAT_3GP_CONTAINER,
Mike Lockwood792ec842010-09-09 15:30:10 -0400591 MtpConstants.FORMAT_ABSTRACT_AV_PLAYLIST,
Mike Lockwood12b8a992010-09-23 21:33:29 -0400592 MtpConstants.FORMAT_WPL_PLAYLIST,
593 MtpConstants.FORMAT_M3U_PLAYLIST,
594 MtpConstants.FORMAT_PLS_PLAYLIST,
595 MtpConstants.FORMAT_XML_DOCUMENT,
Glenn Kastenf9f223e2011-01-13 11:17:00 -0800596 MtpConstants.FORMAT_FLAC,
Jaesung Chung5a8b9622015-12-18 05:50:21 +0100597 MtpConstants.FORMAT_DNG,
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400598 };
599 }
600
601 private int[] getSupportedCaptureFormats() {
602 // no capture formats yet
603 return null;
604 }
605
Mike Lockwoodae078f72010-09-26 12:35:51 -0400606 static final int[] FILE_PROPERTIES = {
607 // NOTE must match beginning of AUDIO_PROPERTIES, VIDEO_PROPERTIES
608 // and IMAGE_PROPERTIES below
Mike Lockwood5367ab62010-08-30 13:23:02 -0400609 MtpConstants.PROPERTY_STORAGE_ID,
610 MtpConstants.PROPERTY_OBJECT_FORMAT,
Mike Lockwoodd3bfecb2010-09-23 23:04:28 -0400611 MtpConstants.PROPERTY_PROTECTION_STATUS,
Mike Lockwood5367ab62010-08-30 13:23:02 -0400612 MtpConstants.PROPERTY_OBJECT_SIZE,
613 MtpConstants.PROPERTY_OBJECT_FILE_NAME,
Mike Lockwoodd3bfecb2010-09-23 23:04:28 -0400614 MtpConstants.PROPERTY_DATE_MODIFIED,
Mike Lockwood5367ab62010-08-30 13:23:02 -0400615 MtpConstants.PROPERTY_PARENT_OBJECT,
Mike Lockwoodd3bfecb2010-09-23 23:04:28 -0400616 MtpConstants.PROPERTY_PERSISTENT_UID,
617 MtpConstants.PROPERTY_NAME,
Mike Lockwood71827742015-01-23 10:50:08 -0800618 MtpConstants.PROPERTY_DISPLAY_NAME,
Mike Lockwoodae078f72010-09-26 12:35:51 -0400619 MtpConstants.PROPERTY_DATE_ADDED,
620 };
621
622 static final int[] AUDIO_PROPERTIES = {
623 // NOTE must match FILE_PROPERTIES above
624 MtpConstants.PROPERTY_STORAGE_ID,
625 MtpConstants.PROPERTY_OBJECT_FORMAT,
626 MtpConstants.PROPERTY_PROTECTION_STATUS,
627 MtpConstants.PROPERTY_OBJECT_SIZE,
628 MtpConstants.PROPERTY_OBJECT_FILE_NAME,
629 MtpConstants.PROPERTY_DATE_MODIFIED,
630 MtpConstants.PROPERTY_PARENT_OBJECT,
631 MtpConstants.PROPERTY_PERSISTENT_UID,
632 MtpConstants.PROPERTY_NAME,
633 MtpConstants.PROPERTY_DISPLAY_NAME,
634 MtpConstants.PROPERTY_DATE_ADDED,
635
636 // audio specific properties
637 MtpConstants.PROPERTY_ARTIST,
638 MtpConstants.PROPERTY_ALBUM_NAME,
639 MtpConstants.PROPERTY_ALBUM_ARTIST,
640 MtpConstants.PROPERTY_TRACK,
641 MtpConstants.PROPERTY_ORIGINAL_RELEASE_DATE,
642 MtpConstants.PROPERTY_DURATION,
643 MtpConstants.PROPERTY_GENRE,
644 MtpConstants.PROPERTY_COMPOSER,
Mike Lockwood92b53bc2014-03-13 14:51:29 -0700645 MtpConstants.PROPERTY_AUDIO_WAVE_CODEC,
646 MtpConstants.PROPERTY_BITRATE_TYPE,
647 MtpConstants.PROPERTY_AUDIO_BITRATE,
648 MtpConstants.PROPERTY_NUMBER_OF_CHANNELS,
649 MtpConstants.PROPERTY_SAMPLE_RATE,
Mike Lockwoodae078f72010-09-26 12:35:51 -0400650 };
651
652 static final int[] VIDEO_PROPERTIES = {
653 // NOTE must match FILE_PROPERTIES above
654 MtpConstants.PROPERTY_STORAGE_ID,
655 MtpConstants.PROPERTY_OBJECT_FORMAT,
656 MtpConstants.PROPERTY_PROTECTION_STATUS,
657 MtpConstants.PROPERTY_OBJECT_SIZE,
658 MtpConstants.PROPERTY_OBJECT_FILE_NAME,
659 MtpConstants.PROPERTY_DATE_MODIFIED,
660 MtpConstants.PROPERTY_PARENT_OBJECT,
661 MtpConstants.PROPERTY_PERSISTENT_UID,
662 MtpConstants.PROPERTY_NAME,
663 MtpConstants.PROPERTY_DISPLAY_NAME,
664 MtpConstants.PROPERTY_DATE_ADDED,
665
666 // video specific properties
667 MtpConstants.PROPERTY_ARTIST,
668 MtpConstants.PROPERTY_ALBUM_NAME,
669 MtpConstants.PROPERTY_DURATION,
670 MtpConstants.PROPERTY_DESCRIPTION,
671 };
672
673 static final int[] IMAGE_PROPERTIES = {
674 // NOTE must match FILE_PROPERTIES above
675 MtpConstants.PROPERTY_STORAGE_ID,
676 MtpConstants.PROPERTY_OBJECT_FORMAT,
677 MtpConstants.PROPERTY_PROTECTION_STATUS,
678 MtpConstants.PROPERTY_OBJECT_SIZE,
679 MtpConstants.PROPERTY_OBJECT_FILE_NAME,
680 MtpConstants.PROPERTY_DATE_MODIFIED,
681 MtpConstants.PROPERTY_PARENT_OBJECT,
682 MtpConstants.PROPERTY_PERSISTENT_UID,
683 MtpConstants.PROPERTY_NAME,
684 MtpConstants.PROPERTY_DISPLAY_NAME,
685 MtpConstants.PROPERTY_DATE_ADDED,
686
687 // image specific properties
688 MtpConstants.PROPERTY_DESCRIPTION,
689 };
690
691 private int[] getSupportedObjectProperties(int format) {
692 switch (format) {
693 case MtpConstants.FORMAT_MP3:
694 case MtpConstants.FORMAT_WAV:
695 case MtpConstants.FORMAT_WMA:
696 case MtpConstants.FORMAT_OGG:
697 case MtpConstants.FORMAT_AAC:
698 return AUDIO_PROPERTIES;
699 case MtpConstants.FORMAT_MPEG:
700 case MtpConstants.FORMAT_3GP_CONTAINER:
701 case MtpConstants.FORMAT_WMV:
702 return VIDEO_PROPERTIES;
703 case MtpConstants.FORMAT_EXIF_JPEG:
704 case MtpConstants.FORMAT_GIF:
705 case MtpConstants.FORMAT_PNG:
706 case MtpConstants.FORMAT_BMP:
Jaesung Chung5a8b9622015-12-18 05:50:21 +0100707 case MtpConstants.FORMAT_DNG:
Mike Lockwoodae078f72010-09-26 12:35:51 -0400708 return IMAGE_PROPERTIES;
709 default:
710 return FILE_PROPERTIES;
711 }
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400712 }
713
714 private int[] getSupportedDeviceProperties() {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400715 return new int[] {
716 MtpConstants.DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER,
717 MtpConstants.DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME,
Mike Lockwoodea93fa12010-12-07 10:41:35 -0800718 MtpConstants.DEVICE_PROPERTY_IMAGE_SIZE,
Mike Lockwood56c85242014-03-07 13:29:08 -0800719 MtpConstants.DEVICE_PROPERTY_BATTERY_LEVEL,
Jerry Zhang13bb2f42016-12-14 15:39:29 -0800720 MtpConstants.DEVICE_PROPERTY_PERCEIVED_DEVICE_TYPE,
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400721 };
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400722 }
723
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900724 private MtpPropertyList getObjectPropertyList(int handle, int format, int property,
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400725 int groupCode, int depth) {
726 // FIXME - implement group support
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400727 if (groupCode != 0) {
728 return new MtpPropertyList(0, MtpConstants.RESPONSE_SPECIFICATION_BY_GROUP_UNSUPPORTED);
729 }
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400730
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500731 MtpPropertyGroup propertyGroup;
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900732 if (property == 0xffffffff) {
733 if (format == 0 && handle != 0 && handle != 0xffffffff) {
Mike Lockwood71827742015-01-23 10:50:08 -0800734 // return properties based on the object's format
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900735 format = getObjectFormat(handle);
Mike Lockwood71827742015-01-23 10:50:08 -0800736 }
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900737 propertyGroup = mPropertyGroupsByFormat.get(format);
738 if (propertyGroup == null) {
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500739 int[] propertyList = getSupportedObjectProperties(format);
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700740 propertyGroup = new MtpPropertyGroup(this, mMediaProvider,
Dianne Hackborn35654b62013-01-14 17:38:02 -0800741 mVolumeName, propertyList);
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900742 mPropertyGroupsByFormat.put(format, propertyGroup);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400743 }
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500744 } else {
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900745 propertyGroup = mPropertyGroupsByProperty.get(property);
746 if (propertyGroup == null) {
747 final int[] propertyList = new int[] { property };
748 propertyGroup = new MtpPropertyGroup(
749 this, mMediaProvider, mVolumeName, propertyList);
750 mPropertyGroupsByProperty.put(property, propertyGroup);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400751 }
752 }
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500753
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900754 return propertyGroup.getPropertyList(handle, format, depth);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400755 }
756
Mike Lockwood5ebac832010-10-12 11:33:47 -0400757 private int renameFile(int handle, String newName) {
758 Cursor c = null;
759
760 // first compute current path
761 String path = null;
Mike Lockwood5ebac832010-10-12 11:33:47 -0400762 String[] whereArgs = new String[] { Integer.toString(handle) };
763 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700764 c = mMediaProvider.query(mObjectsUri, PATH_PROJECTION, ID_WHERE,
Dianne Hackborn35654b62013-01-14 17:38:02 -0800765 whereArgs, null, null);
Mike Lockwood5ebac832010-10-12 11:33:47 -0400766 if (c != null && c.moveToNext()) {
Mike Lockwood1c4e88d2011-01-12 12:38:41 -0500767 path = c.getString(1);
Mike Lockwood5ebac832010-10-12 11:33:47 -0400768 }
769 } catch (RemoteException e) {
770 Log.e(TAG, "RemoteException in getObjectFilePath", e);
771 return MtpConstants.RESPONSE_GENERAL_ERROR;
772 } finally {
773 if (c != null) {
774 c.close();
775 }
776 }
777 if (path == null) {
778 return MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE;
779 }
Mike Lockwood5ebac832010-10-12 11:33:47 -0400780
Mike Lockwood73e56d92011-12-01 16:58:41 -0500781 // do not allow renaming any of the special subdirectories
782 if (isStorageSubDirectory(path)) {
783 return MtpConstants.RESPONSE_OBJECT_WRITE_PROTECTED;
784 }
785
Mike Lockwood5ebac832010-10-12 11:33:47 -0400786 // now rename the file. make sure this succeeds before updating database
787 File oldFile = new File(path);
788 int lastSlash = path.lastIndexOf('/');
789 if (lastSlash <= 1) {
790 return MtpConstants.RESPONSE_GENERAL_ERROR;
791 }
792 String newPath = path.substring(0, lastSlash + 1) + newName;
793 File newFile = new File(newPath);
794 boolean success = oldFile.renameTo(newFile);
Mike Lockwood5ebac832010-10-12 11:33:47 -0400795 if (!success) {
Mike Lockwoodf26a5862011-01-21 21:00:54 -0800796 Log.w(TAG, "renaming "+ path + " to " + newPath + " failed");
Mike Lockwood5ebac832010-10-12 11:33:47 -0400797 return MtpConstants.RESPONSE_GENERAL_ERROR;
798 }
799
800 // finally update database
801 ContentValues values = new ContentValues();
802 values.put(Files.FileColumns.DATA, newPath);
803 int updated = 0;
804 try {
Mike Lockwood6a6a3af2010-10-12 14:19:51 -0400805 // note - we are relying on a special case in MediaProvider.update() to update
806 // the paths for all children in the case where this is a directory.
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700807 updated = mMediaProvider.update(mObjectsUri, values, ID_WHERE, whereArgs);
Mike Lockwood5ebac832010-10-12 11:33:47 -0400808 } catch (RemoteException e) {
809 Log.e(TAG, "RemoteException in mMediaProvider.update", e);
810 }
Mike Lockwood6a6a3af2010-10-12 14:19:51 -0400811 if (updated == 0) {
Mike Lockwood5ebac832010-10-12 11:33:47 -0400812 Log.e(TAG, "Unable to update path for " + path + " to " + newPath);
813 // this shouldn't happen, but if it does we need to rename the file to its original name
814 newFile.renameTo(oldFile);
815 return MtpConstants.RESPONSE_GENERAL_ERROR;
816 }
817
Marco Nelissenca78f3d2012-01-27 09:43:20 -0800818 // check if nomedia status changed
819 if (newFile.isDirectory()) {
820 // for directories, check if renamed from something hidden to something non-hidden
821 if (oldFile.getName().startsWith(".") && !newPath.startsWith(".")) {
822 // directory was unhidden
823 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700824 mMediaProvider.call(MediaStore.UNHIDE_CALL, newPath, null);
Marco Nelissenca78f3d2012-01-27 09:43:20 -0800825 } catch (RemoteException e) {
826 Log.e(TAG, "failed to unhide/rescan for " + newPath);
827 }
828 }
829 } else {
830 // for files, check if renamed from .nomedia to something else
831 if (oldFile.getName().toLowerCase(Locale.US).equals(".nomedia")
832 && !newPath.toLowerCase(Locale.US).equals(".nomedia")) {
833 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700834 mMediaProvider.call(MediaStore.UNHIDE_CALL, oldFile.getParent(), null);
Marco Nelissenca78f3d2012-01-27 09:43:20 -0800835 } catch (RemoteException e) {
836 Log.e(TAG, "failed to unhide/rescan for " + newPath);
837 }
838 }
839 }
840
Mike Lockwood5ebac832010-10-12 11:33:47 -0400841 return MtpConstants.RESPONSE_OK;
842 }
843
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400844 private int setObjectProperty(int handle, int property,
845 long intValue, String stringValue) {
Mike Lockwood5ebac832010-10-12 11:33:47 -0400846 switch (property) {
847 case MtpConstants.PROPERTY_OBJECT_FILE_NAME:
848 return renameFile(handle, stringValue);
849
850 default:
851 return MtpConstants.RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
852 }
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400853 }
854
855 private int getDeviceProperty(int property, long[] outIntValue, char[] outStringValue) {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400856 switch (property) {
857 case MtpConstants.DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER:
858 case MtpConstants.DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME:
Mike Lockwood775de952011-03-05 17:34:11 -0500859 // writable string properties kept in shared preferences
860 String value = mDeviceProperties.getString(Integer.toString(property), "");
861 int length = value.length();
862 if (length > 255) {
863 length = 255;
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400864 }
Mike Lockwood775de952011-03-05 17:34:11 -0500865 value.getChars(0, length, outStringValue, 0);
866 outStringValue[length] = 0;
867 return MtpConstants.RESPONSE_OK;
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400868
Mike Lockwoodea93fa12010-12-07 10:41:35 -0800869 case MtpConstants.DEVICE_PROPERTY_IMAGE_SIZE:
870 // use screen size as max image size
871 Display display = ((WindowManager)mContext.getSystemService(
872 Context.WINDOW_SERVICE)).getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -0700873 int width = display.getMaximumSizeDimension();
874 int height = display.getMaximumSizeDimension();
Mike Lockwoodea93fa12010-12-07 10:41:35 -0800875 String imageSize = Integer.toString(width) + "x" + Integer.toString(height);
876 imageSize.getChars(0, imageSize.length(), outStringValue, 0);
877 outStringValue[imageSize.length()] = 0;
878 return MtpConstants.RESPONSE_OK;
879
Jerry Zhang13bb2f42016-12-14 15:39:29 -0800880 case MtpConstants.DEVICE_PROPERTY_PERCEIVED_DEVICE_TYPE:
881 outIntValue[0] = mDeviceType;
882 return MtpConstants.RESPONSE_OK;
883
Mike Lockwood56c85242014-03-07 13:29:08 -0800884 // DEVICE_PROPERTY_BATTERY_LEVEL is implemented in the JNI code
885
Mike Lockwoodea93fa12010-12-07 10:41:35 -0800886 default:
887 return MtpConstants.RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
888 }
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400889 }
890
891 private int setDeviceProperty(int property, long intValue, String stringValue) {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400892 switch (property) {
893 case MtpConstants.DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER:
894 case MtpConstants.DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME:
Mike Lockwood775de952011-03-05 17:34:11 -0500895 // writable string properties kept in shared prefs
896 SharedPreferences.Editor e = mDeviceProperties.edit();
897 e.putString(Integer.toString(property), stringValue);
898 return (e.commit() ? MtpConstants.RESPONSE_OK
899 : MtpConstants.RESPONSE_GENERAL_ERROR);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400900 }
901
902 return MtpConstants.RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
903 }
904
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400905 private boolean getObjectInfo(int handle, int[] outStorageFormatParent,
Mike Lockwood1341f1e2013-04-01 10:52:47 -0700906 char[] outName, long[] outCreatedModified) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400907 Cursor c = null;
908 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700909 c = mMediaProvider.query(mObjectsUri, OBJECT_INFO_PROJECTION,
Jeff Brown75ea64f2012-01-25 19:37:13 -0800910 ID_WHERE, new String[] { Integer.toString(handle) }, null, null);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400911 if (c != null && c.moveToNext()) {
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400912 outStorageFormatParent[0] = c.getInt(1);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400913 outStorageFormatParent[1] = c.getInt(2);
914 outStorageFormatParent[2] = c.getInt(3);
915
916 // extract name from path
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400917 String path = c.getString(4);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400918 int lastSlash = path.lastIndexOf('/');
919 int start = (lastSlash >= 0 ? lastSlash + 1 : 0);
920 int end = path.length();
921 if (end - start > 255) {
922 end = start + 255;
923 }
924 path.getChars(start, end, outName, 0);
925 outName[end - start] = 0;
926
Mike Lockwood1341f1e2013-04-01 10:52:47 -0700927 outCreatedModified[0] = c.getLong(5);
928 outCreatedModified[1] = c.getLong(6);
929 // use modification date as creation date if date added is not set
930 if (outCreatedModified[0] == 0) {
931 outCreatedModified[0] = outCreatedModified[1];
932 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400933 return true;
934 }
935 } catch (RemoteException e) {
Mike Lockwood2b5f9ad12010-10-29 19:16:27 -0400936 Log.e(TAG, "RemoteException in getObjectInfo", e);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400937 } finally {
938 if (c != null) {
939 c.close();
940 }
941 }
942 return false;
943 }
944
Mike Lockwood365e03e2010-12-08 16:08:01 -0800945 private int getObjectFilePath(int handle, char[] outFilePath, long[] outFileLengthFormat) {
Mike Lockwood01788562010-10-11 11:22:19 -0400946 if (handle == 0) {
947 // special case root directory
948 mMediaStoragePath.getChars(0, mMediaStoragePath.length(), outFilePath, 0);
949 outFilePath[mMediaStoragePath.length()] = 0;
Mike Lockwood365e03e2010-12-08 16:08:01 -0800950 outFileLengthFormat[0] = 0;
951 outFileLengthFormat[1] = MtpConstants.FORMAT_ASSOCIATION;
Mike Lockwood01788562010-10-11 11:22:19 -0400952 return MtpConstants.RESPONSE_OK;
953 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400954 Cursor c = null;
955 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700956 c = mMediaProvider.query(mObjectsUri, PATH_FORMAT_PROJECTION,
Jeff Brown75ea64f2012-01-25 19:37:13 -0800957 ID_WHERE, new String[] { Integer.toString(handle) }, null, null);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400958 if (c != null && c.moveToNext()) {
Mike Lockwood1c4e88d2011-01-12 12:38:41 -0500959 String path = c.getString(1);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400960 path.getChars(0, path.length(), outFilePath, 0);
961 outFilePath[path.length()] = 0;
Mike Lockwoodf6f16612012-09-12 15:50:59 -0700962 // File transfers from device to host will likely fail if the size is incorrect.
963 // So to be safe, use the actual file size here.
964 outFileLengthFormat[0] = new File(path).length();
965 outFileLengthFormat[1] = c.getLong(2);
Mike Lockwood5367ab62010-08-30 13:23:02 -0400966 return MtpConstants.RESPONSE_OK;
Mike Lockwood59c777a2010-08-02 10:37:41 -0400967 } else {
Mike Lockwood5367ab62010-08-30 13:23:02 -0400968 return MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400969 }
970 } catch (RemoteException e) {
971 Log.e(TAG, "RemoteException in getObjectFilePath", e);
Mike Lockwood5367ab62010-08-30 13:23:02 -0400972 return MtpConstants.RESPONSE_GENERAL_ERROR;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400973 } finally {
974 if (c != null) {
975 c.close();
976 }
977 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400978 }
979
Mike Lockwood71827742015-01-23 10:50:08 -0800980 private int getObjectFormat(int handle) {
981 Cursor c = null;
982 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700983 c = mMediaProvider.query(mObjectsUri, FORMAT_PROJECTION,
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900984 ID_WHERE, new String[] { Integer.toString(handle) }, null, null);
Mike Lockwood71827742015-01-23 10:50:08 -0800985 if (c != null && c.moveToNext()) {
986 return c.getInt(1);
987 } else {
988 return -1;
989 }
990 } catch (RemoteException e) {
991 Log.e(TAG, "RemoteException in getObjectFilePath", e);
992 return -1;
993 } finally {
994 if (c != null) {
995 c.close();
996 }
997 }
998 }
999
Mike Lockwood59c777a2010-08-02 10:37:41 -04001000 private int deleteFile(int handle) {
Mike Lockwood2837eef2010-08-31 16:25:12 -04001001 mDatabaseModified = true;
Mike Lockwood55f808c2010-12-14 13:14:29 -08001002 String path = null;
1003 int format = 0;
1004
1005 Cursor c = null;
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001006 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001007 c = mMediaProvider.query(mObjectsUri, PATH_FORMAT_PROJECTION,
Jeff Brown75ea64f2012-01-25 19:37:13 -08001008 ID_WHERE, new String[] { Integer.toString(handle) }, null, null);
Mike Lockwood55f808c2010-12-14 13:14:29 -08001009 if (c != null && c.moveToNext()) {
1010 // don't convert to media path here, since we will be matching
1011 // against paths in the database matching /data/media
1012 path = c.getString(1);
Mike Lockwoodf6f16612012-09-12 15:50:59 -07001013 format = c.getInt(2);
Mike Lockwood55f808c2010-12-14 13:14:29 -08001014 } else {
1015 return MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE;
1016 }
1017
1018 if (path == null || format == 0) {
1019 return MtpConstants.RESPONSE_GENERAL_ERROR;
1020 }
1021
Mike Lockwood73e56d92011-12-01 16:58:41 -05001022 // do not allow deleting any of the special subdirectories
1023 if (isStorageSubDirectory(path)) {
1024 return MtpConstants.RESPONSE_OBJECT_WRITE_PROTECTED;
1025 }
1026
Mike Lockwood55f808c2010-12-14 13:14:29 -08001027 if (format == MtpConstants.FORMAT_ASSOCIATION) {
1028 // recursive case - delete all children first
1029 Uri uri = Files.getMtpObjectsUri(mVolumeName);
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001030 int count = mMediaProvider.delete(uri,
Mike Lockwood1e855d92012-06-26 16:31:41 -07001031 // the 'like' makes it use the index, the 'lower()' makes it correct
1032 // when the path contains sqlite wildcard characters
1033 "_data LIKE ?1 AND lower(substr(_data,1,?2))=lower(?3)",
1034 new String[] { path + "/%",Integer.toString(path.length() + 1), path + "/"});
Mike Lockwood55f808c2010-12-14 13:14:29 -08001035 }
1036
1037 Uri uri = Files.getMtpObjectsUri(mVolumeName, handle);
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001038 if (mMediaProvider.delete(uri, null, null) > 0) {
Marco Nelissenca78f3d2012-01-27 09:43:20 -08001039 if (format != MtpConstants.FORMAT_ASSOCIATION
1040 && path.toLowerCase(Locale.US).endsWith("/.nomedia")) {
1041 try {
1042 String parentPath = path.substring(0, path.lastIndexOf("/"));
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001043 mMediaProvider.call(MediaStore.UNHIDE_CALL, parentPath, null);
Marco Nelissenca78f3d2012-01-27 09:43:20 -08001044 } catch (RemoteException e) {
1045 Log.e(TAG, "failed to unhide/rescan for " + path);
1046 }
1047 }
Mike Lockwood5367ab62010-08-30 13:23:02 -04001048 return MtpConstants.RESPONSE_OK;
Mike Lockwood59c777a2010-08-02 10:37:41 -04001049 } else {
Mike Lockwood5367ab62010-08-30 13:23:02 -04001050 return MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood59c777a2010-08-02 10:37:41 -04001051 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001052 } catch (RemoteException e) {
1053 Log.e(TAG, "RemoteException in deleteFile", e);
Mike Lockwood5367ab62010-08-30 13:23:02 -04001054 return MtpConstants.RESPONSE_GENERAL_ERROR;
Mike Lockwood55f808c2010-12-14 13:14:29 -08001055 } finally {
1056 if (c != null) {
1057 c.close();
1058 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001059 }
1060 }
1061
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001062 private int[] getObjectReferences(int handle) {
Mike Lockwood8490e662010-09-09 14:16:22 -04001063 Uri uri = Files.getMtpReferencesUri(mVolumeName, handle);
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001064 Cursor c = null;
1065 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001066 c = mMediaProvider.query(uri, ID_PROJECTION, null, null, null, null);
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001067 if (c == null) {
1068 return null;
1069 }
1070 int count = c.getCount();
1071 if (count > 0) {
1072 int[] result = new int[count];
1073 for (int i = 0; i < count; i++) {
1074 c.moveToNext();
1075 result[i] = c.getInt(0);
1076 }
1077 return result;
1078 }
1079 } catch (RemoteException e) {
1080 Log.e(TAG, "RemoteException in getObjectList", e);
1081 } finally {
1082 if (c != null) {
1083 c.close();
1084 }
1085 }
1086 return null;
1087 }
1088
1089 private int setObjectReferences(int handle, int[] references) {
Mike Lockwood2837eef2010-08-31 16:25:12 -04001090 mDatabaseModified = true;
Mike Lockwood8490e662010-09-09 14:16:22 -04001091 Uri uri = Files.getMtpReferencesUri(mVolumeName, handle);
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001092 int count = references.length;
1093 ContentValues[] valuesList = new ContentValues[count];
1094 for (int i = 0; i < count; i++) {
1095 ContentValues values = new ContentValues();
Mike Lockwood3b2a62e2010-09-08 12:47:57 -04001096 values.put(Files.FileColumns._ID, references[i]);
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001097 valuesList[i] = values;
1098 }
1099 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001100 if (mMediaProvider.bulkInsert(uri, valuesList) > 0) {
Mike Lockwood5367ab62010-08-30 13:23:02 -04001101 return MtpConstants.RESPONSE_OK;
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001102 }
1103 } catch (RemoteException e) {
1104 Log.e(TAG, "RemoteException in setObjectReferences", e);
1105 }
Mike Lockwood5367ab62010-08-30 13:23:02 -04001106 return MtpConstants.RESPONSE_GENERAL_ERROR;
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001107 }
1108
Mike Lockwood2837eef2010-08-31 16:25:12 -04001109 private void sessionStarted() {
Mike Lockwood2837eef2010-08-31 16:25:12 -04001110 mDatabaseModified = false;
1111 }
1112
1113 private void sessionEnded() {
Mike Lockwood2837eef2010-08-31 16:25:12 -04001114 if (mDatabaseModified) {
Mike Lockwooda3156052010-11-20 12:28:27 -05001115 mContext.sendBroadcast(new Intent(MediaStore.ACTION_MTP_SESSION_END));
Mike Lockwood2837eef2010-08-31 16:25:12 -04001116 mDatabaseModified = false;
1117 }
1118 }
1119
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001120 // used by the JNI code
Ashok Bhate2e59322013-12-17 19:04:19 +00001121 private long mNativeContext;
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001122
1123 private native final void native_setup();
1124 private native final void native_finalize();
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001125}