From 118ae6a7599855537a8497a124a799e3550defa3 Mon Sep 17 00:00:00 2001 From: Omar Eissa Date: Thu, 27 Feb 2025 11:31:14 +0000 Subject: Don't call notifyChange on main thread Call notifyChange for attaching volume on Foreground instead of main thread. When MediaProvider starts up it attaches existing volumes (most probably only internal volume), attachVolume operation involves notifying content observers about the volume attachment. Currently, this happens on main thread during MediaProvider initialization, which would take time if SyncManager is busy with other requests. Running notifyChange on foreground thread saves us about 20% of MP initialization time. Test: manual & atest MediaProviderTests Bug: 399347278 Flag: EXEMPT bug fix Change-Id: If6f50169df2985d7eee8140b91b6ee83ef723851 --- src/com/android/providers/media/MediaProvider.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/com/android/providers/media/MediaProvider.java b/src/com/android/providers/media/MediaProvider.java index d0d5d345d..348a36582 100644 --- a/src/com/android/providers/media/MediaProvider.java +++ b/src/com/android/providers/media/MediaProvider.java @@ -11888,14 +11888,15 @@ public class MediaProvider extends ContentProvider { final ContentResolver resolver = getContext().getContentResolver(); final Uri uri = getBaseContentUri(volumeName); // TODO(b/182396009) we probably also want to notify clone profile (and vice versa) - resolver.notifyChange(getBaseContentUri(volumeName), null); + ForegroundThread.getExecutor().execute(() -> { + resolver.notifyChange(getBaseContentUri(volumeName), null); + }); if (LOGV) Log.v(TAG, "Attached volume: " + volume); if (!MediaStore.VOLUME_INTERNAL.equals(volumeName)) { - // Also notify on synthetic view of all devices - resolver.notifyChange(getBaseContentUri(MediaStore.VOLUME_EXTERNAL), null); - ForegroundThread.getExecutor().execute(() -> { + // Also notify on synthetic view of all devices + resolver.notifyChange(getBaseContentUri(MediaStore.VOLUME_EXTERNAL), null); mExternalDatabase.runWithTransaction((db) -> { ensureNecessaryFolders(volume, db); return null; @@ -11956,11 +11957,15 @@ public class MediaProvider extends ContentProvider { } final ContentResolver resolver = getContext().getContentResolver(); - resolver.notifyChange(getBaseContentUri(volumeName), null); + ForegroundThread.getExecutor().execute(() -> { + resolver.notifyChange(getBaseContentUri(volumeName), null); + }); if (!MediaStore.VOLUME_INTERNAL.equals(volumeName)) { - // Also notify on synthetic view of all devices - resolver.notifyChange(getBaseContentUri(MediaStore.VOLUME_EXTERNAL), null); + ForegroundThread.getExecutor().execute(() -> { + // Also notify on synthetic view of all devices + resolver.notifyChange(getBaseContentUri(MediaStore.VOLUME_EXTERNAL), null); + }); } if (LOGV) Log.v(TAG, "Detached volume: " + volumeName); -- cgit v1.2.3-59-g8ed1b