Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2925)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationManagerProxyImpl.java

Issue 2808163002: [Android O] Refactor channel initialization (Closed)
Patch Set: fix comments Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationManagerProxyImpl.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationManagerProxyImpl.java b/chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationManagerProxyImpl.java
index b05fd0047b17adb015a4506b96e789b5e158fdd9..46d2a103ce07b3e821cd369ceaa0366c47709b0d 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationManagerProxyImpl.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/notifications/NotificationManagerProxyImpl.java
@@ -7,11 +7,20 @@ package org.chromium.chrome.browser.notifications;
import android.app.Notification;
import android.app.NotificationManager;
+import org.chromium.base.BuildInfo;
+import org.chromium.base.ContextUtils;
+import org.chromium.base.Log;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
/**
* Default implementation of the NotificationManagerProxy, which passes through all calls to the
* normal Android Notification Manager.
*/
public class NotificationManagerProxyImpl implements NotificationManagerProxy {
+ private static final String TAG = "NotifManagerProxy";
private final NotificationManager mNotificationManager;
public NotificationManagerProxyImpl(NotificationManager notificationManager) {
@@ -34,6 +43,75 @@ public class NotificationManagerProxyImpl implements NotificationManagerProxy {
}
@Override
+ public void createNotificationChannel(ChannelsInitializer.Channel channel) {
+ assert BuildInfo.isAtLeastO();
+ /*
+ The code in the try-block uses reflection in order to compile as it calls APIs newer than
+ our compileSdkVersion of Android. The equivalent code without reflection looks like this:
+
+ channel.setGroup(channelGroupId);
+ channel.setShowBadge(false);
+ mNotificationManager.createNotificationChannel(channel);
+ */
+ // TODO(crbug.com/707804) Stop using reflection once compileSdkVersion is high enough.
+ try {
+ // Create channel
+ Class<?> channelClass = Class.forName("android.app.NotificationChannel");
+ Constructor<?> channelConstructor = channelClass.getDeclaredConstructor(
+ String.class, CharSequence.class, int.class);
+ Object channelObject = channelConstructor.newInstance(channel.mId,
+ ContextUtils.getApplicationContext().getString(channel.mNameResId),
+ channel.mImportance);
+
+ // Set group on channel
+ Method setGroupMethod = channelClass.getMethod("setGroup", String.class);
+ setGroupMethod.invoke(channelObject, channel.mGroupId);
+
+ // Set channel to not badge on app icon
+ Method setShowBadgeMethod = channelClass.getMethod("setShowBadge", boolean.class);
+ setShowBadgeMethod.invoke(channelObject, false);
+
+ // Register channel
+ Method createNotificationChannelMethod = mNotificationManager.getClass().getMethod(
+ "createNotificationChannel", channelClass);
+ createNotificationChannelMethod.invoke(mNotificationManager, channelObject);
+
+ } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException
+ | InstantiationException | InvocationTargetException e) {
+ Log.e(TAG, "Error initializing notification channel:", e);
+ }
+ }
+
+ @Override
+ public void createNotificationChannelGroup(ChannelsInitializer.ChannelGroup channelGroup) {
+ assert BuildInfo.isAtLeastO();
+ /*
+ The code in the try-block uses reflection in order to compile as it calls APIs newer than
+ our compileSdkVersion of Android. The equivalent code without reflection looks like this:
+
+ mNotificationManager.createNotificationChannelGroup(channelGroup);
+ */
+ // 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 channelGroupObject = channelGroupConstructor.newInstance(channelGroup.mId,
+ ContextUtils.getApplicationContext().getString(channelGroup.mNameResId));
+
+ // Register channel group
+ Method createNotificationChannelGroupMethod = mNotificationManager.getClass().getMethod(
+ "createNotificationChannelGroup", channelGroupClass);
+ createNotificationChannelGroupMethod.invoke(mNotificationManager, channelGroupObject);
+
+ } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException
+ | InstantiationException | InvocationTargetException e) {
+ Log.e(TAG, "Error initializing notification channel group:", e);
+ }
+ }
+
+ @Override
public void notify(int id, Notification notification) {
mNotificationManager.notify(id, notification);
}

Powered by Google App Engine
This is Rietveld 408576698