blob: b924cec63823a1480c4a07ab287ea27990c65dfd [file] [log] [blame]
Sunny Goyal54e91342018-11-14 11:59:02 -08001/*
2 * Copyright (C) 2018 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.app.prediction;
17
18import android.annotation.NonNull;
19import android.annotation.Nullable;
20import android.annotation.SystemApi;
Winson Chung5208cbe2019-01-11 20:51:46 -080021import android.annotation.TestApi;
Sunny Goyal54e91342018-11-14 11:59:02 -080022import android.content.pm.ShortcutInfo;
23import android.os.Parcel;
24import android.os.Parcelable;
25import android.os.UserHandle;
26
27import com.android.internal.util.Preconditions;
28
29/**
30 * A representation of a launchable target.
31 * @hide
32 */
33@SystemApi
Winson Chung5208cbe2019-01-11 20:51:46 -080034@TestApi
Sunny Goyal54e91342018-11-14 11:59:02 -080035public final class AppTarget implements Parcelable {
36
37 private final AppTargetId mId;
38 private final String mPackageName;
39 private final String mClassName;
40 private final UserHandle mUser;
41
42 private final ShortcutInfo mShortcutInfo;
43
44 private int mRank;
45
46 /**
Winson Chung5208cbe2019-01-11 20:51:46 -080047 * TODO(b/123591863): Add java docs
48 *
Sunny Goyal54e91342018-11-14 11:59:02 -080049 * @hide
50 */
Winson Chung5208cbe2019-01-11 20:51:46 -080051 @SystemApi
52 @TestApi
Sunny Goyal54e91342018-11-14 11:59:02 -080053 public AppTarget(@NonNull AppTargetId id, @NonNull String packageName,
54 @Nullable String className, @NonNull UserHandle user) {
55 mId = id;
56 mShortcutInfo = null;
57
58 mPackageName = Preconditions.checkNotNull(packageName);
59 mClassName = className;
60 mUser = Preconditions.checkNotNull(user);
61 }
62
63 /**
Winson Chung5208cbe2019-01-11 20:51:46 -080064 * TODO(b/123591863): Add java docs
65 *
Sunny Goyal54e91342018-11-14 11:59:02 -080066 * @hide
67 */
Winson Chung5208cbe2019-01-11 20:51:46 -080068 @SystemApi
69 public AppTarget(@NonNull AppTargetId id, @NonNull ShortcutInfo shortcutInfo,
70 @Nullable String className) {
Sunny Goyal54e91342018-11-14 11:59:02 -080071 mId = id;
72 mShortcutInfo = Preconditions.checkNotNull(shortcutInfo);
73
74 mPackageName = mShortcutInfo.getPackage();
75 mUser = mShortcutInfo.getUserHandle();
Winson Chung5208cbe2019-01-11 20:51:46 -080076 mClassName = className;
Sunny Goyal54e91342018-11-14 11:59:02 -080077 }
78
79 private AppTarget(Parcel parcel) {
80 mId = parcel.readTypedObject(AppTargetId.CREATOR);
81 mShortcutInfo = parcel.readTypedObject(ShortcutInfo.CREATOR);
82 if (mShortcutInfo == null) {
83 mPackageName = parcel.readString();
Sunny Goyal54e91342018-11-14 11:59:02 -080084 mUser = UserHandle.of(parcel.readInt());
85 } else {
86 mPackageName = mShortcutInfo.getPackage();
87 mUser = mShortcutInfo.getUserHandle();
Sunny Goyal54e91342018-11-14 11:59:02 -080088 }
Winson Chung5208cbe2019-01-11 20:51:46 -080089 mClassName = parcel.readString();
Sunny Goyal54e91342018-11-14 11:59:02 -080090 mRank = parcel.readInt();
91 }
92
93 /**
94 * Returns the target id.
95 */
96 @NonNull
97 public AppTargetId getId() {
98 return mId;
99 }
100
101 /**
102 * Returns the class name for the app target.
103 */
104 @Nullable
105 public String getClassName() {
106 return mClassName;
107 }
108
109 /**
110 * Returns the package name for the app target.
111 */
112 @NonNull
113 public String getPackageName() {
114 return mPackageName;
115 }
116
117 /**
118 * Returns the user for the app target.
119 */
120 @NonNull
121 public UserHandle getUser() {
122 return mUser;
123 }
124
125 /**
126 * Returns the shortcut info for the target.
127 */
128 @Nullable
129 public ShortcutInfo getShortcutInfo() {
130 return mShortcutInfo;
131 }
132
133 /**
134 * Sets the rank of the for the target.
135 * @hide
136 */
137 public void setRank(int rank) {
138 mRank = rank;
139 }
140
Winson Chung5208cbe2019-01-11 20:51:46 -0800141 /**
142 * Returns the rank for the target.
143 */
Sunny Goyal54e91342018-11-14 11:59:02 -0800144 public int getRank() {
145 return mRank;
146 }
147
148 @Override
Winson Chung5208cbe2019-01-11 20:51:46 -0800149 public boolean equals(Object o) {
150 if (!getClass().equals(o != null ? o.getClass() : null)) return false;
151
152 AppTarget other = (AppTarget) o;
153 boolean sameClassName = (mClassName == null && other.mClassName == null)
154 || (mClassName != null && mClassName.equals(other.mClassName));
155 boolean sameShortcutInfo = (mShortcutInfo == null && other.mShortcutInfo == null)
156 || (mShortcutInfo != null && other.mShortcutInfo != null
157 && mShortcutInfo.getId() == other.mShortcutInfo.getId());
158 return mId.equals(other.mId)
159 && mPackageName.equals(other.mPackageName)
160 && sameClassName
161 && mUser.equals(other.mUser)
162 && sameShortcutInfo
163 && mRank == other.mRank;
164 }
165
166 @Override
Sunny Goyal54e91342018-11-14 11:59:02 -0800167 public int describeContents() {
168 return 0;
169 }
170
171 @Override
172 public void writeToParcel(Parcel dest, int flags) {
173 dest.writeTypedObject(mId, flags);
174 dest.writeTypedObject(mShortcutInfo, flags);
175 if (mShortcutInfo == null) {
176 dest.writeString(mPackageName);
Sunny Goyal54e91342018-11-14 11:59:02 -0800177 dest.writeInt(mUser.getIdentifier());
178 }
Winson Chung5208cbe2019-01-11 20:51:46 -0800179 dest.writeString(mClassName);
Sunny Goyal54e91342018-11-14 11:59:02 -0800180 dest.writeInt(mRank);
181 }
182
183 /**
184 * @see Parcelable.Creator
185 */
186 public static final Parcelable.Creator<AppTarget> CREATOR =
187 new Parcelable.Creator<AppTarget>() {
188 public AppTarget createFromParcel(Parcel parcel) {
189 return new AppTarget(parcel);
190 }
191
192 public AppTarget[] newArray(int size) {
193 return new AppTarget[size];
194 }
195 };
196}