Allow all downloads to be writable.
Jeff Sharkey [Tue, 1 Oct 2013 21:02:35 +0000 (14:02 -0700)]
Bug: 10943812
Change-Id: Ib0cb3e608c1f40a2e2fcd6e493c8f920d8b4221b

src/com/android/providers/downloads/DownloadProvider.java
src/com/android/providers/downloads/DownloadStorageProvider.java
src/com/android/providers/downloads/OpenHelper.java

index cb3dc18..750ee0f 100644 (file)
@@ -1195,10 +1195,8 @@ public final class DownloadProvider extends ContentProvider {
             logVerboseOpenFileInfo(uri, mode);
         }
 
-        final Cursor cursor = query(uri, new String[] {
-                Downloads.Impl._DATA, Downloads.Impl.COLUMN_ALLOW_WRITE }, null, null, null);
+        final Cursor cursor = query(uri, new String[] { Downloads.Impl._DATA }, null, null, null);
         String path;
-        boolean allowWrite;
         try {
             int count = (cursor != null) ? cursor.getCount() : 0;
             if (count != 1) {
@@ -1211,7 +1209,6 @@ public final class DownloadProvider extends ContentProvider {
 
             cursor.moveToFirst();
             path = cursor.getString(0);
-            allowWrite = cursor.getInt(1) != 0;
         } finally {
             IoUtils.closeQuietly(cursor);
         }
@@ -1222,19 +1219,14 @@ public final class DownloadProvider extends ContentProvider {
         if (!Helpers.isFilenameValid(path, mDownloadsDataDir)) {
             throw new FileNotFoundException("Invalid filename: " + path);
         }
-        if (!allowWrite && !"r".equals(mode)) {
-            throw new FileNotFoundException("Bad mode for " + uri + ": " + mode);
-        }
 
         final File file = new File(path);
-
-        ParcelFileDescriptor ret;
         if ("r".equals(mode)) {
-            ret = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
+            return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
         } else {
             try {
                 // When finished writing, update size and timestamp
-                ret = ParcelFileDescriptor.open(file, ParcelFileDescriptor.parseMode(mode),
+                return ParcelFileDescriptor.open(file, ParcelFileDescriptor.parseMode(mode),
                         mHandler, new OnCloseListener() {
                             @Override
                             public void onClose(IOException e) {
@@ -1249,14 +1241,6 @@ public final class DownloadProvider extends ContentProvider {
                 throw new FileNotFoundException("Failed to open for writing: " + e);
             }
         }
-
-        if (ret == null) {
-            if (Constants.LOGV) {
-                Log.v(Constants.TAG, "couldn't open file");
-            }
-            throw new FileNotFoundException("couldn't open file");
-        }
-        return ret;
     }
 
     @Override
index 622633a..c32f745 100644 (file)
@@ -292,7 +292,8 @@ public class DownloadStorageProvider extends DocumentsProvider {
         String mimeType = cursor.getString(
                 cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_MEDIA_TYPE));
         if (mimeType == null) {
-            mimeType = "application/octet-stream";
+            // Provide fake MIME type so it's openable
+            mimeType = "vnd.android.document/file";
         }
         Long size = cursor.getLong(
                 cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
@@ -327,17 +328,11 @@ public class DownloadStorageProvider extends DocumentsProvider {
                 break;
         }
 
-        int flags = Document.FLAG_SUPPORTS_DELETE;
+        int flags = Document.FLAG_SUPPORTS_DELETE | Document.FLAG_SUPPORTS_WRITE;
         if (mimeType != null && mimeType.startsWith("image/")) {
             flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
         }
 
-        final int allowWrite = cursor.getInt(
-                cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_ALLOW_WRITE));
-        if (allowWrite != 0) {
-            flags |= Document.FLAG_SUPPORTS_WRITE;
-        }
-
         final long lastModified = cursor.getLong(
                 cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LAST_MODIFIED_TIMESTAMP));
 
index af7a37f..4eb319c 100644 (file)
@@ -78,7 +78,6 @@ public class OpenHelper {
             mimeType = DownloadDrmHelper.getOriginalMimeType(context, file, mimeType);
 
             final Intent intent = new Intent(Intent.ACTION_VIEW);
-            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
 
             if ("application/vnd.android.package-archive".equals(mimeType)) {
                 // PackageInstaller doesn't like content URIs, so open file
@@ -90,9 +89,12 @@ public class OpenHelper {
                 intent.putExtra(Intent.EXTRA_REFERRER, getRefererUri(context, id));
                 intent.putExtra(Intent.EXTRA_ORIGINATING_UID, getOriginatingUid(context, id));
             } else if ("file".equals(localUri.getScheme())) {
+                intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
+                        | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                 intent.setDataAndType(
                         ContentUris.withAppendedId(ALL_DOWNLOADS_CONTENT_URI, id), mimeType);
             } else {
+                intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                 intent.setDataAndType(localUri, mimeType);
             }