Repository: dotnet/maui
File: src/Core/AndroidNative/maui/src/main/java/com/microsoft/maui/glide/MauiCustomTarget.java (Java)

Code before:
        if (logger.isVerboseLoggable) logger.v("onResourceReady: " + resourceLogIdentifier);

        callback.onComplete(true, resource, this::clear);
    }

    @Override
    public void onLoadCleared(@Nullable Drawable placeholder) {
        if (logger.isVerboseLoggable) logger.v("onLoadCleared: " + resourceLogIdentifier);
    }

    private void post(Runnable runnable) {
        Looper looper = Looper.getMainLooper();
        if (looper.isCurrentThread()) {
            runnable.run();
            return;
        }

        Handler handler = new Handler(looper);
        handler.post(runnable);
    }

    private void clear() {
        // TODO: it looks like no one is really disposing the result on C# side
        // we must fix it there to release the Glide cache entry properly
        post(() -> {
            if (PlatformInterop.isContextDestroyed(context)) {
                if (logger.isVerboseLoggable) {
                    logger.v("clear() skipped - context destroyed: " + resourceLogIdentifier);
                }
                return;
            }
            Glide
                .with(context)
                .clear(this);
        });
    }
}

Review comment (suggestion):
@copilot this line has a build error:
```
/mnt/vss/_work/1/s/.dotnet/packs/Microsoft.Android.Sdk.Linux/35.0.61/tools/Microsoft.Android.Sdk.Bindings.Gradle.targets(83,5): error XAGRDL0000: /mnt/vss/_work/1/s/src/Core/AndroidNative/maui/src/main/java/com/microsoft/maui/glide/MauiCustomTarget.java:88: error: isContextDestroyed(Context) is not public in PlatformInterop; cannot be accessed from outside package [/mnt/vss/_work/1/s/src/Core/src/Core.csproj::TargetFramework=net9.0-android35.0]
/mnt/vss/_work/1/s/.dotnet/packs/Microsoft.Android.Sdk.Linux/35.0.61/tools/Microsoft.Android.Sdk.Bindings.Gradle.targets(83,5): error XAGRDL0000:             if (PlatformInterop.isContextDestroyed(context)) { [/mnt/vss/_work/1/s/src/Core/src/Core.csproj::TargetFramework=net9.0-android35.0]
/mnt/vss/_work/1/s/.dotnet/packs/Microsoft.Android.Sdk.Linux/35.0.61/tools/Microsoft.Android.Sdk.Bindings.Gradle.targets(83,5): error XAGRDL0000:                                ^ [/mnt/vss/_work/1/s/src/Core/src/Core.csproj::TargetFramework=net9.0-android35.0]
```

I don't think "package access" will work, and because it's a different package we have to use `public`?

See: https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

Code after:

        if (logger.isVerboseLoggable) logger.v("onResourceReady: " + resourceLogIdentifier);

        callback.onComplete(true, resource, this::clear);
    }

    @Override
    public void onLoadCleared(@Nullable Drawable placeholder) {
        if (logger.isVerboseLoggable) logger.v("onLoadCleared: " + resourceLogIdentifier);
    }

    private void post(Runnable runnable) {
        Looper looper = Looper.getMainLooper();
        if (looper.isCurrentThread()) {
            runnable.run();
            return;
        }

        Handler handler = new Handler(looper);
        handler.post(runnable);
    }

    private static boolean isContextDestroyed(Context context) {
        Activity activity = getActivity(context);
        if (activity != null) {
            if (activity.isFinishing() || activity.isDestroyed()) {
                return true;
            }
        }
        return false;
    }

    private static Activity getActivity(Context context) {
        if (context == null) {
            return null;
        }
        if (context instanceof Activity) {
            return (Activity) context;
        }
        if (context instanceof ContextWrapper) {
            Context baseContext = ((ContextWrapper) context).getBaseContext();
            return getActivity(baseContext);
        }
        return null;
    }

    private void clear() {
        // TODO: it looks like no one is really disposing the result on C# side
        // we must fix it there to release the Glide cache entry properly
        post(() -> {
            if (isContextDestroyed(context)) {