Skip to content

Commit

Permalink
[GH] Navigation: Allow parsing a NavType#ReferenceType from String
Browse files Browse the repository at this point in the history
If Bundle contents can be extracted as a Reference, allow parsing the Bundle contents as a Reference.

When navigating via deeplink URI, the user is required to string-ify navigation arguments in order to append them to the URI as query parameters. This PR supports retrieving a Reference type from a Bundle

## Proposed Changes
  - replace `NavType#ReferenceType::parseValue`'s method functionality with `Integer.parseInt`

## Testing
Updated `NavTypeTest` to validate that reference types can be inserted and retrieved from a Bundle.
Test: /gradlew test connectedCheck

## Issues Fixed

Fixes: b/179166693

This is an imported pull request from #127.

Resolves #127
Github-Pr-Head-Sha: 78179a9
GitOrigin-RevId: edf5410
Change-Id: I80f4d03a07b04756cd7ea1f3cf43688a115116e4
  • Loading branch information
JvmName authored and Copybara-Service committed Feb 9, 2021
1 parent 392c6c2 commit 15eea2e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package androidx.navigation
import android.content.pm.ActivityInfo
import android.graphics.Bitmap
import android.os.Bundle
import androidx.navigation.common.test.R
import androidx.test.filters.SmallTest
import com.google.common.truth.Truth.assertThat
import org.junit.Test
Expand All @@ -42,6 +43,8 @@ class NavTypeTest {
private val booleans = booleanArrayOf(b, false)
private val s = "a_string"
private val strings = arrayOf("aa", "bb")
private val reference = R.id.nav_id_reference
private val referenceHex = "0x" + R.id.nav_id_reference.toString(16)
private val parcelable = ActivityInfo()
private val parcelables = arrayOf(parcelable)
private val en = Bitmap.Config.ALPHA_8
Expand Down Expand Up @@ -78,6 +81,8 @@ class NavTypeTest {
.isEqualTo(NavType.StringType)
assertThat(NavType.fromArgType("string[]", null))
.isEqualTo(NavType.StringArrayType)
assertThat(NavType.fromArgType("reference", null))
.isEqualTo(NavType.ReferenceType)
assertThat(NavType.fromArgType("android.content.pm.ActivityInfo", null))
.isEqualTo(parcelableNavType)
assertThat(NavType.fromArgType("android.content.pm.ActivityInfo[]", null))
Expand All @@ -98,6 +103,8 @@ class NavTypeTest {
.isEqualTo(NavType.StringType)
assertThat(NavType.inferFromValue("123"))
.isEqualTo(NavType.IntType)
assertThat(NavType.inferFromValue("0xFF"))
.isEqualTo(NavType.IntType)
assertThat(NavType.inferFromValue("123L"))
.isEqualTo(NavType.LongType)
assertThat(NavType.inferFromValue("1.5"))
Expand Down Expand Up @@ -196,6 +203,11 @@ class NavTypeTest {
.isEqualTo(strings)
bundle.clear()

NavType.ReferenceType.put(bundle, key, reference)
assertThat(NavType.ReferenceType.get(bundle, key))
.isEqualTo(reference)
bundle.clear()

parcelableNavType.put(bundle, key, parcelable)
assertThat(parcelableNavType.get(bundle, key))
.isEqualTo(parcelable)
Expand All @@ -221,4 +233,13 @@ class NavTypeTest {
.isEqualTo(serializables)
bundle.clear()
}

@Test
fun parseValueWithHex() {
assertThat(NavType.IntType.parseValue(referenceHex))
.isEqualTo(reference)

assertThat(NavType.ReferenceType.parseValue(referenceHex))
.isEqualTo(reference)
}
}
18 changes: 18 additions & 0 deletions navigation/navigation-common/src/androidTest/res/values/ids.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?><!--
Copyright 2021 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<resources>
<item type="id" name="nav_id_reference" />
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,11 @@ public Integer get(@NonNull Bundle bundle, @NonNull String key) {
@NonNull
@Override
public Integer parseValue(@NonNull String value) {
throw new UnsupportedOperationException(
"References don't support parsing string values.");
if (value.startsWith("0x")) {
return Integer.parseInt(value.substring(2), 16);
} else {
return Integer.parseInt(value);
}
}

@NonNull
Expand Down

0 comments on commit 15eea2e

Please sign in to comment.