diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-10-12 14:27:29 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-10-13 09:35:20 +0000 |
commit | c30a6232df03e1efbd9f3b226777b07e087a1122 (patch) | |
tree | e992f45784689f373bcc38d1b79a239ebe17ee23 /chromium/components/browser_ui/notifications/android | |
parent | 7b5b123ac58f58ffde0f4f6e488bcd09aa4decd3 (diff) | |
download | qtwebengine-chromium-85-based.tar.gz |
BASELINE: Update Chromium to 85.0.4183.14085-based
Change-Id: Iaa42f4680837c57725b1344f108c0196741f6057
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'chromium/components/browser_ui/notifications/android')
2 files changed, 92 insertions, 0 deletions
diff --git a/chromium/components/browser_ui/notifications/android/BUILD.gn b/chromium/components/browser_ui/notifications/android/BUILD.gn index 78dcfecfc03..220f0f987cd 100644 --- a/chromium/components/browser_ui/notifications/android/BUILD.gn +++ b/chromium/components/browser_ui/notifications/android/BUILD.gn @@ -8,6 +8,7 @@ android_library("java") { sources = [ "java/src/org/chromium/components/browser_ui/notifications/ChromeNotification.java", "java/src/org/chromium/components/browser_ui/notifications/ChromeNotificationBuilder.java", + "java/src/org/chromium/components/browser_ui/notifications/ForegroundServiceUtils.java", "java/src/org/chromium/components/browser_ui/notifications/NotificationBuilder.java", "java/src/org/chromium/components/browser_ui/notifications/NotificationCompatBuilder.java", "java/src/org/chromium/components/browser_ui/notifications/NotificationManagerProxy.java", diff --git a/chromium/components/browser_ui/notifications/android/java/src/org/chromium/components/browser_ui/notifications/ForegroundServiceUtils.java b/chromium/components/browser_ui/notifications/android/java/src/org/chromium/components/browser_ui/notifications/ForegroundServiceUtils.java new file mode 100644 index 00000000000..ca6227ee7b0 --- /dev/null +++ b/chromium/components/browser_ui/notifications/android/java/src/org/chromium/components/browser_ui/notifications/ForegroundServiceUtils.java @@ -0,0 +1,91 @@ +// Copyright 2019 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +package org.chromium.components.browser_ui.notifications; + +import android.app.Notification; +import android.app.Service; +import android.content.Intent; +import android.os.Build; + +import androidx.annotation.VisibleForTesting; +import androidx.core.app.ServiceCompat; +import androidx.core.content.ContextCompat; + +import org.chromium.base.ContextUtils; +import org.chromium.base.Log; + +/** + * Utility functions that call into Android foreground service related API, and provides + * compatibility for older Android versions and work around for Android API bugs. + */ +public class ForegroundServiceUtils { + private static final String TAG = "ForegroundService"; + private ForegroundServiceUtils() {} + + /** + * Gets the singleton instance of ForegroundServiceUtils. + */ + public static ForegroundServiceUtils getInstance() { + return ForegroundServiceUtils.LazyHolder.sInstance; + } + + /** + * Sets a mocked instance for testing. + */ + @VisibleForTesting + public static void setInstanceForTesting(ForegroundServiceUtils instance) { + ForegroundServiceUtils.LazyHolder.sInstance = instance; + } + + private static class LazyHolder { + private static ForegroundServiceUtils sInstance = new ForegroundServiceUtils(); + } + + /** + * Starts a service from {@code intent} with the expectation that it will make itself a + * foreground service with {@link android.app.Service#startForeground(int, Notification)}. + * + * @param intent The {@link Intent} to fire to start the service. + */ + public void startForegroundService(Intent intent) { + ContextCompat.startForegroundService(ContextUtils.getApplicationContext(), intent); + } + + /** + * Upgrades a service from background to foreground after calling + * {@link #startForegroundService(Intent)}. + * @param service The service to be foreground. + * @param id The notification id. + * @param notification The notification attached to the foreground service. + * @param foregroundServiceType The type of foreground service. Must be a subset of the + * foreground service types defined in AndroidManifest.xml. + * Use 0 if no foregroundServiceType attribute is defined. + */ + public void startForeground( + Service service, int id, Notification notification, int foregroundServiceType) { + // If android fail to build the notification, do nothing. + if (notification == null) return; + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + service.startForeground(id, notification, foregroundServiceType); + } else { + service.startForeground(id, notification); + } + } + + /** + * Stops the foreground service. See {@link ServiceCompat#stopForeground(Service, int)}. + * @param service The foreground service to stop. + * @param flags The flags to stop foreground service. + */ + public void stopForeground(Service service, int flags) { + // OnePlus devices may throw NullPointerException, see https://crbug.com/992347. + try { + ServiceCompat.stopForeground(service, flags); + } catch (NullPointerException e) { + Log.e(TAG, "Failed to stop foreground service, ", e); + } + } +} |