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 | e4b6012 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 20 | import static android.os.Environment.STANDARD_DIRECTORIES; |
Felipe Leme | 5228bd0 | 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 | 0bdb7c3 | 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 | e4b6012 | 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 | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 31 | import static com.android.documentsui.Metrics.SCOPED_DIRECTORY_ACCESS_DENIED_AND_PERSIST; |
Felipe Leme | e4b6012 | 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 | 0bdb7c3 | 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 | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 39 | |
Felipe Leme | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 40 | import android.annotation.SuppressLint; |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 41 | import android.app.Activity; |
Felipe Leme | 8cf6ce2 | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 42 | import android.app.ActivityManager; |
Felipe Leme | a4f4d7e | 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 | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 48 | import android.content.ContentProviderClient; |
Felipe Leme | a4f4d7e | 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 | 8cf6ce2 | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 53 | import android.content.UriPermission; |
Felipe Leme | a4f4d7e | 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 | 5228bd0 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 58 | import android.os.Parcelable; |
Felipe Leme | a4f4d7e | 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 | 5228bd0 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 62 | import android.os.storage.StorageVolume; |
Felipe Leme | a4f4d7e | 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 | 0bdb7c3 | 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 | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 72 | |
Ben Kwa | f4b0ff6 | 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 | a4f4d7e | 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 | f4b0ff6 | 2016-02-02 12:11:10 -0800 | [diff] [blame] | 81 | private static final String TAG = "OpenExternalDirectory"; |
Felipe Leme | a4f4d7e | 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 | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 87 | private static final String EXTRA_VOLUME_UUID = "com.android.documentsui.VOLUME_UUID"; |
Felipe Leme | b59a8a6 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 88 | private static final String EXTRA_IS_ROOT = "com.android.documentsui.IS_ROOT"; |
Felipe Leme | 8216cea | 2016-03-29 19:06:02 -0700 | [diff] [blame] | 89 | private static final String EXTRA_IS_PRIMARY = "com.android.documentsui.IS_PRIMARY"; |
Felipe Leme | b59a8a6 | 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 | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 92 | |
Felipe Leme | 8cf6ce2 | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 93 | private ContentProviderClient mExternalStorageClient; |
| 94 | |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 95 | @Override |
| 96 | public void onCreate(Bundle savedInstanceState) { |
| 97 | super.onCreate(savedInstanceState); |
Felipe Leme | 0bdb7c3 | 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 | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 102 | |
| 103 | final Intent intent = getIntent(); |
Felipe Leme | 5228bd0 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 104 | if (intent == null) { |
| 105 | if (DEBUG) Log.d(TAG, "missing intent"); |
Felipe Leme | e4b6012 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 106 | logInvalidScopedAccessRequest(this, SCOPED_DIRECTORY_ACCESS_INVALID_ARGUMENTS); |
Felipe Leme | 5228bd0 | 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 | e4b6012 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 116 | logInvalidScopedAccessRequest(this, SCOPED_DIRECTORY_ACCESS_INVALID_ARGUMENTS); |
Felipe Leme | 5228bd0 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 117 | setResult(RESULT_CANCELED); |
| 118 | finish(); |
| 119 | return; |
| 120 | } |
Felipe Leme | b59a8a6 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 121 | String directoryName = intent.getStringExtra(EXTRA_DIRECTORY_NAME ); |
Felipe Leme | 5228bd0 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 122 | if (directoryName == null) { |
Felipe Leme | b59a8a6 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 123 | directoryName = DIRECTORY_ROOT; |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 124 | } |
Felipe Leme | 0bdb7c3 | 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 | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 134 | |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 135 | final int userId = UserHandle.myUserId(); |
Felipe Leme | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 136 | if (!showFragment(this, userId, volume, directoryName)) { |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 137 | setResult(RESULT_CANCELED); |
| 138 | finish(); |
| 139 | return; |
| 140 | } |
| 141 | } |
| 142 | |
Felipe Leme | 8cf6ce2 | 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 | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 151 | /** |
Felipe Leme | 5228bd0 | 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 | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 154 | */ |
Felipe Leme | 8cf6ce2 | 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 | 5228bd0 | 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 | b59a8a6 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 160 | final boolean isRoot = directoryName.equals(DIRECTORY_ROOT); |
Felipe Leme | 8216cea | 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 | b59a8a6 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 168 | final File volumeRoot = storageVolume.getPathFile(); |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 169 | File file; |
| 170 | try { |
Felipe Leme | b59a8a6 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 171 | file = isRoot ? volumeRoot : new File(volumeRoot, directoryName).getCanonicalFile(); |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 172 | } catch (IOException e) { |
Felipe Leme | 5228bd0 | 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 | e4b6012 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 175 | logInvalidScopedAccessRequest(activity, SCOPED_DIRECTORY_ACCESS_ERROR); |
Felipe Leme | a4f4d7e | 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 | b59a8a6 | 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 | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Felipe Leme | 8cf6ce2 | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 198 | // Gets volume label and converted path. |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 199 | String volumeLabel = null; |
Felipe Leme | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 200 | String volumeUuid = null; |
Felipe Leme | a4f4d7e | 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 | b59a8a6 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 203 | File internalRoot = null; |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 204 | for (VolumeInfo volume : volumes) { |
| 205 | if (isRightVolume(volume, root, userId)) { |
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 | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 210 | volumeLabel = sm.getBestVolumeDescription(volume); |
Felipe Leme | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 211 | volumeUuid = volume.getFsUuid(); |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 212 | break; |
| 213 | } |
| 214 | } |
Felipe Leme | e71de81 | 2016-04-18 14:55:15 -0700 | [diff] [blame] | 215 | if (internalRoot == null) { |
| 216 | // Should not happen on normal circumstances, unless app crafted an invalid volume |
| 217 | // using reflection or the list of mounted volumes changed. |
| 218 | Log.e(TAG, "Didn't find right volume for '" + storageVolume.dump() + "' on " + volumes); |
| 219 | return false; |
| 220 | } |
Felipe Leme | 8cf6ce2 | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 221 | |
| 222 | // Checks if the user has granted the permission already. |
Felipe Leme | b59a8a6 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 223 | final Intent intent = getIntentForExistingPermission(activity, isRoot, internalRoot, file); |
Felipe Leme | 8cf6ce2 | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 224 | if (intent != null) { |
Felipe Leme | e4b6012 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 225 | logValidScopedAccessRequest(activity, directory, |
| 226 | SCOPED_DIRECTORY_ACCESS_ALREADY_GRANTED); |
Felipe Leme | 8cf6ce2 | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 227 | activity.setResult(RESULT_OK, intent); |
| 228 | activity.finish(); |
| 229 | return true; |
| 230 | } |
| 231 | |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 232 | if (volumeLabel == null) { |
Felipe Leme | 5228bd0 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 233 | Log.e(TAG, "Could not get volume for " + file); |
Felipe Leme | e4b6012 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 234 | logInvalidScopedAccessRequest(activity, SCOPED_DIRECTORY_ACCESS_ERROR); |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 235 | return false; |
| 236 | } |
| 237 | |
| 238 | // Gets the package label. |
| 239 | final String appLabel = getAppLabel(activity); |
| 240 | if (appLabel == null) { |
Felipe Leme | e4b6012 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 241 | // Error already logged. |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 242 | return false; |
| 243 | } |
| 244 | |
| 245 | // Sets args that will be retrieve on onCreate() |
| 246 | final Bundle args = new Bundle(); |
| 247 | args.putString(EXTRA_FILE, file.getAbsolutePath()); |
| 248 | args.putString(EXTRA_VOLUME_LABEL, volumeLabel); |
Felipe Leme | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 249 | args.putString(EXTRA_VOLUME_UUID, volumeUuid); |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 250 | args.putString(EXTRA_APP_LABEL, appLabel); |
Felipe Leme | b59a8a6 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 251 | args.putBoolean(EXTRA_IS_ROOT, isRoot); |
Felipe Leme | 8216cea | 2016-03-29 19:06:02 -0700 | [diff] [blame] | 252 | args.putBoolean(EXTRA_IS_PRIMARY, isPrimary); |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 253 | |
| 254 | final FragmentManager fm = activity.getFragmentManager(); |
| 255 | final FragmentTransaction ft = fm.beginTransaction(); |
| 256 | final OpenExternalDirectoryDialogFragment fragment = |
| 257 | new OpenExternalDirectoryDialogFragment(); |
| 258 | fragment.setArguments(args); |
| 259 | ft.add(fragment, FM_TAG); |
| 260 | ft.commitAllowingStateLoss(); |
| 261 | |
| 262 | return true; |
| 263 | } |
| 264 | |
| 265 | private static String getAppLabel(Activity activity) { |
| 266 | final String packageName = activity.getCallingPackage(); |
| 267 | final PackageManager pm = activity.getPackageManager(); |
| 268 | try { |
| 269 | return pm.getApplicationLabel(pm.getApplicationInfo(packageName, 0)).toString(); |
| 270 | } catch (NameNotFoundException e) { |
Felipe Leme | e4b6012 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 271 | logInvalidScopedAccessRequest(activity, SCOPED_DIRECTORY_ACCESS_ERROR); |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 272 | Log.w(TAG, "Could not get label for package " + packageName); |
| 273 | return null; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | private static boolean isRightVolume(VolumeInfo volume, String root, int userId) { |
| 278 | final File userPath = volume.getPathForUser(userId); |
| 279 | final String path = userPath == null ? null : volume.getPathForUser(userId).getPath(); |
| 280 | final boolean isVisible = volume.isVisibleForWrite(userId); |
Felipe Leme | 5228bd0 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 281 | if (DEBUG) |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 282 | Log.d(TAG, "Volume: " + volume + " userId: " + userId + " root: " + root |
| 283 | + " volumePath: " + volume.getPath().getPath() |
| 284 | + " pathForUser: " + path |
| 285 | + " internalPathForUser: " + volume.getInternalPath() |
| 286 | + " isVisible: " + isVisible); |
Felipe Leme | 5228bd0 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 287 | |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 288 | return volume.isVisibleForWrite(userId) && root.equals(path); |
| 289 | } |
| 290 | |
Felipe Leme | e4b6012 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 291 | private static Uri getGrantedUriPermission(Context context, ContentProviderClient provider, |
| 292 | File file) { |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 293 | // Calls ExternalStorageProvider to get the doc id for the file |
| 294 | final Bundle bundle; |
| 295 | try { |
| 296 | bundle = provider.call("getDocIdForFileCreateNewDir", file.getPath(), null); |
| 297 | } catch (RemoteException e) { |
| 298 | 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] | 299 | logInvalidScopedAccessRequest(context, SCOPED_DIRECTORY_ACCESS_ERROR); |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 300 | return null; |
| 301 | } |
| 302 | final String docId = bundle == null ? null : bundle.getString("DOC_ID"); |
| 303 | if (docId == null) { |
| 304 | 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] | 305 | logInvalidScopedAccessRequest(context, SCOPED_DIRECTORY_ACCESS_ERROR); |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 306 | return null; |
| 307 | } |
Ben Kwa | 543a292 | 2016-03-22 11:11:46 -0700 | [diff] [blame] | 308 | if (DEBUG) Log.d(TAG, "doc id for " + file + ": " + docId); |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 309 | |
| 310 | final Uri uri = DocumentsContract.buildTreeDocumentUri(EXTERNAL_STORAGE_AUTH, docId); |
| 311 | if (uri == null) { |
| 312 | Log.e(TAG, "Could not get URI for doc id " + docId); |
| 313 | return null; |
| 314 | } |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 315 | if (DEBUG) Log.d(TAG, "URI for " + file + ": " + uri); |
Felipe Leme | 8cf6ce2 | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 316 | return uri; |
| 317 | } |
| 318 | |
Felipe Leme | e4b6012 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 319 | private static Intent createGrantedUriPermissionsIntent(Context context, |
| 320 | ContentProviderClient provider, File file) { |
| 321 | final Uri uri = getGrantedUriPermission(context, provider, file); |
Felipe Leme | 8cf6ce2 | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 322 | return createGrantedUriPermissionsIntent(uri); |
| 323 | } |
| 324 | |
| 325 | private static Intent createGrantedUriPermissionsIntent(Uri uri) { |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 326 | final Intent intent = new Intent(); |
| 327 | intent.setData(uri); |
| 328 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 329 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
| 330 | | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION |
| 331 | | Intent.FLAG_GRANT_PREFIX_URI_PERMISSION); |
| 332 | return intent; |
| 333 | } |
| 334 | |
Felipe Leme | 8cf6ce2 | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 335 | private static Intent getIntentForExistingPermission(OpenExternalDirectoryActivity activity, |
Felipe Leme | b59a8a6 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 336 | boolean isRoot, File root, File file) { |
Felipe Leme | 8cf6ce2 | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 337 | final String packageName = activity.getCallingPackage(); |
Felipe Leme | b59a8a6 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 338 | final ContentProviderClient storageClient = activity.getExternalStorageClient(); |
| 339 | final Uri grantedUri = getGrantedUriPermission(activity, storageClient, file); |
| 340 | final Uri rootUri = root.equals(file) ? grantedUri |
| 341 | : getGrantedUriPermission(activity, storageClient, root); |
| 342 | |
Felipe Leme | 8cf6ce2 | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 343 | if (DEBUG) |
Felipe Leme | b59a8a6 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 344 | Log.d(TAG, "checking if " + packageName + " already has permission for " + grantedUri |
| 345 | + " or its root (" + rootUri + ")"); |
Felipe Leme | 8cf6ce2 | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 346 | final ActivityManager am = |
| 347 | (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE); |
| 348 | for (UriPermission uriPermission : am.getGrantedUriPermissions(packageName).getList()) { |
| 349 | final Uri uri = uriPermission.getUri(); |
Felipe Leme | b59a8a6 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 350 | if (uri == null) { |
| 351 | Log.w(TAG, "null URI for " + uriPermission); |
| 352 | continue; |
| 353 | } |
| 354 | if (uri.equals(grantedUri) || uri.equals(rootUri)) { |
Felipe Leme | 8cf6ce2 | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 355 | if (DEBUG) Log.d(TAG, packageName + " already has permission: " + uriPermission); |
Felipe Leme | b59a8a6 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 356 | return createGrantedUriPermissionsIntent(grantedUri); |
Felipe Leme | 8cf6ce2 | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | if (DEBUG) Log.d(TAG, packageName + " does not have permission for " + grantedUri); |
| 360 | return null; |
| 361 | } |
| 362 | |
Ben Kwa | f4b0ff6 | 2016-02-02 12:11:10 -0800 | [diff] [blame] | 363 | public static class OpenExternalDirectoryDialogFragment extends DialogFragment { |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 364 | |
| 365 | private File mFile; |
Felipe Leme | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 366 | private String mVolumeUuid; |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 367 | private String mVolumeLabel; |
| 368 | private String mAppLabel; |
Felipe Leme | b59a8a6 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 369 | private boolean mIsRoot; |
Felipe Leme | 8216cea | 2016-03-29 19:06:02 -0700 | [diff] [blame] | 370 | private boolean mIsPrimary; |
Felipe Leme | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 371 | private CheckBox mDontAskAgain; |
Felipe Leme | 8cf6ce2 | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 372 | private OpenExternalDirectoryActivity mActivity; |
Felipe Leme | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 373 | private AlertDialog mDialog; |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 374 | |
| 375 | @Override |
| 376 | public void onCreate(Bundle savedInstanceState) { |
| 377 | super.onCreate(savedInstanceState); |
Felipe Leme | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 378 | setRetainInstance(true); |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 379 | final Bundle args = getArguments(); |
| 380 | if (args != null) { |
| 381 | mFile = new File(args.getString(EXTRA_FILE)); |
Felipe Leme | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 382 | mVolumeUuid = args.getString(EXTRA_VOLUME_UUID); |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 383 | mVolumeLabel = args.getString(EXTRA_VOLUME_LABEL); |
| 384 | mAppLabel = args.getString(EXTRA_APP_LABEL); |
Felipe Leme | b59a8a6 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 385 | mIsRoot = args.getBoolean(EXTRA_IS_ROOT); |
Felipe Leme | 8216cea | 2016-03-29 19:06:02 -0700 | [diff] [blame] | 386 | mIsPrimary= args.getBoolean(EXTRA_IS_PRIMARY); |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 387 | } |
Felipe Leme | 8cf6ce2 | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 388 | mActivity = (OpenExternalDirectoryActivity) getActivity(); |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | @Override |
Felipe Leme | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 392 | public void onDestroyView() { |
| 393 | // Workaround for https://code.google.com/p/android/issues/detail?id=17423 |
| 394 | if (mDialog != null && getRetainInstance()) { |
| 395 | mDialog.setDismissMessage(null); |
| 396 | } |
| 397 | super.onDestroyView(); |
| 398 | } |
| 399 | |
| 400 | @Override |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 401 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
Felipe Leme | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 402 | if (mDialog != null) { |
| 403 | if (DEBUG) Log.d(TAG, "fragment.onCreateDialog(): reusing dialog"); |
| 404 | return mDialog; |
| 405 | } |
| 406 | if (mActivity != getActivity()) { |
| 407 | // Sanity check. |
| 408 | Log.wtf(TAG, "activity references don't match on onCreateDialog(): mActivity = " |
| 409 | + mActivity + " , getActivity() = " + getActivity()); |
| 410 | mActivity = (OpenExternalDirectoryActivity) getActivity(); |
| 411 | } |
Felipe Leme | e4b6012 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 412 | final String directory = mFile.getName(); |
Felipe Leme | b59a8a6 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 413 | final String directoryName = mIsRoot ? DIRECTORY_ROOT : directory; |
Felipe Leme | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 414 | final Context context = mActivity.getApplicationContext(); |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 415 | final OnClickListener listener = new OnClickListener() { |
| 416 | |
| 417 | @Override |
| 418 | public void onClick(DialogInterface dialog, int which) { |
| 419 | Intent intent = null; |
| 420 | if (which == DialogInterface.BUTTON_POSITIVE) { |
Felipe Leme | e4b6012 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 421 | intent = createGrantedUriPermissionsIntent(mActivity, |
Felipe Leme | 8cf6ce2 | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 422 | mActivity.getExternalStorageClient(), mFile); |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 423 | } |
| 424 | if (which == DialogInterface.BUTTON_NEGATIVE || intent == null) { |
Felipe Leme | b59a8a6 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 425 | logValidScopedAccessRequest(mActivity, directoryName, |
Felipe Leme | e4b6012 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 426 | SCOPED_DIRECTORY_ACCESS_DENIED); |
Felipe Leme | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 427 | final boolean checked = mDontAskAgain.isChecked(); |
| 428 | if (checked) { |
| 429 | logValidScopedAccessRequest(mActivity, directory, |
| 430 | SCOPED_DIRECTORY_ACCESS_DENIED_AND_PERSIST); |
| 431 | setScopedAccessPermissionStatus(context, mActivity.getCallingPackage(), |
Felipe Leme | b59a8a6 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 432 | mVolumeUuid, directoryName, PERMISSION_NEVER_ASK); |
Felipe Leme | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 433 | } else { |
| 434 | setScopedAccessPermissionStatus(context, mActivity.getCallingPackage(), |
Felipe Leme | b59a8a6 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 435 | mVolumeUuid, directoryName, PERMISSION_ASK_AGAIN); |
Felipe Leme | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 436 | } |
| 437 | mActivity.setResult(RESULT_CANCELED); |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 438 | } else { |
Felipe Leme | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 439 | logValidScopedAccessRequest(mActivity, directory, |
Felipe Leme | e4b6012 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 440 | SCOPED_DIRECTORY_ACCESS_GRANTED); |
Felipe Leme | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 441 | mActivity.setResult(RESULT_OK, intent); |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 442 | } |
Felipe Leme | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 443 | mActivity.finish(); |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 444 | } |
| 445 | }; |
| 446 | |
Felipe Leme | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 447 | @SuppressLint("InflateParams") |
| 448 | // It's ok pass null ViewRoot on AlertDialogs. |
| 449 | 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] | 450 | final CharSequence message; |
| 451 | if (mIsRoot) { |
| 452 | message = TextUtils.expandTemplate(getText( |
| 453 | R.string.open_external_dialog_root_request), mAppLabel, mVolumeLabel); |
| 454 | } else { |
Felipe Leme | 8216cea | 2016-03-29 19:06:02 -0700 | [diff] [blame] | 455 | message = TextUtils.expandTemplate( |
| 456 | getText(mIsPrimary ? R.string.open_external_dialog_request_primary_volume |
| 457 | : R.string.open_external_dialog_request), |
Felipe Leme | b59a8a6 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 458 | mAppLabel, directory, mVolumeLabel); |
| 459 | } |
Felipe Leme | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 460 | final TextView messageField = (TextView) view.findViewById(R.id.message); |
| 461 | messageField.setText(message); |
Felipe Leme | cb5e3e7 | 2016-03-15 16:29:17 -0700 | [diff] [blame] | 462 | mDialog = new AlertDialog.Builder(mActivity, R.style.Theme_AppCompat_Light_Dialog_Alert) |
Felipe Leme | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 463 | .setView(view) |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 464 | .setPositiveButton(R.string.allow, listener) |
| 465 | .setNegativeButton(R.string.deny, listener) |
| 466 | .create(); |
Felipe Leme | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 467 | |
| 468 | mDontAskAgain = (CheckBox) view.findViewById(R.id.do_not_ask_checkbox); |
| 469 | if (getScopedAccessPermissionStatus(context, mActivity.getCallingPackage(), |
Felipe Leme | b59a8a6 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 470 | mVolumeUuid, directoryName) == PERMISSION_ASK_AGAIN) { |
Felipe Leme | 0bdb7c3 | 2016-03-09 17:40:49 -0800 | [diff] [blame] | 471 | mDontAskAgain.setVisibility(View.VISIBLE); |
| 472 | mDontAskAgain.setOnCheckedChangeListener(new OnCheckedChangeListener() { |
| 473 | |
| 474 | @Override |
| 475 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
| 476 | mDialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(!isChecked); |
| 477 | } |
| 478 | }); |
| 479 | } |
| 480 | |
| 481 | return mDialog; |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | @Override |
| 485 | public void onCancel(DialogInterface dialog) { |
| 486 | super.onCancel(dialog); |
| 487 | final Activity activity = getActivity(); |
Felipe Leme | e4b6012 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 488 | logValidScopedAccessRequest(activity, mFile.getName(), SCOPED_DIRECTORY_ACCESS_DENIED); |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 489 | activity.setResult(RESULT_CANCELED); |
| 490 | activity.finish(); |
| 491 | } |
Felipe Leme | 8cf6ce2 | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 492 | } |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 493 | |
Felipe Leme | 8cf6ce2 | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 494 | private synchronized ContentProviderClient getExternalStorageClient() { |
| 495 | if (mExternalStorageClient == null) { |
| 496 | mExternalStorageClient = |
| 497 | getContentResolver().acquireContentProviderClient(EXTERNAL_STORAGE_AUTH); |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 498 | } |
Felipe Leme | 8cf6ce2 | 2016-02-17 17:10:45 -0800 | [diff] [blame] | 499 | return mExternalStorageClient; |
Felipe Leme | a4f4d7e | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 500 | } |
| 501 | } |