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.service.notification;
17 
18 import android.annotation.NonNull;
19 import android.annotation.Nullable;
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 NotifyingApp implements Parcelable, Comparable<NotifyingApp> {
29 
30     private int mUserId;
31     private String mPkg;
32     private long mLastNotified;
33 
NotifyingApp()34     public NotifyingApp() {}
35 
NotifyingApp(Parcel in)36     protected NotifyingApp(Parcel in) {
37         mUserId = in.readInt();
38         mPkg = in.readString();
39         mLastNotified = in.readLong();
40     }
41 
getUserId()42     public int getUserId() {
43         return mUserId;
44     }
45 
46     /**
47      * Sets the userid of the package that sent the notification. Returns self.
48      */
setUserId(int mUserId)49     public NotifyingApp setUserId(int mUserId) {
50         this.mUserId = mUserId;
51         return this;
52     }
53 
getPackage()54     public String getPackage() {
55         return mPkg;
56     }
57 
58     /**
59      * Sets the package that sent the notification. Returns self.
60      */
setPackage(@onNull String mPkg)61     public NotifyingApp setPackage(@NonNull String mPkg) {
62         this.mPkg = mPkg;
63         return this;
64     }
65 
getLastNotified()66     public long getLastNotified() {
67         return mLastNotified;
68     }
69 
70     /**
71      * Sets the time the notification was originally sent. Returns self.
72      */
setLastNotified(long mLastNotified)73     public NotifyingApp setLastNotified(long mLastNotified) {
74         this.mLastNotified = mLastNotified;
75         return this;
76     }
77 
78     public static final @NonNull Creator<NotifyingApp> CREATOR = new Creator<NotifyingApp>() {
79         @Override
80         public NotifyingApp createFromParcel(Parcel in) {
81             return new NotifyingApp(in);
82         }
83 
84         @Override
85         public NotifyingApp[] newArray(int size) {
86             return new NotifyingApp[size];
87         }
88     };
89 
90     @Override
describeContents()91     public int describeContents() {
92         return 0;
93     }
94 
95     @Override
writeToParcel(Parcel dest, int flags)96     public void writeToParcel(Parcel dest, int flags) {
97         dest.writeInt(mUserId);
98         dest.writeString(mPkg);
99         dest.writeLong(mLastNotified);
100     }
101 
102     @Override
equals(@ullable Object o)103     public boolean equals(@Nullable Object o) {
104         if (this == o) return true;
105         if (o == null || getClass() != o.getClass()) return false;
106         NotifyingApp that = (NotifyingApp) o;
107         return getUserId() == that.getUserId()
108                 && getLastNotified() == that.getLastNotified()
109                 && Objects.equals(mPkg, that.mPkg);
110     }
111 
112     @Override
hashCode()113     public int hashCode() {
114         return Objects.hash(getUserId(), mPkg, getLastNotified());
115     }
116 
117     /**
118      * Sorts notifying apps from newest last notified date to oldest.
119      */
120     @Override
compareTo(NotifyingApp o)121     public int compareTo(NotifyingApp o) {
122         if (getLastNotified() == o.getLastNotified()) {
123             if (getUserId() == o.getUserId()) {
124                 return getPackage().compareTo(o.getPackage());
125             }
126             return Integer.compare(getUserId(), o.getUserId());
127         }
128 
129         return -Long.compare(getLastNotified(), o.getLastNotified());
130     }
131 
132     @Override
toString()133     public String toString() {
134         return "NotifyingApp{"
135                 + "mUserId=" + mUserId
136                 + ", mPkg='" + mPkg + '\''
137                 + ", mLastNotified=" + mLastNotified
138                 + '}';
139     }
140 }
141