From fe6a0e47121d17d08c7d913f1db086687a569446 Mon Sep 17 00:00:00 2001 From: cyBerta Date: Wed, 23 Jun 2021 03:27:17 +0200 Subject: initial tor-integration to circumvent blocking attempts of the provider api --- .../bitmaskclient/tor/TorNotificationManager.java | 105 +++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java (limited to 'app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java') diff --git a/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java b/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java new file mode 100644 index 00000000..71a2735c --- /dev/null +++ b/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java @@ -0,0 +1,105 @@ +package se.leap.bitmaskclient.tor; +/** + * Copyright (c) 2021 LEAP Encryption Access Project and contributers + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +import android.annotation.TargetApi; +import android.app.Notification; +import android.app.NotificationChannel; +import android.app.NotificationManager; +import android.content.Context; +import android.os.Build; + +import androidx.annotation.NonNull; +import androidx.core.app.NotificationCompat; + +import se.leap.bitmaskclient.R; + +public class TorNotificationManager { + public final static int TOR_SERVICE_NOTIFICATION_ID = 10; + static final String NOTIFICATION_CHANNEL_NEWSTATUS_ID = "bitmask_tor_service_news"; + + + public TorNotificationManager() {} + + + public static Notification buildTorForegroundNotification(Context context) { + NotificationManager notificationManager = initNotificationManager(context); + if (notificationManager == null) { + return null; + } + NotificationCompat.Builder notificationBuilder = initNotificationBuilderDefaults(context); + return notificationBuilder + .setSmallIcon(R.drawable.ic_bridge_36) + .setWhen(System.currentTimeMillis()) + .setContentTitle("Using Bridges to configure provider.").build(); + } + + public void buildTorNotification(Context context, String state) { + NotificationManager notificationManager = initNotificationManager(context); + if (notificationManager == null) { + return; + } + NotificationCompat.Builder notificationBuilder = initNotificationBuilderDefaults(context); + notificationBuilder + .setSmallIcon(R.drawable.ic_bridge_36) + .setWhen(System.currentTimeMillis()) + .setTicker(state) + .setContentTitle(context.getString(R.string.tor_provider_setup)) + .setContentText(state); + notificationManager.notify(TOR_SERVICE_NOTIFICATION_ID, notificationBuilder.build()); + } + + + private static NotificationManager initNotificationManager(Context context) { + NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + if (notificationManager == null) { + return null; + } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + createNotificationChannel(notificationManager); + } + return notificationManager; + } + + @TargetApi(26) + private static void createNotificationChannel(NotificationManager notificationManager) { + CharSequence name = "Bitmask Tor Service"; + String description = "Informs about usage of bridges to configure Bitmask."; + NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_NEWSTATUS_ID, + name, + NotificationManager.IMPORTANCE_LOW); + channel.setSound(null, null); + channel.setDescription(description); + notificationManager.createNotificationChannel(channel); + } + + private static NotificationCompat.Builder initNotificationBuilderDefaults(Context context) { + NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_NEWSTATUS_ID); + notificationBuilder. + setDefaults(Notification.DEFAULT_ALL). + setAutoCancel(true); + return notificationBuilder; + } + + public void cancelNotifications(Context context) { + NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + if (notificationManager == null) { + return; + } + notificationManager.cancel(TOR_SERVICE_NOTIFICATION_ID); + } +} -- cgit v1.2.3 From a28080ddfe35ac85c7da552739dc27f0687734a2 Mon Sep 17 00:00:00 2001 From: cyBerta Date: Wed, 23 Jun 2021 03:36:40 +0200 Subject: don't use hard coded strings --- app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java') diff --git a/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java b/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java index 71a2735c..4acc2b7e 100644 --- a/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java +++ b/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java @@ -45,7 +45,7 @@ public class TorNotificationManager { return notificationBuilder .setSmallIcon(R.drawable.ic_bridge_36) .setWhen(System.currentTimeMillis()) - .setContentTitle("Using Bridges to configure provider.").build(); + .setContentTitle(context.getString(R.string.tor_provider_setup)).build(); } public void buildTorNotification(Context context, String state) { -- cgit v1.2.3 From 236c17ec3f4348a9f0d4ec4a2454b9fbfaf8707f Mon Sep 17 00:00:00 2001 From: cyBerta Date: Thu, 24 Jun 2021 23:15:41 +0200 Subject: show tor status info in provider setup activies --- .../java/se/leap/bitmaskclient/tor/TorNotificationManager.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java') diff --git a/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java b/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java index 4acc2b7e..71b5c378 100644 --- a/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java +++ b/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java @@ -27,6 +27,8 @@ import androidx.annotation.NonNull; import androidx.core.app.NotificationCompat; import se.leap.bitmaskclient.R; +import se.leap.bitmaskclient.base.models.Provider; +import se.leap.bitmaskclient.base.models.ProviderObservable; public class TorNotificationManager { public final static int TOR_SERVICE_NOTIFICATION_ID = 10; @@ -45,7 +47,7 @@ public class TorNotificationManager { return notificationBuilder .setSmallIcon(R.drawable.ic_bridge_36) .setWhen(System.currentTimeMillis()) - .setContentTitle(context.getString(R.string.tor_provider_setup)).build(); + .setContentTitle(context.getString(R.string.tor_started)).build(); } public void buildTorNotification(Context context, String state) { @@ -58,8 +60,7 @@ public class TorNotificationManager { .setSmallIcon(R.drawable.ic_bridge_36) .setWhen(System.currentTimeMillis()) .setTicker(state) - .setContentTitle(context.getString(R.string.tor_provider_setup)) - .setContentText(state); + .setContentTitle(state); notificationManager.notify(TOR_SERVICE_NOTIFICATION_ID, notificationBuilder.build()); } -- cgit v1.2.3 From 9687a96f2608be215c6ddcc22f2ecfe6a889093a Mon Sep 17 00:00:00 2001 From: cyBerta Date: Thu, 24 Jun 2021 23:57:48 +0200 Subject: use notifications contentText instead of contentTitle, so that the complete tor notification text can be shown --- .../main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java') diff --git a/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java b/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java index 71b5c378..a2401732 100644 --- a/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java +++ b/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java @@ -47,7 +47,7 @@ public class TorNotificationManager { return notificationBuilder .setSmallIcon(R.drawable.ic_bridge_36) .setWhen(System.currentTimeMillis()) - .setContentTitle(context.getString(R.string.tor_started)).build(); + .setContentText(context.getString(R.string.tor_started)).build(); } public void buildTorNotification(Context context, String state) { @@ -60,7 +60,7 @@ public class TorNotificationManager { .setSmallIcon(R.drawable.ic_bridge_36) .setWhen(System.currentTimeMillis()) .setTicker(state) - .setContentTitle(state); + .setContentText(state); notificationManager.notify(TOR_SERVICE_NOTIFICATION_ID, notificationBuilder.build()); } -- cgit v1.2.3 From fcec92a0042477347338a25cd072d622edfa03c9 Mon Sep 17 00:00:00 2001 From: cyBerta Date: Sat, 16 Oct 2021 11:59:42 +0200 Subject: show tor bootstrapping progress in notifications --- .../se/leap/bitmaskclient/tor/TorNotificationManager.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java') diff --git a/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java b/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java index a2401732..8148fd94 100644 --- a/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java +++ b/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java @@ -50,7 +50,7 @@ public class TorNotificationManager { .setContentText(context.getString(R.string.tor_started)).build(); } - public void buildTorNotification(Context context, String state) { + public void buildTorNotification(Context context, String state, String message, int progress) { NotificationManager notificationManager = initNotificationManager(context); if (notificationManager == null) { return; @@ -59,8 +59,15 @@ public class TorNotificationManager { notificationBuilder .setSmallIcon(R.drawable.ic_bridge_36) .setWhen(System.currentTimeMillis()) - .setTicker(state) - .setContentText(state); + .setStyle(new NotificationCompat.BigTextStyle(). + setBigContentTitle(state). + bigText(message)) + .setTicker(message) + .setContentTitle(state) + .setContentText(message); + if (progress > 0) { + notificationBuilder.setProgress(100, progress, false); + } notificationManager.notify(TOR_SERVICE_NOTIFICATION_ID, notificationBuilder.build()); } -- cgit v1.2.3 From f6b8a78812c46679cf416edc303d88e840750827 Mon Sep 17 00:00:00 2001 From: cyBerta Date: Sat, 16 Oct 2021 13:52:24 +0200 Subject: cleanup imports --- .../main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java | 3 --- 1 file changed, 3 deletions(-) (limited to 'app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java') diff --git a/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java b/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java index 8148fd94..45570857 100644 --- a/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java +++ b/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java @@ -23,12 +23,9 @@ import android.app.NotificationManager; import android.content.Context; import android.os.Build; -import androidx.annotation.NonNull; import androidx.core.app.NotificationCompat; import se.leap.bitmaskclient.R; -import se.leap.bitmaskclient.base.models.Provider; -import se.leap.bitmaskclient.base.models.ProviderObservable; public class TorNotificationManager { public final static int TOR_SERVICE_NOTIFICATION_ID = 10; -- cgit v1.2.3 From 7b9cb67eb7aa3eadf99a5187d2ea79b83ab57380 Mon Sep 17 00:00:00 2001 From: cyBerta Date: Fri, 5 Nov 2021 22:53:07 +0100 Subject: localize notification channel description for tor notifications --- .../java/se/leap/bitmaskclient/tor/TorNotificationManager.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java') diff --git a/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java b/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java index 45570857..c8bc0e2d 100644 --- a/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java +++ b/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java @@ -75,15 +75,16 @@ public class TorNotificationManager { return null; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - createNotificationChannel(notificationManager); + createNotificationChannel(context, notificationManager); } return notificationManager; } @TargetApi(26) - private static void createNotificationChannel(NotificationManager notificationManager) { - CharSequence name = "Bitmask Tor Service"; - String description = "Informs about usage of bridges to configure Bitmask."; + private static void createNotificationChannel(Context context, NotificationManager notificationManager) { + String appName = context.getString(R.string.app_name); + CharSequence name = context.getString(R.string.channel_name_tor_service, appName); + String description = context.getString(R.string.channel_description_tor_service, appName); NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_NEWSTATUS_ID, name, NotificationManager.IMPORTANCE_LOW); -- cgit v1.2.3 From f317ef54d24d987698f7fe26eaccc53b0a51fe12 Mon Sep 17 00:00:00 2001 From: cyBerta Date: Fri, 5 Nov 2021 22:55:37 +0100 Subject: tweak notification settings --- .../main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java') diff --git a/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java b/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java index c8bc0e2d..507de4ae 100644 --- a/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java +++ b/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java @@ -61,6 +61,7 @@ public class TorNotificationManager { bigText(message)) .setTicker(message) .setContentTitle(state) + .setOnlyAlertOnce(true) .setContentText(message); if (progress > 0) { notificationBuilder.setProgress(100, progress, false); @@ -97,7 +98,8 @@ public class TorNotificationManager { NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_NEWSTATUS_ID); notificationBuilder. setDefaults(Notification.DEFAULT_ALL). - setAutoCancel(true); + setLocalOnly(true). + setAutoCancel(false); return notificationBuilder; } -- cgit v1.2.3 From c38240b5776cc2e5b1057652787367ffb80eb5a1 Mon Sep 17 00:00:00 2001 From: cyBerta Date: Tue, 9 Nov 2021 23:35:21 +0100 Subject: debounce tor notifications to max. 2 per second --- .../se/leap/bitmaskclient/tor/TorNotificationManager.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java') diff --git a/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java b/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java index 507de4ae..3f3fbf4f 100644 --- a/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java +++ b/app/src/main/java/se/leap/bitmaskclient/tor/TorNotificationManager.java @@ -30,6 +30,9 @@ import se.leap.bitmaskclient.R; public class TorNotificationManager { public final static int TOR_SERVICE_NOTIFICATION_ID = 10; static final String NOTIFICATION_CHANNEL_NEWSTATUS_ID = "bitmask_tor_service_news"; + private long lastNotificationTime = 0; + // debounce timeout in milliseconds + private final static long NOTIFICATION_DEBOUNCE_TIME = 500; public TorNotificationManager() {} @@ -48,6 +51,9 @@ public class TorNotificationManager { } public void buildTorNotification(Context context, String state, String message, int progress) { + if (shouldDropNotification()) { + return; + } NotificationManager notificationManager = initNotificationManager(context); if (notificationManager == null) { return; @@ -69,6 +75,15 @@ public class TorNotificationManager { notificationManager.notify(TOR_SERVICE_NOTIFICATION_ID, notificationBuilder.build()); } + private boolean shouldDropNotification() { + long now = System.currentTimeMillis(); + if (now - lastNotificationTime < NOTIFICATION_DEBOUNCE_TIME) { + return true; + } + lastNotificationTime = now; + return false; + } + private static NotificationManager initNotificationManager(Context context) { NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); -- cgit v1.2.3