1 /*
2  * Copyright (C) 2020 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.launcher3.logging;
18 
19 import static java.lang.Math.max;
20 import static java.lang.Math.min;
21 
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 
25 import androidx.annotation.NonNull;
26 import androidx.annotation.Nullable;
27 import androidx.annotation.VisibleForTesting;
28 
29 
30 /**
31  * An opaque identifier used to disambiguate which logs refer to a particular instance of some
32  * UI element. Useful when there might be multiple instances simultaneously active.
33  * Obtain from InstanceIdSequence.  Clipped to range [0, INSTANCE_ID_MAX].
34  *
35  * Copy of frameworks/base/core/java/com/android/internal/logging/InstanceId.java.
36  */
37 public final class InstanceId implements Parcelable {
38     // At most 20 bits: ~1m possibilities, ~0.5% probability of collision in 100 values
39     public static final int INSTANCE_ID_MAX = 1 << 20;
40 
41     private final int mId;
InstanceId(int id)42     public InstanceId(int id) {
43         mId = min(max(0, id), INSTANCE_ID_MAX);
44     }
45 
InstanceId(Parcel in)46     private InstanceId(Parcel in) {
47         this(in.readInt());
48     }
49 
getId()50     public int getId() {
51         return mId;
52     }
53 
54     @NonNull
55     @Override
toString()56     public String toString() {
57         return mId + "";
58     }
59 
60     /**
61      * Create a fake instance ID for testing purposes.  Not for production use. See also
62      * InstanceIdSequenceFake, which is a testing replacement for InstanceIdSequence.
63      * @param id The ID you want to assign.
64      * @return new InstanceId.
65      */
66     @VisibleForTesting
fakeInstanceId(int id)67     public static InstanceId fakeInstanceId(int id) {
68         return new InstanceId(id);
69     }
70 
71     @Override
hashCode()72     public int hashCode() {
73         return mId;
74     }
75 
76     @Override
equals(@ullable Object obj)77     public boolean equals(@Nullable Object obj) {
78         if (!(obj instanceof InstanceId)) {
79             return false;
80         }
81         return mId == ((InstanceId) obj).mId;
82     }
83 
84     @Override
describeContents()85     public int describeContents() {
86         return 0;
87     }
88 
89     @Override
writeToParcel(Parcel out, int flags)90     public void writeToParcel(Parcel out, int flags) {
91         out.writeInt(mId);
92     }
93 
94     public static final Parcelable.Creator<InstanceId> CREATOR =
95             new Parcelable.Creator<InstanceId>() {
96                 @Override
97                 public InstanceId createFromParcel(Parcel in) {
98                     return new InstanceId(in);
99                 }
100 
101                 @Override
102                 public InstanceId[] newArray(int size) {
103                     return new InstanceId[size];
104                 }
105             };
106 
107 }
108