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