sephiroth74 / android-target-tooltip

Create Toast like tooltips, but targets can be specified, plus custom properties and features
MIT License
1.49k stars 278 forks source link

Crashes when loading animation #155

Open vovkab opened 2 years ago

vovkab commented 2 years ago

Looks like library is not loading animation correctly which leads to loading a wrong resource, i.e. it can try to load "dimen" as animation. As a result it crashes since it can't load other resource as animation.

Steps to reproduce:

  1. Remove any animation from style:
    <style name="ToolTipAnimation">
    <item name="android:windowExitAnimation">@anim/ttlm_tooltip_anim_exit</item>
    </style>
  2. Run the app and observer that animation is no longer working correctly. Even though it should have fallback to 0 and ignored if missing.

As I can see the issue is that we are using incorrect indexes when loading resources:

val typedArray = context.theme.obtainStyledAttributes(mAnimationStyleResId, intArrayOf(android.R.attr.windowEnterAnimation, android.R.attr.windowExitAnimation))
mEnterAnimation = typedArray.getResourceId(typedArray.getIndex(0), 0)
mExitAnimation = typedArray.getResourceId(typedArray.getIndex(1), 0)
typedArray.recycle()

According to documentation:

The indices used to retrieve values from this structure correspond to the positions of the attributes given to obtainStyledAttributes. https://developer.android.com/reference/android/content/res/TypedArray

We should be passing index from the array, for example:

val typedArray = context.theme.obtainStyledAttributes(mAnimationStyleResId, intArrayOf(android.R.attr.windowEnterAnimation, android.R.attr.windowExitAnimation))
mEnterAnimation = typedArray.getResourceId(0, 0)
mExitAnimation = typedArray.getResourceId(1, 0)
typedArray.recycle()