diff --git a/res/values/strings.xml b/res/values/strings.xml
index b6c2cf21..4c5567cd 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -297,5 +297,7 @@
Dark mode
More settings
Per-device settings can be found under \'Plugin settings\' from within a device.
+ Show persistent notification
+ Required by Android since Android 8.0
diff --git a/src/org/kde/kdeconnect/BackgroundService.java b/src/org/kde/kdeconnect/BackgroundService.java
index 6e98e5e9..86b6d035 100644
--- a/src/org/kde/kdeconnect/BackgroundService.java
+++ b/src/org/kde/kdeconnect/BackgroundService.java
@@ -138,9 +138,12 @@ public class BackgroundService extends Service {
callback.onDeviceListChanged();
}
- //Update the foreground notification with the currently connected device list
- NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- nm.notify(FOREGROUND_NOTIFICATION_ID, createForegroundNotification());
+
+ if (NotificationHelper.isPersistentNotificationEnabled(this)) {
+ //Update the foreground notification with the currently connected device list
+ NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
+ nm.notify(FOREGROUND_NOTIFICATION_ID, createForegroundNotification());
+ }
}
private void loadRememberedDevicesFromSettings() {
@@ -284,6 +287,17 @@ public class BackgroundService extends Service {
}
}
+
+ public void changePersistentNotificationVisibility(boolean visible) {
+ NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
+ if (visible) {
+ nm.notify(FOREGROUND_NOTIFICATION_ID, createForegroundNotification());
+ } else {
+ stopForeground(true);
+ Start(this);
+ }
+ }
+
private Notification createForegroundNotification() {
//Why is this needed: https://developer.android.com/guide/components/services#Foreground
@@ -364,7 +378,9 @@ public class BackgroundService extends Service {
mutex.unlock();
}
- startForeground(FOREGROUND_NOTIFICATION_ID, createForegroundNotification());
+ if (NotificationHelper.isPersistentNotificationEnabled(this)) {
+ startForeground(FOREGROUND_NOTIFICATION_ID, createForegroundNotification());
+ }
return Service.START_STICKY;
}
diff --git a/src/org/kde/kdeconnect/Helpers/NotificationHelper.java b/src/org/kde/kdeconnect/Helpers/NotificationHelper.java
index d9e14317..decc0483 100644
--- a/src/org/kde/kdeconnect/Helpers/NotificationHelper.java
+++ b/src/org/kde/kdeconnect/Helpers/NotificationHelper.java
@@ -4,6 +4,9 @@ import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
+import android.content.SharedPreferences;
+import android.os.Build;
+import android.preference.PreferenceManager;
import org.kde.kdeconnect_tp.R;
@@ -72,4 +75,18 @@ public class NotificationHelper {
}
+
+ public static void setPersistentNotificationEnabled(Context context, boolean enabled) {
+ SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
+ prefs.edit().putBoolean("persistentNotification", enabled).apply();
+ }
+
+ public static boolean isPersistentNotificationEnabled(Context context) {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ return true;
+ }
+ SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
+ return prefs.getBoolean("persistentNotification", false);
+ }
+
}
diff --git a/src/org/kde/kdeconnect/UserInterface/SettingsFragment.java b/src/org/kde/kdeconnect/UserInterface/SettingsFragment.java
index 20e2057e..4486bae0 100644
--- a/src/org/kde/kdeconnect/UserInterface/SettingsFragment.java
+++ b/src/org/kde/kdeconnect/UserInterface/SettingsFragment.java
@@ -6,11 +6,15 @@ import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v14.preference.SwitchPreference;
+import android.support.v7.preference.CheckBoxPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.support.v7.preference.PreferenceScreen;
+import android.support.v7.preference.TwoStatePreference;
+import org.kde.kdeconnect.BackgroundService;
import org.kde.kdeconnect.Helpers.DeviceHelper;
+import org.kde.kdeconnect.Helpers.NotificationHelper;
import org.kde.kdeconnect_tp.R;
public class SettingsFragment extends PreferenceFragmentCompat implements MainActivity.NameChangeCallback {
@@ -24,6 +28,13 @@ public class SettingsFragment extends PreferenceFragmentCompat implements MainAc
super.onDestroy();
}
+ TwoStatePreference createTwoStatePreferenceCompat(Context context) {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ return new SwitchPreference(context);
+ } else {
+ return new CheckBoxPreference(context);
+ }
+ }
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
@@ -53,7 +64,7 @@ public class SettingsFragment extends PreferenceFragmentCompat implements MainAc
// Dark mode
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
- final SwitchPreference darkThemeSwitch = new SwitchPreference(context);
+ final TwoStatePreference darkThemeSwitch = createTwoStatePreferenceCompat(context);
darkThemeSwitch.setPersistent(false);
darkThemeSwitch.setChecked(ThemeUtil.shouldUseDarkTheme(context));
darkThemeSwitch.setTitle(R.string.settings_dark_mode);
@@ -71,9 +82,25 @@ public class SettingsFragment extends PreferenceFragmentCompat implements MainAc
screen.addPreference(darkThemeSwitch);
}
+ // Persistent notification toggle
+ final TwoStatePreference notificationSwitch = createTwoStatePreferenceCompat(context);
+ notificationSwitch.setPersistent(false);
+ notificationSwitch.setChecked(NotificationHelper.isPersistentNotificationEnabled(context));
+ notificationSwitch.setTitle(R.string.setting_persistent_notification);
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ notificationSwitch.setSummary(R.string.setting_persistent_notification_oreo_description);
+ notificationSwitch.setEnabled(false);
+ }
+ notificationSwitch.setOnPreferenceChangeListener((preference, newValue) -> {
+ final boolean isChecked = (Boolean)newValue;
+ NotificationHelper.setPersistentNotificationEnabled(context, isChecked);
+ BackgroundService.RunCommand(context, service -> {
+ service.changePersistentNotificationVisibility(isChecked);
+ });
- //TODO: Persistent notification toggle for pre-oreo?
-
+ return true;
+ });
+ screen.addPreference(notificationSwitch);
// More settings text
Preference moreSettingsText = new Preference(context);
@@ -92,4 +119,5 @@ public class SettingsFragment extends PreferenceFragmentCompat implements MainAc
public void onNameChanged(String newName) {
renameDevice.setSummary(newName);
}
+
}