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

Commit 6afa25df authored by Chaohui Wang's avatar Chaohui Wang
Browse files

Encode SpaSearchLandingKey with Base64

To ensure the landing key is the same as the indexing key.

Bug: 358238959
Flag: EXEMPT bug fix
Test: manual - search roaming
Test: unit test
Change-Id: I26ce712feaa83e9e70a3eb39039e5c3527f24cca
parent eb1fc841
Loading
Loading
Loading
Loading
+3 −11
Original line number Diff line number Diff line
@@ -27,9 +27,6 @@ import com.android.settings.core.SubSettingLauncher
import com.android.settings.overlay.FeatureFactory.Companion.featureFactory
import com.android.settings.password.PasswordUtils
import com.android.settings.spa.SpaDestination
import com.android.settings.spa.SpaSearchLanding.SpaSearchLandingKey
import com.google.protobuf.ByteString
import com.google.protobuf.InvalidProtocolBufferException

class SpaSearchLandingActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
@@ -48,22 +45,17 @@ class SpaSearchLandingActivity : Activity() {
    companion object {
        @VisibleForTesting
        fun tryLaunch(context: Context, keyString: String) {
            val key =
                try {
                    SpaSearchLandingKey.parseFrom(ByteString.copyFromUtf8(keyString))
                } catch (e: InvalidProtocolBufferException) {
                    Log.w(TAG, "arg key ($keyString) invalid", e)
                    return
                }

            val key = decodeToSpaSearchLandingKey(keyString) ?: return
            if (key.hasSpaPage()) {
                val destination = key.spaPage.destination
                if (destination.isNotEmpty()) {
                    Log.d(TAG, "Launch SPA search result: ${key.spaPage}")
                    SpaDestination(destination = destination, highlightMenuKey = null)
                        .startFromExportedActivity(context)
                }
            }
            if (key.hasFragment()) {
                Log.d(TAG, "Launch fragment search result: ${key.fragment}")
                val arguments =
                    Bundle().apply {
                        key.fragment.argumentsMap.forEach { (k, v) ->
+34 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.
 */

package com.android.settings.spa.search

import android.util.Base64
import android.util.Log
import com.android.settings.spa.SpaSearchLanding.SpaSearchLandingKey

private const val TAG = "SpaSearchLandingKeyExt"

fun SpaSearchLandingKey.encodeToString(): String =
    Base64.encodeToString(toByteArray(), Base64.DEFAULT)

fun decodeToSpaSearchLandingKey(input: String): SpaSearchLandingKey? =
    try {
        SpaSearchLandingKey.parseFrom(Base64.decode(input, Base64.DEFAULT))
    } catch (e: Exception) {
        Log.w(TAG, "SpaSearchLandingKey ($input) invalid", e)
        null
    }
+1 −1
Original line number Diff line number Diff line
@@ -96,7 +96,7 @@ class SpaSearchRepository(
            keywords: String? = null,
        ) =
            SearchIndexableRaw(context).apply {
                key = spaSearchLandingKey.toByteString().toStringUtf8()
                key = spaSearchLandingKey.encodeToString()
                title = itemTitle
                this.keywords = keywords
                intentAction = SEARCH_LANDING_ACTION
+2 −2
Original line number Diff line number Diff line
@@ -30,8 +30,8 @@ import com.android.settings.spa.SpaSearchLanding.BundleValue
import com.android.settings.spa.SpaSearchLanding.SpaSearchLandingFragment
import com.android.settings.spa.SpaSearchLanding.SpaSearchLandingKey
import com.android.settings.spa.search.SpaSearchLandingActivity
import com.android.settings.spa.search.decodeToSpaSearchLandingKey
import com.google.common.truth.Truth.assertThat
import com.google.protobuf.ByteString
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
@@ -104,7 +104,7 @@ class MobileNetworkSettingsSearchIndexTest {
            searchIndexableData.searchIndexProvider.getDynamicRawDataToIndex(context, true)
        assertThat(dynamicRawDataToIndex).hasSize(1)
        val rawData = dynamicRawDataToIndex[0]
        val key = SpaSearchLandingKey.parseFrom(ByteString.copyFromUtf8(rawData.key))
        val key = decodeToSpaSearchLandingKey(rawData.key)
        assertThat(key)
            .isEqualTo(
                SpaSearchLandingKey.newBuilder()
+2 −2
Original line number Diff line number Diff line
@@ -52,7 +52,7 @@ class SpaSearchLandingActivityTest {
                .setSpaPage(SpaSearchLandingSpaPage.newBuilder().setDestination(DESTINATION))
                .build()

        SpaSearchLandingActivity.tryLaunch(context, key.toByteString().toStringUtf8())
        SpaSearchLandingActivity.tryLaunch(context, key.encodeToString())

        verify(context).startActivity(argThat { getStringExtra(KEY_DESTINATION) == DESTINATION })
    }
@@ -70,7 +70,7 @@ class SpaSearchLandingActivityTest {
                            BundleValue.newBuilder().setIntValue(ARGUMENT_VALUE).build()))
                .build()

        SpaSearchLandingActivity.tryLaunch(context, key.toByteString().toStringUtf8())
        SpaSearchLandingActivity.tryLaunch(context, key.encodeToString())

        val intent = argumentCaptor<Intent> { verify(context).startActivity(capture()) }.firstValue
        assertThat(intent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT))
Loading