| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.os; |
| 18 |
|
| 19 |
import android.Manifest; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.SuppressLint; |
| 22 |
import android.annotation.SystemApi; |
| 23 |
import android.annotation.TestApi; |
| 24 |
import android.app.AppGlobals; |
| 25 |
import android.app.AppOpsManager; |
| 26 |
import android.app.admin.DevicePolicyManager; |
| 27 |
import android.compat.Compatibility; |
| 28 |
import android.compat.annotation.ChangeId; |
| 29 |
import android.compat.annotation.Disabled; |
| 30 |
import android.compat.annotation.UnsupportedAppUsage; |
| 31 |
import android.content.Context; |
| 32 |
import android.content.pm.ApplicationInfo; |
| 33 |
import android.content.pm.PackageManager; |
| 34 |
import android.os.storage.StorageManager; |
| 35 |
import android.os.storage.StorageVolume; |
| 36 |
import android.provider.MediaStore; |
| 37 |
import android.text.TextUtils; |
| 38 |
import android.util.Log; |
| 39 |
|
| 40 |
import java.io.File; |
| 41 |
import java.io.IOException; |
| 42 |
import java.util.ArrayList; |
| 43 |
import java.util.Collection; |
| 44 |
import java.util.LinkedList; |
| 45 |
import java.util.List; |
| 46 |
import java.util.Objects; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public class Environment { |
| 52 |
private static final String TAG = "Environment"; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
private static final String ENV_EXTERNAL_STORAGE = "EXTERNAL_STORAGE"; |
| 57 |
private static final String ENV_ANDROID_ROOT = "ANDROID_ROOT"; |
| 58 |
private static final String ENV_ANDROID_DATA = "ANDROID_DATA"; |
| 59 |
private static final String ENV_ANDROID_EXPAND = "ANDROID_EXPAND"; |
| 60 |
private static final String ENV_ANDROID_STORAGE = "ANDROID_STORAGE"; |
| 61 |
private static final String ENV_DOWNLOAD_CACHE = "DOWNLOAD_CACHE"; |
| 62 |
private static final String ENV_OEM_ROOT = "OEM_ROOT"; |
| 63 |
private static final String ENV_ODM_ROOT = "ODM_ROOT"; |
| 64 |
private static final String ENV_VENDOR_ROOT = "VENDOR_ROOT"; |
| 65 |
private static final String ENV_PRODUCT_ROOT = "PRODUCT_ROOT"; |
| 66 |
private static final String ENV_SYSTEM_EXT_ROOT = "SYSTEM_EXT_ROOT"; |
| 67 |
private static final String ENV_APEX_ROOT = "APEX_ROOT"; |
| 68 |
|
| 69 |
|
| 70 |
public static final String DIR_ANDROID = "Android"; |
| 71 |
private static final String DIR_DATA = "data"; |
| 72 |
private static final String DIR_MEDIA = "media"; |
| 73 |
private static final String DIR_OBB = "obb"; |
| 74 |
private static final String DIR_FILES = "files"; |
| 75 |
private static final String DIR_CACHE = "cache"; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public static final String DIR_USER_CE = "user"; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public static final String DIR_USER_DE = "user_de"; |
| 92 |
|
| 93 |
|
| 94 |
@Deprecated |
| 95 |
public static final String DIRECTORY_ANDROID = DIR_ANDROID; |
| 96 |
|
| 97 |
private static final File DIR_ANDROID_ROOT = getDirectory(ENV_ANDROID_ROOT, "/system"); |
| 98 |
private static final String DIR_ANDROID_DATA_PATH = getDirectoryPath(ENV_ANDROID_DATA, "/data"); |
| 99 |
private static final File DIR_ANDROID_DATA = new File(DIR_ANDROID_DATA_PATH); |
| 100 |
private static final File DIR_ANDROID_EXPAND = getDirectory(ENV_ANDROID_EXPAND, "/mnt/expand"); |
| 101 |
private static final File DIR_ANDROID_STORAGE = getDirectory(ENV_ANDROID_STORAGE, "/storage"); |
| 102 |
private static final File DIR_DOWNLOAD_CACHE = getDirectory(ENV_DOWNLOAD_CACHE, "/cache"); |
| 103 |
private static final File DIR_METADATA = new File("/metadata"); |
| 104 |
private static final File DIR_OEM_ROOT = getDirectory(ENV_OEM_ROOT, "/oem"); |
| 105 |
private static final File DIR_ODM_ROOT = getDirectory(ENV_ODM_ROOT, "/odm"); |
| 106 |
private static final File DIR_VENDOR_ROOT = getDirectory(ENV_VENDOR_ROOT, "/vendor"); |
| 107 |
private static final File DIR_PRODUCT_ROOT = getDirectory(ENV_PRODUCT_ROOT, "/product"); |
| 108 |
private static final File DIR_SYSTEM_EXT_ROOT = getDirectory(ENV_SYSTEM_EXT_ROOT, |
| 109 |
"/system_ext"); |
| 110 |
private static final File DIR_APEX_ROOT = getDirectory(ENV_APEX_ROOT, |
| 111 |
"/apex"); |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
@ChangeId |
| 133 |
private static final long DEFAULT_SCOPED_STORAGE = 149924527L; |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
@ChangeId |
| 153 |
@Disabled |
| 154 |
private static final long FORCE_ENABLE_SCOPED_STORAGE = 132649864L; |
| 155 |
|
| 156 |
@UnsupportedAppUsage |
| 157 |
private static UserEnvironment sCurrentUser; |
| 158 |
private static boolean sUserRequired; |
| 159 |
|
| 160 |
static { |
| 161 |
initForCurrentUser(); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
@UnsupportedAppUsage |
| 166 |
public static void initForCurrentUser() { |
| 167 |
final int userId = UserHandle.myUserId(); |
| 168 |
sCurrentUser = new UserEnvironment(userId); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
public static class UserEnvironment { |
| 173 |
private final int mUserId; |
| 174 |
|
| 175 |
@UnsupportedAppUsage |
| 176 |
public UserEnvironment(int userId) { |
| 177 |
mUserId = userId; |
| 178 |
} |
| 179 |
|
| 180 |
@UnsupportedAppUsage |
| 181 |
public File[] getExternalDirs() { |
| 182 |
final StorageVolume[] volumes = StorageManager.getVolumeList(mUserId, |
| 183 |
StorageManager.FLAG_FOR_WRITE); |
| 184 |
final File[] files = new File[volumes.length]; |
| 185 |
for (int i = 0; i < volumes.length; i++) { |
| 186 |
files[i] = volumes[i].getPathFile(); |
| 187 |
} |
| 188 |
return files; |
| 189 |
} |
| 190 |
|
| 191 |
@UnsupportedAppUsage |
| 192 |
public File getExternalStorageDirectory() { |
| 193 |
return getExternalDirs()[0]; |
| 194 |
} |
| 195 |
|
| 196 |
@UnsupportedAppUsage |
| 197 |
public File getExternalStoragePublicDirectory(String type) { |
| 198 |
return buildExternalStoragePublicDirs(type)[0]; |
| 199 |
} |
| 200 |
|
| 201 |
public File[] buildExternalStoragePublicDirs(String type) { |
| 202 |
return buildPaths(getExternalDirs(), type); |
| 203 |
} |
| 204 |
|
| 205 |
public File[] buildExternalStorageAndroidDataDirs() { |
| 206 |
return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA); |
| 207 |
} |
| 208 |
|
| 209 |
public File[] buildExternalStorageAndroidObbDirs() { |
| 210 |
return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB); |
| 211 |
} |
| 212 |
|
| 213 |
public File[] buildExternalStorageAppDataDirs(String packageName) { |
| 214 |
return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName); |
| 215 |
} |
| 216 |
|
| 217 |
public File[] buildExternalStorageAppMediaDirs(String packageName) { |
| 218 |
return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_MEDIA, packageName); |
| 219 |
} |
| 220 |
|
| 221 |
public File[] buildExternalStorageAppObbDirs(String packageName) { |
| 222 |
return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB, packageName); |
| 223 |
} |
| 224 |
|
| 225 |
public File[] buildExternalStorageAppFilesDirs(String packageName) { |
| 226 |
return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_FILES); |
| 227 |
} |
| 228 |
|
| 229 |
public File[] buildExternalStorageAppCacheDirs(String packageName) { |
| 230 |
return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_CACHE); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
public static @NonNull File getRootDirectory() { |
| 239 |
return DIR_ANDROID_ROOT; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
public static @NonNull File getStorageDirectory() { |
| 248 |
return DIR_ANDROID_STORAGE; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
@SystemApi |
| 258 |
public static @NonNull File getOemDirectory() { |
| 259 |
return DIR_OEM_ROOT; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
@SystemApi |
| 269 |
public static @NonNull File getOdmDirectory() { |
| 270 |
return DIR_ODM_ROOT; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
@SystemApi |
| 279 |
public static @NonNull File getVendorDirectory() { |
| 280 |
return DIR_VENDOR_ROOT; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
@SystemApi |
| 290 |
public static @NonNull File getProductDirectory() { |
| 291 |
return DIR_PRODUCT_ROOT; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
@SystemApi |
| 304 |
@Deprecated |
| 305 |
public static @NonNull File getProductServicesDirectory() { |
| 306 |
return getDirectory("PRODUCT_SERVICES_ROOT", "/product_services"); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
@SystemApi |
| 316 |
public static @NonNull File getSystemExtDirectory() { |
| 317 |
return DIR_SYSTEM_EXT_ROOT; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
public static @NonNull File getApexDirectory() { |
| 327 |
return DIR_APEX_ROOT; |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
@Deprecated |
| 343 |
public static File getUserSystemDirectory(int userId) { |
| 344 |
return new File(new File(getDataSystemDirectory(), "users"), Integer.toString(userId)); |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
@Deprecated |
| 360 |
public static File getUserConfigDirectory(int userId) { |
| 361 |
return new File(new File(new File( |
| 362 |
getDataDirectory(), "misc"), "user"), Integer.toString(userId)); |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
public static File getDataDirectory() { |
| 369 |
return DIR_ANDROID_DATA; |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
public static String getDataDirectoryPath() { |
| 377 |
return DIR_ANDROID_DATA_PATH; |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
public static File getDataDirectory(String volumeUuid) { |
| 382 |
if (TextUtils.isEmpty(volumeUuid)) { |
| 383 |
return DIR_ANDROID_DATA; |
| 384 |
} else { |
| 385 |
return new File("/mnt/expand/" + volumeUuid); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
public static String getDataDirectoryPath(String volumeUuid) { |
| 391 |
if (TextUtils.isEmpty(volumeUuid)) { |
| 392 |
return DIR_ANDROID_DATA_PATH; |
| 393 |
} else { |
| 394 |
return getExpandDirectory().getAbsolutePath() + File.separator + volumeUuid; |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
public static File getExpandDirectory() { |
| 400 |
return DIR_ANDROID_EXPAND; |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
@UnsupportedAppUsage |
| 405 |
public static File getDataSystemDirectory() { |
| 406 |
return new File(getDataDirectory(), "system"); |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
public static File getDataSystemDeDirectory() { |
| 414 |
return buildPath(getDataDirectory(), "system_de"); |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
public static File getDataSystemCeDirectory() { |
| 422 |
return buildPath(getDataDirectory(), "system_ce"); |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
public static File getDataSystemCeDirectory(int userId) { |
| 442 |
return buildPath(getDataDirectory(), "system_ce", String.valueOf(userId)); |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
public static File getDataSystemDeDirectory(int userId) { |
| 462 |
return buildPath(getDataDirectory(), "system_de", String.valueOf(userId)); |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
public static File getDataMiscDirectory() { |
| 467 |
return new File(getDataDirectory(), "misc"); |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
public static File getDataMiscCeDirectory() { |
| 472 |
return buildPath(getDataDirectory(), "misc_ce"); |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
public static File getDataMiscCeDirectory(int userId) { |
| 477 |
return buildPath(getDataDirectory(), "misc_ce", String.valueOf(userId)); |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
private static File getDataMiscCeDirectory(String volumeUuid, int userId) { |
| 482 |
return buildPath(getDataDirectory(volumeUuid), "misc_ce", String.valueOf(userId)); |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
public static File getDataMiscCeSharedSdkSandboxDirectory(String volumeUuid, int userId, |
| 487 |
String packageName) { |
| 488 |
return buildPath(getDataMiscCeDirectory(volumeUuid, userId), "sdksandbox", |
| 489 |
packageName, "shared"); |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
public static File getDataMiscDeDirectory(int userId) { |
| 494 |
return buildPath(getDataDirectory(), "misc_de", String.valueOf(userId)); |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
private static File getDataMiscDeDirectory(String volumeUuid, int userId) { |
| 499 |
return buildPath(getDataDirectory(volumeUuid), "misc_de", String.valueOf(userId)); |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
public static File getDataMiscDeSharedSdkSandboxDirectory(String volumeUuid, int userId, |
| 504 |
String packageName) { |
| 505 |
return buildPath(getDataMiscDeDirectory(volumeUuid, userId), "sdksandbox", |
| 506 |
packageName, "shared"); |
| 507 |
} |
| 508 |
|
| 509 |
private static File getDataProfilesDeDirectory(int userId) { |
| 510 |
return buildPath(getDataDirectory(), "misc", "profiles", "cur", String.valueOf(userId)); |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
public static File getDataVendorCeDirectory(int userId) { |
| 515 |
return buildPath(getDataDirectory(), "vendor_ce", String.valueOf(userId)); |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
public static File getDataVendorDeDirectory(int userId) { |
| 520 |
return buildPath(getDataDirectory(), "vendor_de", String.valueOf(userId)); |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
public static File getDataRefProfilesDePackageDirectory(String packageName) { |
| 525 |
return buildPath(getDataDirectory(), "misc", "profiles", "ref", packageName); |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
public static File getDataProfilesDePackageDirectory(int userId, String packageName) { |
| 530 |
return buildPath(getDataProfilesDeDirectory(userId), packageName); |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
public static File getDataAppDirectory(String volumeUuid) { |
| 535 |
return new File(getDataDirectory(volumeUuid), "app"); |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
public static File getDataStagingDirectory(String volumeUuid) { |
| 540 |
return new File(getDataDirectory(volumeUuid), "app-staging"); |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
public static File getDataUserCeDirectory(String volumeUuid) { |
| 545 |
return new File(getDataDirectory(volumeUuid), DIR_USER_CE); |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
public static File getDataUserCeDirectory(String volumeUuid, int userId) { |
| 550 |
return new File(getDataUserCeDirectory(volumeUuid), String.valueOf(userId)); |
| 551 |
} |
| 552 |
|
| 553 |
|
| 554 |
public static File getDataUserCePackageDirectory(String volumeUuid, int userId, |
| 555 |
String packageName) { |
| 556 |
|
| 557 |
return new File(getDataUserCeDirectory(volumeUuid, userId), packageName); |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
public static File getDataUserDeDirectory(String volumeUuid) { |
| 562 |
return new File(getDataDirectory(volumeUuid), DIR_USER_DE); |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
public static File getDataUserDeDirectory(String volumeUuid, int userId) { |
| 567 |
return new File(getDataUserDeDirectory(volumeUuid), String.valueOf(userId)); |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
public static File getDataUserDePackageDirectory(String volumeUuid, int userId, |
| 572 |
String packageName) { |
| 573 |
|
| 574 |
return new File(getDataUserDeDirectory(volumeUuid, userId), packageName); |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
public static File getDataPreloadsDirectory() { |
| 585 |
return new File(getDataDirectory(), "preloads"); |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
public static File getDataPreloadsDemoDirectory() { |
| 593 |
return new File(getDataPreloadsDirectory(), "demo"); |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
public static File getDataPreloadsAppsDirectory() { |
| 601 |
return new File(getDataPreloadsDirectory(), "apps"); |
| 602 |
} |
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
public static File getDataPreloadsMediaDirectory() { |
| 609 |
return new File(getDataPreloadsDirectory(), "media"); |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
public static File getDataPreloadsFileCacheDirectory(String packageName) { |
| 618 |
return new File(getDataPreloadsFileCacheDirectory(), packageName); |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
public static File getDataPreloadsFileCacheDirectory() { |
| 627 |
return new File(getDataPreloadsDirectory(), "file_cache"); |
| 628 |
} |
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
public static File getPackageCacheDirectory() { |
| 635 |
return new File(getDataSystemDirectory(), "package_cache"); |
| 636 |
} |
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
@SystemApi |
| 646 |
public static @NonNull Collection<File> getInternalMediaDirectories() { |
| 647 |
final ArrayList<File> res = new ArrayList<>(); |
| 648 |
addCanonicalFile(res, new File(Environment.getRootDirectory(), "media")); |
| 649 |
addCanonicalFile(res, new File(Environment.getOemDirectory(), "media")); |
| 650 |
addCanonicalFile(res, new File(Environment.getProductDirectory(), "media")); |
| 651 |
return res; |
| 652 |
} |
| 653 |
|
| 654 |
private static void addCanonicalFile(List<File> list, File file) { |
| 655 |
try { |
| 656 |
list.add(file.getCanonicalFile()); |
| 657 |
} catch (IOException e) { |
| 658 |
Log.w(TAG, "Failed to resolve " + file + ": " + e); |
| 659 |
list.add(file); |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
public static File getExternalStorageDirectory() { |
| 728 |
throwIfUserRequired(); |
| 729 |
return sCurrentUser.getExternalDirs()[0]; |
| 730 |
} |
| 731 |
|
| 732 |
|
| 733 |
@UnsupportedAppUsage |
| 734 |
public static File getLegacyExternalStorageDirectory() { |
| 735 |
return new File(System.getenv(ENV_EXTERNAL_STORAGE)); |
| 736 |
} |
| 737 |
|
| 738 |
|
| 739 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 740 |
public static File getLegacyExternalStorageObbDirectory() { |
| 741 |
return buildPath(getLegacyExternalStorageDirectory(), DIR_ANDROID, DIR_OBB); |
| 742 |
} |
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
public static String DIRECTORY_MUSIC = "Music"; |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
public static String DIRECTORY_PODCASTS = "Podcasts"; |
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
public static String DIRECTORY_RINGTONES = "Ringtones"; |
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
public static String DIRECTORY_ALARMS = "Alarms"; |
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
public static String DIRECTORY_NOTIFICATIONS = "Notifications"; |
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
public static String DIRECTORY_PICTURES = "Pictures"; |
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
public static String DIRECTORY_MOVIES = "Movies"; |
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
public static String DIRECTORY_DOWNLOADS = "Download"; |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
public static String DIRECTORY_DCIM = "DCIM"; |
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
public static String DIRECTORY_DOCUMENTS = "Documents"; |
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
public static String DIRECTORY_SCREENSHOTS = "Screenshots"; |
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
public static String DIRECTORY_AUDIOBOOKS = "Audiobooks"; |
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
@NonNull |
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
@SuppressLint({"MutableBareField", "AllUpper"}) |
| 880 |
public static String DIRECTORY_RECORDINGS = "Recordings"; |
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
public static final String[] STANDARD_DIRECTORIES = { |
| 903 |
DIRECTORY_MUSIC, |
| 904 |
DIRECTORY_PODCASTS, |
| 905 |
DIRECTORY_RINGTONES, |
| 906 |
DIRECTORY_ALARMS, |
| 907 |
DIRECTORY_NOTIFICATIONS, |
| 908 |
DIRECTORY_PICTURES, |
| 909 |
DIRECTORY_MOVIES, |
| 910 |
DIRECTORY_DOWNLOADS, |
| 911 |
DIRECTORY_DCIM, |
| 912 |
DIRECTORY_DOCUMENTS, |
| 913 |
DIRECTORY_AUDIOBOOKS, |
| 914 |
DIRECTORY_RECORDINGS, |
| 915 |
}; |
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
public static boolean isStandardDirectory(String dir) { |
| 921 |
for (String valid : STANDARD_DIRECTORIES) { |
| 922 |
if (valid.equals(dir)) { |
| 923 |
return true; |
| 924 |
} |
| 925 |
} |
| 926 |
return false; |
| 927 |
} |
| 928 |
|
| 929 |
public static final int HAS_MUSIC = 1 << 0; |
| 930 |
public static final int HAS_PODCASTS = 1 << 1; |
| 931 |
public static final int HAS_RINGTONES = 1 << 2; |
| 932 |
public static final int HAS_ALARMS = 1 << 3; |
| 933 |
public static final int HAS_NOTIFICATIONS = 1 << 4; |
| 934 |
public static final int HAS_PICTURES = 1 << 5; |
| 935 |
public static final int HAS_MOVIES = 1 << 6; |
| 936 |
public static final int HAS_DOWNLOADS = 1 << 7; |
| 937 |
public static final int HAS_DCIM = 1 << 8; |
| 938 |
public static final int HAS_DOCUMENTS = 1 << 9; |
| 939 |
public static final int HAS_AUDIOBOOKS = 1 << 10; |
| 940 |
public static final int HAS_RECORDINGS = 1 << 11; |
| 941 |
|
| 942 |
public static final int HAS_ANDROID = 1 << 16; |
| 943 |
public static final int HAS_OTHER = 1 << 17; |
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
public static int classifyExternalStorageDirectory(File dir) { |
| 954 |
int res = 0; |
| 955 |
for (File f : FileUtils.listFilesOrEmpty(dir)) { |
| 956 |
if (f.isFile() && isInterestingFile(f)) { |
| 957 |
res |= HAS_OTHER; |
| 958 |
} else if (f.isDirectory() && hasInterestingFiles(f)) { |
| 959 |
final String name = f.getName(); |
| 960 |
if (DIRECTORY_MUSIC.equals(name)) res |= HAS_MUSIC; |
| 961 |
else if (DIRECTORY_PODCASTS.equals(name)) res |= HAS_PODCASTS; |
| 962 |
else if (DIRECTORY_RINGTONES.equals(name)) res |= HAS_RINGTONES; |
| 963 |
else if (DIRECTORY_ALARMS.equals(name)) res |= HAS_ALARMS; |
| 964 |
else if (DIRECTORY_NOTIFICATIONS.equals(name)) res |= HAS_NOTIFICATIONS; |
| 965 |
else if (DIRECTORY_PICTURES.equals(name)) res |= HAS_PICTURES; |
| 966 |
else if (DIRECTORY_MOVIES.equals(name)) res |= HAS_MOVIES; |
| 967 |
else if (DIRECTORY_DOWNLOADS.equals(name)) res |= HAS_DOWNLOADS; |
| 968 |
else if (DIRECTORY_DCIM.equals(name)) res |= HAS_DCIM; |
| 969 |
else if (DIRECTORY_DOCUMENTS.equals(name)) res |= HAS_DOCUMENTS; |
| 970 |
else if (DIRECTORY_AUDIOBOOKS.equals(name)) res |= HAS_AUDIOBOOKS; |
| 971 |
else if (DIRECTORY_RECORDINGS.equals(name)) res |= HAS_RECORDINGS; |
| 972 |
else if (DIRECTORY_ANDROID.equals(name)) res |= HAS_ANDROID; |
| 973 |
else res |= HAS_OTHER; |
| 974 |
} |
| 975 |
} |
| 976 |
return res; |
| 977 |
} |
| 978 |
|
| 979 |
private static boolean hasInterestingFiles(File dir) { |
| 980 |
final LinkedList<File> explore = new LinkedList<>(); |
| 981 |
explore.add(dir); |
| 982 |
while (!explore.isEmpty()) { |
| 983 |
dir = explore.pop(); |
| 984 |
for (File f : FileUtils.listFilesOrEmpty(dir)) { |
| 985 |
if (isInterestingFile(f)) return true; |
| 986 |
if (f.isDirectory()) explore.add(f); |
| 987 |
} |
| 988 |
} |
| 989 |
return false; |
| 990 |
} |
| 991 |
|
| 992 |
private static boolean isInterestingFile(File file) { |
| 993 |
if (file.isFile()) { |
| 994 |
final String name = file.getName().toLowerCase(); |
| 995 |
if (name.endsWith(".exe") || name.equals("autorun.inf") |
| 996 |
|| name.equals("launchpad.zip") || name.equals(".nomedia")) { |
| 997 |
return false; |
| 998 |
} else { |
| 999 |
return true; |
| 1000 |
} |
| 1001 |
} else { |
| 1002 |
return false; |
| 1003 |
} |
| 1004 |
} |
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
public static File getExternalStoragePublicDirectory(String type) { |
| 1038 |
throwIfUserRequired(); |
| 1039 |
return sCurrentUser.buildExternalStoragePublicDirs(type)[0]; |
| 1040 |
} |
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
@UnsupportedAppUsage |
| 1047 |
public static File[] buildExternalStorageAndroidDataDirs() { |
| 1048 |
throwIfUserRequired(); |
| 1049 |
return sCurrentUser.buildExternalStorageAndroidDataDirs(); |
| 1050 |
} |
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
public static File[] buildExternalStorageAndroidObbDirs() { |
| 1057 |
throwIfUserRequired(); |
| 1058 |
return sCurrentUser.buildExternalStorageAndroidObbDirs(); |
| 1059 |
} |
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
@UnsupportedAppUsage |
| 1066 |
public static File[] buildExternalStorageAppDataDirs(String packageName) { |
| 1067 |
throwIfUserRequired(); |
| 1068 |
return sCurrentUser.buildExternalStorageAppDataDirs(packageName); |
| 1069 |
} |
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
@UnsupportedAppUsage |
| 1076 |
public static File[] buildExternalStorageAppMediaDirs(String packageName) { |
| 1077 |
throwIfUserRequired(); |
| 1078 |
return sCurrentUser.buildExternalStorageAppMediaDirs(packageName); |
| 1079 |
} |
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 1086 |
public static File[] buildExternalStorageAppObbDirs(String packageName) { |
| 1087 |
throwIfUserRequired(); |
| 1088 |
return sCurrentUser.buildExternalStorageAppObbDirs(packageName); |
| 1089 |
} |
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
@UnsupportedAppUsage |
| 1096 |
public static File[] buildExternalStorageAppFilesDirs(String packageName) { |
| 1097 |
throwIfUserRequired(); |
| 1098 |
return sCurrentUser.buildExternalStorageAppFilesDirs(packageName); |
| 1099 |
} |
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
@UnsupportedAppUsage |
| 1106 |
public static File[] buildExternalStorageAppCacheDirs(String packageName) { |
| 1107 |
throwIfUserRequired(); |
| 1108 |
return sCurrentUser.buildExternalStorageAppCacheDirs(packageName); |
| 1109 |
} |
| 1110 |
|
| 1111 |
|
| 1112 |
public static File[] buildExternalStoragePublicDirs(@NonNull String dirType) { |
| 1113 |
throwIfUserRequired(); |
| 1114 |
return sCurrentUser.buildExternalStoragePublicDirs(dirType); |
| 1115 |
} |
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
public static File getDownloadCacheDirectory() { |
| 1121 |
return DIR_DOWNLOAD_CACHE; |
| 1122 |
} |
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
public static @NonNull File getMetadataDirectory() { |
| 1130 |
return DIR_METADATA; |
| 1131 |
} |
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
public static final String MEDIA_UNKNOWN = "unknown"; |
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
public static final String MEDIA_REMOVED = "removed"; |
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
public static final String MEDIA_UNMOUNTED = "unmounted"; |
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
public static final String MEDIA_CHECKING = "checking"; |
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
public static final String MEDIA_NOFS = "nofs"; |
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
public static final String MEDIA_MOUNTED = "mounted"; |
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro"; |
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
public static final String MEDIA_SHARED = "shared"; |
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
public static final String MEDIA_BAD_REMOVAL = "bad_removal"; |
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
public static final String MEDIA_UNMOUNTABLE = "unmountable"; |
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
public static final String MEDIA_EJECTING = "ejecting"; |
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
public static String getExternalStorageState() { |
| 1227 |
final File externalDir = sCurrentUser.getExternalDirs()[0]; |
| 1228 |
return getExternalStorageState(externalDir); |
| 1229 |
} |
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
@Deprecated |
| 1235 |
public static String getStorageState(File path) { |
| 1236 |
return getExternalStorageState(path); |
| 1237 |
} |
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
public static String getExternalStorageState(File path) { |
| 1250 |
final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId()); |
| 1251 |
if (volume != null) { |
| 1252 |
return volume.getState(); |
| 1253 |
} else { |
| 1254 |
return MEDIA_UNKNOWN; |
| 1255 |
} |
| 1256 |
} |
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
public static boolean isExternalStorageRemovable() { |
| 1267 |
final File externalDir = sCurrentUser.getExternalDirs()[0]; |
| 1268 |
return isExternalStorageRemovable(externalDir); |
| 1269 |
} |
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
public static boolean isExternalStorageRemovable(@NonNull File path) { |
| 1282 |
final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId()); |
| 1283 |
if (volume != null) { |
| 1284 |
return volume.isRemovable(); |
| 1285 |
} else { |
| 1286 |
throw new IllegalArgumentException("Failed to find storage device at " + path); |
| 1287 |
} |
| 1288 |
} |
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
public static boolean isExternalStorageEmulated() { |
| 1305 |
final File externalDir = sCurrentUser.getExternalDirs()[0]; |
| 1306 |
return isExternalStorageEmulated(externalDir); |
| 1307 |
} |
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
public static boolean isExternalStorageEmulated(@NonNull File path) { |
| 1325 |
final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId()); |
| 1326 |
if (volume != null) { |
| 1327 |
return volume.isEmulated(); |
| 1328 |
} else { |
| 1329 |
throw new IllegalArgumentException("Failed to find storage device at " + path); |
| 1330 |
} |
| 1331 |
} |
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
public static boolean isExternalStorageLegacy() { |
| 1346 |
final File externalDir = sCurrentUser.getExternalDirs()[0]; |
| 1347 |
return isExternalStorageLegacy(externalDir); |
| 1348 |
} |
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
public static boolean isExternalStorageLegacy(@NonNull File path) { |
| 1366 |
final Context context = AppGlobals.getInitialApplication(); |
| 1367 |
final int uid = context.getApplicationInfo().uid; |
| 1368 |
|
| 1369 |
if (Process.isIsolated(uid) || Process.isSdkSandboxUid(uid)) { |
| 1370 |
return false; |
| 1371 |
} |
| 1372 |
|
| 1373 |
final PackageManager packageManager = context.getPackageManager(); |
| 1374 |
if (packageManager.isInstantApp()) { |
| 1375 |
return false; |
| 1376 |
} |
| 1377 |
|
| 1378 |
|
| 1379 |
final String packageName = AppGlobals.getInitialPackage(); |
| 1380 |
try { |
| 1381 |
final PackageManager.Property noAppStorageProp = packageManager.getProperty( |
| 1382 |
PackageManager.PROPERTY_NO_APP_DATA_STORAGE, packageName); |
| 1383 |
if (noAppStorageProp != null && noAppStorageProp.getBoolean()) { |
| 1384 |
return false; |
| 1385 |
} |
| 1386 |
} catch (PackageManager.NameNotFoundException ignore) { |
| 1387 |
|
| 1388 |
} |
| 1389 |
|
| 1390 |
boolean defaultScopedStorage = Compatibility.isChangeEnabled(DEFAULT_SCOPED_STORAGE); |
| 1391 |
boolean forceEnableScopedStorage = Compatibility.isChangeEnabled( |
| 1392 |
FORCE_ENABLE_SCOPED_STORAGE); |
| 1393 |
|
| 1394 |
|
| 1395 |
if (isScopedStorageEnforced(defaultScopedStorage, forceEnableScopedStorage)) { |
| 1396 |
return false; |
| 1397 |
} |
| 1398 |
|
| 1399 |
|
| 1400 |
if (isScopedStorageDisabled(defaultScopedStorage, forceEnableScopedStorage)) { |
| 1401 |
return true; |
| 1402 |
} |
| 1403 |
|
| 1404 |
final AppOpsManager appOps = context.getSystemService(AppOpsManager.class); |
| 1405 |
final String opPackageName = context.getOpPackageName(); |
| 1406 |
|
| 1407 |
if (appOps.noteOpNoThrow(AppOpsManager.OP_LEGACY_STORAGE, uid, |
| 1408 |
opPackageName) == AppOpsManager.MODE_ALLOWED) { |
| 1409 |
return true; |
| 1410 |
} |
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
return appOps.noteOpNoThrow(AppOpsManager.OP_NO_ISOLATED_STORAGE, uid, |
| 1415 |
opPackageName) == AppOpsManager.MODE_ALLOWED; |
| 1416 |
} |
| 1417 |
|
| 1418 |
private static boolean isScopedStorageEnforced(boolean defaultScopedStorage, |
| 1419 |
boolean forceEnableScopedStorage) { |
| 1420 |
return defaultScopedStorage && forceEnableScopedStorage; |
| 1421 |
} |
| 1422 |
|
| 1423 |
private static boolean isScopedStorageDisabled(boolean defaultScopedStorage, |
| 1424 |
boolean forceEnableScopedStorage) { |
| 1425 |
return !defaultScopedStorage && !forceEnableScopedStorage; |
| 1426 |
} |
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
public static boolean isExternalStorageManager() { |
| 1437 |
final File externalDir = sCurrentUser.getExternalDirs()[0]; |
| 1438 |
return isExternalStorageManager(externalDir); |
| 1439 |
} |
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
public static boolean isExternalStorageManager(@NonNull File path) { |
| 1449 |
final Context context = Objects.requireNonNull(AppGlobals.getInitialApplication()); |
| 1450 |
String packageName = Objects.requireNonNull(context.getPackageName()); |
| 1451 |
int uid = context.getApplicationInfo().uid; |
| 1452 |
|
| 1453 |
final AppOpsManager appOps = context.getSystemService(AppOpsManager.class); |
| 1454 |
final int opMode = |
| 1455 |
appOps.checkOpNoThrow(AppOpsManager.OP_MANAGE_EXTERNAL_STORAGE, uid, packageName); |
| 1456 |
|
| 1457 |
switch (opMode) { |
| 1458 |
case AppOpsManager.MODE_DEFAULT: |
| 1459 |
return PackageManager.PERMISSION_GRANTED |
| 1460 |
== context.checkPermission( |
| 1461 |
Manifest.permission.MANAGE_EXTERNAL_STORAGE, Process.myPid(), uid); |
| 1462 |
case AppOpsManager.MODE_ALLOWED: |
| 1463 |
return true; |
| 1464 |
case AppOpsManager.MODE_ERRORED: |
| 1465 |
case AppOpsManager.MODE_IGNORED: |
| 1466 |
return false; |
| 1467 |
default: |
| 1468 |
throw new IllegalStateException("Unknown AppOpsManager mode " + opMode); |
| 1469 |
} |
| 1470 |
} |
| 1471 |
|
| 1472 |
static File getDirectory(String variableName, String defaultPath) { |
| 1473 |
String path = System.getenv(variableName); |
| 1474 |
return path == null ? new File(defaultPath) : new File(path); |
| 1475 |
} |
| 1476 |
|
| 1477 |
@NonNull |
| 1478 |
static String getDirectoryPath(@NonNull String variableName, @NonNull String defaultPath) { |
| 1479 |
String path = System.getenv(variableName); |
| 1480 |
return path == null ? defaultPath : path; |
| 1481 |
} |
| 1482 |
|
| 1483 |
|
| 1484 |
public static void setUserRequired(boolean userRequired) { |
| 1485 |
sUserRequired = userRequired; |
| 1486 |
} |
| 1487 |
|
| 1488 |
private static void throwIfUserRequired() { |
| 1489 |
if (sUserRequired) { |
| 1490 |
Log.wtf(TAG, "Path requests must specify a user by using UserEnvironment", |
| 1491 |
new Throwable()); |
| 1492 |
} |
| 1493 |
} |
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
@UnsupportedAppUsage |
| 1501 |
public static File[] buildPaths(File[] base, String... segments) { |
| 1502 |
File[] result = new File[base.length]; |
| 1503 |
for (int i = 0; i < base.length; i++) { |
| 1504 |
result[i] = buildPath(base[i], segments); |
| 1505 |
} |
| 1506 |
return result; |
| 1507 |
} |
| 1508 |
|
| 1509 |
|
| 1510 |
|
| 1511 |
|
| 1512 |
|
| 1513 |
|
| 1514 |
@TestApi |
| 1515 |
public static File buildPath(File base, String... segments) { |
| 1516 |
File cur = base; |
| 1517 |
for (String segment : segments) { |
| 1518 |
if (cur == null) { |
| 1519 |
cur = new File(segment); |
| 1520 |
} else { |
| 1521 |
cur = new File(cur, segment); |
| 1522 |
} |
| 1523 |
} |
| 1524 |
return cur; |
| 1525 |
} |
| 1526 |
|
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
|
| 1540 |
@UnsupportedAppUsage |
| 1541 |
@Deprecated |
| 1542 |
public static File maybeTranslateEmulatedPathToInternal(File path) { |
| 1543 |
return StorageManager.maybeTranslateEmulatedPathToInternal(path); |
| 1544 |
} |
| 1545 |
} |