Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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.documentsui; |
| 18 | |
| 19 | import static android.os.Environment.isStandardDirectory; |
Felipe Leme | 34a9d52 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 20 | import static android.os.storage.StorageVolume.EXTRA_DIRECTORY_NAME; |
| 21 | import static android.os.storage.StorageVolume.EXTRA_STORAGE_VOLUME; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 22 | import static com.android.documentsui.Shared.DEBUG; |
| 23 | |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 24 | import android.app.Activity; |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 25 | import android.app.ActivityManager; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 26 | import android.app.AlertDialog; |
| 27 | import android.app.Dialog; |
| 28 | import android.app.DialogFragment; |
| 29 | import android.app.FragmentManager; |
| 30 | import android.app.FragmentTransaction; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 31 | import android.content.ContentProviderClient; |
| 32 | import android.content.ContentResolver; |
| 33 | import android.content.Context; |
| 34 | import android.content.DialogInterface; |
| 35 | import android.content.DialogInterface.OnClickListener; |
| 36 | import android.content.Intent; |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 37 | import android.content.UriPermission; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 38 | import android.content.pm.PackageManager; |
| 39 | import android.content.pm.PackageManager.NameNotFoundException; |
| 40 | import android.net.Uri; |
| 41 | import android.os.Bundle; |
Felipe Leme | 34a9d52 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 42 | import android.os.Parcelable; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 43 | import android.os.RemoteException; |
| 44 | import android.os.UserHandle; |
| 45 | import android.os.storage.StorageManager; |
Felipe Leme | 34a9d52 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 46 | import android.os.storage.StorageVolume; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 47 | import android.os.storage.VolumeInfo; |
| 48 | import android.provider.DocumentsContract; |
| 49 | import android.text.TextUtils; |
| 50 | import android.util.Log; |
| 51 | |
Ben Kwa | e3aee18 | 2016-02-02 12:11:10 -0800 | [diff] [blame] | 52 | import java.io.File; |
| 53 | import java.io.IOException; |
| 54 | import java.util.List; |
| 55 | |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 56 | /** |
| 57 | * Activity responsible for handling {@link Intent#ACTION_OPEN_EXTERNAL_DOCUMENT}. |
| 58 | */ |
| 59 | public class OpenExternalDirectoryActivity extends Activity { |
Ben Kwa | e3aee18 | 2016-02-02 12:11:10 -0800 | [diff] [blame] | 60 | private static final String TAG = "OpenExternalDirectory"; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 61 | private static final String FM_TAG = "open_external_directory"; |
| 62 | private static final String EXTERNAL_STORAGE_AUTH = "com.android.externalstorage.documents"; |
| 63 | private static final String EXTRA_FILE = "com.android.documentsui.FILE"; |
| 64 | private static final String EXTRA_APP_LABEL = "com.android.documentsui.APP_LABEL"; |
| 65 | private static final String EXTRA_VOLUME_LABEL = "com.android.documentsui.VOLUME_LABEL"; |
| 66 | |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 67 | private ContentProviderClient mExternalStorageClient; |
| 68 | |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 69 | @Override |
| 70 | public void onCreate(Bundle savedInstanceState) { |
| 71 | super.onCreate(savedInstanceState); |
| 72 | |
| 73 | final Intent intent = getIntent(); |
Felipe Leme | 34a9d52 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 74 | if (intent == null) { |
| 75 | if (DEBUG) Log.d(TAG, "missing intent"); |
| 76 | setResult(RESULT_CANCELED); |
| 77 | finish(); |
| 78 | return; |
| 79 | } |
| 80 | final Parcelable storageVolume = intent.getParcelableExtra(EXTRA_STORAGE_VOLUME); |
| 81 | if (!(storageVolume instanceof StorageVolume)) { |
| 82 | if (DEBUG) |
| 83 | Log.d(TAG, "extra " + EXTRA_STORAGE_VOLUME + " is not a StorageVolume: " |
| 84 | + storageVolume); |
| 85 | setResult(RESULT_CANCELED); |
| 86 | finish(); |
| 87 | return; |
| 88 | } |
| 89 | final String directoryName = intent.getStringExtra(EXTRA_DIRECTORY_NAME); |
| 90 | if (directoryName == null) { |
| 91 | if (DEBUG) Log.d(TAG, "missing extra " + EXTRA_DIRECTORY_NAME + " on " + intent); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 92 | setResult(RESULT_CANCELED); |
| 93 | finish(); |
| 94 | return; |
| 95 | } |
| 96 | |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 97 | final int userId = UserHandle.myUserId(); |
Felipe Leme | 34a9d52 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 98 | if (!showFragment(this, userId, (StorageVolume) storageVolume, directoryName)) { |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 99 | setResult(RESULT_CANCELED); |
| 100 | finish(); |
| 101 | return; |
| 102 | } |
| 103 | } |
| 104 | |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 105 | @Override |
| 106 | public void onDestroy() { |
| 107 | super.onDestroy(); |
| 108 | if (mExternalStorageClient != null) { |
| 109 | mExternalStorageClient.close(); |
| 110 | } |
| 111 | } |
| 112 | |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 113 | /** |
Felipe Leme | 34a9d52 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 114 | * Validates the given path (volume + directory) and display the appropriate dialog asking the |
| 115 | * user to grant access to it. |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 116 | */ |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 117 | private static boolean showFragment(OpenExternalDirectoryActivity activity, int userId, |
| 118 | StorageVolume storageVolume, String directoryName) { |
Felipe Leme | 34a9d52 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 119 | if (DEBUG) |
| 120 | Log.d(TAG, "showFragment() for volume " + storageVolume.dump() + ", directory " |
| 121 | + directoryName + ", and user " + userId); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 122 | File file; |
| 123 | try { |
Felipe Leme | 34a9d52 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 124 | file = new File(storageVolume.getPathFile(), directoryName).getCanonicalFile(); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 125 | } catch (IOException e) { |
Felipe Leme | 34a9d52 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 126 | Log.e(TAG, "Could not get canonical file for volume " + storageVolume.dump() |
| 127 | + " and directory " + directoryName); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 128 | return false; |
| 129 | } |
| 130 | final StorageManager sm = |
| 131 | (StorageManager) activity.getSystemService(Context.STORAGE_SERVICE); |
| 132 | |
| 133 | final String root = file.getParent(); |
| 134 | final String directory = file.getName(); |
| 135 | |
| 136 | // Verify directory is valid. |
| 137 | if (TextUtils.isEmpty(directory) || !isStandardDirectory(directory)) { |
Felipe Leme | 34a9d52 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 138 | if (DEBUG) |
| 139 | Log.d(TAG, "Directory '" + directory + "' is not standard (full path: '" |
| 140 | + file.getAbsolutePath() + "')"); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 141 | return false; |
| 142 | } |
| 143 | |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 144 | // Gets volume label and converted path. |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 145 | String volumeLabel = null; |
| 146 | final List<VolumeInfo> volumes = sm.getVolumes(); |
| 147 | if (DEBUG) Log.d(TAG, "Number of volumes: " + volumes.size()); |
| 148 | for (VolumeInfo volume : volumes) { |
| 149 | if (isRightVolume(volume, root, userId)) { |
| 150 | final File internalRoot = volume.getInternalPathForUser(userId); |
| 151 | // Must convert path before calling getDocIdForFileCreateNewDir() |
| 152 | if (DEBUG) Log.d(TAG, "Converting " + root + " to " + internalRoot); |
| 153 | file = new File(internalRoot, directory); |
| 154 | volumeLabel = sm.getBestVolumeDescription(volume); |
| 155 | break; |
| 156 | } |
| 157 | } |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 158 | |
| 159 | // Checks if the user has granted the permission already. |
| 160 | final Intent intent = getIntentForExistingPermission(activity, file); |
| 161 | if (intent != null) { |
| 162 | activity.setResult(RESULT_OK, intent); |
| 163 | activity.finish(); |
| 164 | return true; |
| 165 | } |
| 166 | |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 167 | if (volumeLabel == null) { |
Felipe Leme | 34a9d52 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 168 | Log.e(TAG, "Could not get volume for " + file); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 169 | return false; |
| 170 | } |
| 171 | |
| 172 | // Gets the package label. |
| 173 | final String appLabel = getAppLabel(activity); |
| 174 | if (appLabel == null) { |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | // Sets args that will be retrieve on onCreate() |
| 179 | final Bundle args = new Bundle(); |
| 180 | args.putString(EXTRA_FILE, file.getAbsolutePath()); |
| 181 | args.putString(EXTRA_VOLUME_LABEL, volumeLabel); |
| 182 | args.putString(EXTRA_APP_LABEL, appLabel); |
| 183 | |
| 184 | final FragmentManager fm = activity.getFragmentManager(); |
| 185 | final FragmentTransaction ft = fm.beginTransaction(); |
| 186 | final OpenExternalDirectoryDialogFragment fragment = |
| 187 | new OpenExternalDirectoryDialogFragment(); |
| 188 | fragment.setArguments(args); |
| 189 | ft.add(fragment, FM_TAG); |
| 190 | ft.commitAllowingStateLoss(); |
| 191 | |
| 192 | return true; |
| 193 | } |
| 194 | |
| 195 | private static String getAppLabel(Activity activity) { |
| 196 | final String packageName = activity.getCallingPackage(); |
| 197 | final PackageManager pm = activity.getPackageManager(); |
| 198 | try { |
| 199 | return pm.getApplicationLabel(pm.getApplicationInfo(packageName, 0)).toString(); |
| 200 | } catch (NameNotFoundException e) { |
| 201 | Log.w(TAG, "Could not get label for package " + packageName); |
| 202 | return null; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | private static boolean isRightVolume(VolumeInfo volume, String root, int userId) { |
| 207 | final File userPath = volume.getPathForUser(userId); |
| 208 | final String path = userPath == null ? null : volume.getPathForUser(userId).getPath(); |
| 209 | final boolean isVisible = volume.isVisibleForWrite(userId); |
Felipe Leme | 34a9d52 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 210 | if (DEBUG) |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 211 | Log.d(TAG, "Volume: " + volume + " userId: " + userId + " root: " + root |
| 212 | + " volumePath: " + volume.getPath().getPath() |
| 213 | + " pathForUser: " + path |
| 214 | + " internalPathForUser: " + volume.getInternalPath() |
| 215 | + " isVisible: " + isVisible); |
Felipe Leme | 34a9d52 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 216 | |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 217 | return volume.isVisibleForWrite(userId) && root.equals(path); |
| 218 | } |
| 219 | |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 220 | private static Uri getGrantedUriPermission(ContentProviderClient provider, File file) { |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 221 | // Calls ExternalStorageProvider to get the doc id for the file |
| 222 | final Bundle bundle; |
| 223 | try { |
| 224 | bundle = provider.call("getDocIdForFileCreateNewDir", file.getPath(), null); |
| 225 | } catch (RemoteException e) { |
| 226 | Log.e(TAG, "Did not get doc id from External Storage provider for " + file, e); |
| 227 | return null; |
| 228 | } |
| 229 | final String docId = bundle == null ? null : bundle.getString("DOC_ID"); |
| 230 | if (docId == null) { |
| 231 | Log.e(TAG, "Did not get doc id from External Storage provider for " + file); |
| 232 | return null; |
| 233 | } |
| 234 | Log.d(TAG, "doc id for " + file + ": " + docId); |
| 235 | |
| 236 | final Uri uri = DocumentsContract.buildTreeDocumentUri(EXTERNAL_STORAGE_AUTH, docId); |
| 237 | if (uri == null) { |
| 238 | Log.e(TAG, "Could not get URI for doc id " + docId); |
| 239 | return null; |
| 240 | } |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 241 | if (DEBUG) Log.d(TAG, "URI for " + file + ": " + uri); |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 242 | return uri; |
| 243 | } |
| 244 | |
| 245 | private static Intent createGrantedUriPermissionsIntent(ContentProviderClient provider, |
| 246 | File file) { |
| 247 | final Uri uri = getGrantedUriPermission(provider, file); |
| 248 | return createGrantedUriPermissionsIntent(uri); |
| 249 | } |
| 250 | |
| 251 | private static Intent createGrantedUriPermissionsIntent(Uri uri) { |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 252 | final Intent intent = new Intent(); |
| 253 | intent.setData(uri); |
| 254 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 255 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
| 256 | | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION |
| 257 | | Intent.FLAG_GRANT_PREFIX_URI_PERMISSION); |
| 258 | return intent; |
| 259 | } |
| 260 | |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 261 | private static Intent getIntentForExistingPermission(OpenExternalDirectoryActivity activity, |
| 262 | File file) { |
| 263 | final String packageName = activity.getCallingPackage(); |
| 264 | final Uri grantedUri = getGrantedUriPermission(activity.getExternalStorageClient(), file); |
| 265 | if (DEBUG) |
| 266 | Log.d(TAG, "checking if " + packageName + " already has permission for " + grantedUri); |
| 267 | final ActivityManager am = |
| 268 | (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE); |
| 269 | for (UriPermission uriPermission : am.getGrantedUriPermissions(packageName).getList()) { |
| 270 | final Uri uri = uriPermission.getUri(); |
| 271 | if (uri.equals(grantedUri)) { |
| 272 | if (DEBUG) Log.d(TAG, packageName + " already has permission: " + uriPermission); |
| 273 | return createGrantedUriPermissionsIntent(uri); |
| 274 | } |
| 275 | } |
| 276 | if (DEBUG) Log.d(TAG, packageName + " does not have permission for " + grantedUri); |
| 277 | return null; |
| 278 | } |
| 279 | |
Ben Kwa | e3aee18 | 2016-02-02 12:11:10 -0800 | [diff] [blame] | 280 | public static class OpenExternalDirectoryDialogFragment extends DialogFragment { |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 281 | |
| 282 | private File mFile; |
| 283 | private String mVolumeLabel; |
| 284 | private String mAppLabel; |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 285 | private OpenExternalDirectoryActivity mActivity; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 286 | |
| 287 | @Override |
| 288 | public void onCreate(Bundle savedInstanceState) { |
| 289 | super.onCreate(savedInstanceState); |
| 290 | final Bundle args = getArguments(); |
| 291 | if (args != null) { |
| 292 | mFile = new File(args.getString(EXTRA_FILE)); |
| 293 | mVolumeLabel = args.getString(EXTRA_VOLUME_LABEL); |
| 294 | mAppLabel = args.getString(EXTRA_APP_LABEL); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 295 | } |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 296 | mActivity = (OpenExternalDirectoryActivity) getActivity(); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | @Override |
| 300 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 301 | final String folder = mFile.getName(); |
| 302 | final Activity activity = getActivity(); |
| 303 | final OnClickListener listener = new OnClickListener() { |
| 304 | |
| 305 | @Override |
| 306 | public void onClick(DialogInterface dialog, int which) { |
| 307 | Intent intent = null; |
| 308 | if (which == DialogInterface.BUTTON_POSITIVE) { |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 309 | intent = createGrantedUriPermissionsIntent( |
| 310 | mActivity.getExternalStorageClient(), mFile); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 311 | } |
| 312 | if (which == DialogInterface.BUTTON_NEGATIVE || intent == null) { |
| 313 | activity.setResult(RESULT_CANCELED); |
| 314 | } else { |
| 315 | activity.setResult(RESULT_OK, intent); |
| 316 | } |
| 317 | activity.finish(); |
| 318 | } |
| 319 | }; |
| 320 | |
| 321 | final CharSequence message = TextUtils |
| 322 | .expandTemplate( |
| 323 | getText(R.string.open_external_dialog_request), mAppLabel, folder, |
| 324 | mVolumeLabel); |
| 325 | return new AlertDialog.Builder(activity, R.style.AlertDialogTheme) |
| 326 | .setMessage(message) |
| 327 | .setPositiveButton(R.string.allow, listener) |
| 328 | .setNegativeButton(R.string.deny, listener) |
| 329 | .create(); |
| 330 | } |
| 331 | |
| 332 | @Override |
| 333 | public void onCancel(DialogInterface dialog) { |
| 334 | super.onCancel(dialog); |
| 335 | final Activity activity = getActivity(); |
| 336 | activity.setResult(RESULT_CANCELED); |
| 337 | activity.finish(); |
| 338 | } |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 339 | } |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 340 | |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 341 | private synchronized ContentProviderClient getExternalStorageClient() { |
| 342 | if (mExternalStorageClient == null) { |
| 343 | mExternalStorageClient = |
| 344 | getContentResolver().acquireContentProviderClient(EXTERNAL_STORAGE_AUTH); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 345 | } |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 346 | return mExternalStorageClient; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 347 | } |
| 348 | } |