184 Commits

Author SHA1 Message Date
Daichi Hirono
f7e0799fc1 Handle error result returned from MTP API correctly.
* Check null result for all MTP APIs that can return null.
 * Don't regard valid parent handles that are more than 0x80000000 as
   error.
 * Check boolean result from importFile.

Change-Id: I262511ac3c935ebb54e52c03eaefd3535a3e2f29
Fix: 28155538
2016-04-13 13:59:54 +09:00
Daichi Hirono
6b34b31059 Merge "Fix flakiness DocumentLoader#testCancelTask." into nyc-dev 2016-04-12 03:54:59 +00:00
Daichi Hirono
3edcde2953 Fix flakiness DocumentLoader#testCancelTask.
For testing, we use blockDocument and unblockDocument test mehtods to
control flow of DocumentLoader's background thread.
Previously testCancelTask may exit before the background thread goes
back from blocking, which causes InterruptedException.

Fixes: 28125289
Change-Id: Id03826733c5b6f1da66b9280838eb1d2897ed5fc
2016-04-12 12:38:03 +09:00
Daichi Hirono
df839271e3 Merge "Fix crash when deleting multiple files." into nyc-dev 2016-04-11 07:52:03 +00:00
Daichi Hirono
76be46f4d9 Fix crash when deleting multiple files.
When deleting files, MtpDocumentsProvider clears LoadingTask in
DocumentsLoader to update directory contents list. Previously it can
clear ongoing task, and it skips calling Mapper#stopAddingDocuments.
Since Mapper#startAddingDocuments and Mapper#stopAddingDocuments must be
called 1 to 1, it causes precondition check failure at the next call of
Mapper#startAddingDocuments.

Change-Id: I23e2b117da826297e45404be4db4cc29f96e5510
Fix: 28076320
2016-04-11 14:32:03 +09:00
Daichi Hirono
d0ef1394d2 Remove LOG_NDEBUG 0 from AppFuse JNI.
"LOG_NDEBUG 0" enforces to output verbose log even for release build.

Change-Id: I9ab2f5b86065d360222cbabc8d6be2431b06d785
Fix: 28055420
2016-04-08 10:07:01 +09:00
Daichi Hirono
f4e7fa8038 Use AppFuse to write document.
Previously MtpDocumentsProvider used pipes to transfer bytes from an
application to the provider when writing a document.  The problem was
application could not ensure that the last chunk of bytes was
successfully written to MTP device, since pipes had been already closed
when the provider transferred bytes to MTP device. Though the provider
encountered an error, the provider could not report the error to an
application.

The CL switches the method to transfer bytes from pipes to AppFuse. Now
application can flush() bytes on the file descriptor, and flush will not
complete until the provider completes writing bytes to MTP device.

Fixed: 23093747
Change-Id: I4e28f8cbf19d6c97e591943349a7535241d768f7
2016-03-29 16:27:59 +09:00
Daichi Hirono
77a1c65610 Open 4G+ file by using AppFuse.
To open files by using AppFuse, the provider needs to know the size of
file. Previously we cannot open 4G+ files because we cannot obtain file
size for such large files.

Now MtpDatabase contains correct size for 4GB+ file size. The CL starts
opening files by using AppFuse which obtains partial bytes by using
getObjectPartial64 operation.

FIXED=26840097

Change-Id: I1cb41972175c2b98f4aa76981decc6b3ad35486d
2016-03-28 14:57:23 +09:00
Daichi Hirono
1c431625b3 Merge "Get object size that is more than 4GB." into nyc-dev 2016-03-28 04:36:01 +00:00
Daichi Hirono
64111e08d9 Get object size that is more than 4GB.
MtpObjectInfo contains object size as 32-bit integer and the provider
needs to invoke MtpDevice#getObjectSizeLong hidden API to get 64-bit
object size.

The CL switches to use MtpDevice#getObjectSizeLong hidden API if
MtpObjectInfo#getCompressedSize() returns 0xffffffffL, which means the
object size is more than 4GB.

BUG=27805369

Change-Id: I87ea02c09aa784246cf016def309d1f39ed20e90
2016-03-28 13:34:03 +09:00
Daichi Hirono
09ece6c68b Implement FUSE_WRITE command in app fuse.
The CL adds a handler for FUSE_WRITE command which invokes a Java
handler.

BUG=23093747

Change-Id: I1903fca6b5663e6241ad540a89fe812310ba6810
(cherry picked from commit 35693da25af11583053d4af6a70d4acbf446978d)
2016-03-25 11:18:44 +09:00
Daichi Hirono
01e236b984 Update an icon for MtpDocumentsProvider.
BUG=27135431

