contacts-android: Permissions module extension `xxxWithPermission()` may crash app

Code example:

When running an insert after, done after queryWithPermission() it crashes the app with the below stacktrace.

  suspend fun create(name: String, phone: String?, email: String?): Long? {
    val rawContact = NewRawContact().apply {
      setName { displayName = name }
      if (phone != null) {
        addPhone {
          number = phone
          type = PhoneEntity.Type.OTHER
        }
      }
      if (email != null) {
        addEmail {
          address = email
          type = EmailEntity.Type.OTHER
        }
      }
    }
    return contacts
      .insertWithPermission()
      .rawContacts(rawContact)
      .commitWithContext()
      .rawContactId(rawContact)
  }

Stacktrace:

    java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=42, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.alorma.tempcontacts/com.karumi.dexter.DexterActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
        at android.app.ActivityThread.deliverResults(ActivityThread.java:5360)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:5401)
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2267)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:8167)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
     Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
        at android.view.ViewRootImpl.setView(ViewRootImpl.java:1147)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:471)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:95)
 

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 20 (19 by maintainers)

Most upvoted comments

This is included in 0.2.4!

No breaking changes for library users!

It works without allowBlanks(true) yes

NO, I not used insertWithPermission(), as I managed. permissions on other way

I successfully created contact using allowBlanks(true) and requesting permissions READ, WRITE and GET_ACCOUNTS

Uhmmm, no, i had it disabled.

As well, my project is a single activity

Unrelated to the crash, I noticed that you can reduce the lines of code in your app.

So instead of,

val rawContact = NewRawContact().apply {
      setName { displayName = name }
      if (phone != null) {
        addPhone {
          number = phone
          type = PhoneEntity.Type.OTHER
        }
      }
      if (email != null) {
        addEmail {
          address = email
          type = EmailEntity.Type.OTHER
        }
      }
}

You can just do,

val rawContact = NewRawContact().apply {
      setName { displayName = name }
      addPhone {
        number = phone
        type = PhoneEntity.Type.OTHER
      }
      addEmail {
        address = email
        type = EmailEntity.Type.OTHER
      }
}

You don’t need the null checks. All data entity properties (i.e. number, type, address) are nullable. If the important data (i.e. the phone number, email address) are null or blank, the insert APIs will ignore it and it will NOT be inserted into the database. You can read more about this in How do I learn more about “blank” data?.

I run this library on app made with compose

It also happens when denying permission.