blob: 3d1ddc3ca77becb112bbca7cfa9bcbf13135b557 [file] [log] [blame]
Felipe Leme044c63b2019-02-12 14:35:20 -08001/*
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 */
16package android.content;
17
18import android.annotation.NonNull;
Felipe Leme044c63b2019-02-12 14:35:20 -080019import android.os.Parcel;
20import android.os.Parcelable;
21
22import com.android.internal.util.Preconditions;
23
24import 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
34public final class LocusId implements Parcelable {
35
Felipe Leme7e735752019-03-01 16:27:24 -080036 private final String mId;
Felipe Leme044c63b2019-02-12 14:35:20 -080037
38 /**
39 * Default constructor.
40 */
Felipe Leme7e735752019-03-01 16:27:24 -080041 public LocusId(@NonNull String id) {
42 mId = Preconditions.checkNotNull(id);
Felipe Leme044c63b2019-02-12 14:35:20 -080043 }
44
45 /**
Felipe Leme7e735752019-03-01 16:27:24 -080046 * Gets the {@code id} associated with the locus.
Felipe Leme044c63b2019-02-12 14:35:20 -080047 */
48 @NonNull
Felipe Leme7e735752019-03-01 16:27:24 -080049 public String getId() {
50 return mId;
Felipe Leme044c63b2019-02-12 14:35:20 -080051 }
52
53 @Override
54 public int hashCode() {
55 final int prime = 31;
56 int result = 1;
Felipe Leme7e735752019-03-01 16:27:24 -080057 result = prime * result + ((mId == null) ? 0 : mId.hashCode());
Felipe Leme044c63b2019-02-12 14:35:20 -080058 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 Leme7e735752019-03-01 16:27:24 -080067 if (mId == null) {
68 if (other.mId != null) return false;
Felipe Leme044c63b2019-02-12 14:35:20 -080069 } else {
Felipe Leme7e735752019-03-01 16:27:24 -080070 if (!mId.equals(other.mId)) return false;
Felipe Leme044c63b2019-02-12 14:35:20 -080071 }
72 return true;
73 }
74
75 @Override
76 public String toString() {
Felipe Leme7e735752019-03-01 16:27:24 -080077 return "LocusId[" + getSanitizedId() + "]";
Felipe Leme044c63b2019-02-12 14:35:20 -080078 }
79
80 /** @hide */
81 public void dump(@NonNull PrintWriter pw) {
Felipe Leme7e735752019-03-01 16:27:24 -080082 pw.print("id:"); pw.println(getSanitizedId());
Felipe Leme044c63b2019-02-12 14:35:20 -080083 }
84
Felipe Leme7e735752019-03-01 16:27:24 -080085 @NonNull
86 private String getSanitizedId() {
87 final int size = mId.length();
Felipe Leme044c63b2019-02-12 14:35:20 -080088 return size + "_chars";
89 }
90
91 @Override
92 public int describeContents() {
93 return 0;
94 }
95
96 @Override
Felipe Leme7e735752019-03-01 16:27:24 -080097 public void writeToParcel(Parcel parcel, int flags) {
98 parcel.writeString(mId);
Felipe Leme044c63b2019-02-12 14:35:20 -080099 }
100
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700101 public static final @android.annotation.NonNull Parcelable.Creator<LocusId> CREATOR =
Felipe Leme044c63b2019-02-12 14:35:20 -0800102 new Parcelable.Creator<LocusId>() {
103
Felipe Lemece6877b2019-02-28 09:02:26 -0800104 @NonNull
Felipe Leme044c63b2019-02-12 14:35:20 -0800105 @Override
Felipe Leme7e735752019-03-01 16:27:24 -0800106 public LocusId createFromParcel(Parcel parcel) {
107 return new LocusId(parcel.readString());
Felipe Leme044c63b2019-02-12 14:35:20 -0800108 }
109
Felipe Lemece6877b2019-02-28 09:02:26 -0800110 @NonNull
Felipe Leme044c63b2019-02-12 14:35:20 -0800111 @Override
112 public LocusId[] newArray(int size) {
113 return new LocusId[size];
114 }
115 };
116}