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

Commit b9875626 authored by Jacky Wang's avatar Jacky Wang
Browse files

[Catalyst] Make BundleProto use Parcel for unknown type

Fix: 418243848
Flag: EXEMPT library
Test: Unit test & manual
Change-Id: I75adf89e449deef9c3fee65a9d3e8c6c269b8361
parent 998008ae
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ package {
    // to get the below license kinds:
    //   SPDX-license-identifier-Apache-2.0
    default_applicable_licenses: ["frameworks_base_license"],
    default_team: "trendy_team_android_settings_app",
}

android_library {
+4 −1
Original line number Diff line number Diff line
@@ -197,8 +197,11 @@ message IntentProto {

// Proto of android.os.Bundle
message BundleProto {
  // Bundle data.
  // Bundle data. Absent when `parcel_bytes` is provided.
  map<string, BundleValue> values = 1;
  // The byte array that this `Bundle` object is marshalled by `Parcel`. It is flexible to support
  // unknown value types (e.g. `Parcelable`) but the data size is way larger than `values`.
  bytes parcel_bytes = 2;

  message BundleValue {
    // Bundle data value for the associated key name.
+28 −3
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.settingslib.graph

import android.annotation.SuppressLint
import android.content.ComponentName
import android.content.Context
import android.content.Intent
@@ -25,6 +26,8 @@ import com.android.settingslib.graph.proto.BundleProto
import com.android.settingslib.graph.proto.BundleProto.BundleValue
import com.android.settingslib.graph.proto.IntentProto
import com.android.settingslib.graph.proto.TextProto
import com.android.settingslib.metadata.marshallParcel
import com.android.settingslib.metadata.unmarshallBundle
import com.google.protobuf.ByteString

fun TextProto.getText(context: Context): String? =
@@ -44,6 +47,7 @@ fun Intent.toProto(): IntentProto = intentProto {
    this@toProto.type?.let { mimeType = it }
}

@SuppressLint("WrongConstant")
fun IntentProto.toIntent(): Intent {
    val intent = Intent()
    if (hasComponent()) intent.component = ComponentName.unflattenFromString(component)
@@ -56,7 +60,25 @@ fun IntentProto.toIntent(): Intent {
    return intent
}

@Suppress("DEPRECATION")
fun Bundle.toProto(): BundleProto = bundleProto {
    fun Any.hasUnknownType(): Boolean =
        when (this@hasUnknownType) {
            is String,
            is ByteArray,
            is Int,
            is Long,
            is Boolean,
            is Double -> false
            is Bundle -> keySet().any { get(it)?.hasUnknownType() == true }
            else -> true
        }

    if (this@toProto.hasUnknownType()) {
        parcelBytes = ByteString.copyFrom(marshallParcel())
        return@bundleProto
    }

    fun toProto(value: Any): BundleValue = bundleValueProto {
        when (value) {
            is String -> stringValue = value
@@ -71,12 +93,14 @@ fun Bundle.toProto(): BundleProto = bundleProto {
    }

    for (key in keySet()) {
        @Suppress("DEPRECATION") get(key)?.let { putValues(key, toProto(it)) }
        get(key)?.let { putValues(key, toProto(it)) }
    }
}

fun BundleProto.toBundle(): Bundle =
    Bundle().apply {
fun BundleProto.toBundle(): Bundle {
    val bytes = parcelBytes.toByteArray()
    if (bytes.isNotEmpty()) return bytes.unmarshallBundle()
    return Bundle().apply {
        for ((key, value) in valuesMap) {
            when {
                value.hasBooleanValue() -> putBoolean(key, value.booleanValue)
@@ -90,3 +114,4 @@ fun BundleProto.toBundle(): Bundle =
            }
        }
    }
}
+21 −0
Original line number Diff line number Diff line
package {
    default_applicable_licenses: ["frameworks_base_license"],
}

android_app {
    name: "SettingsLibGraphShell",
    platform_apis: true,
}

android_robolectric_test {
    name: "SettingsLibGraphTest",
    srcs: ["src/**/*.kt"],
    static_libs: [
        "SettingsLibGraph",
        "androidx.test.ext.junit",
        "truth",
    ],
    instrumentation_for: "SettingsLibGraphShell",
    coverage_libs: ["SettingsLibGraph"],
    upstream: true,
}
+2 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.android.settingslib.graph.test" />
Loading