blob: 8437d03f215d62ef2aac968cc7040eb1a1dabccb [file] [log] [blame]
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mike Lockwood0cd01362010-12-30 11:54:33 -050017package android.mtp;
Mike Lockwoodd21eac92010-07-03 00:44:05 -040018
Mike Lockwood56c85242014-03-07 13:29:08 -080019import android.content.BroadcastReceiver;
Jeff Sharkey60cfad82016-01-05 17:30:57 -070020import android.content.ContentProviderClient;
Mike Lockwoodd815f792010-07-12 08:49:01 -040021import android.content.ContentValues;
Jeff Sharkey60cfad82016-01-05 17:30:57 -070022import android.content.Context;
Mike Lockwood2837eef2010-08-31 16:25:12 -040023import android.content.Intent;
Mike Lockwood56c85242014-03-07 13:29:08 -080024import android.content.IntentFilter;
Mike Lockwood775de952011-03-05 17:34:11 -050025import android.content.SharedPreferences;
Mike Lockwoodd21eac92010-07-03 00:44:05 -040026import android.database.Cursor;
Mike Lockwood59e3f0d2010-09-02 14:57:30 -040027import android.database.sqlite.SQLiteDatabase;
Mike Lockwood0cd01362010-12-30 11:54:33 -050028import android.media.MediaScanner;
Mike Lockwoodd21eac92010-07-03 00:44:05 -040029import android.net.Uri;
Mike Lockwood56c85242014-03-07 13:29:08 -080030import android.os.BatteryManager;
Mike Lockwoodd21eac92010-07-03 00:44:05 -040031import android.os.RemoteException;
Jerry Zhang13bb2f42016-12-14 15:39:29 -080032import android.os.SystemProperties;
Mike Lockwooda3156052010-11-20 12:28:27 -050033import android.provider.MediaStore;
Mike Lockwood9a2046f2010-08-03 15:30:09 -040034import android.provider.MediaStore.Audio;
Mike Lockwood3b2a62e2010-09-08 12:47:57 -040035import android.provider.MediaStore.Files;
Mike Lockwoodae078f72010-09-26 12:35:51 -040036import android.provider.MediaStore.MediaColumns;
Mike Lockwoodd21eac92010-07-03 00:44:05 -040037import android.util.Log;
Mike Lockwoodea93fa12010-12-07 10:41:35 -080038import android.view.Display;
39import android.view.WindowManager;
Mike Lockwoodd21eac92010-07-03 00:44:05 -040040
Jeff Sharkey60cfad82016-01-05 17:30:57 -070041import dalvik.system.CloseGuard;
42
Mike Lockwood5ebac832010-10-12 11:33:47 -040043import java.io.File;
Marco Nelissen5f411692014-09-26 16:03:49 -070044import java.io.IOException;
Mike Lockwood7d7fb632010-12-01 18:46:23 -050045import java.util.HashMap;
dujin.chafe464a72011-11-22 12:13:33 +090046import java.util.Locale;
Jeff Sharkey60cfad82016-01-05 17:30:57 -070047import java.util.concurrent.atomic.AtomicBoolean;
Mike Lockwood5ebac832010-10-12 11:33:47 -040048
Mike Lockwoodd21eac92010-07-03 00:44:05 -040049/**
50 * {@hide}
51 */
Jeff Sharkey60cfad82016-01-05 17:30:57 -070052public class MtpDatabase implements AutoCloseable {
Mike Lockwoodd21eac92010-07-03 00:44:05 -040053 private static final String TAG = "MtpDatabase";
54
Mike Lockwood2837eef2010-08-31 16:25:12 -040055 private final Context mContext;
Dianne Hackborn35654b62013-01-14 17:38:02 -080056 private final String mPackageName;
Jeff Sharkey60cfad82016-01-05 17:30:57 -070057 private final ContentProviderClient mMediaProvider;
Mike Lockwoodd21eac92010-07-03 00:44:05 -040058 private final String mVolumeName;
59 private final Uri mObjectsUri;
Jeff Sharkey60cfad82016-01-05 17:30:57 -070060 private final MediaScanner mMediaScanner;
61
62 private final AtomicBoolean mClosed = new AtomicBoolean();
63 private final CloseGuard mCloseGuard = CloseGuard.get();
64
Mike Lockwood73e56d92011-12-01 16:58:41 -050065 // path to primary storage
66 private final String mMediaStoragePath;
67 // if not null, restrict all queries to these subdirectories
68 private final String[] mSubDirectories;
69 // where clause for restricting queries to files in mSubDirectories
70 private String mSubDirectoriesWhere;
71 // where arguments for restricting queries to files in mSubDirectories
72 private String[] mSubDirectoriesWhereArgs;
73
Mike Lockwoodb239b6832011-04-05 10:21:27 -040074 private final HashMap<String, MtpStorage> mStorageMap = new HashMap<String, MtpStorage>();
Mike Lockwoodd21eac92010-07-03 00:44:05 -040075
Mike Lockwood7d7fb632010-12-01 18:46:23 -050076 // cached property groups for single properties
77 private final HashMap<Integer, MtpPropertyGroup> mPropertyGroupsByProperty
78 = new HashMap<Integer, MtpPropertyGroup>();
79
80 // cached property groups for all properties for a given format
81 private final HashMap<Integer, MtpPropertyGroup> mPropertyGroupsByFormat
82 = new HashMap<Integer, MtpPropertyGroup>();
83
Mike Lockwood2837eef2010-08-31 16:25:12 -040084 // true if the database has been modified in the current MTP session
85 private boolean mDatabaseModified;
86
Mike Lockwood775de952011-03-05 17:34:11 -050087 // SharedPreferences for writable MTP device properties
88 private SharedPreferences mDeviceProperties;
Mike Lockwood59e3f0d2010-09-02 14:57:30 -040089 private static final int DEVICE_PROPERTIES_DATABASE_VERSION = 1;
90
Mike Lockwoodd21eac92010-07-03 00:44:05 -040091 private static final String[] ID_PROJECTION = new String[] {
Mike Lockwood3b2a62e2010-09-08 12:47:57 -040092 Files.FileColumns._ID, // 0
Mike Lockwoodd21eac92010-07-03 00:44:05 -040093 };
Mike Lockwood6a6a3af2010-10-12 14:19:51 -040094 private static final String[] PATH_PROJECTION = new String[] {
Mike Lockwood5ebac832010-10-12 11:33:47 -040095 Files.FileColumns._ID, // 0
96 Files.FileColumns.DATA, // 1
Mike Lockwood5ebac832010-10-12 11:33:47 -040097 };
Mike Lockwood71827742015-01-23 10:50:08 -080098 private static final String[] FORMAT_PROJECTION = new String[] {
99 Files.FileColumns._ID, // 0
100 Files.FileColumns.FORMAT, // 1
101 };
Mike Lockwoodf6f16612012-09-12 15:50:59 -0700102 private static final String[] PATH_FORMAT_PROJECTION = new String[] {
Mike Lockwood3b2a62e2010-09-08 12:47:57 -0400103 Files.FileColumns._ID, // 0
104 Files.FileColumns.DATA, // 1
Mike Lockwoodf6f16612012-09-12 15:50:59 -0700105 Files.FileColumns.FORMAT, // 2
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400106 };
107 private static final String[] OBJECT_INFO_PROJECTION = new String[] {
Mike Lockwood3b2a62e2010-09-08 12:47:57 -0400108 Files.FileColumns._ID, // 0
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400109 Files.FileColumns.STORAGE_ID, // 1
Mike Lockwood3b2a62e2010-09-08 12:47:57 -0400110 Files.FileColumns.FORMAT, // 2
111 Files.FileColumns.PARENT, // 3
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400112 Files.FileColumns.DATA, // 4
Mike Lockwood1341f1e2013-04-01 10:52:47 -0700113 Files.FileColumns.DATE_ADDED, // 5
114 Files.FileColumns.DATE_MODIFIED, // 6
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400115 };
Mike Lockwood3b2a62e2010-09-08 12:47:57 -0400116 private static final String ID_WHERE = Files.FileColumns._ID + "=?";
Mike Lockwoodbafca212010-12-13 21:50:09 -0800117 private static final String PATH_WHERE = Files.FileColumns.DATA + "=?";
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400118
119 private static final String STORAGE_WHERE = Files.FileColumns.STORAGE_ID + "=?";
Mike Lockwood58e6831c2012-09-11 10:49:34 -0700120 private static final String FORMAT_WHERE = Files.FileColumns.FORMAT + "=?";
121 private static final String PARENT_WHERE = Files.FileColumns.PARENT + "=?";
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400122 private static final String STORAGE_FORMAT_WHERE = STORAGE_WHERE + " AND "
Mike Lockwood3b2a62e2010-09-08 12:47:57 -0400123 + Files.FileColumns.FORMAT + "=?";
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400124 private static final String STORAGE_PARENT_WHERE = STORAGE_WHERE + " AND "
125 + Files.FileColumns.PARENT + "=?";
126 private static final String FORMAT_PARENT_WHERE = FORMAT_WHERE + " AND "
127 + Files.FileColumns.PARENT + "=?";
128 private static final String STORAGE_FORMAT_PARENT_WHERE = STORAGE_FORMAT_WHERE + " AND "
129 + Files.FileColumns.PARENT + "=?";
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400130
Mike Lockwood56c85242014-03-07 13:29:08 -0800131 private MtpServer mServer;
132
133 // read from native code
134 private int mBatteryLevel;
135 private int mBatteryScale;
Mike Lockwoodd815f792010-07-12 08:49:01 -0400136
Jerry Zhang13bb2f42016-12-14 15:39:29 -0800137 private int mDeviceType;
138
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400139 static {
140 System.loadLibrary("media_jni");
141 }
142
Mike Lockwood56c85242014-03-07 13:29:08 -0800143 private BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
144 @Override
145 public void onReceive(Context context, Intent intent) {
146 String action = intent.getAction();
147 if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
148 mBatteryScale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
149 int newLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
150 if (newLevel != mBatteryLevel) {
151 mBatteryLevel = newLevel;
152 if (mServer != null) {
153 // send device property changed event
154 mServer.sendDevicePropertyChanged(
155 MtpConstants.DEVICE_PROPERTY_BATTERY_LEVEL);
156 }
157 }
158 }
159 }
160 };
161
Mike Lockwood73e56d92011-12-01 16:58:41 -0500162 public MtpDatabase(Context context, String volumeName, String storagePath,
163 String[] subDirectories) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400164 native_setup();
165
Mike Lockwood2837eef2010-08-31 16:25:12 -0400166 mContext = context;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800167 mPackageName = context.getPackageName();
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700168 mMediaProvider = context.getContentResolver()
169 .acquireContentProviderClient(MediaStore.AUTHORITY);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400170 mVolumeName = volumeName;
Mike Lockwood01788562010-10-11 11:22:19 -0400171 mMediaStoragePath = storagePath;
Mike Lockwood8490e662010-09-09 14:16:22 -0400172 mObjectsUri = Files.getMtpObjectsUri(volumeName);
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700173 mMediaScanner = new MediaScanner(context, mVolumeName);
dujin.chafe464a72011-11-22 12:13:33 +0900174
Mike Lockwood73e56d92011-12-01 16:58:41 -0500175 mSubDirectories = subDirectories;
176 if (subDirectories != null) {
177 // Compute "where" string for restricting queries to subdirectories
178 StringBuilder builder = new StringBuilder();
179 builder.append("(");
180 int count = subDirectories.length;
181 for (int i = 0; i < count; i++) {
182 builder.append(Files.FileColumns.DATA + "=? OR "
183 + Files.FileColumns.DATA + " LIKE ?");
184 if (i != count - 1) {
185 builder.append(" OR ");
186 }
187 }
188 builder.append(")");
189 mSubDirectoriesWhere = builder.toString();
190
191 // Compute "where" arguments for restricting queries to subdirectories
192 mSubDirectoriesWhereArgs = new String[count * 2];
193 for (int i = 0, j = 0; i < count; i++) {
194 String path = subDirectories[i];
195 mSubDirectoriesWhereArgs[j++] = path;
196 mSubDirectoriesWhereArgs[j++] = path + "/%";
197 }
198 }
199
Mike Lockwood775de952011-03-05 17:34:11 -0500200 initDeviceProperties(context);
Jerry Zhang13bb2f42016-12-14 15:39:29 -0800201 mDeviceType = SystemProperties.getInt("sys.usb.mtp.device_type", 0);
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700202
203 mCloseGuard.open("close");
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400204 }
205
Mike Lockwood56c85242014-03-07 13:29:08 -0800206 public void setServer(MtpServer server) {
207 mServer = server;
208
Marco Nelissen1632fae2014-03-27 13:25:14 -0700209 // always unregister before registering
210 try {
211 mContext.unregisterReceiver(mBatteryReceiver);
212 } catch (IllegalArgumentException e) {
213 // wasn't previously registered, ignore
214 }
215
Mike Lockwood56c85242014-03-07 13:29:08 -0800216 // register for battery notifications when we are connected
217 if (server != null) {
218 mContext.registerReceiver(mBatteryReceiver,
219 new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Mike Lockwood56c85242014-03-07 13:29:08 -0800220 }
221 }
222
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400223 @Override
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700224 public void close() {
225 mCloseGuard.close();
226 if (mClosed.compareAndSet(false, true)) {
227 mMediaScanner.close();
228 mMediaProvider.close();
229 native_finalize();
230 }
231 }
232
233 @Override
Mike Lockwooddbead322010-08-30 09:27:55 -0400234 protected void finalize() throws Throwable {
235 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700236 mCloseGuard.warnIfOpen();
237 close();
Mike Lockwooddbead322010-08-30 09:27:55 -0400238 } finally {
239 super.finalize();
240 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400241 }
242
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400243 public void addStorage(MtpStorage storage) {
244 mStorageMap.put(storage.getPath(), storage);
245 }
246
247 public void removeStorage(MtpStorage storage) {
248 mStorageMap.remove(storage.getPath());
249 }
250
Mike Lockwood775de952011-03-05 17:34:11 -0500251 private void initDeviceProperties(Context context) {
252 final String devicePropertiesName = "device-properties";
253 mDeviceProperties = context.getSharedPreferences(devicePropertiesName, Context.MODE_PRIVATE);
254 File databaseFile = context.getDatabasePath(devicePropertiesName);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400255
Mike Lockwood775de952011-03-05 17:34:11 -0500256 if (databaseFile.exists()) {
257 // for backward compatibility - read device properties from sqlite database
258 // and migrate them to shared prefs
259 SQLiteDatabase db = null;
260 Cursor c = null;
261 try {
262 db = context.openOrCreateDatabase("device-properties", Context.MODE_PRIVATE, null);
263 if (db != null) {
264 c = db.query("properties", new String[] { "_id", "code", "value" },
265 null, null, null, null, null);
266 if (c != null) {
267 SharedPreferences.Editor e = mDeviceProperties.edit();
268 while (c.moveToNext()) {
269 String name = c.getString(1);
270 String value = c.getString(2);
271 e.putString(name, value);
272 }
273 e.commit();
274 }
275 }
276 } catch (Exception e) {
277 Log.e(TAG, "failed to migrate device properties", e);
278 } finally {
279 if (c != null) c.close();
280 if (db != null) db.close();
281 }
jangwon.lee3ed02532013-07-22 19:52:48 +0900282 context.deleteDatabase(devicePropertiesName);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400283 }
284 }
285
Mike Lockwood73e56d92011-12-01 16:58:41 -0500286 // check to see if the path is contained in one of our storage subdirectories
287 // returns true if we have no special subdirectories
288 private boolean inStorageSubDirectory(String path) {
289 if (mSubDirectories == null) return true;
290 if (path == null) return false;
291
292 boolean allowed = false;
293 int pathLength = path.length();
294 for (int i = 0; i < mSubDirectories.length && !allowed; i++) {
295 String subdir = mSubDirectories[i];
296 int subdirLength = subdir.length();
297 if (subdirLength < pathLength &&
298 path.charAt(subdirLength) == '/' &&
299 path.startsWith(subdir)) {
300 allowed = true;
301 }
302 }
303 return allowed;
304 }
305
306 // check to see if the path matches one of our storage subdirectories
307 // returns true if we have no special subdirectories
308 private boolean isStorageSubDirectory(String path) {
309 if (mSubDirectories == null) return false;
310 for (int i = 0; i < mSubDirectories.length; i++) {
311 if (path.equals(mSubDirectories[i])) {
312 return true;
313 }
314 }
315 return false;
316 }
317
Marco Nelissen5f411692014-09-26 16:03:49 -0700318 // returns true if the path is in the storage root
319 private boolean inStorageRoot(String path) {
320 try {
321 File f = new File(path);
322 String canonical = f.getCanonicalPath();
Marco Nelissenc1fda122014-10-15 14:32:22 -0700323 for (String root: mStorageMap.keySet()) {
324 if (canonical.startsWith(root)) {
325 return true;
326 }
Marco Nelissen5f411692014-09-26 16:03:49 -0700327 }
328 } catch (IOException e) {
329 // ignore
330 }
331 return false;
332 }
333
Mike Lockwoodd815f792010-07-12 08:49:01 -0400334 private int beginSendObject(String path, int format, int parent,
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400335 int storageId, long size, long modified) {
Marco Nelissen5f411692014-09-26 16:03:49 -0700336 // if the path is outside of the storage root, do not allow access
337 if (!inStorageRoot(path)) {
338 Log.e(TAG, "attempt to put file outside of storage area: " + path);
339 return -1;
340 }
Mike Lockwood73e56d92011-12-01 16:58:41 -0500341 // if mSubDirectories is not null, do not allow copying files to any other locations
342 if (!inStorageSubDirectory(path)) return -1;
343
344 // make sure the object does not exist
Mike Lockwoodbafca212010-12-13 21:50:09 -0800345 if (path != null) {
346 Cursor c = null;
347 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700348 c = mMediaProvider.query(mObjectsUri, ID_PROJECTION, PATH_WHERE,
Jeff Brown75ea64f2012-01-25 19:37:13 -0800349 new String[] { path }, null, null);
Mike Lockwoodbafca212010-12-13 21:50:09 -0800350 if (c != null && c.getCount() > 0) {
351 Log.w(TAG, "file already exists in beginSendObject: " + path);
352 return -1;
353 }
354 } catch (RemoteException e) {
355 Log.e(TAG, "RemoteException in beginSendObject", e);
356 } finally {
357 if (c != null) {
358 c.close();
359 }
360 }
361 }
362
Mike Lockwood2837eef2010-08-31 16:25:12 -0400363 mDatabaseModified = true;
Mike Lockwoodd815f792010-07-12 08:49:01 -0400364 ContentValues values = new ContentValues();
Mike Lockwood3b2a62e2010-09-08 12:47:57 -0400365 values.put(Files.FileColumns.DATA, path);
366 values.put(Files.FileColumns.FORMAT, format);
367 values.put(Files.FileColumns.PARENT, parent);
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400368 values.put(Files.FileColumns.STORAGE_ID, storageId);
Mike Lockwood3b2a62e2010-09-08 12:47:57 -0400369 values.put(Files.FileColumns.SIZE, size);
370 values.put(Files.FileColumns.DATE_MODIFIED, modified);
Mike Lockwoodd815f792010-07-12 08:49:01 -0400371
372 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700373 Uri uri = mMediaProvider.insert(mObjectsUri, values);
Mike Lockwoodd815f792010-07-12 08:49:01 -0400374 if (uri != null) {
375 return Integer.parseInt(uri.getPathSegments().get(2));
376 } else {
377 return -1;
378 }
379 } catch (RemoteException e) {
380 Log.e(TAG, "RemoteException in beginSendObject", e);
381 return -1;
382 }
383 }
384
Mike Lockwood7a0bd172011-01-18 11:06:19 -0800385 private void endSendObject(String path, int handle, int format, boolean succeeded) {
Mike Lockwoodd815f792010-07-12 08:49:01 -0400386 if (succeeded) {
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400387 // handle abstract playlists separately
388 // they do not exist in the file system so don't use the media scanner here
Mike Lockwood5367ab62010-08-30 13:23:02 -0400389 if (format == MtpConstants.FORMAT_ABSTRACT_AV_PLAYLIST) {
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400390 // extract name from path
391 String name = path;
392 int lastSlash = name.lastIndexOf('/');
393 if (lastSlash >= 0) {
394 name = name.substring(lastSlash + 1);
395 }
Mike Lockwood8cc6eb12011-01-18 13:13:05 -0800396 // strip trailing ".pla" from the name
397 if (name.endsWith(".pla")) {
398 name = name.substring(0, name.length() - 4);
399 }
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400400
401 ContentValues values = new ContentValues(1);
402 values.put(Audio.Playlists.DATA, path);
403 values.put(Audio.Playlists.NAME, name);
Mike Lockwood0b58c192010-11-17 15:42:09 -0500404 values.put(Files.FileColumns.FORMAT, format);
Mike Lockwood8ed67ac2011-01-18 13:27:25 -0800405 values.put(Files.FileColumns.DATE_MODIFIED, System.currentTimeMillis() / 1000);
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400406 values.put(MediaColumns.MEDIA_SCANNER_NEW_OBJECT_ID, handle);
407 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700408 Uri uri = mMediaProvider.insert(
Dianne Hackborn35654b62013-01-14 17:38:02 -0800409 Audio.Playlists.EXTERNAL_CONTENT_URI, values);
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400410 } catch (RemoteException e) {
411 Log.e(TAG, "RemoteException in endSendObject", e);
412 }
413 } else {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700414 mMediaScanner.scanMtpFile(path, handle, format);
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400415 }
Mike Lockwoodd815f792010-07-12 08:49:01 -0400416 } else {
417 deleteFile(handle);
418 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400419 }
420
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400421 private Cursor createObjectQuery(int storageID, int format, int parent) throws RemoteException {
Mike Lockwood73e56d92011-12-01 16:58:41 -0500422 String where;
423 String[] whereArgs;
424
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400425 if (storageID == 0xFFFFFFFF) {
426 // query all stores
427 if (format == 0) {
428 // query all formats
429 if (parent == 0) {
430 // query all objects
Mike Lockwood73e56d92011-12-01 16:58:41 -0500431 where = null;
432 whereArgs = null;
433 } else {
434 if (parent == 0xFFFFFFFF) {
435 // all objects in root of store
436 parent = 0;
437 }
438 where = PARENT_WHERE;
439 whereArgs = new String[] { Integer.toString(parent) };
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400440 }
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400441 } else {
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400442 // query specific format
443 if (parent == 0) {
444 // query all objects
Mike Lockwood73e56d92011-12-01 16:58:41 -0500445 where = FORMAT_WHERE;
446 whereArgs = new String[] { Integer.toString(format) };
447 } else {
448 if (parent == 0xFFFFFFFF) {
449 // all objects in root of store
450 parent = 0;
451 }
452 where = FORMAT_PARENT_WHERE;
453 whereArgs = new String[] { Integer.toString(format),
454 Integer.toString(parent) };
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400455 }
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400456 }
457 } else {
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400458 // query specific store
459 if (format == 0) {
460 // query all formats
461 if (parent == 0) {
462 // query all objects
Mike Lockwood73e56d92011-12-01 16:58:41 -0500463 where = STORAGE_WHERE;
464 whereArgs = new String[] { Integer.toString(storageID) };
465 } else {
466 if (parent == 0xFFFFFFFF) {
467 // all objects in root of store
468 parent = 0;
469 }
470 where = STORAGE_PARENT_WHERE;
471 whereArgs = new String[] { Integer.toString(storageID),
472 Integer.toString(parent) };
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400473 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400474 } else {
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400475 // query specific format
476 if (parent == 0) {
477 // query all objects
Mike Lockwood73e56d92011-12-01 16:58:41 -0500478 where = STORAGE_FORMAT_WHERE;
479 whereArgs = new String[] { Integer.toString(storageID),
480 Integer.toString(format) };
481 } else {
482 if (parent == 0xFFFFFFFF) {
483 // all objects in root of store
484 parent = 0;
485 }
486 where = STORAGE_FORMAT_PARENT_WHERE;
487 whereArgs = new String[] { Integer.toString(storageID),
488 Integer.toString(format),
489 Integer.toString(parent) };
Mike Lockwood6acc90f2011-06-17 13:44:24 -0400490 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400491 }
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400492 }
Mike Lockwood73e56d92011-12-01 16:58:41 -0500493
494 // if we are restricting queries to mSubDirectories, we need to add the restriction
495 // onto our "where" arguments
496 if (mSubDirectoriesWhere != null) {
497 if (where == null) {
498 where = mSubDirectoriesWhere;
499 whereArgs = mSubDirectoriesWhereArgs;
500 } else {
501 where = where + " AND " + mSubDirectoriesWhere;
502
503 // create new array to hold whereArgs and mSubDirectoriesWhereArgs
504 String[] newWhereArgs =
505 new String[whereArgs.length + mSubDirectoriesWhereArgs.length];
506 int i, j;
507 for (i = 0; i < whereArgs.length; i++) {
508 newWhereArgs[i] = whereArgs[i];
509 }
510 for (j = 0; j < mSubDirectoriesWhereArgs.length; i++, j++) {
511 newWhereArgs[i] = mSubDirectoriesWhereArgs[j];
512 }
513 whereArgs = newWhereArgs;
514 }
515 }
516
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700517 return mMediaProvider.query(mObjectsUri, ID_PROJECTION, where,
Dianne Hackborn35654b62013-01-14 17:38:02 -0800518 whereArgs, null, null);
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400519 }
520
521 private int[] getObjectList(int storageID, int format, int parent) {
522 Cursor c = null;
523 try {
524 c = createObjectQuery(storageID, format, parent);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400525 if (c == null) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400526 return null;
527 }
528 int count = c.getCount();
529 if (count > 0) {
530 int[] result = new int[count];
531 for (int i = 0; i < count; i++) {
532 c.moveToNext();
533 result[i] = c.getInt(0);
534 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400535 return result;
536 }
537 } catch (RemoteException e) {
538 Log.e(TAG, "RemoteException in getObjectList", e);
539 } finally {
540 if (c != null) {
541 c.close();
542 }
543 }
544 return null;
545 }
546
Mike Lockwood7a047c82010-08-02 10:52:20 -0400547 private int getNumObjects(int storageID, int format, int parent) {
Mike Lockwood7a047c82010-08-02 10:52:20 -0400548 Cursor c = null;
549 try {
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400550 c = createObjectQuery(storageID, format, parent);
Mike Lockwood7a047c82010-08-02 10:52:20 -0400551 if (c != null) {
552 return c.getCount();
553 }
554 } catch (RemoteException e) {
555 Log.e(TAG, "RemoteException in getNumObjects", e);
556 } finally {
557 if (c != null) {
558 c.close();
559 }
560 }
561 return -1;
562 }
563
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400564 private int[] getSupportedPlaybackFormats() {
565 return new int[] {
Mike Lockwoode5211692010-09-08 13:50:45 -0400566 // allow transfering arbitrary files
567 MtpConstants.FORMAT_UNDEFINED,
Mike Lockwood12b8a992010-09-23 21:33:29 -0400568
Mike Lockwood792ec842010-09-09 15:30:10 -0400569 MtpConstants.FORMAT_ASSOCIATION,
Mike Lockwood12b8a992010-09-23 21:33:29 -0400570 MtpConstants.FORMAT_TEXT,
571 MtpConstants.FORMAT_HTML,
572 MtpConstants.FORMAT_WAV,
573 MtpConstants.FORMAT_MP3,
574 MtpConstants.FORMAT_MPEG,
575 MtpConstants.FORMAT_EXIF_JPEG,
576 MtpConstants.FORMAT_TIFF_EP,
bo huang240582e2012-02-27 16:27:00 +0800577 MtpConstants.FORMAT_BMP,
Mike Lockwood12b8a992010-09-23 21:33:29 -0400578 MtpConstants.FORMAT_GIF,
579 MtpConstants.FORMAT_JFIF,
580 MtpConstants.FORMAT_PNG,
581 MtpConstants.FORMAT_TIFF,
582 MtpConstants.FORMAT_WMA,
583 MtpConstants.FORMAT_OGG,
584 MtpConstants.FORMAT_AAC,
585 MtpConstants.FORMAT_MP4_CONTAINER,
586 MtpConstants.FORMAT_MP2,
587 MtpConstants.FORMAT_3GP_CONTAINER,
Mike Lockwood792ec842010-09-09 15:30:10 -0400588 MtpConstants.FORMAT_ABSTRACT_AV_PLAYLIST,
Mike Lockwood12b8a992010-09-23 21:33:29 -0400589 MtpConstants.FORMAT_WPL_PLAYLIST,
590 MtpConstants.FORMAT_M3U_PLAYLIST,
591 MtpConstants.FORMAT_PLS_PLAYLIST,
592 MtpConstants.FORMAT_XML_DOCUMENT,
Glenn Kastenf9f223e2011-01-13 11:17:00 -0800593 MtpConstants.FORMAT_FLAC,
Jaesung Chung5a8b9622015-12-18 05:50:21 +0100594 MtpConstants.FORMAT_DNG,
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400595 };
596 }
597
598 private int[] getSupportedCaptureFormats() {
599 // no capture formats yet
600 return null;
601 }
602
Mike Lockwoodae078f72010-09-26 12:35:51 -0400603 static final int[] FILE_PROPERTIES = {
604 // NOTE must match beginning of AUDIO_PROPERTIES, VIDEO_PROPERTIES
605 // and IMAGE_PROPERTIES below
Mike Lockwood5367ab62010-08-30 13:23:02 -0400606 MtpConstants.PROPERTY_STORAGE_ID,
607 MtpConstants.PROPERTY_OBJECT_FORMAT,
Mike Lockwoodd3bfecb2010-09-23 23:04:28 -0400608 MtpConstants.PROPERTY_PROTECTION_STATUS,
Mike Lockwood5367ab62010-08-30 13:23:02 -0400609 MtpConstants.PROPERTY_OBJECT_SIZE,
610 MtpConstants.PROPERTY_OBJECT_FILE_NAME,
Mike Lockwoodd3bfecb2010-09-23 23:04:28 -0400611 MtpConstants.PROPERTY_DATE_MODIFIED,
Mike Lockwood5367ab62010-08-30 13:23:02 -0400612 MtpConstants.PROPERTY_PARENT_OBJECT,
Mike Lockwoodd3bfecb2010-09-23 23:04:28 -0400613 MtpConstants.PROPERTY_PERSISTENT_UID,
614 MtpConstants.PROPERTY_NAME,
Mike Lockwood71827742015-01-23 10:50:08 -0800615 MtpConstants.PROPERTY_DISPLAY_NAME,
Mike Lockwoodae078f72010-09-26 12:35:51 -0400616 MtpConstants.PROPERTY_DATE_ADDED,
617 };
618
619 static final int[] AUDIO_PROPERTIES = {
620 // NOTE must match FILE_PROPERTIES above
621 MtpConstants.PROPERTY_STORAGE_ID,
622 MtpConstants.PROPERTY_OBJECT_FORMAT,
623 MtpConstants.PROPERTY_PROTECTION_STATUS,
624 MtpConstants.PROPERTY_OBJECT_SIZE,
625 MtpConstants.PROPERTY_OBJECT_FILE_NAME,
626 MtpConstants.PROPERTY_DATE_MODIFIED,
627 MtpConstants.PROPERTY_PARENT_OBJECT,
628 MtpConstants.PROPERTY_PERSISTENT_UID,
629 MtpConstants.PROPERTY_NAME,
630 MtpConstants.PROPERTY_DISPLAY_NAME,
631 MtpConstants.PROPERTY_DATE_ADDED,
632
633 // audio specific properties
634 MtpConstants.PROPERTY_ARTIST,
635 MtpConstants.PROPERTY_ALBUM_NAME,
636 MtpConstants.PROPERTY_ALBUM_ARTIST,
637 MtpConstants.PROPERTY_TRACK,
638 MtpConstants.PROPERTY_ORIGINAL_RELEASE_DATE,
639 MtpConstants.PROPERTY_DURATION,
640 MtpConstants.PROPERTY_GENRE,
641 MtpConstants.PROPERTY_COMPOSER,
Mike Lockwood92b53bc2014-03-13 14:51:29 -0700642 MtpConstants.PROPERTY_AUDIO_WAVE_CODEC,
643 MtpConstants.PROPERTY_BITRATE_TYPE,
644 MtpConstants.PROPERTY_AUDIO_BITRATE,
645 MtpConstants.PROPERTY_NUMBER_OF_CHANNELS,
646 MtpConstants.PROPERTY_SAMPLE_RATE,
Mike Lockwoodae078f72010-09-26 12:35:51 -0400647 };
648
649 static final int[] VIDEO_PROPERTIES = {
650 // NOTE must match FILE_PROPERTIES above
651 MtpConstants.PROPERTY_STORAGE_ID,
652 MtpConstants.PROPERTY_OBJECT_FORMAT,
653 MtpConstants.PROPERTY_PROTECTION_STATUS,
654 MtpConstants.PROPERTY_OBJECT_SIZE,
655 MtpConstants.PROPERTY_OBJECT_FILE_NAME,
656 MtpConstants.PROPERTY_DATE_MODIFIED,
657 MtpConstants.PROPERTY_PARENT_OBJECT,
658 MtpConstants.PROPERTY_PERSISTENT_UID,
659 MtpConstants.PROPERTY_NAME,
660 MtpConstants.PROPERTY_DISPLAY_NAME,
661 MtpConstants.PROPERTY_DATE_ADDED,
662
663 // video specific properties
664 MtpConstants.PROPERTY_ARTIST,
665 MtpConstants.PROPERTY_ALBUM_NAME,
666 MtpConstants.PROPERTY_DURATION,
667 MtpConstants.PROPERTY_DESCRIPTION,
668 };
669
670 static final int[] IMAGE_PROPERTIES = {
671 // NOTE must match FILE_PROPERTIES above
672 MtpConstants.PROPERTY_STORAGE_ID,
673 MtpConstants.PROPERTY_OBJECT_FORMAT,
674 MtpConstants.PROPERTY_PROTECTION_STATUS,
675 MtpConstants.PROPERTY_OBJECT_SIZE,
676 MtpConstants.PROPERTY_OBJECT_FILE_NAME,
677 MtpConstants.PROPERTY_DATE_MODIFIED,
678 MtpConstants.PROPERTY_PARENT_OBJECT,
679 MtpConstants.PROPERTY_PERSISTENT_UID,
680 MtpConstants.PROPERTY_NAME,
681 MtpConstants.PROPERTY_DISPLAY_NAME,
682 MtpConstants.PROPERTY_DATE_ADDED,
683
684 // image specific properties
685 MtpConstants.PROPERTY_DESCRIPTION,
686 };
687
688 private int[] getSupportedObjectProperties(int format) {
689 switch (format) {
690 case MtpConstants.FORMAT_MP3:
691 case MtpConstants.FORMAT_WAV:
692 case MtpConstants.FORMAT_WMA:
693 case MtpConstants.FORMAT_OGG:
694 case MtpConstants.FORMAT_AAC:
695 return AUDIO_PROPERTIES;
696 case MtpConstants.FORMAT_MPEG:
697 case MtpConstants.FORMAT_3GP_CONTAINER:
698 case MtpConstants.FORMAT_WMV:
699 return VIDEO_PROPERTIES;
700 case MtpConstants.FORMAT_EXIF_JPEG:
701 case MtpConstants.FORMAT_GIF:
702 case MtpConstants.FORMAT_PNG:
703 case MtpConstants.FORMAT_BMP:
Jaesung Chung5a8b9622015-12-18 05:50:21 +0100704 case MtpConstants.FORMAT_DNG:
Mike Lockwoodae078f72010-09-26 12:35:51 -0400705 return IMAGE_PROPERTIES;
706 default:
707 return FILE_PROPERTIES;
708 }
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400709 }
710
711 private int[] getSupportedDeviceProperties() {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400712 return new int[] {
713 MtpConstants.DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER,
714 MtpConstants.DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME,
Mike Lockwoodea93fa12010-12-07 10:41:35 -0800715 MtpConstants.DEVICE_PROPERTY_IMAGE_SIZE,
Mike Lockwood56c85242014-03-07 13:29:08 -0800716 MtpConstants.DEVICE_PROPERTY_BATTERY_LEVEL,
Jerry Zhang13bb2f42016-12-14 15:39:29 -0800717 MtpConstants.DEVICE_PROPERTY_PERCEIVED_DEVICE_TYPE,
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400718 };
Mike Lockwood4b322ce2010-08-10 07:37:50 -0400719 }
720
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900721 private MtpPropertyList getObjectPropertyList(int handle, int format, int property,
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400722 int groupCode, int depth) {
723 // FIXME - implement group support
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400724 if (groupCode != 0) {
725 return new MtpPropertyList(0, MtpConstants.RESPONSE_SPECIFICATION_BY_GROUP_UNSUPPORTED);
726 }
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400727
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500728 MtpPropertyGroup propertyGroup;
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900729 if (property == 0xffffffff) {
730 if (format == 0 && handle != 0 && handle != 0xffffffff) {
Mike Lockwood71827742015-01-23 10:50:08 -0800731 // return properties based on the object's format
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900732 format = getObjectFormat(handle);
Mike Lockwood71827742015-01-23 10:50:08 -0800733 }
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900734 propertyGroup = mPropertyGroupsByFormat.get(format);
735 if (propertyGroup == null) {
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500736 int[] propertyList = getSupportedObjectProperties(format);
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700737 propertyGroup = new MtpPropertyGroup(this, mMediaProvider,
Dianne Hackborn35654b62013-01-14 17:38:02 -0800738 mVolumeName, propertyList);
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900739 mPropertyGroupsByFormat.put(format, propertyGroup);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400740 }
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500741 } else {
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900742 propertyGroup = mPropertyGroupsByProperty.get(property);
743 if (propertyGroup == null) {
744 final int[] propertyList = new int[] { property };
745 propertyGroup = new MtpPropertyGroup(
746 this, mMediaProvider, mVolumeName, propertyList);
747 mPropertyGroupsByProperty.put(property, propertyGroup);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400748 }
749 }
Mike Lockwood7d7fb632010-12-01 18:46:23 -0500750
Daichi Hirono486ad2e2016-02-29 17:28:47 +0900751 return propertyGroup.getPropertyList(handle, format, depth);
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400752 }
753
Mike Lockwood5ebac832010-10-12 11:33:47 -0400754 private int renameFile(int handle, String newName) {
755 Cursor c = null;
756
757 // first compute current path
758 String path = null;
Mike Lockwood5ebac832010-10-12 11:33:47 -0400759 String[] whereArgs = new String[] { Integer.toString(handle) };
760 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700761 c = mMediaProvider.query(mObjectsUri, PATH_PROJECTION, ID_WHERE,
Dianne Hackborn35654b62013-01-14 17:38:02 -0800762 whereArgs, null, null);
Mike Lockwood5ebac832010-10-12 11:33:47 -0400763 if (c != null && c.moveToNext()) {
Mike Lockwood1c4e88d2011-01-12 12:38:41 -0500764 path = c.getString(1);
Mike Lockwood5ebac832010-10-12 11:33:47 -0400765 }
766 } catch (RemoteException e) {
767 Log.e(TAG, "RemoteException in getObjectFilePath", e);
768 return MtpConstants.RESPONSE_GENERAL_ERROR;
769 } finally {
770 if (c != null) {
771 c.close();
772 }
773 }
774 if (path == null) {
775 return MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE;
776 }
Mike Lockwood5ebac832010-10-12 11:33:47 -0400777
Mike Lockwood73e56d92011-12-01 16:58:41 -0500778 // do not allow renaming any of the special subdirectories
779 if (isStorageSubDirectory(path)) {
780 return MtpConstants.RESPONSE_OBJECT_WRITE_PROTECTED;
781 }
782
Mike Lockwood5ebac832010-10-12 11:33:47 -0400783 // now rename the file. make sure this succeeds before updating database
784 File oldFile = new File(path);
785 int lastSlash = path.lastIndexOf('/');
786 if (lastSlash <= 1) {
787 return MtpConstants.RESPONSE_GENERAL_ERROR;
788 }
789 String newPath = path.substring(0, lastSlash + 1) + newName;
790 File newFile = new File(newPath);
791 boolean success = oldFile.renameTo(newFile);
Mike Lockwood5ebac832010-10-12 11:33:47 -0400792 if (!success) {
Mike Lockwoodf26a5862011-01-21 21:00:54 -0800793 Log.w(TAG, "renaming "+ path + " to " + newPath + " failed");
Mike Lockwood5ebac832010-10-12 11:33:47 -0400794 return MtpConstants.RESPONSE_GENERAL_ERROR;
795 }
796
797 // finally update database
798 ContentValues values = new ContentValues();
799 values.put(Files.FileColumns.DATA, newPath);
800 int updated = 0;
801 try {
Mike Lockwood6a6a3af2010-10-12 14:19:51 -0400802 // note - we are relying on a special case in MediaProvider.update() to update
803 // the paths for all children in the case where this is a directory.
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700804 updated = mMediaProvider.update(mObjectsUri, values, ID_WHERE, whereArgs);
Mike Lockwood5ebac832010-10-12 11:33:47 -0400805 } catch (RemoteException e) {
806 Log.e(TAG, "RemoteException in mMediaProvider.update", e);
807 }
Mike Lockwood6a6a3af2010-10-12 14:19:51 -0400808 if (updated == 0) {
Mike Lockwood5ebac832010-10-12 11:33:47 -0400809 Log.e(TAG, "Unable to update path for " + path + " to " + newPath);
810 // this shouldn't happen, but if it does we need to rename the file to its original name
811 newFile.renameTo(oldFile);
812 return MtpConstants.RESPONSE_GENERAL_ERROR;
813 }
814
Marco Nelissenca78f3d2012-01-27 09:43:20 -0800815 // check if nomedia status changed
816 if (newFile.isDirectory()) {
817 // for directories, check if renamed from something hidden to something non-hidden
818 if (oldFile.getName().startsWith(".") && !newPath.startsWith(".")) {
819 // directory was unhidden
820 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700821 mMediaProvider.call(MediaStore.UNHIDE_CALL, newPath, null);
Marco Nelissenca78f3d2012-01-27 09:43:20 -0800822 } catch (RemoteException e) {
823 Log.e(TAG, "failed to unhide/rescan for " + newPath);
824 }
825 }
826 } else {
827 // for files, check if renamed from .nomedia to something else
828 if (oldFile.getName().toLowerCase(Locale.US).equals(".nomedia")
829 && !newPath.toLowerCase(Locale.US).equals(".nomedia")) {
830 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700831 mMediaProvider.call(MediaStore.UNHIDE_CALL, oldFile.getParent(), null);
Marco Nelissenca78f3d2012-01-27 09:43:20 -0800832 } catch (RemoteException e) {
833 Log.e(TAG, "failed to unhide/rescan for " + newPath);
834 }
835 }
836 }
837
Mike Lockwood5ebac832010-10-12 11:33:47 -0400838 return MtpConstants.RESPONSE_OK;
839 }
840
Jerry Zhang952558d42017-09-26 17:49:52 -0700841 private int moveObject(int handle, int newParent, String newPath) {
842 String[] whereArgs = new String[] { Integer.toString(handle) };
843
844 // do not allow renaming any of the special subdirectories
845 if (isStorageSubDirectory(newPath)) {
846 return MtpConstants.RESPONSE_OBJECT_WRITE_PROTECTED;
847 }
848
849 // update database
850 ContentValues values = new ContentValues();
851 values.put(Files.FileColumns.DATA, newPath);
852 values.put(Files.FileColumns.PARENT, newParent);
853 int updated = 0;
854 try {
855 // note - we are relying on a special case in MediaProvider.update() to update
856 // the paths for all children in the case where this is a directory.
857 updated = mMediaProvider.update(mObjectsUri, values, ID_WHERE, whereArgs);
858 } catch (RemoteException e) {
859 Log.e(TAG, "RemoteException in mMediaProvider.update", e);
860 }
861 if (updated == 0) {
862 Log.e(TAG, "Unable to update path for " + handle + " to " + newPath);
863 return MtpConstants.RESPONSE_GENERAL_ERROR;
864 }
865 return MtpConstants.RESPONSE_OK;
866 }
867
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400868 private int setObjectProperty(int handle, int property,
869 long intValue, String stringValue) {
Mike Lockwood5ebac832010-10-12 11:33:47 -0400870 switch (property) {
871 case MtpConstants.PROPERTY_OBJECT_FILE_NAME:
872 return renameFile(handle, stringValue);
873
874 default:
875 return MtpConstants.RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
876 }
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400877 }
878
879 private int getDeviceProperty(int property, long[] outIntValue, char[] outStringValue) {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400880 switch (property) {
881 case MtpConstants.DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER:
882 case MtpConstants.DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME:
Mike Lockwood775de952011-03-05 17:34:11 -0500883 // writable string properties kept in shared preferences
884 String value = mDeviceProperties.getString(Integer.toString(property), "");
885 int length = value.length();
886 if (length > 255) {
887 length = 255;
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400888 }
Mike Lockwood775de952011-03-05 17:34:11 -0500889 value.getChars(0, length, outStringValue, 0);
890 outStringValue[length] = 0;
891 return MtpConstants.RESPONSE_OK;
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400892
Mike Lockwoodea93fa12010-12-07 10:41:35 -0800893 case MtpConstants.DEVICE_PROPERTY_IMAGE_SIZE:
894 // use screen size as max image size
895 Display display = ((WindowManager)mContext.getSystemService(
896 Context.WINDOW_SERVICE)).getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -0700897 int width = display.getMaximumSizeDimension();
898 int height = display.getMaximumSizeDimension();
Mike Lockwoodea93fa12010-12-07 10:41:35 -0800899 String imageSize = Integer.toString(width) + "x" + Integer.toString(height);
900 imageSize.getChars(0, imageSize.length(), outStringValue, 0);
901 outStringValue[imageSize.length()] = 0;
902 return MtpConstants.RESPONSE_OK;
903
Jerry Zhang13bb2f42016-12-14 15:39:29 -0800904 case MtpConstants.DEVICE_PROPERTY_PERCEIVED_DEVICE_TYPE:
905 outIntValue[0] = mDeviceType;
906 return MtpConstants.RESPONSE_OK;
907
Mike Lockwood56c85242014-03-07 13:29:08 -0800908 // DEVICE_PROPERTY_BATTERY_LEVEL is implemented in the JNI code
909
Mike Lockwoodea93fa12010-12-07 10:41:35 -0800910 default:
911 return MtpConstants.RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
912 }
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400913 }
914
915 private int setDeviceProperty(int property, long intValue, String stringValue) {
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400916 switch (property) {
917 case MtpConstants.DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER:
918 case MtpConstants.DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME:
Mike Lockwood775de952011-03-05 17:34:11 -0500919 // writable string properties kept in shared prefs
920 SharedPreferences.Editor e = mDeviceProperties.edit();
921 e.putString(Integer.toString(property), stringValue);
922 return (e.commit() ? MtpConstants.RESPONSE_OK
923 : MtpConstants.RESPONSE_GENERAL_ERROR);
Mike Lockwood59e3f0d2010-09-02 14:57:30 -0400924 }
925
926 return MtpConstants.RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
927 }
928
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400929 private boolean getObjectInfo(int handle, int[] outStorageFormatParent,
Mike Lockwood1341f1e2013-04-01 10:52:47 -0700930 char[] outName, long[] outCreatedModified) {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400931 Cursor c = null;
932 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700933 c = mMediaProvider.query(mObjectsUri, OBJECT_INFO_PROJECTION,
Jeff Brown75ea64f2012-01-25 19:37:13 -0800934 ID_WHERE, new String[] { Integer.toString(handle) }, null, null);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400935 if (c != null && c.moveToNext()) {
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400936 outStorageFormatParent[0] = c.getInt(1);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400937 outStorageFormatParent[1] = c.getInt(2);
938 outStorageFormatParent[2] = c.getInt(3);
939
940 // extract name from path
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400941 String path = c.getString(4);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400942 int lastSlash = path.lastIndexOf('/');
943 int start = (lastSlash >= 0 ? lastSlash + 1 : 0);
944 int end = path.length();
945 if (end - start > 255) {
946 end = start + 255;
947 }
948 path.getChars(start, end, outName, 0);
949 outName[end - start] = 0;
950
Mike Lockwood1341f1e2013-04-01 10:52:47 -0700951 outCreatedModified[0] = c.getLong(5);
952 outCreatedModified[1] = c.getLong(6);
953 // use modification date as creation date if date added is not set
954 if (outCreatedModified[0] == 0) {
955 outCreatedModified[0] = outCreatedModified[1];
956 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400957 return true;
958 }
959 } catch (RemoteException e) {
Mike Lockwood2b5f9ad12010-10-29 19:16:27 -0400960 Log.e(TAG, "RemoteException in getObjectInfo", e);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400961 } finally {
962 if (c != null) {
963 c.close();
964 }
965 }
966 return false;
967 }
968
Mike Lockwood365e03e2010-12-08 16:08:01 -0800969 private int getObjectFilePath(int handle, char[] outFilePath, long[] outFileLengthFormat) {
Mike Lockwood01788562010-10-11 11:22:19 -0400970 if (handle == 0) {
971 // special case root directory
972 mMediaStoragePath.getChars(0, mMediaStoragePath.length(), outFilePath, 0);
973 outFilePath[mMediaStoragePath.length()] = 0;
Mike Lockwood365e03e2010-12-08 16:08:01 -0800974 outFileLengthFormat[0] = 0;
975 outFileLengthFormat[1] = MtpConstants.FORMAT_ASSOCIATION;
Mike Lockwood01788562010-10-11 11:22:19 -0400976 return MtpConstants.RESPONSE_OK;
977 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400978 Cursor c = null;
979 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -0700980 c = mMediaProvider.query(mObjectsUri, PATH_FORMAT_PROJECTION,
Jeff Brown75ea64f2012-01-25 19:37:13 -0800981 ID_WHERE, new String[] { Integer.toString(handle) }, null, null);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400982 if (c != null && c.moveToNext()) {
Mike Lockwood1c4e88d2011-01-12 12:38:41 -0500983 String path = c.getString(1);
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400984 path.getChars(0, path.length(), outFilePath, 0);
985 outFilePath[path.length()] = 0;
Mike Lockwoodf6f16612012-09-12 15:50:59 -0700986 // File transfers from device to host will likely fail if the size is incorrect.
987 // So to be safe, use the actual file size here.
988 outFileLengthFormat[0] = new File(path).length();
989 outFileLengthFormat[1] = c.getLong(2);
Mike Lockwood5367ab62010-08-30 13:23:02 -0400990 return MtpConstants.RESPONSE_OK;
Mike Lockwood59c777a2010-08-02 10:37:41 -0400991 } else {
Mike Lockwood5367ab62010-08-30 13:23:02 -0400992 return MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400993 }
994 } catch (RemoteException e) {
995 Log.e(TAG, "RemoteException in getObjectFilePath", e);
Mike Lockwood5367ab62010-08-30 13:23:02 -0400996 return MtpConstants.RESPONSE_GENERAL_ERROR;
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400997 } finally {
998 if (c != null) {
999 c.close();
1000 }
1001 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001002 }
1003
Mike Lockwood71827742015-01-23 10:50:08 -08001004 private int getObjectFormat(int handle) {
1005 Cursor c = null;
1006 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001007 c = mMediaProvider.query(mObjectsUri, FORMAT_PROJECTION,
Daichi Hirono486ad2e2016-02-29 17:28:47 +09001008 ID_WHERE, new String[] { Integer.toString(handle) }, null, null);
Mike Lockwood71827742015-01-23 10:50:08 -08001009 if (c != null && c.moveToNext()) {
1010 return c.getInt(1);
1011 } else {
1012 return -1;
1013 }
1014 } catch (RemoteException e) {
1015 Log.e(TAG, "RemoteException in getObjectFilePath", e);
1016 return -1;
1017 } finally {
1018 if (c != null) {
1019 c.close();
1020 }
1021 }
1022 }
1023
Mike Lockwood59c777a2010-08-02 10:37:41 -04001024 private int deleteFile(int handle) {
Mike Lockwood2837eef2010-08-31 16:25:12 -04001025 mDatabaseModified = true;
Mike Lockwood55f808c2010-12-14 13:14:29 -08001026 String path = null;
1027 int format = 0;
1028
1029 Cursor c = null;
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001030 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001031 c = mMediaProvider.query(mObjectsUri, PATH_FORMAT_PROJECTION,
Jeff Brown75ea64f2012-01-25 19:37:13 -08001032 ID_WHERE, new String[] { Integer.toString(handle) }, null, null);
Mike Lockwood55f808c2010-12-14 13:14:29 -08001033 if (c != null && c.moveToNext()) {
1034 // don't convert to media path here, since we will be matching
1035 // against paths in the database matching /data/media
1036 path = c.getString(1);
Mike Lockwoodf6f16612012-09-12 15:50:59 -07001037 format = c.getInt(2);
Mike Lockwood55f808c2010-12-14 13:14:29 -08001038 } else {
1039 return MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE;
1040 }
1041
1042 if (path == null || format == 0) {
1043 return MtpConstants.RESPONSE_GENERAL_ERROR;
1044 }
1045
Mike Lockwood73e56d92011-12-01 16:58:41 -05001046 // do not allow deleting any of the special subdirectories
1047 if (isStorageSubDirectory(path)) {
1048 return MtpConstants.RESPONSE_OBJECT_WRITE_PROTECTED;
1049 }
1050
Mike Lockwood55f808c2010-12-14 13:14:29 -08001051 if (format == MtpConstants.FORMAT_ASSOCIATION) {
1052 // recursive case - delete all children first
1053 Uri uri = Files.getMtpObjectsUri(mVolumeName);
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001054 int count = mMediaProvider.delete(uri,
Mike Lockwood1e855d92012-06-26 16:31:41 -07001055 // the 'like' makes it use the index, the 'lower()' makes it correct
1056 // when the path contains sqlite wildcard characters
1057 "_data LIKE ?1 AND lower(substr(_data,1,?2))=lower(?3)",
1058 new String[] { path + "/%",Integer.toString(path.length() + 1), path + "/"});
Mike Lockwood55f808c2010-12-14 13:14:29 -08001059 }
1060
1061 Uri uri = Files.getMtpObjectsUri(mVolumeName, handle);
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001062 if (mMediaProvider.delete(uri, null, null) > 0) {
Marco Nelissenca78f3d2012-01-27 09:43:20 -08001063 if (format != MtpConstants.FORMAT_ASSOCIATION
1064 && path.toLowerCase(Locale.US).endsWith("/.nomedia")) {
1065 try {
1066 String parentPath = path.substring(0, path.lastIndexOf("/"));
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001067 mMediaProvider.call(MediaStore.UNHIDE_CALL, parentPath, null);
Marco Nelissenca78f3d2012-01-27 09:43:20 -08001068 } catch (RemoteException e) {
1069 Log.e(TAG, "failed to unhide/rescan for " + path);
1070 }
1071 }
Mike Lockwood5367ab62010-08-30 13:23:02 -04001072 return MtpConstants.RESPONSE_OK;
Mike Lockwood59c777a2010-08-02 10:37:41 -04001073 } else {
Mike Lockwood5367ab62010-08-30 13:23:02 -04001074 return MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE;
Mike Lockwood59c777a2010-08-02 10:37:41 -04001075 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001076 } catch (RemoteException e) {
1077 Log.e(TAG, "RemoteException in deleteFile", e);
Mike Lockwood5367ab62010-08-30 13:23:02 -04001078 return MtpConstants.RESPONSE_GENERAL_ERROR;
Mike Lockwood55f808c2010-12-14 13:14:29 -08001079 } finally {
1080 if (c != null) {
1081 c.close();
1082 }
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001083 }
1084 }
1085
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001086 private int[] getObjectReferences(int handle) {
Mike Lockwood8490e662010-09-09 14:16:22 -04001087 Uri uri = Files.getMtpReferencesUri(mVolumeName, handle);
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001088 Cursor c = null;
1089 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001090 c = mMediaProvider.query(uri, ID_PROJECTION, null, null, null, null);
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001091 if (c == null) {
1092 return null;
1093 }
1094 int count = c.getCount();
1095 if (count > 0) {
1096 int[] result = new int[count];
1097 for (int i = 0; i < count; i++) {
1098 c.moveToNext();
1099 result[i] = c.getInt(0);
1100 }
1101 return result;
1102 }
1103 } catch (RemoteException e) {
1104 Log.e(TAG, "RemoteException in getObjectList", e);
1105 } finally {
1106 if (c != null) {
1107 c.close();
1108 }
1109 }
1110 return null;
1111 }
1112
1113 private int setObjectReferences(int handle, int[] references) {
Mike Lockwood2837eef2010-08-31 16:25:12 -04001114 mDatabaseModified = true;
Mike Lockwood8490e662010-09-09 14:16:22 -04001115 Uri uri = Files.getMtpReferencesUri(mVolumeName, handle);
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001116 int count = references.length;
1117 ContentValues[] valuesList = new ContentValues[count];
1118 for (int i = 0; i < count; i++) {
1119 ContentValues values = new ContentValues();
Mike Lockwood3b2a62e2010-09-08 12:47:57 -04001120 values.put(Files.FileColumns._ID, references[i]);
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001121 valuesList[i] = values;
1122 }
1123 try {
Jeff Sharkey60cfad82016-01-05 17:30:57 -07001124 if (mMediaProvider.bulkInsert(uri, valuesList) > 0) {
Mike Lockwood5367ab62010-08-30 13:23:02 -04001125 return MtpConstants.RESPONSE_OK;
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001126 }
1127 } catch (RemoteException e) {
1128 Log.e(TAG, "RemoteException in setObjectReferences", e);
1129 }
Mike Lockwood5367ab62010-08-30 13:23:02 -04001130 return MtpConstants.RESPONSE_GENERAL_ERROR;
Mike Lockwood9a2046f2010-08-03 15:30:09 -04001131 }
1132
Mike Lockwood2837eef2010-08-31 16:25:12 -04001133 private void sessionStarted() {
Mike Lockwood2837eef2010-08-31 16:25:12 -04001134 mDatabaseModified = false;
1135 }
1136
1137 private void sessionEnded() {
Mike Lockwood2837eef2010-08-31 16:25:12 -04001138 if (mDatabaseModified) {
Mike Lockwooda3156052010-11-20 12:28:27 -05001139 mContext.sendBroadcast(new Intent(MediaStore.ACTION_MTP_SESSION_END));
Mike Lockwood2837eef2010-08-31 16:25:12 -04001140 mDatabaseModified = false;
1141 }
1142 }
1143
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001144 // used by the JNI code
Ashok Bhate2e59322013-12-17 19:04:19 +00001145 private long mNativeContext;
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001146
1147 private native final void native_setup();
1148 private native final void native_finalize();
Mike Lockwoodd21eac92010-07-03 00:44:05 -04001149}