nextcloud / android-common

Android libraries for common code in Nextcloud's Android apps (WIP)
MIT License
3 stars 3 forks source link

`AndroidViewThemeUtils#highlightText` throws `IndexOutOfBoundsException` #145

Closed stefan-niedermann closed 1 year ago

stefan-niedermann commented 1 year ago

Conditions

Reproduce

  1. Checkout 145-highlight-text
  2. Launch the sample app
  3. Press the Apply Theme button

Stacktrace

com.nextcloud.android.common.sample  E  FATAL EXCEPTION: main
Process: com.nextcloud.android.common.sample, PID: 4254
java.lang.IndexOutOfBoundsException: setSpan (15 ... 2) has end before start
    at android.text.SpannableStringInternal.checkRange(SpannableStringInternal.java:485)
    at android.text.SpannableStringInternal.setSpan(SpannableStringInternal.java:199)
    at android.text.SpannableStringInternal.setSpan(SpannableStringInternal.java:186)
    at android.text.SpannableString.setSpan(SpannableString.java:60)
    at com.nextcloud.android.common.ui.theme.utils.AndroidViewThemeUtils.spanText(AndroidViewThemeUtils.kt:584)
    at com.nextcloud.android.common.ui.theme.utils.AndroidViewThemeUtils.highlightText(AndroidViewThemeUtils.kt:562)
    at com.nextcloud.android.common.ui.theme.utils.AndroidViewThemeUtils.access$highlightText(AndroidViewThemeUtils.kt:75)
    at com.nextcloud.android.common.ui.theme.utils.AndroidViewThemeUtils$highlightText$1.invoke(AndroidViewThemeUtils.kt:548)
    at com.nextcloud.android.common.ui.theme.utils.AndroidViewThemeUtils$highlightText$1.invoke(AndroidViewThemeUtils.kt:547)
    at com.nextcloud.android.common.ui.theme.ViewThemeUtilsBase.withScheme(ViewThemeUtilsBase.kt:49)
    at com.nextcloud.android.common.ui.theme.utils.AndroidViewThemeUtils.highlightText(AndroidViewThemeUtils.kt:547)
    at com.nextcloud.android.common.ui.theme.utils.AndroidViewThemeUtils.highlightText$default(AndroidViewThemeUtils.kt:541)
    at com.nextcloud.android.common.sample.MainActivity.applyTheme(MainActivity.kt:87)
    at com.nextcloud.android.common.sample.MainActivity.access$applyTheme(MainActivity.kt:34)
    at com.nextcloud.android.common.sample.MainActivity$onCreate$2.invoke(MainActivity.kt:59)
    at com.nextcloud.android.common.sample.MainActivity$onCreate$2.invoke(MainActivity.kt:59)
    at com.nextcloud.android.common.sample.MainActivity$sam$androidx_lifecycle_Observer$0.onChanged(Unknown Source:2)
    at androidx.lifecycle.LiveData.considerNotify(LiveData.java:133)
    at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:151)
    at androidx.lifecycle.LiveData.setValue(LiveData.java:309)
    at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50)
    at com.nextcloud.android.common.sample.MainActivity.onCreate$lambda$0(MainActivity.kt:49)
    at com.nextcloud.android.common.sample.MainActivity.$r8$lambda$ev6Kxy-ymus-tRcdeQloM960R20(Unknown Source:0)
    at com.nextcloud.android.common.sample.MainActivity$$ExternalSyntheticLambda0.onClick(Unknown Source:4)
    at android.view.View.performClick(View.java:7506)
    at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1211)
    at android.view.View.performClickInternal(View.java:7483)
    at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0)
    at android.view.View$PerformClick.run(View.java:29334)
    at android.os.Handler.handleCallback(Handler.java:942)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:201)
    at android.os.Looper.loop(Looper.java:288)
    at android.app.ActivityThread.main(ActivityThread.java:7872)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

Refs: https://github.com/nextcloud/notes-android/issues/1833

tobiasKaminsky commented 1 year ago

I added a test case to sample project.

tobiasKaminsky commented 1 year ago

This way we can add more tests in future