| Index: chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationBuilderForO.java
|
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationBuilderForO.java b/chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationBuilderForO.java
|
| index 2daa053716ff1592fcba69f894c8d72d35d86602..b207b3a5e00d6b7df3b0e73f7d81530baa83e409 100644
|
| --- a/chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationBuilderForO.java
|
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationBuilderForO.java
|
| @@ -6,13 +6,11 @@ package org.chromium.chrome.browser.notifications;
|
|
|
| import android.annotation.TargetApi;
|
| import android.app.Notification;
|
| -import android.app.NotificationManager;
|
| import android.content.Context;
|
|
|
| import org.chromium.base.BuildInfo;
|
| import org.chromium.base.Log;
|
|
|
| -import java.lang.reflect.Constructor;
|
| import java.lang.reflect.InvocationTargetException;
|
| import java.lang.reflect.Method;
|
|
|
| @@ -23,77 +21,17 @@ import java.lang.reflect.Method;
|
| public class NotificationBuilderForO extends NotificationBuilder {
|
| private static final String TAG = "NotifBuilderForO";
|
|
|
| - public NotificationBuilderForO(Context context, String channelId, String channelName,
|
| - String channelGroupId, String channelGroupName) {
|
| + public NotificationBuilderForO(Context context, @ChannelsInitializer.ChannelId String channelId,
|
| + ChannelsInitializer channelsInitializer) {
|
| super(context);
|
| assert BuildInfo.isAtLeastO();
|
| - NotificationManager notificationManager =
|
| - (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
| - /*
|
| - The code in the try-block uses reflection in order to compile as it calls APIs newer than
|
| - our target version of Android. The equivalent code without reflection is as follows:
|
| -
|
| - notificationManager.createNotificationChannelGroup(
|
| - new NotificationChannelGroup(channelGroupId, channelGroupName));
|
| - NotificationChannel channel = new NotificationChannel(
|
| - channelId, channelName, importance);
|
| - channel.setGroup(channelGroupId);
|
| - channel.setShowBadge(false);
|
| - notificationManager.createNotificationChannel(channel);
|
| - notificationBuilder.setChannel(channelId);
|
| -
|
| - Note this whole class can be removed once we target O, and channels may be set
|
| - inline where notifications are created, using the pattern above.
|
| -
|
| - In the longer term we may wish to initialize our channels in response to BOOT_COMPLETED or
|
| - ACTION_PACKAGE_REPLACED, as per Android guidelines.
|
| - */
|
| + channelsInitializer.ensureInitialized(channelId);
|
| + // TODO(crbug.com/707804) Stop using reflection once compileSdkVersion is high enough.
|
| try {
|
| - // Create channel group
|
| - Class<?> channelGroupClass = Class.forName("android.app.NotificationChannelGroup");
|
| - Constructor<?> channelGroupConstructor =
|
| - channelGroupClass.getDeclaredConstructor(String.class, CharSequence.class);
|
| - Object channelGroup =
|
| - channelGroupConstructor.newInstance(channelGroupId, channelGroupName);
|
| -
|
| - // Register channel group
|
| - Method createNotificationChannelGroupMethod = notificationManager.getClass().getMethod(
|
| - "createNotificationChannelGroup", channelGroupClass);
|
| - createNotificationChannelGroupMethod.invoke(notificationManager, channelGroup);
|
| -
|
| - // Create channel
|
| - Class<?> channelClass = Class.forName("android.app.NotificationChannel");
|
| - Constructor<?> channelConstructor = channelClass.getDeclaredConstructor(
|
| - String.class, CharSequence.class, int.class);
|
| - Object channel = channelConstructor.newInstance(
|
| - channelId, channelName, getChannelImportance(channelId));
|
| -
|
| - // Set group on channel
|
| - Method setGroupMethod = channelClass.getMethod("setGroup", String.class);
|
| - setGroupMethod.invoke(channel, channelGroupId);
|
| -
|
| - // Set channel to not badge on app icon
|
| - Method setShowBadgeMethod = channelClass.getMethod("setShowBadge", boolean.class);
|
| - setShowBadgeMethod.invoke(channel, false);
|
| -
|
| - // Register channel
|
| - Method createNotificationChannelMethod = notificationManager.getClass().getMethod(
|
| - "createNotificationChannel", channelClass);
|
| - createNotificationChannelMethod.invoke(notificationManager, channel);
|
| -
|
| - // Set channel on builder
|
| - Method setChannelMethod =
|
| - Notification.Builder.class.getMethod("setChannel", String.class);
|
| - setChannelMethod.invoke(mBuilder, channelId);
|
| - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException
|
| - | InstantiationException | InvocationTargetException e) {
|
| - Log.e(TAG, "Error initializing notification builder:", e);
|
| + Method setChannel = Notification.Builder.class.getMethod("setChannel", String.class);
|
| + setChannel.invoke(mBuilder, channelId);
|
| + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
|
| + Log.e(TAG, "Error setting channel on notification builder:", e);
|
| }
|
| }
|
| -
|
| - private static int getChannelImportance(String channelId) {
|
| - return NotificationConstants.CHANNEL_ID_SITES.equals(channelId)
|
| - ? NotificationManager.IMPORTANCE_DEFAULT
|
| - : NotificationManager.IMPORTANCE_LOW;
|
| - }
|
| }
|
|
|