1 /*
2  * Copyright 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 android.uwb;
18 
19 import android.content.AttributionSource;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 
23 import java.util.Objects;
24 
25 /**
26  * @hide
27  */
28 public final class SessionHandle implements Parcelable  {
29     private final int mId;
30     private final String mPackageName;
31     private final int mUid;
32     private final int mPid;
33 
SessionHandle(int id, AttributionSource attributionSource, int pid)34     public SessionHandle(int id, AttributionSource attributionSource, int pid) {
35         this(id, attributionSource.getPackageName(), attributionSource.getUid(), pid);
36     }
37 
SessionHandle(int id, String packageName, int uid, int pid)38     private SessionHandle(int id, String packageName, int uid, int pid) {
39         mId = id;
40         mPackageName = packageName;
41         mUid = uid;
42         mPid = pid;
43     }
44 
SessionHandle(Parcel in)45     protected SessionHandle(Parcel in) {
46         mId = in.readInt();
47         mPackageName = in.readString();
48         mUid = in.readInt();
49         mPid = in.readInt();
50     }
51 
52     public static final Creator<SessionHandle> CREATOR = new Creator<SessionHandle>() {
53         @Override
54         public SessionHandle createFromParcel(Parcel in) {
55             return new SessionHandle(in);
56         }
57 
58         @Override
59         public SessionHandle[] newArray(int size) {
60             return new SessionHandle[size];
61         }
62     };
63 
getId()64     public int getId() {
65         return mId;
66     }
67 
getPackageName()68     public String getPackageName() {
69         return mPackageName;
70     }
71 
getUid()72     public int getUid() {
73         return mUid;
74     }
75 
getPid()76     public int getPid() {
77         return mPid;
78     }
79 
80     @Override
describeContents()81     public int describeContents() {
82         return 0;
83     }
84 
85     @Override
writeToParcel(Parcel dest, int flags)86     public void writeToParcel(Parcel dest, int flags) {
87         dest.writeInt(mId);
88         dest.writeString(mPackageName);
89         dest.writeInt(mUid);
90         dest.writeInt(mPid);
91     }
92 
93     @Override
equals(Object obj)94     public boolean equals(Object obj) {
95         if (this == obj) {
96             return true;
97         }
98 
99         if (obj instanceof SessionHandle) {
100             SessionHandle other = (SessionHandle) obj;
101             return mId == other.mId
102                     && Objects.equals(mPackageName, other.mPackageName)
103                     && mUid == other.mUid
104                     && mPid == other.mPid;
105         }
106         return false;
107     }
108 
109     @Override
hashCode()110     public int hashCode() {
111         return Objects.hash(mId, mPackageName, mUid, mPid);
112     }
113 
114     @Override
toString()115     public String toString() {
116         return "SessionHandle [id=" + mId + ", package-name: " + mPackageName
117                 + ", uid: " + mUid + ", pid: " + mPid + "]";
118     }
119 }
120