Change-Id: Ic8619a533bdf2426fdf0d3691a883606fa027fb1
2016-03-23 11:07:29 +09:00
Daichi Hirono
f416030277 Merge "Fix null pointer exception when MTP/PTP device is detached." into nyc-dev 2016-03-22 08:56:00 +00:00
Daichi Hirono
071313e8b5 Don't map root documents in DocumentLoader.
Root documents must be added by RootScanner.
Previously DocumentLoader unintentionally tries to load unmapped root
documents, which causes crash.

BUG=27731902

Change-Id: I8674473d80d6e7f758f6636d30148f5d2118bc25
2016-03-22 13:46:52 +09:00
Daichi Hirono
b3383fdac0 Add root flag correctly.
BUG=27369570

Change-Id: I93b9b662667009a374d879c7ab667b10fb7cbf7c
2016-03-22 13:42:21 +09:00
Daichi Hirono
678ed36beb Count error document to complete adding documents to the database.
Previously DocumentLoader#LoaderTask had a counter to count loaded
documents and completes adding documents to the database.  However it
does not count documents where a MTP device returns an error for
getObjectInfo. The CL fixes the problem to ensure we complete documents
loading.

BUG=27729653

Change-Id: I696eac790a6535f1bd7a1855dc2d6f932e32eae5
2016-03-22 13:41:05 +09:00
Daichi Hirono
e80b147cf7 Fix null pointer exception when MTP/PTP device is detached.
FIXED=27743872

Change-Id: I9b754ba7efbc4958a47aafe332f1909bc0d50457
2016-03-22 11:05:22 +09:00
Daichi Hirono
07db6f3969 Merge "Fix the logic to add suffix when copying." into nyc-dev 2016-03-22 01:35:00 +00:00
Daichi Hirono
4f04fd358d Fix the logic to add suffix when copying.
Previously the logic add '.' even if the extension is empty.

BUG=27729309

Change-Id: I3fa4b09fea977b5c716ec5cbbbc7d9f4ab96db08
2016-03-22 10:30:47 +09:00
Daichi Hirono
2917fa9dd5 Change the title of DocumentsUI from "Files" to "Downloads".
BUG=27683277

Change-Id: I429cae8804b8f318f58039fc63d081c2a864fad0
2016-03-18 17:46:57 +09:00
Daichi Hirono
1360868de8 Merge "Add suffix number when copying a file." into nyc-dev 2016-03-17 04:53:35 +00:00
Daichi Hirono
fc7fb7533f Add suffix number when copying a file.
If we have an existing file in the destination directory, which has the
same name with the source file, adding suffix number is
DocumentsProvider's responsibility.

Because MTP does not provide a way to check existance of files with
given name, the logic is implemented as try-and error strategy. The CL
lets If we MtpDocumentsProvider assume we have a file that shares the
same name with the source file if it failed to invoke
MtpDevice#sendObjectInfo. In this case MtpDocumentsProvider retry to
invoke sendObjectInfo with new name with suffix number.

BUG=26991190

Change-Id: I223ac5031f079bc91eb27709b0356f621a1ed55b
2016-03-17 13:52:21 +09:00
Daichi Hirono
4327283240 Merge "Handle SQLite disk IO error in MtpDocumentsProvider#onCreate." into nyc-dev 2016-03-16 07:34:15 +00:00
Daichi Hirono
5884e1fa87 Handle SQLite disk IO error in MtpDocumentsProvider#onCreate.
The error should be regarded as an expected error that failes onCreate,
rather than crash.

BUG=27244513

Change-Id: Icf5164b0cba6cb919077539afe7701ada1e0c247
2016-03-16 14:36:27 +09:00
Daichi Hirono
aa0fa38432 Merge "Regard timeout as an error in the MtpDocumentsProvider test." into nyc-dev 2016-03-15 02:36:46 +00:00
Daichi Hirono
acb0e27bb3 Regard timeout as an error in the MtpDocumentsProvider test.
Previously if DocumentsProvider found timeout when terminatnig
RootScanner's background thread, it just output it in error log. Thus
the timeout is not regarded as an error in MtpDocumentsProviderTest, and
it makes flaky PipeManagerTest which runs just after
MtpDocumentsProviderTest.

The CL

 * lets MtpDocumentsProvider throw TimeoutException for timeout.
 * removes redundant resumeRootScanner calls to avoid timeout of
   RootScanner#pause.

Also the CL did cleanup the logic that pauses RootScanner when we don't
find any devices. Previously the logic was in
MtpDocumentsProvider#closeInternal but it is not efficient because we
invokes RootScanner#resume just after
MtpDocumentsProvider#closeInternal. Now the CL moves the logic to
RootScanner so that it can pause itself automatically.

BUG=27638500

Change-Id: Ic11bca67c099cbb0f46679db2f035988045d67d6
2016-03-15 11:35:34 +09:00
Daichi Hirono
497b473b8a MtpDocumentsProvider returns mime type from file extension.
File extensions contain more information to determine mime type than MTP
format codes. The CL lets MtpDocumentsProvider return mime type from
file extensions if it's not inconsitent with format code.

