denzilferreira / aware-client

AWARE's Android client. This application allows researchers to easily collect sensor/plugin data locally or remotely to an AWARE server instance.
https://awareframework.com
88 stars 78 forks source link

ClassCastException when trying to load plugin #296

Open adamleko opened 4 years ago

adamleko commented 4 years ago

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.