| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.notifications; | 5 package org.chromium.chrome.browser.notifications; |
| 6 | 6 |
| 7 import android.app.Notification; | 7 import android.app.Notification; |
| 8 import android.app.NotificationManager; | 8 import android.app.NotificationManager; |
| 9 | 9 |
| 10 import org.chromium.base.BuildInfo; |
| 11 import org.chromium.base.ContextUtils; |
| 12 import org.chromium.base.Log; |
| 13 |
| 14 import java.lang.reflect.Constructor; |
| 15 import java.lang.reflect.InvocationTargetException; |
| 16 import java.lang.reflect.Method; |
| 17 |
| 10 /** | 18 /** |
| 11 * Default implementation of the NotificationManagerProxy, which passes through
all calls to the | 19 * Default implementation of the NotificationManagerProxy, which passes through
all calls to the |
| 12 * normal Android Notification Manager. | 20 * normal Android Notification Manager. |
| 13 */ | 21 */ |
| 14 public class NotificationManagerProxyImpl implements NotificationManagerProxy { | 22 public class NotificationManagerProxyImpl implements NotificationManagerProxy { |
| 23 private static final String TAG = "NotifManagerProxy"; |
| 15 private final NotificationManager mNotificationManager; | 24 private final NotificationManager mNotificationManager; |
| 16 | 25 |
| 17 public NotificationManagerProxyImpl(NotificationManager notificationManager)
{ | 26 public NotificationManagerProxyImpl(NotificationManager notificationManager)
{ |
| 18 mNotificationManager = notificationManager; | 27 mNotificationManager = notificationManager; |
| 19 } | 28 } |
| 20 | 29 |
| 21 @Override | 30 @Override |
| 22 public void cancel(int id) { | 31 public void cancel(int id) { |
| 23 mNotificationManager.cancel(id); | 32 mNotificationManager.cancel(id); |
| 24 } | 33 } |
| 25 | 34 |
| 26 @Override | 35 @Override |
| 27 public void cancel(String tag, int id) { | 36 public void cancel(String tag, int id) { |
| 28 mNotificationManager.cancel(tag, id); | 37 mNotificationManager.cancel(tag, id); |
| 29 } | 38 } |
| 30 | 39 |
| 31 @Override | 40 @Override |
| 32 public void cancelAll() { | 41 public void cancelAll() { |
| 33 mNotificationManager.cancelAll(); | 42 mNotificationManager.cancelAll(); |
| 34 } | 43 } |
| 35 | 44 |
| 36 @Override | 45 @Override |
| 46 public void createNotificationChannel(ChannelsInitializer.Channel channel) { |
| 47 assert BuildInfo.isAtLeastO(); |
| 48 /* |
| 49 The code in the try-block uses reflection in order to compile as it call
s APIs newer than |
| 50 our compileSdkVersion of Android. The equivalent code without reflection
looks like this: |
| 51 |
| 52 channel.setGroup(channelGroupId); |
| 53 channel.setShowBadge(false); |
| 54 mNotificationManager.createNotificationChannel(channel); |
| 55 */ |
| 56 // TODO(crbug.com/707804) Stop using reflection once compileSdkVersion i
s high enough. |
| 57 try { |
| 58 // Create channel |
| 59 Class<?> channelClass = Class.forName("android.app.NotificationChann
el"); |
| 60 Constructor<?> channelConstructor = channelClass.getDeclaredConstruc
tor( |
| 61 String.class, CharSequence.class, int.class); |
| 62 Object channelObject = channelConstructor.newInstance(channel.mId, |
| 63 ContextUtils.getApplicationContext().getString(channel.mName
ResId), |
| 64 channel.mImportance); |
| 65 |
| 66 // Set group on channel |
| 67 Method setGroupMethod = channelClass.getMethod("setGroup", String.cl
ass); |
| 68 setGroupMethod.invoke(channelObject, channel.mGroupId); |
| 69 |
| 70 // Set channel to not badge on app icon |
| 71 Method setShowBadgeMethod = channelClass.getMethod("setShowBadge", b
oolean.class); |
| 72 setShowBadgeMethod.invoke(channelObject, false); |
| 73 |
| 74 // Register channel |
| 75 Method createNotificationChannelMethod = mNotificationManager.getCla
ss().getMethod( |
| 76 "createNotificationChannel", channelClass); |
| 77 createNotificationChannelMethod.invoke(mNotificationManager, channel
Object); |
| 78 |
| 79 } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessE
xception |
| 80 | InstantiationException | InvocationTargetException e) { |
| 81 Log.e(TAG, "Error initializing notification channel:", e); |
| 82 } |
| 83 } |
| 84 |
| 85 @Override |
| 86 public void createNotificationChannelGroup(ChannelsInitializer.ChannelGroup
channelGroup) { |
| 87 assert BuildInfo.isAtLeastO(); |
| 88 /* |
| 89 The code in the try-block uses reflection in order to compile as it call
s APIs newer than |
| 90 our compileSdkVersion of Android. The equivalent code without reflection
looks like this: |
| 91 |
| 92 mNotificationManager.createNotificationChannelGroup(channelGroup); |
| 93 */ |
| 94 // TODO(crbug.com/707804) Stop using reflection once compileSdkVersion i
s high enough. |
| 95 try { |
| 96 // Create channel group |
| 97 Class<?> channelGroupClass = Class.forName("android.app.Notification
ChannelGroup"); |
| 98 Constructor<?> channelGroupConstructor = |
| 99 channelGroupClass.getDeclaredConstructor(String.class, CharS
equence.class); |
| 100 Object channelGroupObject = channelGroupConstructor.newInstance(chan
nelGroup.mId, |
| 101 ContextUtils.getApplicationContext().getString(channelGroup.
mNameResId)); |
| 102 |
| 103 // Register channel group |
| 104 Method createNotificationChannelGroupMethod = mNotificationManager.g
etClass().getMethod( |
| 105 "createNotificationChannelGroup", channelGroupClass); |
| 106 createNotificationChannelGroupMethod.invoke(mNotificationManager, ch
annelGroupObject); |
| 107 |
| 108 } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessE
xception |
| 109 | InstantiationException | InvocationTargetException e) { |
| 110 Log.e(TAG, "Error initializing notification channel group:", e); |
| 111 } |
| 112 } |
| 113 |
| 114 @Override |
| 37 public void notify(int id, Notification notification) { | 115 public void notify(int id, Notification notification) { |
| 38 mNotificationManager.notify(id, notification); | 116 mNotificationManager.notify(id, notification); |
| 39 } | 117 } |
| 40 | 118 |
| 41 @Override | 119 @Override |
| 42 public void notify(String tag, int id, Notification notification) { | 120 public void notify(String tag, int id, Notification notification) { |
| 43 mNotificationManager.notify(tag, id, notification); | 121 mNotificationManager.notify(tag, id, notification); |
| 44 } | 122 } |
| 45 } | 123 } |
| OLD | NEW |