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