Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
| 17 | package com.android.mtp; |
| 18 | |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 19 | import android.content.ContentResolver; |
Daichi Hirono | 66fcb4b | 2017-03-23 15:24:13 +0900 | [diff] [blame] | 20 | import android.content.ContentValues; |
Daichi Hirono | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 21 | import android.content.Context; |
Daichi Hirono | 3bb37e7 | 2016-02-29 15:30:56 +0900 | [diff] [blame] | 22 | import android.content.UriPermission; |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 23 | import android.content.res.AssetFileDescriptor; |
Daichi Hirono | 17c8d8b | 2015-10-12 11:28:46 -0700 | [diff] [blame] | 24 | import android.content.res.Resources; |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 25 | import android.database.Cursor; |
Daichi Hirono | 66fcb4b | 2017-03-23 15:24:13 +0900 | [diff] [blame] | 26 | import android.database.DatabaseUtils; |
Daichi Hirono | c18f807 | 2016-02-10 14:59:52 -0800 | [diff] [blame] | 27 | import android.database.MatrixCursor; |
Daichi Hirono | 5884e1f | 2016-03-16 14:36:27 +0900 | [diff] [blame] | 28 | import android.database.sqlite.SQLiteDiskIOException; |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 29 | import android.graphics.Point; |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 30 | import android.media.MediaFile; |
| 31 | import android.mtp.MtpConstants; |
Tomasz Mikolajewski | bb430fa | 2015-08-25 18:34:30 +0900 | [diff] [blame] | 32 | import android.mtp.MtpObjectInfo; |
Daichi Hirono | 3bb37e7 | 2016-02-29 15:30:56 +0900 | [diff] [blame] | 33 | import android.net.Uri; |
Daichi Hirono | c18f807 | 2016-02-10 14:59:52 -0800 | [diff] [blame] | 34 | import android.os.Bundle; |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 35 | import android.os.CancellationSignal; |
Daichi Hirono | fc7fb75 | 2016-03-15 19:19:31 +0900 | [diff] [blame] | 36 | import android.os.FileUtils; |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 37 | import android.os.ParcelFileDescriptor; |
Daichi Hirono | e80ea38 | 2016-11-15 13:07:01 +0900 | [diff] [blame] | 38 | import android.os.ProxyFileDescriptorCallback; |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 39 | import android.os.storage.StorageManager; |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 40 | import android.provider.DocumentsContract.Document; |
Daichi Hirono | b9ffa2a | 2016-11-01 18:41:43 +0900 | [diff] [blame] | 41 | import android.provider.DocumentsContract.Path; |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 42 | import android.provider.DocumentsContract.Root; |
Tomasz Mikolajewski | bb430fa | 2015-08-25 18:34:30 +0900 | [diff] [blame] | 43 | import android.provider.DocumentsContract; |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 44 | import android.provider.DocumentsProvider; |
Daichi Hirono | 3bb37e7 | 2016-02-29 15:30:56 +0900 | [diff] [blame] | 45 | import android.provider.Settings; |
Daichi Hirono | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 46 | import android.system.ErrnoException; |
Daichi Hirono | e80ea38 | 2016-11-15 13:07:01 +0900 | [diff] [blame] | 47 | import android.system.OsConstants; |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 48 | import android.util.Log; |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 49 | |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 50 | import com.android.internal.annotations.GuardedBy; |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 51 | import com.android.internal.annotations.VisibleForTesting; |
| 52 | |
| 53 | import java.io.FileNotFoundException; |
| 54 | import java.io.IOException; |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 55 | import java.util.HashMap; |
Daichi Hirono | b9ffa2a | 2016-11-01 18:41:43 +0900 | [diff] [blame] | 56 | import java.util.LinkedList; |
Daichi Hirono | 3bb37e7 | 2016-02-29 15:30:56 +0900 | [diff] [blame] | 57 | import java.util.List; |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 58 | import java.util.Map; |
Daichi Hirono | acb0e27 | 2016-03-14 21:49:14 +0900 | [diff] [blame] | 59 | import java.util.concurrent.TimeoutException; |
Daichi Hirono | e80ea38 | 2016-11-15 13:07:01 +0900 | [diff] [blame] | 60 | import libcore.io.IoUtils; |
| 61 | |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 62 | /** |
| 63 | * DocumentsProvider for MTP devices. |
| 64 | */ |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 65 | public class MtpDocumentsProvider extends DocumentsProvider { |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 66 | static final String AUTHORITY = "com.android.mtp.documents"; |
| 67 | static final String TAG = "MtpDocumentsProvider"; |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 68 | static final String[] DEFAULT_ROOT_PROJECTION = new String[] { |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 69 | Root.COLUMN_ROOT_ID, Root.COLUMN_FLAGS, Root.COLUMN_ICON, |
| 70 | Root.COLUMN_TITLE, Root.COLUMN_DOCUMENT_ID, |
| 71 | Root.COLUMN_AVAILABLE_BYTES, |
| 72 | }; |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 73 | static final String[] DEFAULT_DOCUMENT_PROJECTION = new String[] { |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 74 | Document.COLUMN_DOCUMENT_ID, Document.COLUMN_MIME_TYPE, |
| 75 | Document.COLUMN_DISPLAY_NAME, Document.COLUMN_LAST_MODIFIED, |
| 76 | Document.COLUMN_FLAGS, Document.COLUMN_SIZE, |
| 77 | }; |
| 78 | |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 79 | static final boolean DEBUG = false; |
Daichi Hirono | 19aa932 | 2016-02-04 14:19:52 +0900 | [diff] [blame] | 80 | |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 81 | private final Object mDeviceListLock = new Object(); |
| 82 | |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 83 | private static MtpDocumentsProvider sSingleton; |
| 84 | |
| 85 | private MtpManager mMtpManager; |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 86 | private ContentResolver mResolver; |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 87 | @GuardedBy("mDeviceListLock") |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 88 | private Map<Integer, DeviceToolkit> mDeviceToolkits; |
Daichi Hirono | 8b9024f | 2015-08-12 12:59:09 +0900 | [diff] [blame] | 89 | private RootScanner mRootScanner; |
Daichi Hirono | 17c8d8b | 2015-10-12 11:28:46 -0700 | [diff] [blame] | 90 | private Resources mResources; |
Daichi Hirono | dc47344 | 2015-11-13 15:42:28 +0900 | [diff] [blame] | 91 | private MtpDatabase mDatabase; |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 92 | private ServiceIntentSender mIntentSender; |
Daichi Hirono | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 93 | private Context mContext; |
Daichi Hirono | e80ea38 | 2016-11-15 13:07:01 +0900 | [diff] [blame] | 94 | private StorageManager mStorageManager; |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 95 | |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 96 | /** |
| 97 | * Provides singleton instance to MtpDocumentsService. |
| 98 | */ |
| 99 | static MtpDocumentsProvider getInstance() { |
| 100 | return sSingleton; |
| 101 | } |
| 102 | |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 103 | @Override |
| 104 | public boolean onCreate() { |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 105 | sSingleton = this; |
Daichi Hirono | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 106 | mContext = getContext(); |
Daichi Hirono | 17c8d8b | 2015-10-12 11:28:46 -0700 | [diff] [blame] | 107 | mResources = getContext().getResources(); |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 108 | mMtpManager = new MtpManager(getContext()); |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 109 | mResolver = getContext().getContentResolver(); |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 110 | mDeviceToolkits = new HashMap<Integer, DeviceToolkit>(); |
Daichi Hirono | 47eb192 | 2015-11-16 13:01:31 +0900 | [diff] [blame] | 111 | mDatabase = new MtpDatabase(getContext(), MtpDatabaseConstants.FLAG_DATABASE_IN_FILE); |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 112 | mRootScanner = new RootScanner(mResolver, mMtpManager, mDatabase); |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 113 | mIntentSender = new ServiceIntentSender(getContext()); |
Daichi Hirono | e80ea38 | 2016-11-15 13:07:01 +0900 | [diff] [blame] | 114 | mStorageManager = getContext().getSystemService(StorageManager.class); |
Daichi Hirono | 3bb37e7 | 2016-02-29 15:30:56 +0900 | [diff] [blame] | 115 | |
| 116 | // Check boot count and cleans database if it's first time to launch MtpDocumentsProvider |
| 117 | // after booting. |
Daichi Hirono | 5884e1f | 2016-03-16 14:36:27 +0900 | [diff] [blame] | 118 | try { |
| 119 | final int bootCount = Settings.Global.getInt(mResolver, Settings.Global.BOOT_COUNT, -1); |
| 120 | final int lastBootCount = mDatabase.getLastBootCount(); |
| 121 | if (bootCount != -1 && bootCount != lastBootCount) { |
| 122 | mDatabase.setLastBootCount(bootCount); |
| 123 | final List<UriPermission> permissions = |
| 124 | mResolver.getOutgoingPersistedUriPermissions(); |
| 125 | final Uri[] uris = new Uri[permissions.size()]; |
| 126 | for (int i = 0; i < permissions.size(); i++) { |
| 127 | uris[i] = permissions.get(i).getUri(); |
| 128 | } |
| 129 | mDatabase.cleanDatabase(uris); |
Daichi Hirono | 3bb37e7 | 2016-02-29 15:30:56 +0900 | [diff] [blame] | 130 | } |
Daichi Hirono | 5884e1f | 2016-03-16 14:36:27 +0900 | [diff] [blame] | 131 | } catch (SQLiteDiskIOException error) { |
| 132 | // It can happen due to disk shortage. |
| 133 | Log.e(TAG, "Failed to clean database.", error); |
| 134 | return false; |
Daichi Hirono | 3bb37e7 | 2016-02-29 15:30:56 +0900 | [diff] [blame] | 135 | } |
| 136 | |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 137 | resume(); |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 138 | return true; |
| 139 | } |
| 140 | |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 141 | @VisibleForTesting |
Daichi Hirono | b36b155 | 2016-01-25 14:26:14 +0900 | [diff] [blame] | 142 | boolean onCreateForTesting( |
Daichi Hirono | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 143 | Context context, |
Daichi Hirono | dc47344 | 2015-11-13 15:42:28 +0900 | [diff] [blame] | 144 | Resources resources, |
| 145 | MtpManager mtpManager, |
| 146 | ContentResolver resolver, |
Daichi Hirono | b36b155 | 2016-01-25 14:26:14 +0900 | [diff] [blame] | 147 | MtpDatabase database, |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 148 | StorageManager storageManager, |
| 149 | ServiceIntentSender intentSender) { |
Daichi Hirono | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 150 | mContext = context; |
Daichi Hirono | 17c8d8b | 2015-10-12 11:28:46 -0700 | [diff] [blame] | 151 | mResources = resources; |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 152 | mMtpManager = mtpManager; |
| 153 | mResolver = resolver; |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 154 | mDeviceToolkits = new HashMap<Integer, DeviceToolkit>(); |
Daichi Hirono | dc47344 | 2015-11-13 15:42:28 +0900 | [diff] [blame] | 155 | mDatabase = database; |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 156 | mRootScanner = new RootScanner(mResolver, mMtpManager, mDatabase); |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 157 | mIntentSender = intentSender; |
Daichi Hirono | e80ea38 | 2016-11-15 13:07:01 +0900 | [diff] [blame] | 158 | mStorageManager = storageManager; |
Daichi Hirono | 3bb37e7 | 2016-02-29 15:30:56 +0900 | [diff] [blame] | 159 | |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 160 | resume(); |
Daichi Hirono | b36b155 | 2016-01-25 14:26:14 +0900 | [diff] [blame] | 161 | return true; |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 162 | } |
| 163 | |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 164 | @Override |
| 165 | public Cursor queryRoots(String[] projection) throws FileNotFoundException { |
Daichi Hirono | 50d17aa | 2015-07-28 15:49:01 +0900 | [diff] [blame] | 166 | if (projection == null) { |
| 167 | projection = MtpDocumentsProvider.DEFAULT_ROOT_PROJECTION; |
| 168 | } |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 169 | final Cursor cursor = mDatabase.queryRoots(mResources, projection); |
Daichi Hirono | 50d17aa | 2015-07-28 15:49:01 +0900 | [diff] [blame] | 170 | cursor.setNotificationUri( |
| 171 | mResolver, DocumentsContract.buildRootsUri(MtpDocumentsProvider.AUTHORITY)); |
| 172 | return cursor; |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public Cursor queryDocument(String documentId, String[] projection) |
| 177 | throws FileNotFoundException { |
Daichi Hirono | e5323b7 | 2015-07-29 16:10:47 +0900 | [diff] [blame] | 178 | if (projection == null) { |
| 179 | projection = MtpDocumentsProvider.DEFAULT_DOCUMENT_PROJECTION; |
| 180 | } |
Daichi Hirono | 66fcb4b | 2017-03-23 15:24:13 +0900 | [diff] [blame] | 181 | final Cursor cursor = mDatabase.queryDocument(documentId, projection); |
| 182 | final int cursorCount = cursor.getCount(); |
| 183 | if (cursorCount == 0) { |
| 184 | cursor.close(); |
| 185 | throw new FileNotFoundException(); |
| 186 | } else if (cursorCount != 1) { |
| 187 | cursor.close(); |
| 188 | Log.wtf(TAG, "Unexpected cursor size: " + cursorCount); |
| 189 | return null; |
| 190 | } |
| 191 | |
| 192 | final Identifier identifier = mDatabase.createIdentifier(documentId); |
| 193 | if (identifier.mDocumentType != MtpDatabaseConstants.DOCUMENT_TYPE_DEVICE) { |
| 194 | return cursor; |
| 195 | } |
| 196 | final String[] storageDocIds = mDatabase.getStorageDocumentIds(documentId); |
| 197 | if (storageDocIds.length != 1) { |
| 198 | return mDatabase.queryDocument(documentId, projection); |
| 199 | } |
| 200 | |
| 201 | // If the documentId specifies a device having exact one storage, we repalce some device |
| 202 | // attributes with the storage attributes. |
| 203 | try { |
| 204 | final String storageName; |
| 205 | final int storageFlags; |
| 206 | try (final Cursor storageCursor = mDatabase.queryDocument( |
| 207 | storageDocIds[0], |
| 208 | MtpDatabase.strings(Document.COLUMN_DISPLAY_NAME, Document.COLUMN_FLAGS))) { |
| 209 | if (!storageCursor.moveToNext()) { |
| 210 | throw new FileNotFoundException(); |
| 211 | } |
| 212 | storageName = storageCursor.getString(0); |
| 213 | storageFlags = storageCursor.getInt(1); |
| 214 | } |
| 215 | |
| 216 | cursor.moveToNext(); |
| 217 | final ContentValues values = new ContentValues(); |
| 218 | DatabaseUtils.cursorRowToContentValues(cursor, values); |
| 219 | if (values.containsKey(Document.COLUMN_DISPLAY_NAME)) { |
| 220 | values.put(Document.COLUMN_DISPLAY_NAME, mResources.getString( |
| 221 | R.string.root_name, |
| 222 | values.getAsString(Document.COLUMN_DISPLAY_NAME), |
| 223 | storageName)); |
| 224 | } |
| 225 | values.put(Document.COLUMN_FLAGS, storageFlags); |
| 226 | final MatrixCursor output = new MatrixCursor(projection, 1); |
| 227 | MtpDatabase.putValuesToCursor(values, output); |
| 228 | return output; |
| 229 | } finally { |
| 230 | cursor.close(); |
| 231 | } |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | @Override |
Daichi Hirono | 124d060 | 2015-08-11 17:08:35 +0900 | [diff] [blame] | 235 | public Cursor queryChildDocuments(String parentDocumentId, |
| 236 | String[] projection, String sortOrder) throws FileNotFoundException { |
Daichi Hirono | 19aa932 | 2016-02-04 14:19:52 +0900 | [diff] [blame] | 237 | if (DEBUG) { |
| 238 | Log.d(TAG, "queryChildDocuments: " + parentDocumentId); |
| 239 | } |
Daichi Hirono | 124d060 | 2015-08-11 17:08:35 +0900 | [diff] [blame] | 240 | if (projection == null) { |
| 241 | projection = MtpDocumentsProvider.DEFAULT_DOCUMENT_PROJECTION; |
| 242 | } |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 243 | Identifier parentIdentifier = mDatabase.createIdentifier(parentDocumentId); |
Daichi Hirono | 124d060 | 2015-08-11 17:08:35 +0900 | [diff] [blame] | 244 | try { |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 245 | openDevice(parentIdentifier.mDeviceId); |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 246 | if (parentIdentifier.mDocumentType == MtpDatabaseConstants.DOCUMENT_TYPE_DEVICE) { |
Daichi Hirono | 2965776 | 2016-02-10 16:55:37 -0800 | [diff] [blame] | 247 | final String[] storageDocIds = mDatabase.getStorageDocumentIds(parentDocumentId); |
| 248 | if (storageDocIds.length == 0) { |
| 249 | // Remote device does not provide storages. Maybe it is locked. |
| 250 | return createErrorCursor(projection, R.string.error_locked_device); |
| 251 | } else if (storageDocIds.length > 1) { |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 252 | // Returns storage list from database. |
| 253 | return mDatabase.queryChildDocuments(projection, parentDocumentId); |
| 254 | } |
Daichi Hirono | 2965776 | 2016-02-10 16:55:37 -0800 | [diff] [blame] | 255 | |
| 256 | // Exact one storage is found. Skip storage and returns object in the single |
| 257 | // storage. |
| 258 | parentIdentifier = mDatabase.createIdentifier(storageDocIds[0]); |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 259 | } |
Daichi Hirono | 2965776 | 2016-02-10 16:55:37 -0800 | [diff] [blame] | 260 | |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 261 | // Returns object list from document loader. |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 262 | return getDocumentLoader(parentIdentifier).queryChildDocuments( |
| 263 | projection, parentIdentifier); |
Daichi Hirono | c18f807 | 2016-02-10 14:59:52 -0800 | [diff] [blame] | 264 | } catch (BusyDeviceException exception) { |
Daichi Hirono | 2965776 | 2016-02-10 16:55:37 -0800 | [diff] [blame] | 265 | return createErrorCursor(projection, R.string.error_busy_device); |
Daichi Hirono | 124d060 | 2015-08-11 17:08:35 +0900 | [diff] [blame] | 266 | } catch (IOException exception) { |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 267 | Log.e(MtpDocumentsProvider.TAG, "queryChildDocuments", exception); |
Daichi Hirono | 124d060 | 2015-08-11 17:08:35 +0900 | [diff] [blame] | 268 | throw new FileNotFoundException(exception.getMessage()); |
| 269 | } |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | @Override |
Daichi Hirono | 8ba4191 | 2015-07-30 21:22:57 +0900 | [diff] [blame] | 273 | public ParcelFileDescriptor openDocument( |
| 274 | String documentId, String mode, CancellationSignal signal) |
| 275 | throws FileNotFoundException { |
Daichi Hirono | 6213cef | 2016-02-05 17:21:13 +0900 | [diff] [blame] | 276 | if (DEBUG) { |
| 277 | Log.d(TAG, "openDocument: " + documentId); |
| 278 | } |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 279 | final Identifier identifier = mDatabase.createIdentifier(documentId); |
Daichi Hirono | 8ba4191 | 2015-07-30 21:22:57 +0900 | [diff] [blame] | 280 | try { |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 281 | openDevice(identifier.mDeviceId); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 282 | final MtpDeviceRecord device = getDeviceToolkit(identifier.mDeviceId).mDeviceRecord; |
Daichi Hirono | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 283 | // Turn off MODE_CREATE because openDocument does not allow to create new files. |
| 284 | final int modeFlag = |
| 285 | ParcelFileDescriptor.parseMode(mode) & ~ParcelFileDescriptor.MODE_CREATE; |
| 286 | if ((modeFlag & ParcelFileDescriptor.MODE_READ_ONLY) != 0) { |
| 287 | long fileSize; |
| 288 | try { |
| 289 | fileSize = getFileSize(documentId); |
| 290 | } catch (UnsupportedOperationException exception) { |
| 291 | fileSize = -1; |
| 292 | } |
| 293 | if (MtpDeviceRecord.isPartialReadSupported( |
| 294 | device.operationsSupported, fileSize)) { |
Daichi Hirono | e80ea38 | 2016-11-15 13:07:01 +0900 | [diff] [blame] | 295 | |
| 296 | return mStorageManager.openProxyFileDescriptor( |
| 297 | modeFlag, |
| 298 | new MtpProxyFileDescriptorCallback(Integer.parseInt(documentId))); |
Daichi Hirono | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 299 | } else { |
| 300 | // If getPartialObject{|64} are not supported for the device, returns |
| 301 | // non-seekable pipe FD instead. |
| 302 | return getPipeManager(identifier).readDocument(mMtpManager, identifier); |
| 303 | } |
| 304 | } else if ((modeFlag & ParcelFileDescriptor.MODE_WRITE_ONLY) != 0) { |
| 305 | // TODO: Clear the parent document loader task (if exists) and call notify |
| 306 | // when writing is completed. |
| 307 | if (MtpDeviceRecord.isWritingSupported(device.operationsSupported)) { |
Daichi Hirono | e80ea38 | 2016-11-15 13:07:01 +0900 | [diff] [blame] | 308 | return mStorageManager.openProxyFileDescriptor( |
| 309 | modeFlag, |
| 310 | new MtpProxyFileDescriptorCallback(Integer.parseInt(documentId))); |
Daichi Hirono | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 311 | } else { |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 312 | throw new UnsupportedOperationException( |
Daichi Hirono | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 313 | "The device does not support writing operation."); |
| 314 | } |
| 315 | } else { |
| 316 | // TODO: Add support for "rw" mode. |
| 317 | throw new UnsupportedOperationException("The provider does not support 'rw' mode."); |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 318 | } |
Daichi Hirono | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 319 | } catch (FileNotFoundException | RuntimeException error) { |
| 320 | Log.e(MtpDocumentsProvider.TAG, "openDocument", error); |
| 321 | throw error; |
Daichi Hirono | 8ba4191 | 2015-07-30 21:22:57 +0900 | [diff] [blame] | 322 | } catch (IOException error) { |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 323 | Log.e(MtpDocumentsProvider.TAG, "openDocument", error); |
Daichi Hirono | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 324 | throw new IllegalStateException(error); |
Daichi Hirono | 8ba4191 | 2015-07-30 21:22:57 +0900 | [diff] [blame] | 325 | } |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 326 | } |
| 327 | |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 328 | @Override |
| 329 | public AssetFileDescriptor openDocumentThumbnail( |
| 330 | String documentId, |
| 331 | Point sizeHint, |
| 332 | CancellationSignal signal) throws FileNotFoundException { |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 333 | final Identifier identifier = mDatabase.createIdentifier(documentId); |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 334 | try { |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 335 | openDevice(identifier.mDeviceId); |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 336 | return new AssetFileDescriptor( |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 337 | getPipeManager(identifier).readThumbnail(mMtpManager, identifier), |
Daichi Hirono | 573c1fb | 2015-08-11 19:31:30 +0900 | [diff] [blame] | 338 | 0, // Start offset. |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 339 | AssetFileDescriptor.UNKNOWN_LENGTH); |
| 340 | } catch (IOException error) { |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 341 | Log.e(MtpDocumentsProvider.TAG, "openDocumentThumbnail", error); |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 342 | throw new FileNotFoundException(error.getMessage()); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | @Override |
| 347 | public void deleteDocument(String documentId) throws FileNotFoundException { |
| 348 | try { |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 349 | final Identifier identifier = mDatabase.createIdentifier(documentId); |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 350 | openDevice(identifier.mDeviceId); |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 351 | final Identifier parentIdentifier = mDatabase.getParentIdentifier(documentId); |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 352 | mMtpManager.deleteDocument(identifier.mDeviceId, identifier.mObjectHandle); |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 353 | mDatabase.deleteDocument(documentId); |
Daichi Hirono | 76be46f | 2016-04-08 09:48:02 +0900 | [diff] [blame] | 354 | getDocumentLoader(parentIdentifier).cancelTask(parentIdentifier); |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 355 | notifyChildDocumentsChange(parentIdentifier.mDocumentId); |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 356 | if (parentIdentifier.mDocumentType == MtpDatabaseConstants.DOCUMENT_TYPE_STORAGE) { |
| 357 | // If the parent is storage, the object might be appeared as child of device because |
| 358 | // we skip storage when the device has only one storage. |
| 359 | final Identifier deviceIdentifier = mDatabase.getParentIdentifier( |
| 360 | parentIdentifier.mDocumentId); |
| 361 | notifyChildDocumentsChange(deviceIdentifier.mDocumentId); |
| 362 | } |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 363 | } catch (IOException error) { |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 364 | Log.e(MtpDocumentsProvider.TAG, "deleteDocument", error); |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 365 | throw new FileNotFoundException(error.getMessage()); |
| 366 | } |
| 367 | } |
| 368 | |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 369 | @Override |
| 370 | public void onTrimMemory(int level) { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 371 | synchronized (mDeviceListLock) { |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 372 | for (final DeviceToolkit toolkit : mDeviceToolkits.values()) { |
| 373 | toolkit.mDocumentLoader.clearCompletedTasks(); |
| 374 | } |
| 375 | } |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 376 | } |
| 377 | |
Tomasz Mikolajewski | 87763e6 | 2015-08-10 10:10:22 +0900 | [diff] [blame] | 378 | @Override |
| 379 | public String createDocument(String parentDocumentId, String mimeType, String displayName) |
| 380 | throws FileNotFoundException { |
Daichi Hirono | 6213cef | 2016-02-05 17:21:13 +0900 | [diff] [blame] | 381 | if (DEBUG) { |
| 382 | Log.d(TAG, "createDocument: " + displayName); |
| 383 | } |
Daichi Hirono | fc7fb75 | 2016-03-15 19:19:31 +0900 | [diff] [blame] | 384 | final Identifier parentId; |
| 385 | final MtpDeviceRecord record; |
| 386 | final ParcelFileDescriptor[] pipe; |
Tomasz Mikolajewski | 87763e6 | 2015-08-10 10:10:22 +0900 | [diff] [blame] | 387 | try { |
Daichi Hirono | fc7fb75 | 2016-03-15 19:19:31 +0900 | [diff] [blame] | 388 | parentId = mDatabase.createIdentifier(parentDocumentId); |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 389 | openDevice(parentId.mDeviceId); |
Daichi Hirono | fc7fb75 | 2016-03-15 19:19:31 +0900 | [diff] [blame] | 390 | record = getDeviceToolkit(parentId.mDeviceId).mDeviceRecord; |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 391 | if (!MtpDeviceRecord.isWritingSupported(record.operationsSupported)) { |
Daichi Hirono | fc7fb75 | 2016-03-15 19:19:31 +0900 | [diff] [blame] | 392 | throw new UnsupportedOperationException( |
| 393 | "Writing operation is not supported by the device."); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 394 | } |
Daichi Hirono | 35b2ec5 | 2016-11-02 14:51:26 +0900 | [diff] [blame] | 395 | |
| 396 | final int parentObjectHandle; |
| 397 | final int storageId; |
| 398 | switch (parentId.mDocumentType) { |
| 399 | case MtpDatabaseConstants.DOCUMENT_TYPE_DEVICE: |
| 400 | final String[] storageDocumentIds = |
| 401 | mDatabase.getStorageDocumentIds(parentId.mDocumentId); |
| 402 | if (storageDocumentIds.length == 1) { |
| 403 | final String newDocumentId = |
| 404 | createDocument(storageDocumentIds[0], mimeType, displayName); |
| 405 | notifyChildDocumentsChange(parentDocumentId); |
| 406 | return newDocumentId; |
| 407 | } else { |
| 408 | throw new UnsupportedOperationException( |
| 409 | "Cannot create a file under the device."); |
| 410 | } |
| 411 | case MtpDatabaseConstants.DOCUMENT_TYPE_STORAGE: |
| 412 | storageId = parentId.mStorageId; |
| 413 | parentObjectHandle = -1; |
| 414 | break; |
| 415 | case MtpDatabaseConstants.DOCUMENT_TYPE_OBJECT: |
| 416 | storageId = parentId.mStorageId; |
| 417 | parentObjectHandle = parentId.mObjectHandle; |
| 418 | break; |
| 419 | default: |
| 420 | throw new IllegalArgumentException("Unexpected document type."); |
| 421 | } |
| 422 | |
Daichi Hirono | fc7fb75 | 2016-03-15 19:19:31 +0900 | [diff] [blame] | 423 | pipe = ParcelFileDescriptor.createReliablePipe(); |
| 424 | int objectHandle = -1; |
| 425 | MtpObjectInfo info = null; |
| 426 | try { |
| 427 | pipe[0].close(); // 0 bytes for a new document. |
| 428 | |
| 429 | final int formatCode = Document.MIME_TYPE_DIR.equals(mimeType) ? |
| 430 | MtpConstants.FORMAT_ASSOCIATION : |
| 431 | MediaFile.getFormatCode(displayName, mimeType); |
| 432 | info = new MtpObjectInfo.Builder() |
Daichi Hirono | 35b2ec5 | 2016-11-02 14:51:26 +0900 | [diff] [blame] | 433 | .setStorageId(storageId) |
| 434 | .setParent(parentObjectHandle) |
Daichi Hirono | fc7fb75 | 2016-03-15 19:19:31 +0900 | [diff] [blame] | 435 | .setFormat(formatCode) |
| 436 | .setName(displayName) |
| 437 | .build(); |
| 438 | |
| 439 | final String[] parts = FileUtils.splitFileName(mimeType, displayName); |
| 440 | final String baseName = parts[0]; |
| 441 | final String extension = parts[1]; |
| 442 | for (int i = 0; i <= 32; i++) { |
| 443 | final MtpObjectInfo infoUniqueName; |
| 444 | if (i == 0) { |
| 445 | infoUniqueName = info; |
| 446 | } else { |
Daichi Hirono | 4f04fd3 | 2016-03-18 18:29:56 +0900 | [diff] [blame] | 447 | String suffixedName = baseName + " (" + i + " )"; |
| 448 | if (!extension.isEmpty()) { |
| 449 | suffixedName += "." + extension; |
| 450 | } |
| 451 | infoUniqueName = |
| 452 | new MtpObjectInfo.Builder(info).setName(suffixedName).build(); |
Daichi Hirono | fc7fb75 | 2016-03-15 19:19:31 +0900 | [diff] [blame] | 453 | } |
| 454 | try { |
| 455 | objectHandle = mMtpManager.createDocument( |
| 456 | parentId.mDeviceId, infoUniqueName, pipe[1]); |
| 457 | break; |
| 458 | } catch (SendObjectInfoFailure exp) { |
| 459 | // This can be caused when we have an existing file with the same name. |
| 460 | continue; |
| 461 | } |
| 462 | } |
| 463 | } finally { |
| 464 | pipe[1].close(); |
| 465 | } |
| 466 | if (objectHandle == -1) { |
| 467 | throw new IllegalArgumentException( |
| 468 | "The file name \"" + displayName + "\" is conflicted with existing files " + |
| 469 | "and the provider failed to find unique name."); |
| 470 | } |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 471 | final MtpObjectInfo infoWithHandle = |
| 472 | new MtpObjectInfo.Builder(info).setObjectHandle(objectHandle).build(); |
| 473 | final String documentId = mDatabase.putNewDocument( |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 474 | parentId.mDeviceId, parentDocumentId, record.operationsSupported, |
Daichi Hirono | 64111e0 | 2016-03-24 21:07:38 +0900 | [diff] [blame] | 475 | infoWithHandle, 0l); |
Daichi Hirono | 76be46f | 2016-04-08 09:48:02 +0900 | [diff] [blame] | 476 | getDocumentLoader(parentId).cancelTask(parentId); |
Tomasz Mikolajewski | 87763e6 | 2015-08-10 10:10:22 +0900 | [diff] [blame] | 477 | notifyChildDocumentsChange(parentDocumentId); |
| 478 | return documentId; |
Daichi Hirono | fc7fb75 | 2016-03-15 19:19:31 +0900 | [diff] [blame] | 479 | } catch (FileNotFoundException | RuntimeException error) { |
| 480 | Log.e(TAG, "createDocument", error); |
| 481 | throw error; |
Tomasz Mikolajewski | 87763e6 | 2015-08-10 10:10:22 +0900 | [diff] [blame] | 482 | } catch (IOException error) { |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 483 | Log.e(TAG, "createDocument", error); |
Daichi Hirono | fc7fb75 | 2016-03-15 19:19:31 +0900 | [diff] [blame] | 484 | throw new IllegalStateException(error); |
Tomasz Mikolajewski | 87763e6 | 2015-08-10 10:10:22 +0900 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | |
Daichi Hirono | b9ffa2a | 2016-11-01 18:41:43 +0900 | [diff] [blame] | 488 | @Override |
Garfield Tan | b690b4d | 2017-03-01 16:05:23 -0800 | [diff] [blame] | 489 | public Path findDocumentPath(String parentDocumentId, String childDocumentId) |
Daichi Hirono | b9ffa2a | 2016-11-01 18:41:43 +0900 | [diff] [blame] | 490 | throws FileNotFoundException { |
| 491 | final LinkedList<String> ids = new LinkedList<>(); |
| 492 | final Identifier childIdentifier = mDatabase.createIdentifier(childDocumentId); |
| 493 | |
| 494 | Identifier i = childIdentifier; |
| 495 | outer: while (true) { |
| 496 | if (i.mDocumentId.equals(parentDocumentId)) { |
| 497 | ids.addFirst(i.mDocumentId); |
| 498 | break; |
| 499 | } |
| 500 | switch (i.mDocumentType) { |
| 501 | case MtpDatabaseConstants.DOCUMENT_TYPE_OBJECT: |
| 502 | ids.addFirst(i.mDocumentId); |
| 503 | i = mDatabase.getParentIdentifier(i.mDocumentId); |
| 504 | break; |
| 505 | case MtpDatabaseConstants.DOCUMENT_TYPE_STORAGE: { |
| 506 | // Check if there is the multiple storage. |
| 507 | final Identifier deviceIdentifier = |
| 508 | mDatabase.getParentIdentifier(i.mDocumentId); |
| 509 | final String[] storageIds = |
| 510 | mDatabase.getStorageDocumentIds(deviceIdentifier.mDocumentId); |
| 511 | // Add storage's document ID to the path only when the device has multiple |
| 512 | // storages. |
| 513 | if (storageIds.length > 1) { |
| 514 | ids.addFirst(i.mDocumentId); |
| 515 | break outer; |
| 516 | } |
| 517 | i = deviceIdentifier; |
| 518 | break; |
| 519 | } |
| 520 | case MtpDatabaseConstants.DOCUMENT_TYPE_DEVICE: |
| 521 | ids.addFirst(i.mDocumentId); |
| 522 | break outer; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | if (parentDocumentId != null) { |
| 527 | return new Path(null, ids); |
| 528 | } else { |
| 529 | return new Path(/* Should be same with root ID */ i.mDocumentId, ids); |
| 530 | } |
| 531 | } |
| 532 | |
Daichi Hirono | 29de769 | 2016-11-07 10:58:50 +0900 | [diff] [blame] | 533 | @Override |
| 534 | public boolean isChildDocument(String parentDocumentId, String documentId) { |
| 535 | try { |
| 536 | Identifier identifier = mDatabase.createIdentifier(documentId); |
| 537 | while (true) { |
| 538 | if (parentDocumentId.equals(identifier.mDocumentId)) { |
| 539 | return true; |
| 540 | } |
| 541 | if (identifier.mDocumentType == MtpDatabaseConstants.DOCUMENT_TYPE_DEVICE) { |
| 542 | return false; |
| 543 | } |
| 544 | identifier = mDatabase.getParentIdentifier(identifier.mDocumentId); |
| 545 | } |
| 546 | } catch (FileNotFoundException error) { |
| 547 | return false; |
| 548 | } |
| 549 | } |
| 550 | |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 551 | void openDevice(int deviceId) throws IOException { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 552 | synchronized (mDeviceListLock) { |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 553 | if (mDeviceToolkits.containsKey(deviceId)) { |
| 554 | return; |
| 555 | } |
Daichi Hirono | 19aa932 | 2016-02-04 14:19:52 +0900 | [diff] [blame] | 556 | if (DEBUG) { |
| 557 | Log.d(TAG, "Open device " + deviceId); |
| 558 | } |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 559 | final MtpDeviceRecord device = mMtpManager.openDevice(deviceId); |
Daichi Hirono | 4e94b8d | 2016-02-21 22:42:41 +0900 | [diff] [blame] | 560 | final DeviceToolkit toolkit = |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 561 | new DeviceToolkit(mMtpManager, mResolver, mDatabase, device); |
Daichi Hirono | 4e94b8d | 2016-02-21 22:42:41 +0900 | [diff] [blame] | 562 | mDeviceToolkits.put(deviceId, toolkit); |
Daichi Hirono | 203be49 | 2017-04-03 12:20:42 +0900 | [diff] [blame] | 563 | mIntentSender.sendUpdateNotificationIntent(getOpenedDeviceRecordsCache()); |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 564 | try { |
| 565 | mRootScanner.resume().await(); |
| 566 | } catch (InterruptedException error) { |
| 567 | Log.e(TAG, "openDevice", error); |
| 568 | } |
Daichi Hirono | 4e94b8d | 2016-02-21 22:42:41 +0900 | [diff] [blame] | 569 | // Resume document loader to remap disconnected document ID. Must be invoked after the |
| 570 | // root scanner resumes. |
| 571 | toolkit.mDocumentLoader.resume(); |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 572 | } |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 573 | } |
| 574 | |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 575 | void closeDevice(int deviceId) throws IOException, InterruptedException { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 576 | synchronized (mDeviceListLock) { |
| 577 | closeDeviceInternal(deviceId); |
Daichi Hirono | 203be49 | 2017-04-03 12:20:42 +0900 | [diff] [blame] | 578 | mIntentSender.sendUpdateNotificationIntent(getOpenedDeviceRecordsCache()); |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 579 | } |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 580 | mRootScanner.resume(); |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 581 | } |
| 582 | |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 583 | MtpDeviceRecord[] getOpenedDeviceRecordsCache() { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 584 | synchronized (mDeviceListLock) { |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 585 | final MtpDeviceRecord[] records = new MtpDeviceRecord[mDeviceToolkits.size()]; |
| 586 | int i = 0; |
| 587 | for (final DeviceToolkit toolkit : mDeviceToolkits.values()) { |
| 588 | records[i] = toolkit.mDeviceRecord; |
| 589 | i++; |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 590 | } |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 591 | return records; |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 592 | } |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 593 | } |
Daichi Hirono | 50d17aa | 2015-07-28 15:49:01 +0900 | [diff] [blame] | 594 | |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 595 | /** |
Daichi Hirono | 1e37444 | 2016-02-11 10:08:21 -0800 | [diff] [blame] | 596 | * Obtains document ID for the given device ID. |
| 597 | * @param deviceId |
| 598 | * @return document ID |
| 599 | * @throws FileNotFoundException device ID has not been build. |
| 600 | */ |
| 601 | public String getDeviceDocumentId(int deviceId) throws FileNotFoundException { |
| 602 | return mDatabase.getDeviceDocumentId(deviceId); |
| 603 | } |
| 604 | |
| 605 | /** |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 606 | * Resumes root scanner to handle the update of device list. |
| 607 | */ |
| 608 | void resumeRootScanner() { |
Daichi Hirono | ebd2405 | 2016-02-06 21:05:57 +0900 | [diff] [blame] | 609 | if (DEBUG) { |
| 610 | Log.d(MtpDocumentsProvider.TAG, "resumeRootScanner"); |
| 611 | } |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 612 | mRootScanner.resume(); |
| 613 | } |
| 614 | |
| 615 | /** |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 616 | * Finalize the content provider for unit tests. |
| 617 | */ |
| 618 | @Override |
| 619 | public void shutdown() { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 620 | synchronized (mDeviceListLock) { |
| 621 | try { |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 622 | // Copy the opened key set because it will be modified when closing devices. |
| 623 | final Integer[] keySet = |
| 624 | mDeviceToolkits.keySet().toArray(new Integer[mDeviceToolkits.size()]); |
| 625 | for (final int id : keySet) { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 626 | closeDeviceInternal(id); |
| 627 | } |
Daichi Hirono | 2e9a57b | 2016-02-26 17:41:45 +0900 | [diff] [blame] | 628 | mRootScanner.pause(); |
Daichi Hirono | acb0e27 | 2016-03-14 21:49:14 +0900 | [diff] [blame] | 629 | } catch (InterruptedException | IOException | TimeoutException e) { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 630 | // It should fail unit tests by throwing runtime exception. |
| 631 | throw new RuntimeException(e); |
| 632 | } finally { |
| 633 | mDatabase.close(); |
| 634 | super.shutdown(); |
| 635 | } |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 636 | } |
| 637 | } |
| 638 | |
Daichi Hirono | 5fecc6c | 2015-08-04 17:45:51 +0900 | [diff] [blame] | 639 | private void notifyChildDocumentsChange(String parentDocumentId) { |
| 640 | mResolver.notifyChange( |
| 641 | DocumentsContract.buildChildDocumentsUri(AUTHORITY, parentDocumentId), |
| 642 | null, |
| 643 | false); |
| 644 | } |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 645 | |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 646 | /** |
Daichi Hirono | be38848 | 2015-12-14 16:20:14 +0900 | [diff] [blame] | 647 | * Clears MTP identifier in the database. |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 648 | */ |
| 649 | private void resume() { |
| 650 | synchronized (mDeviceListLock) { |
| 651 | mDatabase.getMapper().clearMapping(); |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 652 | } |
| 653 | } |
| 654 | |
| 655 | private void closeDeviceInternal(int deviceId) throws IOException, InterruptedException { |
| 656 | // TODO: Flush the device before closing (if not closed externally). |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 657 | if (!mDeviceToolkits.containsKey(deviceId)) { |
| 658 | return; |
| 659 | } |
Daichi Hirono | 19aa932 | 2016-02-04 14:19:52 +0900 | [diff] [blame] | 660 | if (DEBUG) { |
| 661 | Log.d(TAG, "Close device " + deviceId); |
| 662 | } |
Daichi Hirono | 24ab92a | 2016-03-04 17:53:03 +0900 | [diff] [blame] | 663 | getDeviceToolkit(deviceId).close(); |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 664 | mDeviceToolkits.remove(deviceId); |
| 665 | mMtpManager.closeDevice(deviceId); |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 666 | } |
| 667 | |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 668 | private DeviceToolkit getDeviceToolkit(int deviceId) throws FileNotFoundException { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 669 | synchronized (mDeviceListLock) { |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 670 | final DeviceToolkit toolkit = mDeviceToolkits.get(deviceId); |
| 671 | if (toolkit == null) { |
| 672 | throw new FileNotFoundException(); |
| 673 | } |
| 674 | return toolkit; |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 675 | } |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | private PipeManager getPipeManager(Identifier identifier) throws FileNotFoundException { |
| 679 | return getDeviceToolkit(identifier.mDeviceId).mPipeManager; |
| 680 | } |
| 681 | |
| 682 | private DocumentLoader getDocumentLoader(Identifier identifier) throws FileNotFoundException { |
| 683 | return getDeviceToolkit(identifier.mDeviceId).mDocumentLoader; |
| 684 | } |
| 685 | |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 686 | private long getFileSize(String documentId) throws FileNotFoundException { |
| 687 | final Cursor cursor = mDatabase.queryDocument( |
| 688 | documentId, |
| 689 | MtpDatabase.strings(Document.COLUMN_SIZE, Document.COLUMN_DISPLAY_NAME)); |
| 690 | try { |
| 691 | if (cursor.moveToNext()) { |
Daichi Hirono | 77a1c656 | 2016-03-28 14:37:12 +0900 | [diff] [blame] | 692 | if (cursor.isNull(0)) { |
| 693 | throw new UnsupportedOperationException(); |
| 694 | } |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 695 | return cursor.getLong(0); |
| 696 | } else { |
| 697 | throw new FileNotFoundException(); |
| 698 | } |
| 699 | } finally { |
| 700 | cursor.close(); |
| 701 | } |
| 702 | } |
| 703 | |
Daichi Hirono | 2965776 | 2016-02-10 16:55:37 -0800 | [diff] [blame] | 704 | /** |
| 705 | * Creates empty cursor with specific error message. |
| 706 | * |
| 707 | * @param projection Column names. |
| 708 | * @param stringResId String resource ID of error message. |
| 709 | * @return Empty cursor with error message. |
| 710 | */ |
| 711 | private Cursor createErrorCursor(String[] projection, int stringResId) { |
| 712 | final Bundle bundle = new Bundle(); |
| 713 | bundle.putString(DocumentsContract.EXTRA_ERROR, mResources.getString(stringResId)); |
| 714 | final Cursor cursor = new MatrixCursor(projection); |
| 715 | cursor.setExtras(bundle); |
| 716 | return cursor; |
| 717 | } |
| 718 | |
Daichi Hirono | 24ab92a | 2016-03-04 17:53:03 +0900 | [diff] [blame] | 719 | private static class DeviceToolkit implements AutoCloseable { |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 720 | public final PipeManager mPipeManager; |
| 721 | public final DocumentLoader mDocumentLoader; |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 722 | public final MtpDeviceRecord mDeviceRecord; |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 723 | |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 724 | public DeviceToolkit(MtpManager manager, |
| 725 | ContentResolver resolver, |
| 726 | MtpDatabase database, |
| 727 | MtpDeviceRecord record) { |
Daichi Hirono | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 728 | mPipeManager = new PipeManager(database); |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 729 | mDocumentLoader = new DocumentLoader(record, manager, resolver, database); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 730 | mDeviceRecord = record; |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 731 | } |
Daichi Hirono | 24ab92a | 2016-03-04 17:53:03 +0900 | [diff] [blame] | 732 | |
| 733 | @Override |
| 734 | public void close() throws InterruptedException { |
| 735 | mPipeManager.close(); |
| 736 | mDocumentLoader.close(); |
| 737 | } |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 738 | } |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 739 | |
Daichi Hirono | e80ea38 | 2016-11-15 13:07:01 +0900 | [diff] [blame] | 740 | private class MtpProxyFileDescriptorCallback extends ProxyFileDescriptorCallback { |
| 741 | private final int mInode; |
| 742 | private MtpFileWriter mWriter; |
Daichi Hirono | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 743 | |
Daichi Hirono | e80ea38 | 2016-11-15 13:07:01 +0900 | [diff] [blame] | 744 | MtpProxyFileDescriptorCallback(int inode) { |
| 745 | mInode = inode; |
Daichi Hirono | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 746 | } |
| 747 | |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 748 | @Override |
Daichi Hirono | e80ea38 | 2016-11-15 13:07:01 +0900 | [diff] [blame] | 749 | public long onGetSize() throws ErrnoException { |
Daichi Hirono | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 750 | try { |
Daichi Hirono | e80ea38 | 2016-11-15 13:07:01 +0900 | [diff] [blame] | 751 | return getFileSize(String.valueOf(mInode)); |
| 752 | } catch (FileNotFoundException e) { |
| 753 | Log.e(TAG, e.getMessage(), e); |
| 754 | throw new ErrnoException("onGetSize", OsConstants.ENOENT); |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | @Override |
| 759 | public int onRead(long offset, int size, byte[] data) throws ErrnoException { |
| 760 | try { |
| 761 | final Identifier identifier = mDatabase.createIdentifier(Integer.toString(mInode)); |
| 762 | final MtpDeviceRecord record = getDeviceToolkit(identifier.mDeviceId).mDeviceRecord; |
| 763 | if (MtpDeviceRecord.isSupported( |
| 764 | record.operationsSupported, MtpConstants.OPERATION_GET_PARTIAL_OBJECT_64)) { |
| 765 | |
| 766 | return (int) mMtpManager.getPartialObject64( |
| 767 | identifier.mDeviceId, identifier.mObjectHandle, offset, size, data); |
| 768 | |
| 769 | } |
| 770 | if (0 <= offset && offset <= 0xffffffffL && MtpDeviceRecord.isSupported( |
| 771 | record.operationsSupported, MtpConstants.OPERATION_GET_PARTIAL_OBJECT)) { |
| 772 | return (int) mMtpManager.getPartialObject( |
| 773 | identifier.mDeviceId, identifier.mObjectHandle, offset, size, data); |
| 774 | } |
| 775 | throw new ErrnoException("onRead", OsConstants.ENOTSUP); |
| 776 | } catch (IOException e) { |
| 777 | Log.e(TAG, e.getMessage(), e); |
| 778 | throw new ErrnoException("onRead", OsConstants.EIO); |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | @Override |
| 783 | public int onWrite(long offset, int size, byte[] data) throws ErrnoException { |
| 784 | try { |
| 785 | if (mWriter == null) { |
| 786 | mWriter = new MtpFileWriter(mContext, String.valueOf(mInode)); |
| 787 | } |
| 788 | return mWriter.write(offset, size, data); |
| 789 | } catch (IOException e) { |
| 790 | Log.e(TAG, e.getMessage(), e); |
| 791 | throw new ErrnoException("onWrite", OsConstants.EIO); |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | @Override |
| 796 | public void onFsync() throws ErrnoException { |
| 797 | tryFsync(); |
| 798 | } |
| 799 | |
| 800 | @Override |
| 801 | public void onRelease() { |
| 802 | try { |
| 803 | tryFsync(); |
| 804 | } catch (ErrnoException error) { |
| 805 | // Cannot recover from the error at onRelease. Client app should use fsync to |
| 806 | // ensure the provider writes data correctly. |
| 807 | Log.e(TAG, "Cannot recover from the error at onRelease.", error); |
Daichi Hirono | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 808 | } finally { |
Daichi Hirono | e80ea38 | 2016-11-15 13:07:01 +0900 | [diff] [blame] | 809 | if (mWriter != null) { |
| 810 | IoUtils.closeQuietly(mWriter); |
| 811 | } |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | private void tryFsync() throws ErrnoException { |
| 816 | try { |
| 817 | if (mWriter != null) { |
| 818 | final MtpDeviceRecord device = |
| 819 | getDeviceToolkit(mDatabase.createIdentifier( |
| 820 | mWriter.getDocumentId()).mDeviceId).mDeviceRecord; |
| 821 | mWriter.flush(mMtpManager, mDatabase, device.operationsSupported); |
| 822 | } |
| 823 | } catch (IOException e) { |
| 824 | Log.e(TAG, e.getMessage(), e); |
| 825 | throw new ErrnoException("onWrite", OsConstants.EIO); |
Daichi Hirono | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 826 | } |
Daichi Hirono | 09ece6c | 2016-01-20 19:09:25 +0900 | [diff] [blame] | 827 | } |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 828 | } |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 829 | } |