Skip to content
This repository has been archived by the owner on Jun 18, 2023. It is now read-only.

ClassCastException when trying to load plugin #296

Open
adamleko opened this issue Oct 15, 2020 · 0 comments
Open

ClassCastException when trying to load plugin #296

adamleko opened this issue Oct 15, 2020 · 0 comments

Comments

@adamleko
Copy link

When installing a plugin on more recent versions of Android, Aware crashes with the following ClassCastException:

AndroidRuntime: FATAL EXCEPTION: main
AndroidRuntime: Process: com.aware.phone, PID: 9202
AndroidRuntime: java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.PACKAGE_ADDED json flg=0x4000010 (has extras) } in com.aware.Aware$AndroidPackageMonitor@1f50455
AndroidRuntime:        at patcher$Args.lambda$-android_app_LoadedApk$ReceiverDispatcher$Args_52851(LoadedApk.java:1332)
AndroidRuntime:        at android.app.-$Lambda$aS31cHIhRx41653CMnd4gZqshIQ.$m$7(Unknown Source:4)
AndroidRuntime:        at android.app.-$Lambda$aS31cHIhRx41653CMnd4gZqshIQ.run(Unknown Source:39)
AndroidRuntime:        at android.os.Handler.handleCallback(Handler.java:790)
AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:99)
AndroidRuntime:        at android.os.Looper.loop(Looper.java:164)
AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:6647)
AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
AndroidRuntime:        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
AndroidRuntime: Caused by: java.lang.ClassCastException: android.graphics.drawable.AdaptiveIconDrawable cannot be cast to Drawable
AndroidRuntime:        at com.aware.utils.PluginsManager.getPluginIcon(PluginsManager.java:238)
AndroidRuntime:        at com.aware.Aware$AndroidPackageMonitor.onReceive(Aware.java:2032)
AndroidRuntime:        at patcher$Args.lambda$-android_app_LoadedApk$ReceiverDispatcher$Args_52851(LoadedApk.java:1322)
AndroidRuntime:        ... 9 more

The underlying cause is the unconditional class cast which only works if the application icon is actually an BitmapDrawable instance.

Newer versions of Android that use adaptive icons (past SDK v26 or so) will always return a AdaptiveIconDrawable when asking for the icon via PackageManager. A simple fix for this is to check the instance type before doing a cast, otherwise drawing the icon on a Canvas to get a bitmap reprsentation.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant