• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     static final int INSTANCE_ID_MAX = 1 << 20;
40 
41     private final int mId;
InstanceId(int id)42     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 
50     @VisibleForTesting
getId()51     public int getId() {
52         return mId;
53     }
54 
55     @NonNull
56     @Override
toString()57     public String toString() {
58         return mId + "";
59     }
60 
61     /**
62      * Create a fake instance ID for testing purposes.  Not for production use. See also
63      * InstanceIdSequenceFake, which is a testing replacement for InstanceIdSequence.
64      * @param id The ID you want to assign.
65      * @return new InstanceId.
66      */
67     @VisibleForTesting
fakeInstanceId(int id)68     public static InstanceId fakeInstanceId(int id) {
69         return new InstanceId(id);
70     }
71 
72     @Override
hashCode()73     public int hashCode() {
74         return mId;
75     }
76 
77     @Override
equals(@ullable Object obj)78     public boolean equals(@Nullable Object obj) {
79         if (!(obj instanceof InstanceId)) {
80             return false;
81         }
82         return mId == ((InstanceId) obj).mId;
83     }
84 
85     @Override
describeContents()86     public int describeContents() {
87         return 0;
88     }
89 
90     @Override
writeToParcel(Parcel out, int flags)91     public void writeToParcel(Parcel out, int flags) {
92         out.writeInt(mId);
93     }
94 
95     public static final Parcelable.Creator<InstanceId> CREATOR =
96             new Parcelable.Creator<InstanceId>() {
97                 @Override
98                 public InstanceId createFromParcel(Parcel in) {
99                     return new InstanceId(in);
100                 }
101 
102                 @Override
103                 public InstanceId[] newArray(int size) {
104                     return new InstanceId[size];
105                 }
106             };
107 
108 }
109