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. |
Felipe Leme | 85e2b01 | 2019-03-05 09:41:03 -0800 | [diff] [blame^] | 40 | * |
| 41 | * @throws IllegalArgumentException if {@code id} is empty or {@code null}. |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 42 | */ |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame] | 43 | public LocusId(@NonNull String id) { |
Felipe Leme | 85e2b01 | 2019-03-05 09:41:03 -0800 | [diff] [blame^] | 44 | mId = Preconditions.checkStringNotEmpty(id, "id cannot be empty"); |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | /** |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame] | 48 | * Gets the {@code id} associated with the locus. |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 49 | */ |
| 50 | @NonNull |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame] | 51 | public String getId() { |
| 52 | return mId; |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public int hashCode() { |
| 57 | final int prime = 31; |
| 58 | int result = 1; |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame] | 59 | result = prime * result + ((mId == null) ? 0 : mId.hashCode()); |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 60 | return result; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public boolean equals(Object obj) { |
| 65 | if (this == obj) return true; |
| 66 | if (obj == null) return false; |
| 67 | if (getClass() != obj.getClass()) return false; |
| 68 | final LocusId other = (LocusId) obj; |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame] | 69 | if (mId == null) { |
| 70 | if (other.mId != null) return false; |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 71 | } else { |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame] | 72 | if (!mId.equals(other.mId)) return false; |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 73 | } |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public String toString() { |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame] | 79 | return "LocusId[" + getSanitizedId() + "]"; |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /** @hide */ |
| 83 | public void dump(@NonNull PrintWriter pw) { |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame] | 84 | pw.print("id:"); pw.println(getSanitizedId()); |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame] | 87 | @NonNull |
| 88 | private String getSanitizedId() { |
| 89 | final int size = mId.length(); |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 90 | return size + "_chars"; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public int describeContents() { |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | @Override |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame] | 99 | public void writeToParcel(Parcel parcel, int flags) { |
| 100 | parcel.writeString(mId); |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 103 | public static final @android.annotation.NonNull Parcelable.Creator<LocusId> CREATOR = |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 104 | new Parcelable.Creator<LocusId>() { |
| 105 | |
Felipe Leme | ce6877b | 2019-02-28 09:02:26 -0800 | [diff] [blame] | 106 | @NonNull |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 107 | @Override |
Felipe Leme | 7e73575 | 2019-03-01 16:27:24 -0800 | [diff] [blame] | 108 | public LocusId createFromParcel(Parcel parcel) { |
| 109 | return new LocusId(parcel.readString()); |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Felipe Leme | ce6877b | 2019-02-28 09:02:26 -0800 | [diff] [blame] | 112 | @NonNull |
Felipe Leme | 044c63b | 2019-02-12 14:35:20 -0800 | [diff] [blame] | 113 | @Override |
| 114 | public LocusId[] newArray(int size) { |
| 115 | return new LocusId[size]; |
| 116 | } |
| 117 | }; |
| 118 | } |