BUG=27004954

Change-Id: I08a4a91235b1d3f48e77b70b28c8c5aedf8d601d
2016-03-14 15:30:37 +09:00
Daichi Hirono
3bb37e7ff0 Cleans up the metadata in MtpDatabase at the first launch after booting.
When rebooting a device, applicaitons lose temporary URI permissions so
we don't need to keep document ID that are not granted persistent URI
permissions.

 1. Check Settings.Global.BOOT_COUNT to find out if it's first time to
    launch MtpDocumentsProvider since booting.
 2. If so, invokes clean up method of MtpDatabase.

BUG=26212981
Change-Id: Ic9a8ca7e7a9cac1ed91fdfb01e9dce14ce819243
2016-03-09 12:32:22 +09:00
Daichi Hirono
37a655aac1 Fix null pointer exception in DocumentLoader.
BUG=27489909

Change-Id: I1ebc9953f6db6639241d0c46257d110c5b0eb5b0
2016-03-07 19:41:10 +09:00
Daichi Hirono
24ab92a5f7 Ensure to complete background thread of PipeManager.
BUG=27488803

Change-Id: Ib540ab42f6263e1aea4c1bb184a4f88aa1454a14
2016-03-04 17:53:03 +09:00
Daichi Hirono
e5b499bcc5 Merge "Fix RootScanner to execute scan at least once before cancelling it." into nyc-dev 2016-03-01 07:50:18 +00:00
Daichi Hirono
2e9a57b0d4 Fix RootScanner to execute scan at least once before cancelling it.
Previously RootScanner is implemented by FutureTask, and if it is
cancelled before it starts running in background thread, the scan will
be never executed.

The CL stops using FutureTask and introduces CountDownLatch to control
UpdateRootsRunnable.

BUG=27369585

Change-Id: Ica8799faba0a8e5ca91a6b8be36dc4f5118d6333
2016-03-01 13:51:06 +09:00
Daichi Hirono
61ba923ca0 Set document flag by referring MTP supported operations.
BUG=26147375

Change-Id: I6c4244f1f1153c1bbbf21ea9d608dc1a92ca70cd
2016-02-26 16:23:30 +09:00
Daichi Hirono
0f32537e40 Stops performing operations that does not supported by MTP device.
MTP devices can return supported operation list. The CL sets root flag
by referring it.

BUG=26147375

Change-Id: I02397821e208cf5a8fcf7457aa279d2818ce24c7
2016-02-26 16:20:02 +09:00
Daichi Hirono
1bc289c92c Merge "Resolve unmapped document when the device is connected." into nyc-dev 2016-02-25 08:37:14 +00:00
Daichi Hirono
5c690557cc Delete disconnected row when the corresponding remote file is deleted in a
MTP device.

The CL has MtpDatabase remove file metadata for the case.

1. Files app opens a MTP device. MtpDocumentsProvider stores the
   metadata of files in MtpDatabase.
2. A user disconnects the MTP device. MtpDatabase marks the files
   metadata as disconnected.
3. A user operates the MTP device and deletes a file in the MTP device.
4. A user connects the MTP device to Android again, and opens the MTP
   device again in Files app.
5. MtpDocumentsProvider updates the metadata of files, it should delete
   metadata of a file that was deleted at step 3.

BUG=27280143

Change-Id: I79b8df2457cd5b36d1706a9c6e3827c8e7d16208
2016-02-23 16:45:42 +09:00
Daichi Hirono
4e94b8deaa Resolve unmapped document when the device is connected.
Once MTP device is disconnected from Android, the files on MTP device
are marked as 'DISCONNECTED' in metadata database. These metadata will
be back when MtpDocumentsProvider finds the reconnected MTP device and
fetches the files again.

Previously the 'DISCONNECTED' files are not automatically
refetched. User needs to see files in Documents UI again to reuse
document ID of 'DISCONNECTED' files. The CL changes DocumentLoader so
that it automatically refetches disconnected documents.

BUG=26212981

Change-Id: I5cb2cc9c11af72632e481c59a505794f43ed62ea
2016-02-23 14:39:08 +09:00
Daichi Hirono
f578fa275a Update object info when writing a file.
The MTP spec does not offer a way to update bytes of exisitng files, so
our provider implementation creates a new file with new bytes and
removes old one.

Previously the new file uses new document ID and the exising document ID
is expired. Also the provider does not update the metadata
database. Thus users see the old flie in DocumentsUI but actually the
files is not accessible.

The CL updates the database with exisitng document ID, so that we can
access the new file with exisiting document ID.

BUG=26549400

Change-Id: I629b707a2e662b34625e8b28857ef818d8933996
2016-02-19 18:05:42 +09:00
Daichi Hirono
ebd2405159 Use device key to map device documents.
The CL introduces MAPPING_KEY column to the database and lets Mapper use
the column to map IDs of devices.

