0013-Bug-28051-Add-a-notification-compatibility-class.patch 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. From 0fde60f64b342fafc7150272c8de0544b0c430df Mon Sep 17 00:00:00 2001
  2. From: Matthew Finkel <Matthew.Finkel@gmail.com>
  3. Date: Wed, 21 Nov 2018 18:05:42 +0000
  4. Subject: [PATCH 13/13] Bug 28051 - Add a notification compatibility class
  5. ---
  6. .../android/service/TorService.java | 7 +-
  7. .../util/NotificationBuilderCompat.java | 162 ++++++++++++++++++
  8. 2 files changed, 166 insertions(+), 3 deletions(-)
  9. create mode 100644 orbotservice/src/main/java/org/torproject/android/service/util/NotificationBuilderCompat.java
  10. diff --git a/orbotservice/src/main/java/org/torproject/android/service/TorService.java b/orbotservice/src/main/java/org/torproject/android/service/TorService.java
  11. index 189ee6ab..745e5e8b 100644
  12. --- a/orbotservice/src/main/java/org/torproject/android/service/TorService.java
  13. +++ b/orbotservice/src/main/java/org/torproject/android/service/TorService.java
  14. @@ -55,6 +55,7 @@ import org.torproject.android.control.TorControlConnection;
  15. import org.torproject.android.service.util.OtherResourceInstaller;
  16. import org.torproject.android.service.vpn.TorifiedApp;
  17. import org.torproject.android.service.util.DummyActivity;
  18. +import org.torproject.android.service.util.NotificationBuilderCompat;
  19. import org.torproject.android.service.util.Prefs;
  20. import org.torproject.android.service.util.TorServiceUtils;
  21. import org.torproject.android.service.util.Utils;
  22. @@ -120,7 +121,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
  23. private int mNetworkType = -1;
  24. private NotificationManager mNotificationManager = null;
  25. - private NotificationCompat.Builder mNotifyBuilder;
  26. + private NotificationBuilderCompat mNotifyBuilder;
  27. private Notification mNotification;
  28. private boolean mNotificationShowing = false;
  29. @@ -251,7 +252,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
  30. mNotificationShowing = false;
  31. }
  32. - private final static String NOTIFICATION_CHANNEL_ID = "orbot_channel_1";
  33. + private final static String NOTIFICATION_CHANNEL_ID = "torbrowser_channel_1";
  34. // Use TargetApi until we use a support library version that adds the
  35. // RequriesApi annotation
  36. @@ -298,7 +299,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
  37. if (mNotifyBuilder == null)
  38. {
  39. - mNotifyBuilder = new NotificationCompat.Builder(this)
  40. + mNotifyBuilder = new NotificationBuilderCompat(this, NOTIFICATION_CHANNEL_ID)
  41. .setContentTitle(getString(R.string.app_name))
  42. .setSmallIcon(R.drawable.ic_stat_tor);
  43. diff --git a/orbotservice/src/main/java/org/torproject/android/service/util/NotificationBuilderCompat.java b/orbotservice/src/main/java/org/torproject/android/service/util/NotificationBuilderCompat.java
  44. new file mode 100644
  45. index 00000000..eac657ff
  46. --- /dev/null
  47. +++ b/orbotservice/src/main/java/org/torproject/android/service/util/NotificationBuilderCompat.java
  48. @@ -0,0 +1,162 @@
  49. +package org.torproject.android.service.util;
  50. +
  51. +import android.app.Notification;
  52. +import android.app.Notification.Builder;
  53. +import android.app.Notification.Style;
  54. +import android.app.PendingIntent;
  55. +import android.content.Context;
  56. +import android.graphics.Bitmap;
  57. +import android.os.Build;
  58. +import android.util.Log;
  59. +
  60. +import java.lang.reflect.Constructor;
  61. +
  62. +/*
  63. + * This is a compatibility wrapper-class around the native
  64. + * android.app.Notification.Builder class. This class is needed
  65. + * because we are currently targeting Android API level 26 and
  66. + * supporting API level 16 as the minimum level, but we're using
  67. + * the Android Support Library 23.4.0. This puts us in a situation
  68. + * where Android API 26 requires "channels", but the support library
  69. + * doesn't know what a channel is.
  70. + *
  71. + * This is a temporary hack until we upgrade to a newer support library
  72. + * (mozilla-central uses 26.1.0, at the time of this writing).
  73. + */
  74. +
  75. +public class NotificationBuilderCompat {
  76. + private static final String LOGTAG = "NotificationBuilderCompat";
  77. + private static final Class notificationBuilderClass = Notification.Builder.class;
  78. +
  79. + /* Credit: http://www.javadocexamples.com/java/lang/Class/getDeclaredConstructor(...%20parameterTypes).html */
  80. + // Constructor signature before Android O
  81. + private static final Class[] REPLICATE_CONSTRUCTOR_PARAMS_PRE_O = new Class[]{Context.class};
  82. + // Constructor signature Android O and newer
  83. + private static final Class[] REPLICATE_CONSTRUCTOR_PARAMS_O_PLUS = new Class[]{Context.class, String.class};
  84. +
  85. + public static final String DEFAULT_CHANNEL_ID = "torbrowser_channel_0";
  86. +
  87. + private Notification.Builder mBuilder;
  88. +
  89. + public NotificationBuilderCompat(Context context, String channelId) {
  90. + Constructor constructor;
  91. +
  92. + // If we think we're running on a device with Oreo or newer, then
  93. + // try constructing a Notification.Builder with a channel Id.
  94. + if (Build.VERSION.SDK_INT >= 26) {
  95. + try {
  96. + constructor = notificationBuilderClass.getConstructor(REPLICATE_CONSTRUCTOR_PARAMS_O_PLUS);
  97. + mBuilder = (Notification.Builder) constructor.newInstance(context, channelId);
  98. + return;
  99. + } catch (Exception e) {}
  100. + }
  101. + try {
  102. + // Fall back on the constructor without a channel ID
  103. + constructor = notificationBuilderClass.getConstructor(REPLICATE_CONSTRUCTOR_PARAMS_PRE_O);
  104. + mBuilder = (Notification.Builder) constructor.newInstance(context);
  105. + } catch (Exception e) {
  106. + mBuilder = new Notification.Builder(context);
  107. + }
  108. + }
  109. +
  110. + public NotificationBuilderCompat(Context context) {
  111. + this(context, DEFAULT_CHANNEL_ID);
  112. + }
  113. +
  114. + public NotificationBuilderCompat setContentText(CharSequence title) {
  115. + mBuilder = mBuilder.setContentText(title);
  116. + return this;
  117. + }
  118. +
  119. + public NotificationBuilderCompat setContentTitle(CharSequence title) {
  120. + mBuilder = mBuilder.setContentTitle(title);
  121. + return this;
  122. + }
  123. +
  124. + public NotificationBuilderCompat setSmallIcon(int icon, int level) {
  125. + mBuilder = mBuilder.setSmallIcon(icon, level);
  126. + return this;
  127. + }
  128. +
  129. + public NotificationBuilderCompat setSmallIcon(int icon) {
  130. + mBuilder = mBuilder.setSmallIcon(icon);
  131. + return this;
  132. + }
  133. +
  134. + public NotificationBuilderCompat setLargeIcon(Bitmap b) {
  135. + mBuilder = mBuilder.setLargeIcon(b);
  136. + return this;
  137. + }
  138. +
  139. + public NotificationBuilderCompat setContentIntent(PendingIntent intent) {
  140. + mBuilder = mBuilder.setContentIntent(intent);
  141. + return this;
  142. + }
  143. +
  144. + public NotificationBuilderCompat setCategory(String category) {
  145. + // This was added in API level 21
  146. + if (Build.VERSION.SDK_INT >= 21) {
  147. + mBuilder = mBuilder.setCategory(category);
  148. + }
  149. + return this;
  150. + }
  151. +
  152. + public NotificationBuilderCompat addAction(int icon, CharSequence title, PendingIntent intent) {
  153. + mBuilder = mBuilder.addAction(icon, title, intent);
  154. + return this;
  155. + }
  156. +
  157. + public NotificationBuilderCompat setOngoing(boolean ongoing) {
  158. + mBuilder = mBuilder.setOngoing(ongoing);
  159. + return this;
  160. + }
  161. +
  162. + public NotificationBuilderCompat setTicker(CharSequence tickerText) {
  163. + mBuilder = mBuilder.setTicker(tickerText);
  164. + return this;
  165. + }
  166. +
  167. + public NotificationBuilderCompat setPriority(int prio) {
  168. + mBuilder = mBuilder.setPriority(prio);
  169. + return this;
  170. + }
  171. +
  172. + public NotificationBuilderCompat setDeleteIntent(PendingIntent intent) {
  173. + mBuilder = mBuilder.setDeleteIntent(intent);
  174. + return this;
  175. + }
  176. +
  177. + public NotificationBuilderCompat setAutoCancel(boolean autoCancel) {
  178. + mBuilder = mBuilder.setAutoCancel(autoCancel);
  179. + return this;
  180. + }
  181. +
  182. + public NotificationBuilderCompat setDefaults(int defaults) {
  183. + mBuilder = mBuilder.setDefaults(defaults);
  184. + return this;
  185. + }
  186. +
  187. + public NotificationBuilderCompat setStyle(Notification.Style style) {
  188. + mBuilder = mBuilder.setStyle(style);
  189. + return this;
  190. + }
  191. +
  192. + public NotificationBuilderCompat setWhen(long when) {
  193. + mBuilder = mBuilder.setWhen(when);
  194. + return this;
  195. + }
  196. +
  197. + public NotificationBuilderCompat setProgress(int max, int progress, boolean indeterminate) {
  198. + mBuilder = mBuilder.setProgress(max, progress, indeterminate);
  199. + return this;
  200. + }
  201. +
  202. + public NotificationBuilderCompat setLights(int argb, int onMs, int offMs) {
  203. + mBuilder = mBuilder.setLights(argb, onMs, offMs);
  204. + return this;
  205. + }
  206. +
  207. + public Notification build() {
  208. + return mBuilder.build();
  209. + }
  210. +}
  211. --
  212. 2.17.1