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 | 3e166b2 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 20 | import static android.os.Environment.STANDARD_DIRECTORIES; |
Felipe Leme | 34a9d52 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 21 | import static android.os.storage.StorageVolume.EXTRA_DIRECTORY_NAME; |
| 22 | import static android.os.storage.StorageVolume.EXTRA_STORAGE_VOLUME; |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 23 | import static com.android.documentsui.LocalPreferences.getScopedAccessPermissionStatus; |
| 24 | import static com.android.documentsui.LocalPreferences.PERMISSION_ASK; |
| 25 | import static com.android.documentsui.LocalPreferences.PERMISSION_ASK_AGAIN; |
| 26 | import static com.android.documentsui.LocalPreferences.PERMISSION_NEVER_ASK; |
| 27 | import static com.android.documentsui.LocalPreferences.setScopedAccessPermissionStatus; |
| 28 | import static com.android.documentsui.Metrics.SCOPED_DIRECTORY_ACCESS_ALREADY_DENIED; |
Felipe Leme | 3e166b2 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 29 | import static com.android.documentsui.Metrics.SCOPED_DIRECTORY_ACCESS_ALREADY_GRANTED; |
| 30 | import static com.android.documentsui.Metrics.SCOPED_DIRECTORY_ACCESS_DENIED; |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 31 | import static com.android.documentsui.Metrics.SCOPED_DIRECTORY_ACCESS_DENIED_AND_PERSIST; |
Felipe Leme | 3e166b2 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 32 | import static com.android.documentsui.Metrics.SCOPED_DIRECTORY_ACCESS_ERROR; |
| 33 | import static com.android.documentsui.Metrics.SCOPED_DIRECTORY_ACCESS_GRANTED; |
| 34 | import static com.android.documentsui.Metrics.SCOPED_DIRECTORY_ACCESS_INVALID_ARGUMENTS; |
| 35 | import static com.android.documentsui.Metrics.SCOPED_DIRECTORY_ACCESS_INVALID_DIRECTORY; |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 36 | import static com.android.documentsui.Metrics.logInvalidScopedAccessRequest; |
| 37 | import static com.android.documentsui.Metrics.logValidScopedAccessRequest; |
| 38 | import static com.android.documentsui.Shared.DEBUG; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 39 | |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 40 | import android.annotation.SuppressLint; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 41 | import android.app.Activity; |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 42 | import android.app.ActivityManager; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 43 | import android.app.AlertDialog; |
| 44 | import android.app.Dialog; |
| 45 | import android.app.DialogFragment; |
| 46 | import android.app.FragmentManager; |
| 47 | import android.app.FragmentTransaction; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 48 | import android.content.ContentProviderClient; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 49 | import android.content.Context; |
| 50 | import android.content.DialogInterface; |
| 51 | import android.content.DialogInterface.OnClickListener; |
| 52 | import android.content.Intent; |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 53 | import android.content.UriPermission; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 54 | import android.content.pm.PackageManager; |
| 55 | import android.content.pm.PackageManager.NameNotFoundException; |
| 56 | import android.net.Uri; |
| 57 | import android.os.Bundle; |
Felipe Leme | 34a9d52 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 58 | import android.os.Parcelable; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 59 | import android.os.RemoteException; |
| 60 | import android.os.UserHandle; |
| 61 | import android.os.storage.StorageManager; |
Felipe Leme | 34a9d52 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 62 | import android.os.storage.StorageVolume; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 63 | import android.os.storage.VolumeInfo; |
| 64 | import android.provider.DocumentsContract; |
| 65 | import android.text.TextUtils; |
| 66 | import android.util.Log; |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 67 | import android.view.View; |
| 68 | import android.widget.CheckBox; |
| 69 | import android.widget.CompoundButton; |
| 70 | import android.widget.CompoundButton.OnCheckedChangeListener; |
| 71 | import android.widget.TextView; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 72 | |
Ben Kwa | e3aee18 | 2016-02-02 12:11:10 -0800 | [diff] [blame] | 73 | import java.io.File; |
| 74 | import java.io.IOException; |
| 75 | import java.util.List; |
| 76 | |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 77 | /** |
| 78 | * Activity responsible for handling {@link Intent#ACTION_OPEN_EXTERNAL_DOCUMENT}. |
| 79 | */ |
| 80 | public class OpenExternalDirectoryActivity extends Activity { |
Ben Kwa | e3aee18 | 2016-02-02 12:11:10 -0800 | [diff] [blame] | 81 | private static final String TAG = "OpenExternalDirectory"; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 82 | private static final String FM_TAG = "open_external_directory"; |
| 83 | private static final String EXTERNAL_STORAGE_AUTH = "com.android.externalstorage.documents"; |
| 84 | private static final String EXTRA_FILE = "com.android.documentsui.FILE"; |
| 85 | private static final String EXTRA_APP_LABEL = "com.android.documentsui.APP_LABEL"; |
| 86 | private static final String EXTRA_VOLUME_LABEL = "com.android.documentsui.VOLUME_LABEL"; |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 87 | private static final String EXTRA_VOLUME_UUID = "com.android.documentsui.VOLUME_UUID"; |
Felipe Leme | db892b8 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 88 | private static final String EXTRA_IS_ROOT = "com.android.documentsui.IS_ROOT"; |
| 89 | // Special directory name representing the full volume |
| 90 | static final String DIRECTORY_ROOT = "ROOT_DIRECTORY"; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 91 | |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 92 | private ContentProviderClient mExternalStorageClient; |
| 93 | |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 94 | @Override |
| 95 | public void onCreate(Bundle savedInstanceState) { |
| 96 | super.onCreate(savedInstanceState); |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 97 | if (savedInstanceState != null) { |
| 98 | if (DEBUG) Log.d(TAG, "activity.onCreateDialog(): reusing instance"); |
| 99 | return; |
| 100 | } |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 101 | |
| 102 | final Intent intent = getIntent(); |
Felipe Leme | 34a9d52 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 103 | if (intent == null) { |
| 104 | if (DEBUG) Log.d(TAG, "missing intent"); |
Felipe Leme | 3e166b2 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 105 | logInvalidScopedAccessRequest(this, SCOPED_DIRECTORY_ACCESS_INVALID_ARGUMENTS); |
Felipe Leme | 34a9d52 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 106 | setResult(RESULT_CANCELED); |
| 107 | finish(); |
| 108 | return; |
| 109 | } |
| 110 | final Parcelable storageVolume = intent.getParcelableExtra(EXTRA_STORAGE_VOLUME); |
| 111 | if (!(storageVolume instanceof StorageVolume)) { |
| 112 | if (DEBUG) |
| 113 | Log.d(TAG, "extra " + EXTRA_STORAGE_VOLUME + " is not a StorageVolume: " |
| 114 | + storageVolume); |
Felipe Leme | 3e166b2 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 115 | logInvalidScopedAccessRequest(this, SCOPED_DIRECTORY_ACCESS_INVALID_ARGUMENTS); |
Felipe Leme | 34a9d52 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 116 | setResult(RESULT_CANCELED); |
| 117 | finish(); |
| 118 | return; |
| 119 | } |
Felipe Leme | db892b8 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 120 | String directoryName = intent.getStringExtra(EXTRA_DIRECTORY_NAME ); |
Felipe Leme | 34a9d52 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 121 | if (directoryName == null) { |
Felipe Leme | db892b8 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 122 | directoryName = DIRECTORY_ROOT; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 123 | } |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 124 | final StorageVolume volume = (StorageVolume) storageVolume; |
| 125 | if (getScopedAccessPermissionStatus(getApplicationContext(), getCallingPackage(), |
| 126 | volume.getUuid(), directoryName) == PERMISSION_NEVER_ASK) { |
| 127 | logValidScopedAccessRequest(this, directoryName, |
| 128 | SCOPED_DIRECTORY_ACCESS_ALREADY_DENIED); |
| 129 | setResult(RESULT_CANCELED); |
| 130 | finish(); |
| 131 | return; |
| 132 | } |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 133 | |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 134 | final int userId = UserHandle.myUserId(); |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 135 | if (!showFragment(this, userId, volume, directoryName)) { |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 136 | setResult(RESULT_CANCELED); |
| 137 | finish(); |
| 138 | return; |
| 139 | } |
| 140 | } |
| 141 | |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 142 | @Override |
| 143 | public void onDestroy() { |
| 144 | super.onDestroy(); |
| 145 | if (mExternalStorageClient != null) { |
| 146 | mExternalStorageClient.close(); |
| 147 | } |
| 148 | } |
| 149 | |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 150 | /** |
Felipe Leme | 34a9d52 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 151 | * Validates the given path (volume + directory) and display the appropriate dialog asking the |
| 152 | * user to grant access to it. |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 153 | */ |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 154 | private static boolean showFragment(OpenExternalDirectoryActivity activity, int userId, |
| 155 | StorageVolume storageVolume, String directoryName) { |
Felipe Leme | 34a9d52 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 156 | if (DEBUG) |
| 157 | Log.d(TAG, "showFragment() for volume " + storageVolume.dump() + ", directory " |
| 158 | + directoryName + ", and user " + userId); |
Felipe Leme | db892b8 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 159 | final boolean isRoot = directoryName.equals(DIRECTORY_ROOT); |
| 160 | final File volumeRoot = storageVolume.getPathFile(); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 161 | File file; |
| 162 | try { |
Felipe Leme | db892b8 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 163 | file = isRoot ? volumeRoot : new File(volumeRoot, directoryName).getCanonicalFile(); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 164 | } catch (IOException e) { |
Felipe Leme | 34a9d52 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 165 | Log.e(TAG, "Could not get canonical file for volume " + storageVolume.dump() |
| 166 | + " and directory " + directoryName); |
Felipe Leme | 3e166b2 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 167 | logInvalidScopedAccessRequest(activity, SCOPED_DIRECTORY_ACCESS_ERROR); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 168 | return false; |
| 169 | } |
| 170 | final StorageManager sm = |
| 171 | (StorageManager) activity.getSystemService(Context.STORAGE_SERVICE); |
| 172 | |
Felipe Leme | db892b8 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 173 | final String root, directory; |
| 174 | if (isRoot) { |
| 175 | root = volumeRoot.getAbsolutePath(); |
| 176 | directory = "."; |
| 177 | } else { |
| 178 | root = file.getParent(); |
| 179 | directory = file.getName(); |
| 180 | // Verify directory is valid. |
| 181 | if (TextUtils.isEmpty(directory) || !isStandardDirectory(directory)) { |
| 182 | if (DEBUG) |
| 183 | Log.d(TAG, "Directory '" + directory + "' is not standard (full path: '" |
| 184 | + file.getAbsolutePath() + "')"); |
| 185 | logInvalidScopedAccessRequest(activity, SCOPED_DIRECTORY_ACCESS_INVALID_DIRECTORY); |
| 186 | return false; |
| 187 | } |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 190 | // Gets volume label and converted path. |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 191 | String volumeLabel = null; |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 192 | String volumeUuid = null; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 193 | final List<VolumeInfo> volumes = sm.getVolumes(); |
| 194 | if (DEBUG) Log.d(TAG, "Number of volumes: " + volumes.size()); |
Felipe Leme | db892b8 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 195 | File internalRoot = null; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 196 | for (VolumeInfo volume : volumes) { |
| 197 | if (isRightVolume(volume, root, userId)) { |
Felipe Leme | db892b8 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 198 | internalRoot = volume.getInternalPathForUser(userId); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 199 | // Must convert path before calling getDocIdForFileCreateNewDir() |
| 200 | if (DEBUG) Log.d(TAG, "Converting " + root + " to " + internalRoot); |
Felipe Leme | db892b8 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 201 | file = isRoot ? internalRoot : new File(internalRoot, directory); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 202 | volumeLabel = sm.getBestVolumeDescription(volume); |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 203 | volumeUuid = volume.getFsUuid(); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 204 | break; |
| 205 | } |
| 206 | } |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 207 | |
| 208 | // Checks if the user has granted the permission already. |
Felipe Leme | db892b8 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 209 | final Intent intent = getIntentForExistingPermission(activity, isRoot, internalRoot, file); |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 210 | if (intent != null) { |
Felipe Leme | 3e166b2 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 211 | logValidScopedAccessRequest(activity, directory, |
| 212 | SCOPED_DIRECTORY_ACCESS_ALREADY_GRANTED); |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 213 | activity.setResult(RESULT_OK, intent); |
| 214 | activity.finish(); |
| 215 | return true; |
| 216 | } |
| 217 | |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 218 | if (volumeLabel == null) { |
Felipe Leme | 34a9d52 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 219 | Log.e(TAG, "Could not get volume for " + file); |
Felipe Leme | 3e166b2 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 220 | logInvalidScopedAccessRequest(activity, SCOPED_DIRECTORY_ACCESS_ERROR); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 221 | return false; |
| 222 | } |
| 223 | |
| 224 | // Gets the package label. |
| 225 | final String appLabel = getAppLabel(activity); |
| 226 | if (appLabel == null) { |
Felipe Leme | 3e166b2 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 227 | // Error already logged. |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 228 | return false; |
| 229 | } |
| 230 | |
| 231 | // Sets args that will be retrieve on onCreate() |
| 232 | final Bundle args = new Bundle(); |
| 233 | args.putString(EXTRA_FILE, file.getAbsolutePath()); |
| 234 | args.putString(EXTRA_VOLUME_LABEL, volumeLabel); |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 235 | args.putString(EXTRA_VOLUME_UUID, volumeUuid); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 236 | args.putString(EXTRA_APP_LABEL, appLabel); |
Felipe Leme | db892b8 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 237 | args.putBoolean(EXTRA_IS_ROOT, isRoot); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 238 | |
| 239 | final FragmentManager fm = activity.getFragmentManager(); |
| 240 | final FragmentTransaction ft = fm.beginTransaction(); |
| 241 | final OpenExternalDirectoryDialogFragment fragment = |
| 242 | new OpenExternalDirectoryDialogFragment(); |
| 243 | fragment.setArguments(args); |
| 244 | ft.add(fragment, FM_TAG); |
| 245 | ft.commitAllowingStateLoss(); |
| 246 | |
| 247 | return true; |
| 248 | } |
| 249 | |
| 250 | private static String getAppLabel(Activity activity) { |
| 251 | final String packageName = activity.getCallingPackage(); |
| 252 | final PackageManager pm = activity.getPackageManager(); |
| 253 | try { |
| 254 | return pm.getApplicationLabel(pm.getApplicationInfo(packageName, 0)).toString(); |
| 255 | } catch (NameNotFoundException e) { |
Felipe Leme | 3e166b2 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 256 | logInvalidScopedAccessRequest(activity, SCOPED_DIRECTORY_ACCESS_ERROR); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 257 | Log.w(TAG, "Could not get label for package " + packageName); |
| 258 | return null; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | private static boolean isRightVolume(VolumeInfo volume, String root, int userId) { |
| 263 | final File userPath = volume.getPathForUser(userId); |
| 264 | final String path = userPath == null ? null : volume.getPathForUser(userId).getPath(); |
| 265 | final boolean isVisible = volume.isVisibleForWrite(userId); |
Felipe Leme | 34a9d52 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 266 | if (DEBUG) |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 267 | Log.d(TAG, "Volume: " + volume + " userId: " + userId + " root: " + root |
| 268 | + " volumePath: " + volume.getPath().getPath() |
| 269 | + " pathForUser: " + path |
| 270 | + " internalPathForUser: " + volume.getInternalPath() |
| 271 | + " isVisible: " + isVisible); |
Felipe Leme | 34a9d52 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 272 | |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 273 | return volume.isVisibleForWrite(userId) && root.equals(path); |
| 274 | } |
| 275 | |
Felipe Leme | 3e166b2 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 276 | private static Uri getGrantedUriPermission(Context context, ContentProviderClient provider, |
| 277 | File file) { |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 278 | // Calls ExternalStorageProvider to get the doc id for the file |
| 279 | final Bundle bundle; |
| 280 | try { |
| 281 | bundle = provider.call("getDocIdForFileCreateNewDir", file.getPath(), null); |
| 282 | } catch (RemoteException e) { |
| 283 | Log.e(TAG, "Did not get doc id from External Storage provider for " + file, e); |
Felipe Leme | 3e166b2 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 284 | logInvalidScopedAccessRequest(context, SCOPED_DIRECTORY_ACCESS_ERROR); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 285 | return null; |
| 286 | } |
| 287 | final String docId = bundle == null ? null : bundle.getString("DOC_ID"); |
| 288 | if (docId == null) { |
| 289 | Log.e(TAG, "Did not get doc id from External Storage provider for " + file); |
Felipe Leme | 3e166b2 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 290 | logInvalidScopedAccessRequest(context, SCOPED_DIRECTORY_ACCESS_ERROR); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 291 | return null; |
| 292 | } |
Ben Kwa | ffa829f | 2016-03-22 11:11:46 -0700 | [diff] [blame] | 293 | if (DEBUG) Log.d(TAG, "doc id for " + file + ": " + docId); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 294 | |
| 295 | final Uri uri = DocumentsContract.buildTreeDocumentUri(EXTERNAL_STORAGE_AUTH, docId); |
| 296 | if (uri == null) { |
| 297 | Log.e(TAG, "Could not get URI for doc id " + docId); |
| 298 | return null; |
| 299 | } |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 300 | if (DEBUG) Log.d(TAG, "URI for " + file + ": " + uri); |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 301 | return uri; |
| 302 | } |
| 303 | |
Felipe Leme | 3e166b2 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 304 | private static Intent createGrantedUriPermissionsIntent(Context context, |
| 305 | ContentProviderClient provider, File file) { |
| 306 | final Uri uri = getGrantedUriPermission(context, provider, file); |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 307 | return createGrantedUriPermissionsIntent(uri); |
| 308 | } |
| 309 | |
| 310 | private static Intent createGrantedUriPermissionsIntent(Uri uri) { |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 311 | final Intent intent = new Intent(); |
| 312 | intent.setData(uri); |
| 313 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 314 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
| 315 | | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION |
| 316 | | Intent.FLAG_GRANT_PREFIX_URI_PERMISSION); |
| 317 | return intent; |
| 318 | } |
| 319 | |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 320 | private static Intent getIntentForExistingPermission(OpenExternalDirectoryActivity activity, |
Felipe Leme | db892b8 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 321 | boolean isRoot, File root, File file) { |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 322 | final String packageName = activity.getCallingPackage(); |
Felipe Leme | db892b8 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 323 | final ContentProviderClient storageClient = activity.getExternalStorageClient(); |
| 324 | final Uri grantedUri = getGrantedUriPermission(activity, storageClient, file); |
| 325 | final Uri rootUri = root.equals(file) ? grantedUri |
| 326 | : getGrantedUriPermission(activity, storageClient, root); |
| 327 | |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 328 | if (DEBUG) |
Felipe Leme | db892b8 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 329 | Log.d(TAG, "checking if " + packageName + " already has permission for " + grantedUri |
| 330 | + " or its root (" + rootUri + ")"); |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 331 | final ActivityManager am = |
| 332 | (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE); |
| 333 | for (UriPermission uriPermission : am.getGrantedUriPermissions(packageName).getList()) { |
| 334 | final Uri uri = uriPermission.getUri(); |
Felipe Leme | db892b8 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 335 | if (uri == null) { |
| 336 | Log.w(TAG, "null URI for " + uriPermission); |
| 337 | continue; |
| 338 | } |
| 339 | if (uri.equals(grantedUri) || uri.equals(rootUri)) { |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 340 | if (DEBUG) Log.d(TAG, packageName + " already has permission: " + uriPermission); |
Felipe Leme | db892b8 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 341 | return createGrantedUriPermissionsIntent(grantedUri); |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | if (DEBUG) Log.d(TAG, packageName + " does not have permission for " + grantedUri); |
| 345 | return null; |
| 346 | } |
| 347 | |
Ben Kwa | e3aee18 | 2016-02-02 12:11:10 -0800 | [diff] [blame] | 348 | public static class OpenExternalDirectoryDialogFragment extends DialogFragment { |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 349 | |
| 350 | private File mFile; |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 351 | private String mVolumeUuid; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 352 | private String mVolumeLabel; |
| 353 | private String mAppLabel; |
Felipe Leme | db892b8 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 354 | private boolean mIsRoot; |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 355 | private CheckBox mDontAskAgain; |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 356 | private OpenExternalDirectoryActivity mActivity; |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 357 | private AlertDialog mDialog; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 358 | |
| 359 | @Override |
| 360 | public void onCreate(Bundle savedInstanceState) { |
| 361 | super.onCreate(savedInstanceState); |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 362 | setRetainInstance(true); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 363 | final Bundle args = getArguments(); |
| 364 | if (args != null) { |
| 365 | mFile = new File(args.getString(EXTRA_FILE)); |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 366 | mVolumeUuid = args.getString(EXTRA_VOLUME_UUID); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 367 | mVolumeLabel = args.getString(EXTRA_VOLUME_LABEL); |
| 368 | mAppLabel = args.getString(EXTRA_APP_LABEL); |
Felipe Leme | db892b8 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 369 | mIsRoot = args.getBoolean(EXTRA_IS_ROOT); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 370 | } |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 371 | mActivity = (OpenExternalDirectoryActivity) getActivity(); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | @Override |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 375 | public void onDestroyView() { |
| 376 | // Workaround for https://code.google.com/p/android/issues/detail?id=17423 |
| 377 | if (mDialog != null && getRetainInstance()) { |
| 378 | mDialog.setDismissMessage(null); |
| 379 | } |
| 380 | super.onDestroyView(); |
| 381 | } |
| 382 | |
| 383 | @Override |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 384 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 385 | if (mDialog != null) { |
| 386 | if (DEBUG) Log.d(TAG, "fragment.onCreateDialog(): reusing dialog"); |
| 387 | return mDialog; |
| 388 | } |
| 389 | if (mActivity != getActivity()) { |
| 390 | // Sanity check. |
| 391 | Log.wtf(TAG, "activity references don't match on onCreateDialog(): mActivity = " |
| 392 | + mActivity + " , getActivity() = " + getActivity()); |
| 393 | mActivity = (OpenExternalDirectoryActivity) getActivity(); |
| 394 | } |
Felipe Leme | 3e166b2 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 395 | final String directory = mFile.getName(); |
Felipe Leme | db892b8 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 396 | final String directoryName = mIsRoot ? DIRECTORY_ROOT : directory; |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 397 | final Context context = mActivity.getApplicationContext(); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 398 | final OnClickListener listener = new OnClickListener() { |
| 399 | |
| 400 | @Override |
| 401 | public void onClick(DialogInterface dialog, int which) { |
| 402 | Intent intent = null; |
| 403 | if (which == DialogInterface.BUTTON_POSITIVE) { |
Felipe Leme | 3e166b2 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 404 | intent = createGrantedUriPermissionsIntent(mActivity, |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 405 | mActivity.getExternalStorageClient(), mFile); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 406 | } |
| 407 | if (which == DialogInterface.BUTTON_NEGATIVE || intent == null) { |
Felipe Leme | db892b8 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 408 | logValidScopedAccessRequest(mActivity, directoryName, |
Felipe Leme | 3e166b2 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 409 | SCOPED_DIRECTORY_ACCESS_DENIED); |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 410 | final boolean checked = mDontAskAgain.isChecked(); |
| 411 | if (checked) { |
| 412 | logValidScopedAccessRequest(mActivity, directory, |
| 413 | SCOPED_DIRECTORY_ACCESS_DENIED_AND_PERSIST); |
| 414 | setScopedAccessPermissionStatus(context, mActivity.getCallingPackage(), |
Felipe Leme | db892b8 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 415 | mVolumeUuid, directoryName, PERMISSION_NEVER_ASK); |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 416 | } else { |
| 417 | setScopedAccessPermissionStatus(context, mActivity.getCallingPackage(), |
Felipe Leme | db892b8 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 418 | mVolumeUuid, directoryName, PERMISSION_ASK_AGAIN); |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 419 | } |
| 420 | mActivity.setResult(RESULT_CANCELED); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 421 | } else { |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 422 | logValidScopedAccessRequest(mActivity, directory, |
Felipe Leme | 3e166b2 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 423 | SCOPED_DIRECTORY_ACCESS_GRANTED); |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 424 | mActivity.setResult(RESULT_OK, intent); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 425 | } |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 426 | mActivity.finish(); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 427 | } |
| 428 | }; |
| 429 | |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 430 | @SuppressLint("InflateParams") |
| 431 | // It's ok pass null ViewRoot on AlertDialogs. |
| 432 | final View view = View.inflate(mActivity, R.layout.dialog_open_scoped_directory, null); |
Felipe Leme | db892b8 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 433 | final CharSequence message; |
| 434 | if (mIsRoot) { |
| 435 | message = TextUtils.expandTemplate(getText( |
| 436 | R.string.open_external_dialog_root_request), mAppLabel, mVolumeLabel); |
| 437 | } else { |
| 438 | message = TextUtils.expandTemplate(getText(R.string.open_external_dialog_request), |
| 439 | mAppLabel, directory, mVolumeLabel); |
| 440 | } |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 441 | final TextView messageField = (TextView) view.findViewById(R.id.message); |
| 442 | messageField.setText(message); |
Felipe Leme | 493611f | 2016-03-15 16:29:17 -0700 | [diff] [blame] | 443 | mDialog = new AlertDialog.Builder(mActivity, R.style.Theme_AppCompat_Light_Dialog_Alert) |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 444 | .setView(view) |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 445 | .setPositiveButton(R.string.allow, listener) |
| 446 | .setNegativeButton(R.string.deny, listener) |
| 447 | .create(); |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 448 | |
| 449 | mDontAskAgain = (CheckBox) view.findViewById(R.id.do_not_ask_checkbox); |
| 450 | if (getScopedAccessPermissionStatus(context, mActivity.getCallingPackage(), |
Felipe Leme | db892b8 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 451 | mVolumeUuid, directoryName) == PERMISSION_ASK_AGAIN) { |
Felipe Leme | adccb99 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 452 | mDontAskAgain.setVisibility(View.VISIBLE); |
| 453 | mDontAskAgain.setOnCheckedChangeListener(new OnCheckedChangeListener() { |
| 454 | |
| 455 | @Override |
| 456 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
| 457 | mDialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(!isChecked); |
| 458 | } |
| 459 | }); |
| 460 | } |
| 461 | |
| 462 | return mDialog; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | @Override |
| 466 | public void onCancel(DialogInterface dialog) { |
| 467 | super.onCancel(dialog); |
| 468 | final Activity activity = getActivity(); |
Felipe Leme | 3e166b2 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 469 | logValidScopedAccessRequest(activity, mFile.getName(), SCOPED_DIRECTORY_ACCESS_DENIED); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 470 | activity.setResult(RESULT_CANCELED); |
| 471 | activity.finish(); |
| 472 | } |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 473 | } |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 474 | |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 475 | private synchronized ContentProviderClient getExternalStorageClient() { |
| 476 | if (mExternalStorageClient == null) { |
| 477 | mExternalStorageClient = |
| 478 | getContentResolver().acquireContentProviderClient(EXTERNAL_STORAGE_AUTH); |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 479 | } |
Felipe Leme | 560d23a | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 480 | return mExternalStorageClient; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 481 | } |
| 482 | } |