Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 265e2bf8 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Copy over slice parsing from jetpack Test: Built & deployed locally"

parents 417f0a0b e4374aaf
Loading
Loading
Loading
Loading
+42 −23
Original line number Diff line number Diff line
@@ -16,8 +16,8 @@

package com.android.credentialmanager.jetpack.provider

import android.app.PendingIntent
import android.app.slice.Slice
import android.credentials.ui.Entry
import android.graphics.drawable.Icon

/**
@@ -32,37 +32,56 @@ class CredentialEntryUi(
  val userDisplayName: CharSequence?,
  val entryIcon: Icon?,
  val lastUsedTimeMillis: Long?,
  // TODO: Remove note
  val note: CharSequence?,
) {
  companion object {
    // Copied over from jetpack
    const val SLICE_HINT_TYPE_DISPLAY_NAME =
            "androidx.credentials.provider.credentialEntry.SLICE_HINT_TYPE_DISPLAY_NAME"
    const val SLICE_HINT_USERNAME =
            "androidx.credentials.provider.credentialEntry.SLICE_HINT_USER_NAME"
    const val SLICE_HINT_DISPLAYNAME =
            "androidx.credentials.provider.credentialEntry.SLICE_HINT_CREDENTIAL_TYPE_DISPLAY_NAME"
    const val SLICE_HINT_LAST_USED_TIME_MILLIS =
            "androidx.credentials.provider.credentialEntry.SLICE_HINT_LAST_USED_TIME_MILLIS"
    const val SLICE_HINT_ICON =
            "androidx.credentials.provider.credentialEntry.SLICE_HINT_PROFILE_ICON"
    const val SLICE_HINT_PENDING_INTENT =
            "androidx.credentials.provider.credentialEntry.SLICE_HINT_PENDING_INTENT"

    /**
     * Returns an instance of [CredentialEntryUi] derived from a [Slice] object.
     *
     * @param slice the [Slice] object constructed through jetpack library
     */
    @JvmStatic
    fun fromSlice(slice: Slice): CredentialEntryUi {
      var credentialType = slice.spec!!.type
      var credentialTypeDisplayName: CharSequence? = null
      var userName: CharSequence? = null
      var userDisplayName: CharSequence? = null
      var entryIcon: Icon? = null
      var lastUsedTimeMillis: Long? = null
      var username: CharSequence? = null
      var displayName: CharSequence = ""
      var icon: Icon? = null
      var pendingIntent: PendingIntent? = null
      var lastUsedTimeMillis: Long = 0
      var note: CharSequence? = null
      var typeDisplayName: CharSequence = ""

      val items = slice.items
      items.forEach {
        if (it.hasHint(Entry.HINT_CREDENTIAL_TYPE_DISPLAY_NAME)) {
          credentialTypeDisplayName = it.text
        } else if (it.hasHint(Entry.HINT_USER_NAME)) {
          userName = it.text
        } else if (it.hasHint(Entry.HINT_PASSKEY_USER_DISPLAY_NAME)) {
          userDisplayName = it.text
        } else if (it.hasHint(Entry.HINT_PROFILE_ICON)) {
          entryIcon = it.icon
        } else if (it.hasHint(Entry.HINT_LAST_USED_TIME_MILLIS)) {
      slice.items.forEach {
        if (it.hasHint(SLICE_HINT_TYPE_DISPLAY_NAME)) {
          typeDisplayName = it.text
        } else if (it.hasHint(SLICE_HINT_USERNAME)) {
          username = it.text
        } else if (it.hasHint(SLICE_HINT_DISPLAYNAME)) {
          displayName = it.text
        } else if (it.hasHint(SLICE_HINT_ICON)) {
          icon = it.icon
        } else if (it.hasHint(SLICE_HINT_PENDING_INTENT)) {
          pendingIntent = it.action
        } else if (it.hasHint(SLICE_HINT_LAST_USED_TIME_MILLIS)) {
          lastUsedTimeMillis = it.long
        } else if (it.hasHint(Entry.HINT_NOTE)) {
          note = it.text
        }
      }

      return CredentialEntryUi(
        credentialType, credentialTypeDisplayName!!, userName!!, userDisplayName, entryIcon,
              slice.spec!!.type, typeDisplayName, username!!, displayName, icon,
              lastUsedTimeMillis, note,
      )
    }
+34 −27
Original line number Diff line number Diff line
@@ -16,8 +16,8 @@

package com.android.credentialmanager.jetpack.provider

import android.app.PendingIntent
import android.app.slice.Slice
import android.credentials.ui.Entry
import android.graphics.drawable.Icon

/**
@@ -35,38 +35,45 @@ class SaveEntryUi(
  val lastUsedTimeMillis: Long?,
) {
  companion object {
    fun fromSlice(slice: Slice): SaveEntryUi {
      var userProviderAccountName: CharSequence? = null
      var credentialTypeIcon: Icon? = null
      var profileIcon: Icon? = null
      var passwordCount: Int? = null
      var passkeyCount: Int? = null
      var totalCredentialCount: Int? = null
      var lastUsedTimeMillis: Long? = null
    const val SLICE_HINT_ACCOUNT_NAME =
            "androidx.credentials.provider.createEntry.SLICE_HINT_USER_PROVIDER_ACCOUNT_NAME"
    const val SLICE_HINT_ICON =
            "androidx.credentials.provider.createEntry.SLICE_HINT_PROFILE_ICON"
    const val SLICE_HINT_CREDENTIAL_COUNT_INFORMATION =
            "androidx.credentials.provider.createEntry.SLICE_HINT_CREDENTIAL_COUNT_INFORMATION"
    const val SLICE_HINT_LAST_USED_TIME_MILLIS =
            "androidx.credentials.provider.createEntry.SLICE_HINT_LAST_USED_TIME_MILLIS"
    const val SLICE_HINT_PENDING_INTENT =
            "androidx.credentials.provider.createEntry.SLICE_HINT_PENDING_INTENT"

    /**
     * Returns an instance of [SaveEntryUi] derived from a [Slice] object.
     *
     * @param slice the [Slice] object constructed through the jetpack library
     */
    @JvmStatic
    fun fromSlice(slice: Slice): SaveEntryUi {
      var accountName: CharSequence? = null
      var icon: Icon? = null
      var pendingIntent: PendingIntent? = null
      var lastUsedTimeMillis: Long = 0

      val items = slice.items
      items.forEach {
        if (it.hasHint(Entry.HINT_USER_PROVIDER_ACCOUNT_NAME)) {
          userProviderAccountName = it.text
        } else if (it.hasHint(Entry.HINT_CREDENTIAL_TYPE_ICON)) {
          credentialTypeIcon = it.icon
        } else if (it.hasHint(Entry.HINT_PROFILE_ICON)) {
          profileIcon = it.icon
        } else if (it.hasHint(Entry.HINT_PASSWORD_COUNT)) {
          passwordCount = it.int
        } else if (it.hasHint(Entry.HINT_PASSKEY_COUNT)) {
          passkeyCount = it.int
        } else if (it.hasHint(Entry.HINT_TOTAL_CREDENTIAL_COUNT)) {
          totalCredentialCount = it.int
        } else if (it.hasHint(Entry.HINT_LAST_USED_TIME_MILLIS)) {
      slice.items.forEach {
        if (it.hasHint(SLICE_HINT_ACCOUNT_NAME)) {
          accountName = it.text
        } else if (it.hasHint(SLICE_HINT_ICON)) {
          icon = it.icon
        } else if (it.hasHint(SLICE_HINT_PENDING_INTENT)) {
          pendingIntent = it.action
        } else if (it.hasHint(SLICE_HINT_LAST_USED_TIME_MILLIS)) {
          lastUsedTimeMillis = it.long
        }
      }
      // TODO: fail NPE more elegantly.

      return SaveEntryUi(
        userProviderAccountName!!, credentialTypeIcon, profileIcon,
        passwordCount, passkeyCount, totalCredentialCount, lastUsedTimeMillis,
              // TODO: Add count parsing
              accountName!!, icon, icon,
              0, 0, 0, lastUsedTimeMillis,
      )
    }
  }