Sunny Goyal | 54e9134 | 2018-11-14 11:59:02 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package android.app.prediction; |
| 17 | |
| 18 | import android.annotation.NonNull; |
| 19 | import android.annotation.Nullable; |
| 20 | import android.annotation.SystemApi; |
Winson Chung | 5208cbe | 2019-01-11 20:51:46 -0800 | [diff] [blame] | 21 | import android.annotation.TestApi; |
Sunny Goyal | 54e9134 | 2018-11-14 11:59:02 -0800 | [diff] [blame] | 22 | import android.content.pm.ShortcutInfo; |
| 23 | import android.os.Parcel; |
| 24 | import android.os.Parcelable; |
| 25 | import android.os.UserHandle; |
| 26 | |
| 27 | import com.android.internal.util.Preconditions; |
| 28 | |
| 29 | /** |
| 30 | * A representation of a launchable target. |
| 31 | * @hide |
| 32 | */ |
| 33 | @SystemApi |
Winson Chung | 5208cbe | 2019-01-11 20:51:46 -0800 | [diff] [blame] | 34 | @TestApi |
Sunny Goyal | 54e9134 | 2018-11-14 11:59:02 -0800 | [diff] [blame] | 35 | public 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 Chung | 5208cbe | 2019-01-11 20:51:46 -0800 | [diff] [blame] | 47 | * TODO(b/123591863): Add java docs |
| 48 | * |
Sunny Goyal | 54e9134 | 2018-11-14 11:59:02 -0800 | [diff] [blame] | 49 | * @hide |
| 50 | */ |
Winson Chung | 5208cbe | 2019-01-11 20:51:46 -0800 | [diff] [blame] | 51 | @SystemApi |
| 52 | @TestApi |
Sunny Goyal | 54e9134 | 2018-11-14 11:59:02 -0800 | [diff] [blame] | 53 | 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 Chung | 5208cbe | 2019-01-11 20:51:46 -0800 | [diff] [blame] | 64 | * TODO(b/123591863): Add java docs |
| 65 | * |
Sunny Goyal | 54e9134 | 2018-11-14 11:59:02 -0800 | [diff] [blame] | 66 | * @hide |
| 67 | */ |
Winson Chung | 5208cbe | 2019-01-11 20:51:46 -0800 | [diff] [blame] | 68 | @SystemApi |
| 69 | public AppTarget(@NonNull AppTargetId id, @NonNull ShortcutInfo shortcutInfo, |
| 70 | @Nullable String className) { |
Sunny Goyal | 54e9134 | 2018-11-14 11:59:02 -0800 | [diff] [blame] | 71 | mId = id; |
| 72 | mShortcutInfo = Preconditions.checkNotNull(shortcutInfo); |
| 73 | |
| 74 | mPackageName = mShortcutInfo.getPackage(); |
| 75 | mUser = mShortcutInfo.getUserHandle(); |
Winson Chung | 5208cbe | 2019-01-11 20:51:46 -0800 | [diff] [blame] | 76 | mClassName = className; |
Sunny Goyal | 54e9134 | 2018-11-14 11:59:02 -0800 | [diff] [blame] | 77 | } |
| 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 Goyal | 54e9134 | 2018-11-14 11:59:02 -0800 | [diff] [blame] | 84 | mUser = UserHandle.of(parcel.readInt()); |
| 85 | } else { |
| 86 | mPackageName = mShortcutInfo.getPackage(); |
| 87 | mUser = mShortcutInfo.getUserHandle(); |
Sunny Goyal | 54e9134 | 2018-11-14 11:59:02 -0800 | [diff] [blame] | 88 | } |
Winson Chung | 5208cbe | 2019-01-11 20:51:46 -0800 | [diff] [blame] | 89 | mClassName = parcel.readString(); |
Sunny Goyal | 54e9134 | 2018-11-14 11:59:02 -0800 | [diff] [blame] | 90 | 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 Chung | 5208cbe | 2019-01-11 20:51:46 -0800 | [diff] [blame] | 141 | /** |
| 142 | * Returns the rank for the target. |
| 143 | */ |
Sunny Goyal | 54e9134 | 2018-11-14 11:59:02 -0800 | [diff] [blame] | 144 | public int getRank() { |
| 145 | return mRank; |
| 146 | } |
| 147 | |
| 148 | @Override |
Winson Chung | 5208cbe | 2019-01-11 20:51:46 -0800 | [diff] [blame] | 149 | 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 Goyal | 54e9134 | 2018-11-14 11:59:02 -0800 | [diff] [blame] | 167 | 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 Goyal | 54e9134 | 2018-11-14 11:59:02 -0800 | [diff] [blame] | 177 | dest.writeInt(mUser.getIdentifier()); |
| 178 | } |
Winson Chung | 5208cbe | 2019-01-11 20:51:46 -0800 | [diff] [blame] | 179 | dest.writeString(mClassName); |
Sunny Goyal | 54e9134 | 2018-11-14 11:59:02 -0800 | [diff] [blame] | 180 | 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 | } |