1 /* 2 * Copyright (C) 2025 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settingslib.metadata 18 19 import android.os.Bundle 20 import android.os.Parcel 21 import android.os.Parcelable 22 23 /** 24 * Coordinate to locate a preference. 25 * 26 * Within an app, the preference screen coordinate (unique among screens) plus preference key 27 * (unique on the screen) is used to locate a preference. 28 */ 29 open class PreferenceCoordinate : PreferenceScreenCoordinate { 30 val key: String 31 32 constructor(screenKey: String, key: String) : this(screenKey, null, key) 33 34 constructor(screenKey: String, args: Bundle?, key: String) : super(screenKey, args) { 35 this.key = key 36 } 37 38 constructor(parcel: Parcel) : super(parcel) { 39 this.key = parcel.readString()!! 40 } 41 writeToParcelnull42 override fun writeToParcel(parcel: Parcel, flags: Int) { 43 super.writeToParcel(parcel, flags) 44 parcel.writeString(key) 45 } 46 describeContentsnull47 override fun describeContents() = 0 48 49 override fun equals(other: Any?) = 50 super.equals(other) && key == (other as PreferenceCoordinate).key 51 52 override fun hashCode() = super.hashCode() xor key.hashCode() 53 54 companion object CREATOR : Parcelable.Creator<PreferenceCoordinate> { 55 56 override fun createFromParcel(parcel: Parcel) = PreferenceCoordinate(parcel) 57 58 override fun newArray(size: Int) = arrayOfNulls<PreferenceCoordinate>(size) 59 } 60 } 61 62 /** Coordinate to locate a preference screen. */ 63 open class PreferenceScreenCoordinate : Parcelable { 64 /** Unique preference screen key. */ 65 val screenKey: String 66 67 /** Arguments to create parameterized preference screen. */ 68 val args: Bundle? 69 70 constructor(screenKey: String, args: Bundle?) { 71 this.screenKey = screenKey 72 this.args = args 73 } 74 75 constructor(parcel: Parcel) { 76 screenKey = parcel.readString()!! 77 args = parcel.readBundle(javaClass.classLoader) 78 } 79 writeToParcelnull80 override fun writeToParcel(parcel: Parcel, flags: Int) { 81 parcel.writeString(screenKey) 82 parcel.writeBundle(args) 83 } 84 describeContentsnull85 override fun describeContents() = 0 86 87 override fun equals(other: Any?): Boolean { 88 if (this === other) return true 89 if (javaClass != other?.javaClass) return false 90 other as PreferenceScreenCoordinate 91 return screenKey == other.screenKey && args.contentEquals(other.args) 92 } 93 94 // "args" is not included intentionally, otherwise we need to take care of array, etc. hashCodenull95 override fun hashCode() = screenKey.hashCode() 96 97 companion object CREATOR : Parcelable.Creator<PreferenceScreenCoordinate> { 98 99 override fun createFromParcel(parcel: Parcel) = PreferenceScreenCoordinate(parcel) 100 101 override fun newArray(size: Int) = arrayOfNulls<PreferenceScreenCoordinate>(size) 102 } 103 } 104