4
4
5
5
package org .chromium .chrome .browser .download ;
6
6
7
+ import static org .chromium .chrome .browser .download .DownloadSnackbarController .INVALID_NOTIFICATION_ID ;
8
+
7
9
import android .app .Notification ;
8
10
import android .app .Service ;
9
11
import android .content .Context ;
20
22
*/
21
23
public class DownloadForegroundService extends Service {
22
24
private final IBinder mBinder = new LocalBinder ();
25
+ // Only tracking for UMA purposes.
26
+ private int mPinnedNotification = INVALID_NOTIFICATION_ID ;
23
27
24
28
/**
25
29
* Start the foreground service with this given context.
@@ -41,12 +45,31 @@ public void startOrUpdateForegroundService(int notificationId, Notification noti
41
45
}
42
46
43
47
startForeground (notificationId , notification );
48
+
49
+ // Record when starting foreground and when updating pinned notification.
50
+ if (mPinnedNotification == INVALID_NOTIFICATION_ID ) {
51
+ DownloadNotificationUmaHelper .recordForegroundServiceLifecycleHistogram (
52
+ DownloadNotificationUmaHelper .ForegroundLifecycle .START );
53
+ } else {
54
+ if (mPinnedNotification != notificationId ) {
55
+ DownloadNotificationUmaHelper .recordForegroundServiceLifecycleHistogram (
56
+ DownloadNotificationUmaHelper .ForegroundLifecycle .UPDATE );
57
+ }
58
+ }
59
+ mPinnedNotification = notificationId ;
44
60
}
45
61
46
62
/**
47
63
* Stop the foreground service that is running.
48
64
*/
49
65
public void stopDownloadForegroundService (boolean isCancelled ) {
66
+ // Record when stopping foreground.
67
+ DownloadNotificationUmaHelper .recordForegroundServiceLifecycleHistogram (
68
+ DownloadNotificationUmaHelper .ForegroundLifecycle .STOP );
69
+ DownloadNotificationUmaHelper .recordServiceStoppedHistogram (
70
+ DownloadNotificationUmaHelper .ServiceStopped .STOPPED , true /* withForeground */ );
71
+ mPinnedNotification = INVALID_NOTIFICATION_ID ;
72
+
50
73
// If it's not cancelled, just detach the notification from the service, if possible.
51
74
if (!isCancelled && Build .VERSION .SDK_INT >= 24 ) {
52
75
stopForeground (STOP_FOREGROUND_DETACH );
@@ -61,6 +84,9 @@ public void stopDownloadForegroundService(boolean isCancelled) {
61
84
public int onStartCommand (Intent intent , int flags , int startId ) {
62
85
// In the case the service was restarted when the intent is null.
63
86
if (intent == null ) {
87
+ DownloadNotificationUmaHelper .recordServiceStoppedHistogram (
88
+ DownloadNotificationUmaHelper .ServiceStopped .START_STICKY , true );
89
+
64
90
DownloadForegroundServiceObservers .alertObserversServiceRestarted ();
65
91
66
92
// Allow observers to restart service on their own, if needed.
@@ -73,16 +99,27 @@ public int onStartCommand(Intent intent, int flags, int startId) {
73
99
74
100
@ Override
75
101
public void onDestroy () {
102
+ DownloadNotificationUmaHelper .recordServiceStoppedHistogram (
103
+ DownloadNotificationUmaHelper .ServiceStopped .DESTROYED , true /* withForeground */ );
76
104
DownloadForegroundServiceObservers .alertObserversServiceDestroyed ();
77
105
super .onDestroy ();
78
106
}
79
107
80
108
@ Override
81
109
public void onTaskRemoved (Intent rootIntent ) {
110
+ DownloadNotificationUmaHelper .recordServiceStoppedHistogram (
111
+ DownloadNotificationUmaHelper .ServiceStopped .TASK_REMOVED , true /*withForeground*/ );
82
112
DownloadForegroundServiceObservers .alertObserversTaskRemoved ();
83
113
super .onTaskRemoved (rootIntent );
84
114
}
85
115
116
+ @ Override
117
+ public void onLowMemory () {
118
+ DownloadNotificationUmaHelper .recordServiceStoppedHistogram (
119
+ DownloadNotificationUmaHelper .ServiceStopped .LOW_MEMORY , true /* withForeground */ );
120
+ super .onLowMemory ();
121
+ }
122
+
86
123
@ Nullable
87
124
@ Override
88
125
public IBinder onBind (Intent intent ) {
0 commit comments