1 /*
2  * Copyright (C) 2023 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.car.carlauncher;
18 
19 import android.os.Parcel;
20 
21 import androidx.annotation.Nullable;
22 
23 /**
24  * AppItem of an app including the package name, the display name and the app's metadata.
25  */
26 public class AppItem extends LauncherItem {
27     private AppMetaData mAppMetaData;
28 
AppItem(String packageName, String className, String displayName, @Nullable AppMetaData appMetaData)29     public AppItem(String packageName, String className, String displayName,
30             @Nullable AppMetaData appMetaData) {
31         super(packageName, className, displayName);
32         mAppMetaData = appMetaData;
33     }
34 
AppItem(AppMetaData appMetaData)35     public AppItem(AppMetaData appMetaData) {
36         this(appMetaData.getPackageName(), appMetaData.getClassName(),
37                 appMetaData.getDisplayName(), appMetaData);
38     }
39 
40     public static final Creator<LauncherItem> CREATOR = new Creator<LauncherItem>() {
41         @Override
42         public AppItem createFromParcel(Parcel in) {
43             return new AppItem(in);
44         }
45 
46         @Override
47         public LauncherItem[] newArray(int size) {
48             return new LauncherItem[size];
49         }
50     };
51 
AppItem(Parcel in)52     protected AppItem(Parcel in) {
53         super(in);
54     }
55 
56     @Override
describeContents()57     public int describeContents() {
58         return 0;
59     }
60 
getAppMetaData()61     public AppMetaData getAppMetaData() {
62         return mAppMetaData;
63     }
64 
65     @Override
equals(Object o)66     public boolean equals(Object o) {
67         if (!(o instanceof LauncherItem)) {
68             return false;
69         } else {
70             return areContentsTheSame((AppItem) o);
71         }
72     }
73 
74     @Override
hashCode()75     public int hashCode() {
76         return mAppMetaData == null ? 0 : mAppMetaData.getComponentName().hashCode();
77     }
78 
79     @Override
areContentsTheSame(LauncherItem launcherItem)80     public boolean areContentsTheSame(LauncherItem launcherItem) {
81         if (launcherItem instanceof AppItem) {
82             AppItem appItem = (AppItem) launcherItem;
83             if (mAppMetaData == null) {
84                 return appItem.mAppMetaData == null;
85             }
86             return mAppMetaData.equals(appItem.mAppMetaData);
87         }
88         return false;
89     }
90 }
91