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