1 /* 2 * Copyright 2019 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 androidx.work; 18 19 import android.app.Notification; 20 21 import org.jspecify.annotations.NonNull; 22 23 /** 24 * The information required when a {@link ListenableWorker} runs in the context of a foreground 25 * service. 26 */ 27 // NOTE: once this file is migrated to Kotlin, corresponding stub in lint rules should be migrated. 28 // As a result lint checks should start relying on parameter names instead. 29 public final class ForegroundInfo { 30 31 private final int mNotificationId; 32 private final int mForegroundServiceType; 33 private final Notification mNotification; 34 35 /** 36 * Creates an instance of {@link ForegroundInfo} with a {@link Notification}. 37 * <p> 38 * On API 29 and above, you can specify a {@code foregroundServiceType} by using the 39 * {@link #ForegroundInfo(int, Notification, int)} constructor; otherwise, a default {@code 40 * foregroundServiceType} of {@code 0} will be used. 41 * 42 * @param notificationId The {@link Notification} id 43 * @param notification The {@link Notification} to show when the Worker is running in the 44 * context of a foreground {@link android.app.Service} 45 */ ForegroundInfo(int notificationId, @NonNull Notification notification)46 public ForegroundInfo(int notificationId, @NonNull Notification notification) { 47 this(notificationId, notification, 0); 48 } 49 50 /** 51 * Creates an instance of {@link ForegroundInfo} with a {@link Notification} and foreground 52 * {@link android.app.Service} type. 53 * 54 * For more information look at {@code android.app.Service#startForeground(int, 55 * Notification, int)}. 56 * 57 * @param notificationId The {@link Notification} id 58 * @param notification The {@link Notification} 59 * @param foregroundServiceType The foreground {@link android.content.pm.ServiceInfo} type 60 */ ForegroundInfo( int notificationId, @NonNull Notification notification, int foregroundServiceType)61 public ForegroundInfo( 62 int notificationId, 63 @NonNull Notification notification, 64 int foregroundServiceType) { 65 mNotificationId = notificationId; 66 mNotification = notification; 67 mForegroundServiceType = foregroundServiceType; 68 } 69 70 /** 71 * @return The {@link Notification} id to be used 72 */ getNotificationId()73 public int getNotificationId() { 74 return mNotificationId; 75 } 76 77 /** 78 * @return The foreground {@link android.content.pm.ServiceInfo} type 79 */ getForegroundServiceType()80 public int getForegroundServiceType() { 81 return mForegroundServiceType; 82 } 83 84 /** 85 * @return The user visible {@link Notification} 86 */ getNotification()87 public @NonNull Notification getNotification() { 88 return mNotification; 89 } 90 91 @Override equals(Object o)92 public boolean equals(Object o) { 93 if (this == o) return true; 94 if (o == null || getClass() != o.getClass()) return false; 95 96 ForegroundInfo that = (ForegroundInfo) o; 97 98 if (mNotificationId != that.mNotificationId) return false; 99 if (mForegroundServiceType != that.mForegroundServiceType) return false; 100 return mNotification.equals(that.mNotification); 101 } 102 103 @Override hashCode()104 public int hashCode() { 105 int result = mNotificationId; 106 result = 31 * result + mForegroundServiceType; 107 result = 31 * result + mNotification.hashCode(); 108 return result; 109 } 110 111 @Override toString()112 public String toString() { 113 final StringBuilder sb = new StringBuilder("ForegroundInfo{"); 114 sb.append("mNotificationId=").append(mNotificationId); 115 sb.append(", mForegroundServiceType=").append(mForegroundServiceType); 116 sb.append(", mNotification=").append(mNotification); 117 sb.append('}'); 118 return sb.toString(); 119 } 120 } 121