From 737c63e8b0c39ddd0c37995d1f68556fd4b5c1e0 Mon Sep 17 00:00:00 2001 From: Philipp Crocoll Date: Fri, 9 Feb 2018 12:07:21 +0100 Subject: [PATCH] switch to TargetSDK 26, implement notification channels allowing customization of notification importance on Android 8, see #178 --- .../Properties/AndroidManifest_debug.xml | 2 +- .../Properties/AndroidManifest_net.xml | 2 +- .../Resources/values/strings.xml | 9 ++++ src/keepass2android/app/App.cs | 52 +++++++++++++++++-- .../services/CopyToClipboardService.cs | 2 +- .../services/OngoingNotificationsService.cs | 4 +- 6 files changed, 62 insertions(+), 9 deletions(-) diff --git a/src/keepass2android/Properties/AndroidManifest_debug.xml b/src/keepass2android/Properties/AndroidManifest_debug.xml index 41eebd8e..8904de3d 100644 --- a/src/keepass2android/Properties/AndroidManifest_debug.xml +++ b/src/keepass2android/Properties/AndroidManifest_debug.xml @@ -1,6 +1,6 @@  - + diff --git a/src/keepass2android/Properties/AndroidManifest_net.xml b/src/keepass2android/Properties/AndroidManifest_net.xml index 51c4108d..0eca3213 100644 --- a/src/keepass2android/Properties/AndroidManifest_net.xml +++ b/src/keepass2android/Properties/AndroidManifest_net.xml @@ -4,7 +4,7 @@ android:versionName="1.04" package="keepass2android.keepass2android" android:installLocation="auto"> - + diff --git a/src/keepass2android/Resources/values/strings.xml b/src/keepass2android/Resources/values/strings.xml index aefa97be..c56f5ffc 100644 --- a/src/keepass2android/Resources/values/strings.xml +++ b/src/keepass2android/Resources/values/strings.xml @@ -689,6 +689,15 @@ + Database unlocked + Notification about the database being unlocked + + QuickUnlock + Notification about the database being locked with QuickUnlock + + Entry notifications + Notification to simplify access to the currently selected entry. + Keepass2Android: An error occurred. An unexpected error occurred while running Keepass2Android. Please help us fix this by allowing the app to send error reports. Error reports will never contain any contents of your database or master password. You can disable them in the application settings. diff --git a/src/keepass2android/app/App.cs b/src/keepass2android/app/App.cs index fc859254..8992011c 100644 --- a/src/keepass2android/app/App.cs +++ b/src/keepass2android/app/App.cs @@ -21,6 +21,7 @@ using System.IO; using System.Net.Security; using Android.App; using Android.Content; +using Android.Graphics; using Android.Graphics.Drawables; using Android.OS; using Android.Runtime; @@ -845,7 +846,11 @@ namespace keepass2android #endif public class App : Application { - public App (IntPtr javaReference, JniHandleOwnership transfer) + public const string NotificationChannelIdUnlocked = "channel_db_unlocked_3"; + public const string NotificationChannelIdQuicklocked = "channel_db_quicklocked_3"; + public const string NotificationChannelIdEntry = "channel_db_entry_3"; + + public App (IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) { } @@ -857,14 +862,53 @@ namespace keepass2android Kp2aLog.Log("Creating application "+PackageName+". Version=" + PackageManager.GetPackageInfo(PackageName, 0).VersionCode); + CreateNotificationChannels(); + Kp2a.OnCreate(this); AndroidEnvironment.UnhandledExceptionRaiser += MyApp_UnhandledExceptionHandler; } + private void CreateNotificationChannels() + { + NotificationManager mNotificationManager = + (NotificationManager)GetSystemService(Context.NotificationService); - - - public override void OnTerminate() { + { + string name = GetString(Resource.String.DbUnlockedChannel_name); + string desc = GetString(Resource.String.DbUnlockedChannel_desc); + NotificationChannel mChannel = + new NotificationChannel(NotificationChannelIdUnlocked, name, NotificationImportance.Low); + mChannel.Description = desc; + mChannel.EnableLights(false); + mChannel.EnableVibration(false); + mNotificationManager.CreateNotificationChannel(mChannel); + } + + { + string name = GetString(Resource.String.DbQuicklockedChannel_name); + string desc = GetString(Resource.String.DbQuicklockedChannel_desc); + NotificationChannel mChannel = + new NotificationChannel(NotificationChannelIdQuicklocked, name, NotificationImportance.Min); + mChannel.Description = desc; + mChannel.EnableLights(false); + mChannel.EnableVibration(false); + mNotificationManager.CreateNotificationChannel(mChannel); + } + + { + string name = GetString(Resource.String.EntryChannel_name); + string desc = GetString(Resource.String.EntryChannel_desc); + NotificationChannel mChannel = + new NotificationChannel(NotificationChannelIdEntry, name, NotificationImportance.None); + mChannel.Description = desc; + mChannel.EnableLights(false); + mChannel.EnableVibration(false); + mNotificationManager.CreateNotificationChannel(mChannel); + } + } + + + public override void OnTerminate() { base.OnTerminate(); Kp2aLog.Log("Terminating application"); Kp2a.OnTerminate(); diff --git a/src/keepass2android/services/CopyToClipboardService.cs b/src/keepass2android/services/CopyToClipboardService.cs index 52b67c87..36344f63 100644 --- a/src/keepass2android/services/CopyToClipboardService.cs +++ b/src/keepass2android/services/CopyToClipboardService.cs @@ -198,7 +198,7 @@ namespace keepass2android pending = GetPendingIntent(intentText, descResId); } - var builder = new NotificationCompat.Builder(_ctx); + var builder = new NotificationCompat.Builder(_ctx, App.NotificationChannelIdEntry); builder.SetSmallIcon(drawableResId) .SetContentText(desc) .SetContentTitle(entryName) diff --git a/src/keepass2android/services/OngoingNotificationsService.cs b/src/keepass2android/services/OngoingNotificationsService.cs index 976a1373..5fe3c5c7 100644 --- a/src/keepass2android/services/OngoingNotificationsService.cs +++ b/src/keepass2android/services/OngoingNotificationsService.cs @@ -139,7 +139,7 @@ namespace keepass2android grayIconResouceId = Resource.Drawable.transparent; } NotificationCompat.Builder builder = - new NotificationCompat.Builder(this) + new NotificationCompat.Builder(this, App.NotificationChannelIdQuicklocked) .SetSmallIcon(grayIconResouceId) .SetLargeIcon(MakeLargeIcon(BitmapFactory.DecodeResource(Resources, AppNames.NotificationLockedIcon))) .SetVisibility((int)Android.App.NotificationVisibility.Secret) @@ -183,7 +183,7 @@ namespace keepass2android private Notification GetUnlockedNotification() { NotificationCompat.Builder builder = - new NotificationCompat.Builder(this) + new NotificationCompat.Builder(this, App.NotificationChannelIdUnlocked) .SetOngoing(true) .SetSmallIcon(Resource.Drawable.ic_notify) .SetLargeIcon(MakeLargeIcon(BitmapFactory.DecodeResource(Resources, AppNames.NotificationUnlockedIcon)))