From f094406d36f17c95701aec15eb059c247acbb352 Mon Sep 17 00:00:00 2001 From: Zim Date: Wed, 17 Mar 2021 19:34:24 +0000 Subject: [PATCH] Add isAppIoBlocked TestApi This allows us test the notifyAppIoBlocked and notifyAppIoResumed APIs Also fixed isAppIoBlocked bug where a uid with a blocked counter of '0' would incorrectly result in returning true. Test: m Bug: 181222557 Change-Id: I3e8f040cd0e08cb0765fec0cfafab6428d6a3a00 --- core/api/test-current.txt | 1 + .../android/os/storage/IStorageManager.aidl | 3 ++- .../android/os/storage/StorageManager.java | 24 +++++++++++++++++++ .../android/server/StorageManagerService.java | 9 ++++++- .../server/storage/StorageUserConnection.java | 4 ++++ 5 files changed, 39 insertions(+), 2 deletions(-) diff --git a/core/api/test-current.txt b/core/api/test-current.txt index 811513bfd8c77..62b5f3861e8ff 100644 --- a/core/api/test-current.txt +++ b/core/api/test-current.txt @@ -1827,6 +1827,7 @@ package android.os.storage { public class StorageManager { method @NonNull public static java.util.UUID convert(@NonNull String); method @NonNull public static String convert(@NonNull java.util.UUID); + method public boolean isAppIoBlocked(@NonNull java.util.UUID, int, int, int); method public static boolean isUserKeyUnlocked(int); } diff --git a/core/java/android/os/storage/IStorageManager.aidl b/core/java/android/os/storage/IStorageManager.aidl index d42b147e7ca5a..fbac954b0a35c 100644 --- a/core/java/android/os/storage/IStorageManager.aidl +++ b/core/java/android/os/storage/IStorageManager.aidl @@ -202,4 +202,5 @@ interface IStorageManager { void notifyAppIoBlocked(in String volumeUuid, int uid, int tid, int reason) = 92; void notifyAppIoResumed(in String volumeUuid, int uid, int tid, int reason) = 93; int getExternalStorageMountMode(int uid, in String packageName) = 94; -} + boolean isAppIoBlocked(in String volumeUuid, int uid, int tid, int reason) = 95; +} \ No newline at end of file diff --git a/core/java/android/os/storage/StorageManager.java b/core/java/android/os/storage/StorageManager.java index 6fa59696fe1d9..81071682ab7e5 100644 --- a/core/java/android/os/storage/StorageManager.java +++ b/core/java/android/os/storage/StorageManager.java @@ -2871,6 +2871,30 @@ public class StorageManager { } } + /** + * Check if {@code uid} with {@code tid} is blocked on IO for {@code reason}. + * + * This requires {@link ExternalStorageService} the + * {@link android.Manifest.permission#WRITE_MEDIA_STORAGE} permission. + * + * @param volumeUuid the UUID of the storage volume to check IO blocked status + * @param uid the UID of the app to check IO blocked status + * @param tid the tid of the app to check IO blocked status + * @param reason the reason to check IO blocked status for + * + * @hide + */ + @TestApi + public boolean isAppIoBlocked(@NonNull UUID volumeUuid, int uid, int tid, + @AppIoBlockedReason int reason) { + Objects.requireNonNull(volumeUuid); + try { + return mStorageManager.isAppIoBlocked(convert(volumeUuid), uid, tid, reason); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } + private final Object mFuseAppLoopLock = new Object(); @GuardedBy("mFuseAppLoopLock") diff --git a/services/core/java/com/android/server/StorageManagerService.java b/services/core/java/com/android/server/StorageManagerService.java index ff7f2b2d41c49..7a9a2b5525d98 100644 --- a/services/core/java/com/android/server/StorageManagerService.java +++ b/services/core/java/com/android/server/StorageManagerService.java @@ -3375,7 +3375,7 @@ class StorageManagerService extends IStorageManager.Stub } } - /* + /** * Returns PendingIntent which can be used by Apps with MANAGE_EXTERNAL_STORAGE permission * to launch the manageSpaceActivity of the App specified by packageName. */ @@ -3437,6 +3437,13 @@ class StorageManagerService extends IStorageManager.Stub mStorageSessionController.notifyAppIoResumed(volumeUuid, uid, tid, reason); } + @Override + public boolean isAppIoBlocked(String volumeUuid, int uid, int tid, + @StorageManager.AppIoBlockedReason int reason) { + return isAppIoBlocked(uid); + } + + private boolean isAppIoBlocked(int uid) { return mStorageSessionController.isAppIoBlocked(uid); } diff --git a/services/core/java/com/android/server/storage/StorageUserConnection.java b/services/core/java/com/android/server/storage/StorageUserConnection.java index a0e2286f72e26..0b11b0b6d2129 100644 --- a/services/core/java/com/android/server/storage/StorageUserConnection.java +++ b/services/core/java/com/android/server/storage/StorageUserConnection.java @@ -265,6 +265,10 @@ public final class StorageUserConnection { synchronized (mSessionsLock) { int ioBlockedCounter = mUidsBlockedOnIo.get(uid, 0); if (ioBlockedCounter == 0) { + Slog.w(TAG, "Unexpected app IO resumption for uid: " + uid); + } + + if (ioBlockedCounter <= 1) { mUidsBlockedOnIo.remove(uid); } else { mUidsBlockedOnIo.put(uid, --ioBlockedCounter);