Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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.externalstorage; |
| 18 | |
Garfield Tan | 06940e1 | 2016-10-07 16:03:17 -0700 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Jeff Sharkey | 06823d4 | 2017-05-09 16:55:29 -0600 | [diff] [blame] | 20 | import android.app.usage.StorageStatsManager; |
Jeff Sharkey | db5ef12 | 2013-10-25 17:12:49 -0700 | [diff] [blame] | 21 | import android.content.ContentResolver; |
Garfield Tan | 92b96ba | 2016-11-01 14:33:48 -0700 | [diff] [blame] | 22 | import android.content.UriPermission; |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 23 | import android.database.Cursor; |
| 24 | import android.database.MatrixCursor; |
Jeff Sharkey | 9d0843d | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 25 | import android.database.MatrixCursor.RowBuilder; |
Jeff Sharkey | db5ef12 | 2013-10-25 17:12:49 -0700 | [diff] [blame] | 26 | import android.net.Uri; |
Ben Lin | e7822fb | 2016-06-24 15:21:08 -0700 | [diff] [blame] | 27 | import android.os.Binder; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 28 | import android.os.Bundle; |
Steve McKay | 5c462a0 | 2016-01-29 16:13:21 -0800 | [diff] [blame] | 29 | import android.os.Environment; |
Jeff Sharkey | b78b754d | 2018-01-04 15:07:38 -0700 | [diff] [blame] | 30 | import android.os.IBinder; |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 31 | import android.os.UserHandle; |
Jeff Sharkey | b78b754d | 2018-01-04 15:07:38 -0700 | [diff] [blame] | 32 | import android.os.UserManager; |
Steve McKay | ba23e54 | 2016-03-02 15:15:00 -0800 | [diff] [blame] | 33 | import android.os.storage.DiskInfo; |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 34 | import android.os.storage.StorageManager; |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 35 | import android.os.storage.VolumeInfo; |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 36 | import android.provider.DocumentsContract; |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 37 | import android.provider.DocumentsContract.Document; |
Garfield Tan | aba97f3 | 2016-10-06 17:34:19 +0000 | [diff] [blame] | 38 | import android.provider.DocumentsContract.Path; |
Garfield Tan | 06940e1 | 2016-10-07 16:03:17 -0700 | [diff] [blame] | 39 | import android.provider.DocumentsContract.Root; |
Steve McKay | ecec7cb | 2016-03-02 11:35:39 -0800 | [diff] [blame] | 40 | import android.provider.Settings; |
Jeff Sharkey | b7e1255 | 2014-05-21 22:22:03 -0700 | [diff] [blame] | 41 | import android.text.TextUtils; |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 42 | import android.util.ArrayMap; |
| 43 | import android.util.DebugUtils; |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 44 | import android.util.Log; |
Garfield Tan | aba97f3 | 2016-10-06 17:34:19 +0000 | [diff] [blame] | 45 | import android.util.Pair; |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 46 | |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 47 | import com.android.internal.annotations.GuardedBy; |
Garfield Tan | 75379db | 2017-02-08 15:32:56 -0800 | [diff] [blame] | 48 | import com.android.internal.content.FileSystemProvider; |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 49 | import com.android.internal.util.IndentingPrintWriter; |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 50 | |
| 51 | import java.io.File; |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 52 | import java.io.FileDescriptor; |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 53 | import java.io.FileNotFoundException; |
Jeff Sharkey | 06823d4 | 2017-05-09 16:55:29 -0600 | [diff] [blame] | 54 | import java.io.IOException; |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 55 | import java.io.PrintWriter; |
Garfield Tan | 75379db | 2017-02-08 15:32:56 -0800 | [diff] [blame] | 56 | import java.util.Collections; |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 57 | import java.util.List; |
Garfield Tan | 92b96ba | 2016-11-01 14:33:48 -0700 | [diff] [blame] | 58 | import java.util.Objects; |
Jeff Sharkey | 06823d4 | 2017-05-09 16:55:29 -0600 | [diff] [blame] | 59 | import java.util.UUID; |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 60 | |
Garfield Tan | 75379db | 2017-02-08 15:32:56 -0800 | [diff] [blame] | 61 | public class ExternalStorageProvider extends FileSystemProvider { |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 62 | private static final String TAG = "ExternalStorage"; |
| 63 | |
Steve McKay | ecec7cb | 2016-03-02 11:35:39 -0800 | [diff] [blame] | 64 | private static final boolean DEBUG = false; |
Jeff Sharkey | db5ef12 | 2013-10-25 17:12:49 -0700 | [diff] [blame] | 65 | |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 66 | public static final String AUTHORITY = "com.android.externalstorage.documents"; |
| 67 | |
Makoto Onuki | 14a6df7 | 2015-07-01 14:55:14 -0700 | [diff] [blame] | 68 | private static final Uri BASE_URI = |
| 69 | new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(AUTHORITY).build(); |
| 70 | |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 71 | // docId format: root:path/to/file |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 72 | |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 73 | private static final String[] DEFAULT_ROOT_PROJECTION = new String[] { |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 74 | Root.COLUMN_ROOT_ID, Root.COLUMN_FLAGS, Root.COLUMN_ICON, Root.COLUMN_TITLE, |
| 75 | Root.COLUMN_DOCUMENT_ID, Root.COLUMN_AVAILABLE_BYTES, |
Jeff Sharkey | 9d0843d | 2013-05-07 12:41:33 -0700 | [diff] [blame] | 76 | }; |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 77 | |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 78 | private static final String[] DEFAULT_DOCUMENT_PROJECTION = new String[] { |
| 79 | Document.COLUMN_DOCUMENT_ID, Document.COLUMN_MIME_TYPE, Document.COLUMN_DISPLAY_NAME, |
| 80 | Document.COLUMN_LAST_MODIFIED, Document.COLUMN_FLAGS, Document.COLUMN_SIZE, |
| 81 | }; |
| 82 | |
| 83 | private static class RootInfo { |
| 84 | public String rootId; |
Ben Lin | e7822fb | 2016-06-24 15:21:08 -0700 | [diff] [blame] | 85 | public String volumeId; |
Jeff Sharkey | 06823d4 | 2017-05-09 16:55:29 -0600 | [diff] [blame] | 86 | public UUID storageUuid; |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 87 | public int flags; |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 88 | public String title; |
| 89 | public String docId; |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 90 | public File visiblePath; |
| 91 | public File path; |
Steve McKay | c6a4cd8 | 2015-11-18 14:56:50 -0800 | [diff] [blame] | 92 | public boolean reportAvailableBytes = true; |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 95 | private static final String ROOT_ID_PRIMARY_EMULATED = "primary"; |
Steve McKay | c6a4cd8 | 2015-11-18 14:56:50 -0800 | [diff] [blame] | 96 | private static final String ROOT_ID_HOME = "home"; |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 97 | |
| 98 | private StorageManager mStorageManager; |
Jeff Sharkey | b78b754d | 2018-01-04 15:07:38 -0700 | [diff] [blame] | 99 | private UserManager mUserManager; |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 100 | |
| 101 | private final Object mRootsLock = new Object(); |
| 102 | |
| 103 | @GuardedBy("mRootsLock") |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 104 | private ArrayMap<String, RootInfo> mRoots = new ArrayMap<>(); |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 105 | |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 106 | @Override |
| 107 | public boolean onCreate() { |
Garfield Tan | 75379db | 2017-02-08 15:32:56 -0800 | [diff] [blame] | 108 | super.onCreate(DEFAULT_DOCUMENT_PROJECTION); |
| 109 | |
Jeff Sharkey | b78b754d | 2018-01-04 15:07:38 -0700 | [diff] [blame] | 110 | mStorageManager = getContext().getSystemService(StorageManager.class); |
| 111 | mUserManager = getContext().getSystemService(UserManager.class); |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 112 | |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 113 | updateVolumes(); |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 114 | return true; |
| 115 | } |
| 116 | |
Jeff Sharkey | b78b754d | 2018-01-04 15:07:38 -0700 | [diff] [blame] | 117 | private void enforceShellRestrictions() { |
| 118 | if (UserHandle.getCallingAppId() == android.os.Process.SHELL_UID |
| 119 | && mUserManager.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER)) { |
| 120 | throw new SecurityException( |
| 121 | "Shell user cannot access files for user " + UserHandle.myUserId()); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | protected int enforceReadPermissionInner(Uri uri, String callingPkg, IBinder callerToken) |
| 127 | throws SecurityException { |
| 128 | enforceShellRestrictions(); |
| 129 | return super.enforceReadPermissionInner(uri, callingPkg, callerToken); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | protected int enforceWritePermissionInner(Uri uri, String callingPkg, IBinder callerToken) |
| 134 | throws SecurityException { |
| 135 | enforceShellRestrictions(); |
| 136 | return super.enforceWritePermissionInner(uri, callingPkg, callerToken); |
| 137 | } |
| 138 | |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 139 | public void updateVolumes() { |
| 140 | synchronized (mRootsLock) { |
| 141 | updateVolumesLocked(); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | private void updateVolumesLocked() { |
| 146 | mRoots.clear(); |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 147 | |
Steve McKay | c6a4cd8 | 2015-11-18 14:56:50 -0800 | [diff] [blame] | 148 | VolumeInfo primaryVolume = null; |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 149 | final int userId = UserHandle.myUserId(); |
| 150 | final List<VolumeInfo> volumes = mStorageManager.getVolumes(); |
| 151 | for (VolumeInfo volume : volumes) { |
| 152 | if (!volume.isMountedReadable()) continue; |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 153 | |
| 154 | final String rootId; |
Jeff Sharkey | b521fea | 2015-06-15 21:09:10 -0700 | [diff] [blame] | 155 | final String title; |
Jeff Sharkey | 06823d4 | 2017-05-09 16:55:29 -0600 | [diff] [blame] | 156 | final UUID storageUuid; |
Jeff Sharkey | b521fea | 2015-06-15 21:09:10 -0700 | [diff] [blame] | 157 | if (volume.getType() == VolumeInfo.TYPE_EMULATED) { |
| 158 | // We currently only support a single emulated volume mounted at |
| 159 | // a time, and it's always considered the primary |
Steve McKay | ecec7cb | 2016-03-02 11:35:39 -0800 | [diff] [blame] | 160 | if (DEBUG) Log.d(TAG, "Found primary volume: " + volume); |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 161 | rootId = ROOT_ID_PRIMARY_EMULATED; |
Steve McKay | ecec7cb | 2016-03-02 11:35:39 -0800 | [diff] [blame] | 162 | |
Jeff Sharkey | b521fea | 2015-06-15 21:09:10 -0700 | [diff] [blame] | 163 | if (VolumeInfo.ID_EMULATED_INTERNAL.equals(volume.getId())) { |
Steve McKay | ecec7cb | 2016-03-02 11:35:39 -0800 | [diff] [blame] | 164 | // This is basically the user's primary device storage. |
| 165 | // Use device name for the volume since this is likely same thing |
| 166 | // the user sees when they mount their phone on another device. |
| 167 | String deviceName = Settings.Global.getString( |
| 168 | getContext().getContentResolver(), Settings.Global.DEVICE_NAME); |
| 169 | |
| 170 | // Device name should always be set. In case it isn't, though, |
| 171 | // fall back to a localized "Internal Storage" string. |
| 172 | title = !TextUtils.isEmpty(deviceName) |
| 173 | ? deviceName |
| 174 | : getContext().getString(R.string.root_internal_storage); |
Jeff Sharkey | 06823d4 | 2017-05-09 16:55:29 -0600 | [diff] [blame] | 175 | storageUuid = StorageManager.UUID_DEFAULT; |
Jeff Sharkey | b521fea | 2015-06-15 21:09:10 -0700 | [diff] [blame] | 176 | } else { |
Steve McKay | ecec7cb | 2016-03-02 11:35:39 -0800 | [diff] [blame] | 177 | // This should cover all other storage devices, like an SD card |
| 178 | // or USB OTG drive plugged in. Using getBestVolumeDescription() |
| 179 | // will give us a nice string like "Samsung SD card" or "SanDisk USB drive" |
Jeff Sharkey | b521fea | 2015-06-15 21:09:10 -0700 | [diff] [blame] | 180 | final VolumeInfo privateVol = mStorageManager.findPrivateForEmulated(volume); |
| 181 | title = mStorageManager.getBestVolumeDescription(privateVol); |
Jeff Sharkey | 06823d4 | 2017-05-09 16:55:29 -0600 | [diff] [blame] | 182 | storageUuid = StorageManager.convert(privateVol.fsUuid); |
Jeff Sharkey | b521fea | 2015-06-15 21:09:10 -0700 | [diff] [blame] | 183 | } |
Jeff Sharkey | 3b699d7 | 2016-10-31 14:33:49 -0600 | [diff] [blame] | 184 | } else if (volume.getType() == VolumeInfo.TYPE_PUBLIC |
| 185 | && volume.getMountUserId() == userId) { |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 186 | rootId = volume.getFsUuid(); |
Jeff Sharkey | b521fea | 2015-06-15 21:09:10 -0700 | [diff] [blame] | 187 | title = mStorageManager.getBestVolumeDescription(volume); |
Jeff Sharkey | 06823d4 | 2017-05-09 16:55:29 -0600 | [diff] [blame] | 188 | storageUuid = null; |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 189 | } else { |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 190 | // Unsupported volume; ignore |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 191 | continue; |
| 192 | } |
| 193 | |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 194 | if (TextUtils.isEmpty(rootId)) { |
| 195 | Log.d(TAG, "Missing UUID for " + volume.getId() + "; skipping"); |
| 196 | continue; |
| 197 | } |
| 198 | if (mRoots.containsKey(rootId)) { |
| 199 | Log.w(TAG, "Duplicate UUID " + rootId + " for " + volume.getId() + "; skipping"); |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 200 | continue; |
| 201 | } |
| 202 | |
Steve McKay | c6a4cd8 | 2015-11-18 14:56:50 -0800 | [diff] [blame] | 203 | final RootInfo root = new RootInfo(); |
| 204 | mRoots.put(rootId, root); |
| 205 | |
| 206 | root.rootId = rootId; |
Ben Lin | e7822fb | 2016-06-24 15:21:08 -0700 | [diff] [blame] | 207 | root.volumeId = volume.id; |
Jeff Sharkey | 06823d4 | 2017-05-09 16:55:29 -0600 | [diff] [blame] | 208 | root.storageUuid = storageUuid; |
Steve McKay | efa1761 | 2016-01-29 18:15:39 -0800 | [diff] [blame] | 209 | root.flags = Root.FLAG_LOCAL_ONLY |
Garfield Tan | aba97f3 | 2016-10-06 17:34:19 +0000 | [diff] [blame] | 210 | | Root.FLAG_SUPPORTS_SEARCH |
| 211 | | Root.FLAG_SUPPORTS_IS_CHILD; |
Steve McKay | c6a4cd8 | 2015-11-18 14:56:50 -0800 | [diff] [blame] | 212 | |
Steve McKay | ba23e54 | 2016-03-02 15:15:00 -0800 | [diff] [blame] | 213 | final DiskInfo disk = volume.getDisk(); |
| 214 | if (DEBUG) Log.d(TAG, "Disk for root " + rootId + " is " + disk); |
| 215 | if (disk != null && disk.isSd()) { |
| 216 | root.flags |= Root.FLAG_REMOVABLE_SD; |
| 217 | } else if (disk != null && disk.isUsb()) { |
| 218 | root.flags |= Root.FLAG_REMOVABLE_USB; |
| 219 | } |
| 220 | |
Ben Lin | e7822fb | 2016-06-24 15:21:08 -0700 | [diff] [blame] | 221 | if (!VolumeInfo.ID_EMULATED_INTERNAL.equals(volume.getId())) { |
| 222 | root.flags |= Root.FLAG_SUPPORTS_EJECT; |
| 223 | } |
| 224 | |
Steve McKay | efa1761 | 2016-01-29 18:15:39 -0800 | [diff] [blame] | 225 | if (volume.isPrimary()) { |
| 226 | // save off the primary volume for subsequent "Home" dir initialization. |
| 227 | primaryVolume = volume; |
Aga Wronska | 1719b35 | 2016-03-21 11:28:03 -0700 | [diff] [blame] | 228 | root.flags |= Root.FLAG_ADVANCED; |
Steve McKay | efa1761 | 2016-01-29 18:15:39 -0800 | [diff] [blame] | 229 | } |
Steve McKay | c6a4cd8 | 2015-11-18 14:56:50 -0800 | [diff] [blame] | 230 | // Dunno when this would NOT be the case, but never hurts to be correct. |
| 231 | if (volume.isMountedWritable()) { |
| 232 | root.flags |= Root.FLAG_SUPPORTS_CREATE; |
| 233 | } |
| 234 | root.title = title; |
| 235 | if (volume.getType() == VolumeInfo.TYPE_PUBLIC) { |
| 236 | root.flags |= Root.FLAG_HAS_SETTINGS; |
| 237 | } |
| 238 | if (volume.isVisibleForRead(userId)) { |
| 239 | root.visiblePath = volume.getPathForUser(userId); |
| 240 | } else { |
| 241 | root.visiblePath = null; |
| 242 | } |
| 243 | root.path = volume.getInternalPathForUser(userId); |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 244 | try { |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 245 | root.docId = getDocIdForFile(root.path); |
Steve McKay | c6a4cd8 | 2015-11-18 14:56:50 -0800 | [diff] [blame] | 246 | } catch (FileNotFoundException e) { |
| 247 | throw new IllegalStateException(e); |
| 248 | } |
| 249 | } |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 250 | |
Steve McKay | ecec7cb | 2016-03-02 11:35:39 -0800 | [diff] [blame] | 251 | // Finally, if primary storage is available we add the "Documents" directory. |
| 252 | // If I recall correctly the actual directory is created on demand |
| 253 | // by calling either getPathForUser, or getInternalPathForUser. |
Steve McKay | c6a4cd8 | 2015-11-18 14:56:50 -0800 | [diff] [blame] | 254 | if (primaryVolume != null && primaryVolume.isVisible()) { |
| 255 | final RootInfo root = new RootInfo(); |
| 256 | root.rootId = ROOT_ID_HOME; |
| 257 | mRoots.put(root.rootId, root); |
Steve McKay | ab3b893 | 2016-02-16 11:37:03 -0800 | [diff] [blame] | 258 | root.title = getContext().getString(R.string.root_documents); |
Steve McKay | c6a4cd8 | 2015-11-18 14:56:50 -0800 | [diff] [blame] | 259 | |
| 260 | // Only report bytes on *volumes*...as a matter of policy. |
| 261 | root.reportAvailableBytes = false; |
| 262 | root.flags = Root.FLAG_LOCAL_ONLY | Root.FLAG_SUPPORTS_SEARCH |
| 263 | | Root.FLAG_SUPPORTS_IS_CHILD; |
| 264 | |
| 265 | // Dunno when this would NOT be the case, but never hurts to be correct. |
| 266 | if (primaryVolume.isMountedWritable()) { |
| 267 | root.flags |= Root.FLAG_SUPPORTS_CREATE; |
| 268 | } |
| 269 | |
Steve McKay | ecec7cb | 2016-03-02 11:35:39 -0800 | [diff] [blame] | 270 | // Create the "Documents" directory on disk (don't use the localized title). |
Steve McKay | c6a4cd8 | 2015-11-18 14:56:50 -0800 | [diff] [blame] | 271 | root.visiblePath = new File( |
Steve McKay | ab3b893 | 2016-02-16 11:37:03 -0800 | [diff] [blame] | 272 | primaryVolume.getPathForUser(userId), Environment.DIRECTORY_DOCUMENTS); |
Steve McKay | c6a4cd8 | 2015-11-18 14:56:50 -0800 | [diff] [blame] | 273 | root.path = new File( |
Steve McKay | ab3b893 | 2016-02-16 11:37:03 -0800 | [diff] [blame] | 274 | primaryVolume.getInternalPathForUser(userId), Environment.DIRECTORY_DOCUMENTS); |
Steve McKay | c6a4cd8 | 2015-11-18 14:56:50 -0800 | [diff] [blame] | 275 | try { |
| 276 | root.docId = getDocIdForFile(root.path); |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 277 | } catch (FileNotFoundException e) { |
| 278 | throw new IllegalStateException(e); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | Log.d(TAG, "After updating volumes, found " + mRoots.size() + " active roots"); |
| 283 | |
Makoto Onuki | 14a6df7 | 2015-07-01 14:55:14 -0700 | [diff] [blame] | 284 | // Note this affects content://com.android.externalstorage.documents/root/39BD-07C5 |
| 285 | // as well as content://com.android.externalstorage.documents/document/*/children, |
| 286 | // so just notify on content://com.android.externalstorage.documents/. |
| 287 | getContext().getContentResolver().notifyChange(BASE_URI, null, false); |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 290 | private static String[] resolveRootProjection(String[] projection) { |
| 291 | return projection != null ? projection : DEFAULT_ROOT_PROJECTION; |
| 292 | } |
| 293 | |
Garfield Tan | 75379db | 2017-02-08 15:32:56 -0800 | [diff] [blame] | 294 | @Override |
| 295 | protected String getDocIdForFile(File file) throws FileNotFoundException { |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 296 | return getDocIdForFileMaybeCreate(file, false); |
| 297 | } |
| 298 | |
| 299 | private String getDocIdForFileMaybeCreate(File file, boolean createNewDir) |
| 300 | throws FileNotFoundException { |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 301 | String path = file.getAbsolutePath(); |
Jeff Sharkey | 92d7e69 | 2013-08-02 10:33:21 -0700 | [diff] [blame] | 302 | |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 303 | // Find the most-specific root path |
Garfield Tan | dc9593e | 2017-01-09 18:04:43 -0800 | [diff] [blame] | 304 | boolean visiblePath = false; |
| 305 | RootInfo mostSpecificRoot = getMostSpecificRootForPath(path, false); |
| 306 | |
| 307 | if (mostSpecificRoot == null) { |
| 308 | // Try visible path if no internal path matches. MediaStore uses visible paths. |
| 309 | visiblePath = true; |
| 310 | mostSpecificRoot = getMostSpecificRootForPath(path, true); |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 311 | } |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 312 | |
Garfield Tan | dc9593e | 2017-01-09 18:04:43 -0800 | [diff] [blame] | 313 | if (mostSpecificRoot == null) { |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 314 | throw new FileNotFoundException("Failed to find root that contains " + path); |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 315 | } |
Jeff Sharkey | 20d96d8 | 2013-07-30 17:08:39 -0700 | [diff] [blame] | 316 | |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 317 | // Start at first char of path under root |
Garfield Tan | dc9593e | 2017-01-09 18:04:43 -0800 | [diff] [blame] | 318 | final String rootPath = visiblePath |
| 319 | ? mostSpecificRoot.visiblePath.getAbsolutePath() |
| 320 | : mostSpecificRoot.path.getAbsolutePath(); |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 321 | if (rootPath.equals(path)) { |
| 322 | path = ""; |
| 323 | } else if (rootPath.endsWith("/")) { |
| 324 | path = path.substring(rootPath.length()); |
Jeff Sharkey | 92d7e69 | 2013-08-02 10:33:21 -0700 | [diff] [blame] | 325 | } else { |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 326 | path = path.substring(rootPath.length() + 1); |
Jeff Sharkey | 92d7e69 | 2013-08-02 10:33:21 -0700 | [diff] [blame] | 327 | } |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 328 | |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 329 | if (!file.exists() && createNewDir) { |
| 330 | Log.i(TAG, "Creating new directory " + file); |
| 331 | if (!file.mkdir()) { |
| 332 | Log.e(TAG, "Could not create directory " + file); |
| 333 | } |
| 334 | } |
| 335 | |
Garfield Tan | dc9593e | 2017-01-09 18:04:43 -0800 | [diff] [blame] | 336 | return mostSpecificRoot.rootId + ':' + path; |
| 337 | } |
| 338 | |
| 339 | private RootInfo getMostSpecificRootForPath(String path, boolean visible) { |
| 340 | // Find the most-specific root path |
| 341 | RootInfo mostSpecificRoot = null; |
| 342 | String mostSpecificPath = null; |
| 343 | synchronized (mRootsLock) { |
| 344 | for (int i = 0; i < mRoots.size(); i++) { |
| 345 | final RootInfo root = mRoots.valueAt(i); |
| 346 | final File rootFile = visible ? root.visiblePath : root.path; |
| 347 | if (rootFile != null) { |
| 348 | final String rootPath = rootFile.getAbsolutePath(); |
| 349 | if (path.startsWith(rootPath) && (mostSpecificPath == null |
| 350 | || rootPath.length() > mostSpecificPath.length())) { |
| 351 | mostSpecificRoot = root; |
| 352 | mostSpecificPath = rootPath; |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | return mostSpecificRoot; |
Jeff Sharkey | 20d96d8 | 2013-07-30 17:08:39 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Garfield Tan | 75379db | 2017-02-08 15:32:56 -0800 | [diff] [blame] | 361 | @Override |
| 362 | protected File getFileForDocId(String docId, boolean visible) throws FileNotFoundException { |
Garfield Tan | 06940e1 | 2016-10-07 16:03:17 -0700 | [diff] [blame] | 363 | RootInfo root = getRootFromDocId(docId); |
| 364 | return buildFile(root, docId, visible); |
Garfield Tan | aba97f3 | 2016-10-06 17:34:19 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | private Pair<RootInfo, File> resolveDocId(String docId, boolean visible) |
| 368 | throws FileNotFoundException { |
Garfield Tan | 06940e1 | 2016-10-07 16:03:17 -0700 | [diff] [blame] | 369 | RootInfo root = getRootFromDocId(docId); |
| 370 | return Pair.create(root, buildFile(root, docId, visible)); |
| 371 | } |
| 372 | |
| 373 | private RootInfo getRootFromDocId(String docId) throws FileNotFoundException { |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 374 | final int splitIndex = docId.indexOf(':', 1); |
| 375 | final String tag = docId.substring(0, splitIndex); |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 376 | |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 377 | RootInfo root; |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 378 | synchronized (mRootsLock) { |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 379 | root = mRoots.get(tag); |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 380 | } |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 381 | if (root == null) { |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 382 | throw new FileNotFoundException("No root for " + tag); |
| 383 | } |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 384 | |
Garfield Tan | 06940e1 | 2016-10-07 16:03:17 -0700 | [diff] [blame] | 385 | return root; |
| 386 | } |
| 387 | |
| 388 | private File buildFile(RootInfo root, String docId, boolean visible) |
| 389 | throws FileNotFoundException { |
| 390 | final int splitIndex = docId.indexOf(':', 1); |
| 391 | final String path = docId.substring(splitIndex + 1); |
| 392 | |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 393 | File target = visible ? root.visiblePath : root.path; |
| 394 | if (target == null) { |
| 395 | return null; |
| 396 | } |
Jeff Sharkey | 3e1189b | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 397 | if (!target.exists()) { |
| 398 | target.mkdirs(); |
| 399 | } |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 400 | target = new File(target, path); |
| 401 | if (!target.exists()) { |
| 402 | throw new FileNotFoundException("Missing file for " + docId + " at " + target); |
| 403 | } |
Garfield Tan | 06940e1 | 2016-10-07 16:03:17 -0700 | [diff] [blame] | 404 | return target; |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Garfield Tan | 75379db | 2017-02-08 15:32:56 -0800 | [diff] [blame] | 407 | @Override |
| 408 | protected Uri buildNotificationUri(String docId) { |
| 409 | return DocumentsContract.buildChildDocumentsUri(AUTHORITY, docId); |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | @Override |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 413 | public Cursor queryRoots(String[] projection) throws FileNotFoundException { |
| 414 | final MatrixCursor result = new MatrixCursor(resolveRootProjection(projection)); |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 415 | synchronized (mRootsLock) { |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 416 | for (RootInfo root : mRoots.values()) { |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 417 | final RowBuilder row = result.newRow(); |
| 418 | row.add(Root.COLUMN_ROOT_ID, root.rootId); |
| 419 | row.add(Root.COLUMN_FLAGS, root.flags); |
| 420 | row.add(Root.COLUMN_TITLE, root.title); |
| 421 | row.add(Root.COLUMN_DOCUMENT_ID, root.docId); |
Jeff Sharkey | 06823d4 | 2017-05-09 16:55:29 -0600 | [diff] [blame] | 422 | |
| 423 | long availableBytes = -1; |
| 424 | if (root.reportAvailableBytes) { |
| 425 | if (root.storageUuid != null) { |
| 426 | try { |
| 427 | availableBytes = getContext() |
| 428 | .getSystemService(StorageStatsManager.class) |
| 429 | .getFreeBytes(root.storageUuid); |
| 430 | } catch (IOException e) { |
| 431 | Log.w(TAG, e); |
| 432 | } |
| 433 | } else { |
| 434 | availableBytes = root.path.getUsableSpace(); |
| 435 | } |
| 436 | } |
| 437 | row.add(Root.COLUMN_AVAILABLE_BYTES, availableBytes); |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 438 | } |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 439 | } |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 440 | return result; |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | @Override |
Garfield Tan | b690b4d | 2017-03-01 16:05:23 -0800 | [diff] [blame] | 444 | public Path findDocumentPath(@Nullable String parentDocId, String childDocId) |
Garfield Tan | aba97f3 | 2016-10-06 17:34:19 +0000 | [diff] [blame] | 445 | throws FileNotFoundException { |
Garfield Tan | 06940e1 | 2016-10-07 16:03:17 -0700 | [diff] [blame] | 446 | final Pair<RootInfo, File> resolvedDocId = resolveDocId(childDocId, false); |
| 447 | final RootInfo root = resolvedDocId.first; |
| 448 | File child = resolvedDocId.second; |
Garfield Tan | aba97f3 | 2016-10-06 17:34:19 +0000 | [diff] [blame] | 449 | |
Garfield Tan | 06940e1 | 2016-10-07 16:03:17 -0700 | [diff] [blame] | 450 | final File parent = TextUtils.isEmpty(parentDocId) |
| 451 | ? root.path |
| 452 | : getFileForDocId(parentDocId); |
| 453 | |
Garfield Tan | 75379db | 2017-02-08 15:32:56 -0800 | [diff] [blame] | 454 | return new Path(parentDocId == null ? root.rootId : null, findDocumentPath(parent, child)); |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Garfield Tan | 92b96ba | 2016-11-01 14:33:48 -0700 | [diff] [blame] | 457 | private Uri getDocumentUri(String path, List<UriPermission> accessUriPermissions) |
| 458 | throws FileNotFoundException { |
| 459 | File doc = new File(path); |
| 460 | |
| 461 | final String docId = getDocIdForFile(doc); |
| 462 | |
| 463 | UriPermission docUriPermission = null; |
| 464 | UriPermission treeUriPermission = null; |
| 465 | for (UriPermission uriPermission : accessUriPermissions) { |
| 466 | final Uri uri = uriPermission.getUri(); |
| 467 | if (AUTHORITY.equals(uri.getAuthority())) { |
| 468 | boolean matchesRequestedDoc = false; |
| 469 | if (DocumentsContract.isTreeUri(uri)) { |
| 470 | final String parentDocId = DocumentsContract.getTreeDocumentId(uri); |
Garfield Tan | dc9593e | 2017-01-09 18:04:43 -0800 | [diff] [blame] | 471 | if (isChildDocument(parentDocId, docId)) { |
Garfield Tan | 92b96ba | 2016-11-01 14:33:48 -0700 | [diff] [blame] | 472 | treeUriPermission = uriPermission; |
| 473 | matchesRequestedDoc = true; |
| 474 | } |
| 475 | } else { |
| 476 | final String candidateDocId = DocumentsContract.getDocumentId(uri); |
Garfield Tan | dc9593e | 2017-01-09 18:04:43 -0800 | [diff] [blame] | 477 | if (Objects.equals(docId, candidateDocId)) { |
Garfield Tan | 92b96ba | 2016-11-01 14:33:48 -0700 | [diff] [blame] | 478 | docUriPermission = uriPermission; |
| 479 | matchesRequestedDoc = true; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | if (matchesRequestedDoc && allowsBothReadAndWrite(uriPermission)) { |
| 484 | // This URI permission provides everything an app can get, no need to |
| 485 | // further check any other granted URI. |
| 486 | break; |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | // Full permission URI first. |
| 492 | if (allowsBothReadAndWrite(treeUriPermission)) { |
| 493 | return DocumentsContract.buildDocumentUriUsingTree(treeUriPermission.getUri(), docId); |
| 494 | } |
| 495 | |
| 496 | if (allowsBothReadAndWrite(docUriPermission)) { |
| 497 | return docUriPermission.getUri(); |
| 498 | } |
| 499 | |
| 500 | // Then partial permission URI. |
| 501 | if (treeUriPermission != null) { |
| 502 | return DocumentsContract.buildDocumentUriUsingTree(treeUriPermission.getUri(), docId); |
| 503 | } |
| 504 | |
| 505 | if (docUriPermission != null) { |
| 506 | return docUriPermission.getUri(); |
| 507 | } |
| 508 | |
| 509 | throw new SecurityException("The app is not given any access to the document under path " + |
| 510 | path + " with permissions granted in " + accessUriPermissions); |
| 511 | } |
| 512 | |
| 513 | private static boolean allowsBothReadAndWrite(UriPermission permission) { |
| 514 | return permission != null |
| 515 | && permission.isReadPermission() |
| 516 | && permission.isWritePermission(); |
| 517 | } |
| 518 | |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 519 | @Override |
Jeff Sharkey | 3e1189b | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 520 | public Cursor querySearchDocuments(String rootId, String query, String[] projection) |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 521 | throws FileNotFoundException { |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 522 | final File parent; |
| 523 | synchronized (mRootsLock) { |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 524 | parent = mRoots.get(rootId).path; |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 525 | } |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 526 | |
Garfield Tan | 75379db | 2017-02-08 15:32:56 -0800 | [diff] [blame] | 527 | return querySearchDocuments(parent, query, projection, Collections.emptySet()); |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | @Override |
Garfield Tan | 8787703 | 2017-03-22 12:01:14 -0700 | [diff] [blame] | 531 | public void ejectRoot(String rootId) { |
Ben Lin | e7822fb | 2016-06-24 15:21:08 -0700 | [diff] [blame] | 532 | final long token = Binder.clearCallingIdentity(); |
Ben Lin | e7822fb | 2016-06-24 15:21:08 -0700 | [diff] [blame] | 533 | RootInfo root = mRoots.get(rootId); |
| 534 | if (root != null) { |
| 535 | try { |
| 536 | mStorageManager.unmount(root.volumeId); |
Ben Lin | e7822fb | 2016-06-24 15:21:08 -0700 | [diff] [blame] | 537 | } catch (RuntimeException e) { |
Garfield Tan | 8787703 | 2017-03-22 12:01:14 -0700 | [diff] [blame] | 538 | throw new IllegalStateException(e); |
Ben Lin | e7822fb | 2016-06-24 15:21:08 -0700 | [diff] [blame] | 539 | } finally { |
| 540 | Binder.restoreCallingIdentity(token); |
| 541 | } |
| 542 | } |
Ben Lin | e7822fb | 2016-06-24 15:21:08 -0700 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | @Override |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 546 | public void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
| 547 | final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " ", 160); |
| 548 | synchronized (mRootsLock) { |
| 549 | for (int i = 0; i < mRoots.size(); i++) { |
| 550 | final RootInfo root = mRoots.valueAt(i); |
| 551 | pw.println("Root{" + root.rootId + "}:"); |
| 552 | pw.increaseIndent(); |
| 553 | pw.printPair("flags", DebugUtils.flagsToString(Root.class, "FLAG_", root.flags)); |
| 554 | pw.println(); |
| 555 | pw.printPair("title", root.title); |
| 556 | pw.printPair("docId", root.docId); |
| 557 | pw.println(); |
| 558 | pw.printPair("path", root.path); |
| 559 | pw.printPair("visiblePath", root.visiblePath); |
| 560 | pw.decreaseIndent(); |
| 561 | pw.println(); |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 566 | @Override |
| 567 | public Bundle call(String method, String arg, Bundle extras) { |
| 568 | Bundle bundle = super.call(method, arg, extras); |
| 569 | if (bundle == null && !TextUtils.isEmpty(method)) { |
| 570 | switch (method) { |
| 571 | case "getDocIdForFileCreateNewDir": { |
| 572 | getContext().enforceCallingPermission( |
| 573 | android.Manifest.permission.MANAGE_DOCUMENTS, null); |
| 574 | if (TextUtils.isEmpty(arg)) { |
| 575 | return null; |
| 576 | } |
| 577 | try { |
| 578 | final String docId = getDocIdForFileMaybeCreate(new File(arg), true); |
| 579 | bundle = new Bundle(); |
| 580 | bundle.putString("DOC_ID", docId); |
| 581 | } catch (FileNotFoundException e) { |
| 582 | Log.w(TAG, "file '" + arg + "' not found"); |
| 583 | return null; |
| 584 | } |
| 585 | break; |
| 586 | } |
Garfield Tan | 92b96ba | 2016-11-01 14:33:48 -0700 | [diff] [blame] | 587 | case "getDocumentId": { |
| 588 | final String path = arg; |
| 589 | final List<UriPermission> accessUriPermissions = |
| 590 | extras.getParcelableArrayList(AUTHORITY + ".extra.uriPermissions"); |
| 591 | |
| 592 | try { |
| 593 | final Bundle out = new Bundle(); |
| 594 | final Uri uri = getDocumentUri(path, accessUriPermissions); |
| 595 | out.putParcelable(DocumentsContract.EXTRA_URI, uri); |
| 596 | return out; |
| 597 | } catch (FileNotFoundException e) { |
| 598 | throw new IllegalStateException("File in " + path + " is not found.", e); |
| 599 | } |
| 600 | |
| 601 | } |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 602 | default: |
| 603 | Log.w(TAG, "unknown method passed to call(): " + method); |
| 604 | } |
| 605 | } |
| 606 | return bundle; |
| 607 | } |
Jeff Sharkey | 9e0036e | 2013-04-26 16:54:55 -0700 | [diff] [blame] | 608 | } |