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