Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | package android.content; |
| 17 | |
| 18 | import android.annotation.NonNull; |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 19 | import android.os.Parcel; |
| 20 | import android.os.Parcelable; |
| 21 | |
| 22 | import com.android.internal.util.Preconditions; |
| 23 | |
| 24 | import java.io.PrintWriter; |
| 25 | |
| 26 | /** |
| 27 | * Identifier for an unique state in the application. |
| 28 | * |
| 29 | * <p>Should be stable across reboots and backup / restore. |
| 30 | * |
| 31 | * <p>For example, a chat app could use the context to resume a conversation between 2 users. |
| 32 | */ |
| 33 | // TODO(b/123577059): make sure this is well documented and understandable |
| 34 | public final class LocusId implements Parcelable { |
| 35 | |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame^] | 36 | private final String mId; |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Default constructor. |
| 40 | */ |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame^] | 41 | public LocusId(@NonNull String id) { |
| 42 | mId = Preconditions.checkNotNull(id); |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /** |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame^] | 46 | * Gets the {@code id} associated with the locus. |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 47 | */ |
| 48 | @NonNull |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame^] | 49 | public String getId() { |
| 50 | return mId; |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public int hashCode() { |
| 55 | final int prime = 31; |
| 56 | int result = 1; |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame^] | 57 | result = prime * result + ((mId == null) ? 0 : mId.hashCode()); |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 58 | return result; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public boolean equals(Object obj) { |
| 63 | if (this == obj) return true; |
| 64 | if (obj == null) return false; |
| 65 | if (getClass() != obj.getClass()) return false; |
| 66 | final LocusId other = (LocusId) obj; |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame^] | 67 | if (mId == null) { |
| 68 | if (other.mId != null) return false; |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 69 | } else { |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame^] | 70 | if (!mId.equals(other.mId)) return false; |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 71 | } |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public String toString() { |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame^] | 77 | return "LocusId[" + getSanitizedId() + "]"; |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /** @hide */ |
| 81 | public void dump(@NonNull PrintWriter pw) { |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame^] | 82 | pw.print("id:"); pw.println(getSanitizedId()); |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame^] | 85 | @NonNull |
| 86 | private String getSanitizedId() { |
| 87 | final int size = mId.length(); |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 88 | return size + "_chars"; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public int describeContents() { |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | @Override |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame^] | 97 | public void writeToParcel(Parcel parcel, int flags) { |
| 98 | parcel.writeString(mId); |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 101 | public static final @android.annotation.NonNull Parcelable.Creator<LocusId> CREATOR = |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 102 | new Parcelable.Creator<LocusId>() { |
| 103 | |
Felipe Leme | ce6877b | 2019-02-28 09:02:26 -0800 | [diff] [blame] | 104 | @NonNull |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 105 | @Override |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame^] | 106 | public LocusId createFromParcel(Parcel parcel) { |
| 107 | return new LocusId(parcel.readString()); |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Felipe Leme | ce6877b | 2019-02-28 09:02:26 -0800 | [diff] [blame] | 110 | @NonNull |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 111 | @Override |
| 112 | public LocusId[] newArray(int size) { |
| 113 | return new LocusId[size]; |
| 114 | } |
| 115 | }; |
| 116 | } |