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

Commit 76666b30 authored by Jacky Wang's avatar Jacky Wang
Browse files

[Catalyst] Support parameterized screen for PreferenceCoordinate

Bug: 388420844
Flag: com.android.settings.flags.catalyst
Test: devtool
Change-Id: Id840df810bce5dd2bdd2bde9330c8fa84cddebac
parent 849d1b8a
Loading
Loading
Loading
Loading
+46 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.settingslib.metadata

import android.content.Intent
import android.os.Bundle

@Suppress("DEPRECATION")
fun Bundle?.contentEquals(other: Bundle?): Boolean {
    if (this == null) return other == null
    if (other == null) return false
    if (keySet() != other.keySet()) return false
    fun Any?.valueEquals(other: Any?) =
        when (this) {
            is Bundle -> other is Bundle && this.contentEquals(other)
            is Intent -> other is Intent && this.filterEquals(other)
            is BooleanArray -> other is BooleanArray && this contentEquals other
            is ByteArray -> other is ByteArray && this contentEquals other
            is CharArray -> other is CharArray && this contentEquals other
            is DoubleArray -> other is DoubleArray && this contentEquals other
            is FloatArray -> other is FloatArray && this contentEquals other
            is IntArray -> other is IntArray && this contentEquals other
            is LongArray -> other is LongArray && this contentEquals other
            is ShortArray -> other is ShortArray && this contentEquals other
            is Array<*> -> other is Array<*> && this contentDeepEquals other
            else -> this == other
        }
    for (key in keySet()) {
        if (!get(key).valueEquals(other.get(key))) return false
    }
    return true
}
+63 −5
Original line number Diff line number Diff line
@@ -16,26 +16,41 @@

package com.android.settingslib.metadata

import android.os.Bundle
import android.os.Parcel
import android.os.Parcelable

/**
 * Coordinate to locate a preference.
 *
 * Within an app, the preference screen key (unique among screens) plus preference key (unique on
 * the screen) is used to locate a preference.
 * Within an app, the preference screen coordinate (unique among screens) plus preference key
 * (unique on the screen) is used to locate a preference.
 */
data class PreferenceCoordinate(val screenKey: String, val key: String) : Parcelable {
open class PreferenceCoordinate : PreferenceScreenCoordinate {
    val key: String

    constructor(parcel: Parcel) : this(parcel.readString()!!, parcel.readString()!!)
    constructor(screenKey: String, key: String) : this(screenKey, null, key)

    constructor(screenKey: String, args: Bundle?, key: String) : super(screenKey, args) {
        this.key = key
    }

    constructor(parcel: Parcel) : super(parcel) {
        this.key = parcel.readString()!!
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(screenKey)
        super.writeToParcel(parcel, flags)
        parcel.writeString(key)
    }

    override fun describeContents() = 0

    override fun equals(other: Any?) =
        super.equals(other) && key == (other as PreferenceCoordinate).key

    override fun hashCode() = super.hashCode() xor key.hashCode()

    companion object CREATOR : Parcelable.Creator<PreferenceCoordinate> {

        override fun createFromParcel(parcel: Parcel) = PreferenceCoordinate(parcel)
@@ -43,3 +58,46 @@ data class PreferenceCoordinate(val screenKey: String, val key: String) : Parcel
        override fun newArray(size: Int) = arrayOfNulls<PreferenceCoordinate>(size)
    }
}

/** Coordinate to locate a preference screen. */
open class PreferenceScreenCoordinate : Parcelable {
    /** Unique preference screen key. */
    val screenKey: String

    /** Arguments to create parameterized preference screen. */
    val args: Bundle?

    constructor(screenKey: String, args: Bundle?) {
        this.screenKey = screenKey
        this.args = args
    }

    constructor(parcel: Parcel) {
        screenKey = parcel.readString()!!
        args = parcel.readBundle(javaClass.classLoader)
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(screenKey)
        parcel.writeBundle(args)
    }

    override fun describeContents() = 0

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false
        other as PreferenceScreenCoordinate
        return screenKey == other.screenKey && args.contentEquals(other.args)
    }

    // "args" is not included intentionally, otherwise we need to take care of array, etc.
    override fun hashCode() = screenKey.hashCode()

    companion object CREATOR : Parcelable.Creator<PreferenceScreenCoordinate> {

        override fun createFromParcel(parcel: Parcel) = PreferenceScreenCoordinate(parcel)

        override fun newArray(size: Int) = arrayOfNulls<PreferenceScreenCoordinate>(size)
    }
}