From 41774a562958424b7f128b641c52db8daa771195 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Thu, 20 Oct 2022 15:36:29 +0200 Subject: [PATCH] Disable notifications on Android API levels less than 26 --- sbapp/main.py | 30 +++++---- sbapp/services/sidebandservice.py | 107 ++++++++++++++++-------------- 2 files changed, 75 insertions(+), 62 deletions(-) diff --git a/sbapp/main.py b/sbapp/main.py index 14d7ce1..64d6007 100644 --- a/sbapp/main.py +++ b/sbapp/main.py @@ -55,6 +55,7 @@ if RNS.vendor.platformutils.get_platform() == "android": from android.permissions import request_permissions, check_permission from kivymd.utils.set_bars_colors import set_bars_colors + android_api_version = autoclass('android.os.Build$VERSION').SDK_INT else: from .sideband.core import SidebandCore @@ -151,6 +152,9 @@ class SidebandApp(MDApp): self.sideband.setstate("app.foreground", True) def start_service(self): + if RNS.vendor.platformutils.is_android(): + RNS.log("Running on Android API level "+str(android_api_version)) + RNS.log("Launching platform-specific service for RNS and LXMF") if RNS.vendor.platformutils.get_platform() == "android": self.android_service = autoclass('io.unsigned.sideband.ServiceSidebandservice') @@ -217,10 +221,8 @@ class SidebandApp(MDApp): self.update_ui_colors() - st = time.time() - RNS.log("Recursing widgets...") - for wid in self.root.ids: - RNS.log("Found: "+str(wid)+str(self.root.ids[wid])) + # for wid in self.root.ids: + # RNS.log("Found: "+str(wid)+str(self.root.ids[wid])) def set_bars_colors(self): if RNS.vendor.platformutils.get_platform() == "android": @@ -868,13 +870,13 @@ class SidebandApp(MDApp): ### Settings screen ###################################### def settings_action(self, sender=None): - self.root.ids.screen_manager.transition.direction = "left" - - self.settings_init() - - self.root.ids.screen_manager.current = "settings_screen" self.root.ids.nav_drawer.set_state("closed") - self.sideband.setstate("app.displaying", self.root.ids.screen_manager.current) + def cb(dt): + self.root.ids.screen_manager.transition.direction = "left" + self.settings_init() + self.root.ids.screen_manager.current = "settings_screen" + self.sideband.setstate("app.displaying", self.root.ids.screen_manager.current) + Clock.schedule_once(cb, 0.2) def settings_init(self, sender=None): if not self.settings_ready: @@ -969,8 +971,12 @@ class SidebandApp(MDApp): self.root.ids.settings_propagation_node_address.bind(on_text_validate=save_prop_addr) self.root.ids.settings_propagation_node_address.bind(focus=save_prop_addr) - self.root.ids.settings_notifications_on.active = self.sideband.config["notifications_on"] - self.root.ids.settings_notifications_on.bind(active=save_notifications_on) + if android_api_version >= 26: + self.root.ids.settings_notifications_on.active = self.sideband.config["notifications_on"] + self.root.ids.settings_notifications_on.bind(active=save_notifications_on) + else: + self.root.ids.settings_notifications_on.active = False + self.root.ids.settings_notifications_on.disabled = True self.root.ids.settings_dark_ui.active = self.sideband.config["dark_ui"] self.root.ids.settings_dark_ui.bind(active=save_dark_ui) diff --git a/sbapp/services/sidebandservice.py b/sbapp/services/sidebandservice.py index 826ffe7..59dddca 100644 --- a/sbapp/services/sidebandservice.py +++ b/sbapp/services/sidebandservice.py @@ -14,6 +14,8 @@ else: if RNS.vendor.platformutils.get_platform() == "android": from jnius import autoclass, cast from android import python_act + android_api_version = autoclass('android.os.Build$VERSION').SDK_INT + Context = autoclass('android.content.Context') Intent = autoclass('android.content.Intent') BitmapFactory = autoclass('android.graphics.BitmapFactory') @@ -22,9 +24,11 @@ if RNS.vendor.platformutils.get_platform() == "android": AndroidString = autoclass('java.lang.String') NotificationManager = autoclass('android.app.NotificationManager') Context = autoclass('android.content.Context') - NotificationBuilder = autoclass('android.app.Notification$Builder') - NotificationChannel = autoclass('android.app.NotificationChannel') - + + if android_api_version >= 26: + NotificationBuilder = autoclass('android.app.Notification$Builder') + NotificationChannel = autoclass('android.app.NotificationChannel') + from usb4a import usb from usbserial4a import serial4a from sideband.core import SidebandCore @@ -34,62 +38,65 @@ else: class SidebandService(): def android_notification(self, title="", content="", ticker="", group=None, context_id=None): - package_name = "io.unsigned.sideband" - - if not self.notification_service: - self.notification_service = cast(NotificationManager, self.app_context.getSystemService( - Context.NOTIFICATION_SERVICE - )) - - channel_id = package_name - group_id = "" - if group != None: - channel_id += "."+str(group) - group_id += str(group) - if context_id != None: - channel_id += "."+str(context_id) - group_id += "."+str(context_id) - - if not title or title == "": - channel_name = "Sideband" + if android_api_version < 26: + return else: - channel_name = title + package_name = "io.unsigned.sideband" - self.notification_channel = NotificationChannel(channel_id, channel_name, NotificationManager.IMPORTANCE_DEFAULT) - self.notification_channel.enableVibration(True) - self.notification_channel.setShowBadge(True) - self.notification_service.createNotificationChannel(self.notification_channel) + if not self.notification_service: + self.notification_service = cast(NotificationManager, self.app_context.getSystemService( + Context.NOTIFICATION_SERVICE + )) - notification = NotificationBuilder(self.app_context, channel_id) - notification.setContentTitle(title) - notification.setContentText(AndroidString(content)) - - # if group != None: - # notification.setGroup(group_id) + channel_id = package_name + group_id = "" + if group != None: + channel_id += "."+str(group) + group_id += str(group) + if context_id != None: + channel_id += "."+str(context_id) + group_id += "."+str(context_id) - if not self.notification_small_icon: - path = self.sideband.notification_icon - bitmap = BitmapFactory.decodeFile(path) - self.notification_small_icon = Icon.createWithBitmap(bitmap) + if not title or title == "": + channel_name = "Sideband" + else: + channel_name = title - notification.setSmallIcon(self.notification_small_icon) + self.notification_channel = NotificationChannel(channel_id, channel_name, NotificationManager.IMPORTANCE_DEFAULT) + self.notification_channel.enableVibration(True) + self.notification_channel.setShowBadge(True) + self.notification_service.createNotificationChannel(self.notification_channel) - # large_icon_path = self.sideband.icon - # bitmap_icon = BitmapFactory.decodeFile(large_icon_path) - # notification.setLargeIcon(bitmap_icon) + notification = NotificationBuilder(self.app_context, channel_id) + notification.setContentTitle(title) + notification.setContentText(AndroidString(content)) + + # if group != None: + # notification.setGroup(group_id) - if not self.notification_intent: - notification_intent = Intent(self.app_context, python_act) - notification_intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP) - notification_intent.setAction(Intent.ACTION_MAIN) - notification_intent.addCategory(Intent.CATEGORY_LAUNCHER) - self.notification_intent = PendingIntent.getActivity(self.app_context, 0, notification_intent, 0) + if not self.notification_small_icon: + path = self.sideband.notification_icon + bitmap = BitmapFactory.decodeFile(path) + self.notification_small_icon = Icon.createWithBitmap(bitmap) - notification.setContentIntent(self.notification_intent) - notification.setAutoCancel(True) + notification.setSmallIcon(self.notification_small_icon) - built_notification = notification.build() - self.notification_service.notify(0, built_notification) + # large_icon_path = self.sideband.icon + # bitmap_icon = BitmapFactory.decodeFile(large_icon_path) + # notification.setLargeIcon(bitmap_icon) + + if not self.notification_intent: + notification_intent = Intent(self.app_context, python_act) + notification_intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP) + notification_intent.setAction(Intent.ACTION_MAIN) + notification_intent.addCategory(Intent.CATEGORY_LAUNCHER) + self.notification_intent = PendingIntent.getActivity(self.app_context, 0, notification_intent, 0) + + notification.setContentIntent(self.notification_intent) + notification.setAutoCancel(True) + + built_notification = notification.build() + self.notification_service.notify(0, built_notification) def __init__(self): self.argument = environ.get('PYTHON_SERVICE_ARGUMENT', '')