diff options
| author | 2025-01-23 15:11:36 +0800 | |
|---|---|---|
| committer | 2025-01-23 21:20:03 +0800 | |
| commit | 76666b308c7f889293ec2f09d6e5bcb53d7dc6e0 (patch) | |
| tree | 5937e06d0fe4040b601745fc254a929a847712e1 | |
| parent | 849d1b8a10e649e3e3cb88717d86ffea7a934b01 (diff) | |
[Catalyst] Support parameterized screen for PreferenceCoordinate
Bug: 388420844
Flag: com.android.settings.flags.catalyst
Test: devtool
Change-Id: Id840df810bce5dd2bdd2bde9330c8fa84cddebac
| -rw-r--r-- | packages/SettingsLib/Metadata/src/com/android/settingslib/metadata/Bundles.kt | 46 | ||||
| -rw-r--r-- | packages/SettingsLib/Metadata/src/com/android/settingslib/metadata/PreferenceCoordinate.kt | 68 |
2 files changed, 109 insertions, 5 deletions
diff --git a/packages/SettingsLib/Metadata/src/com/android/settingslib/metadata/Bundles.kt b/packages/SettingsLib/Metadata/src/com/android/settingslib/metadata/Bundles.kt new file mode 100644 index 000000000000..a63576510aec --- /dev/null +++ b/packages/SettingsLib/Metadata/src/com/android/settingslib/metadata/Bundles.kt @@ -0,0 +1,46 @@ +/* + * 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 +} diff --git a/packages/SettingsLib/Metadata/src/com/android/settingslib/metadata/PreferenceCoordinate.kt b/packages/SettingsLib/Metadata/src/com/android/settingslib/metadata/PreferenceCoordinate.kt index 2dd736ae6083..ac08847b6002 100644 --- a/packages/SettingsLib/Metadata/src/com/android/settingslib/metadata/PreferenceCoordinate.kt +++ b/packages/SettingsLib/Metadata/src/com/android/settingslib/metadata/PreferenceCoordinate.kt @@ -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) + } +} |