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