It also removes the concept of mapping mode from Mapper for
simplyfing. Now Mapper just tries to multiple mapping keys (MTP
identifier, display name, and mapping key) to find candidate of ID
mapping.

BUG=26212981

Change-Id: I19f6c7dac146047e9978de4eb33d5076406037ad
(cherry picked from commit 637a2010f4a0c0484b13c4cb87aa2858bdf079b2)
2016-02-17 08:18:13 +00:00
Daichi Hirono
0abde68519 Merge "Add error message for locked device." into nyc-dev 2016-02-12 02:51:17 +00:00
Daichi Hirono
2965776ba4 Add error message for locked device.
If the remote MTP device is a smartphone, it does not provide files
until a user unlocks the device. The CL adds specific error message for
the situation.

BUG=26318917

Change-Id: Ic4c34609c55ec9c99b7b8a9143d6dae3835784e3
2016-02-11 18:45:30 -08:00
Daichi Hirono
46d537716e Merge "Open the connected device in Files app when tapping notification." into nyc-dev 2016-02-12 02:38:29 +00:00
Daichi Hirono
90b345138f Merge "Show specific error message for busy device." into nyc-dev 2016-02-12 01:02:53 +00:00
Daichi Hirono
1e3744441a Open the connected device in Files app when tapping notification.
Previously when tapping the MTP devce notification, MtpDocumentsProvider
opens home direcotry in Files app. The CL adds data URI of connected
device to the intent so that MtpDocumentsProvider can open the connected
device's root in Files app.

BUG=26611224
Change-Id: I97ecd6669852cd2fc875294cbcbf5660fbfaa0da
2016-02-11 16:36:49 -08:00
Daichi Hirono
c18f8076eb Show specific error message for busy device.
When MTP device is busy (e.g. used by other application),
MtpDocumentsProvider cannot open the device. The CL introduces specific
error message for the case.

BUG=26694828

Change-Id: Iffee2e1c554e4089601186469ff0eac2fd04decd
2016-02-11 12:45:55 -08:00
Daichi Hirono
ef2feefab7 Returns "application/octet-stream" for unknown types of MTP files.
According to DocumentsContract mime type field is required.
MtpDocumentsProvider should returns mime type for unknown types of MTP
files as well.

BUG=27148801

Change-Id: I7e4b832f6c00f438cf90190c074f204fdedceef5
2016-02-11 12:41:39 -08:00
Daichi Hirono
8e87364a67 Keep metadata of documents as disconnected status after the device is
disconnected.

To restore Document IDs when the device is reconnected, we need to keep
the metadata in database so that we can use it as hint to remap document
ID with new MTP IDs.

BUG=26212981

Change-Id: Idcc93c41c09d082a709281022c56188dabc80515
(cherry picked from commit 53f5af3f2ba1328d301a0f8a4ae3f574ccc5da65)
2016-02-10 16:59:22 +00:00
Daichi Hirono
619afdaae1 Check parent existance when adding/removing documents.
BUG=26212981
Change-Id: I8109e2324c027ec2182c6f521d57f3fe078a8660
(cherry picked from commit df803ec6ebd47ddc7f97ea8ef13aa359ecc7fb95)
2016-02-10 02:17:28 +00:00
Daichi Hirono
9fca541ab8 Relax mapping rule to make the mapping logic simple.
MtpDocumentsProvider remembers the mapping between SAF's ID and MTP's
ID. Sometimes we need to do heuristic to restore the mapping when MTP
device is reconnected.

Previously we do the mapping files that shares the same name more
strictly. For example,

1. Found file name "test.txt". Assign document ID "1".
2. MTP device is disconnected and the MTP ID of "1" is lost.
3. Found two files that have same name "test.txt" in the same directory.

Previously we don't reuse existing document ID "1" for neither of two
"test.txt" because it's not 1-to-1 mapping and we cannot determine which
one should be mapped with existing document ID. It means we need the
complete list of files in a directory to remap IDs. It takes long time
to fetch all file names in a directory when a directory has 100+
files. It's rare that a MTP device has the two files sharing the same
name in the same directory. Also the strict rule makes the mapping code
more complex.

The CL relax the rule of mapping, and it allows to reuse existing
document ID even if it is not 1-to-1 mapping. For the previous example,
it assigns "1" for either of "test.txt".

BUG=27053734
Change-Id: I19406fafc21f13ab94ba99411ce5e7f55ce7f658
(cherry picked from commit acdbc6e740ffbd465488b6eb0cf9388d43ae860a)
2016-02-09 18:36:27 +00:00
Daichi Hirono
bf1c501306 Merge "Fix NullPointerException around getObjectInfo." 2016-02-05 09:14:37 +00:00