mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-08-30 21:55:10 +00:00
Added a setting to disable the persistent notification pre-oreo
This commit is contained in:
@@ -297,5 +297,7 @@
|
|||||||
<string name="settings_dark_mode">Dark mode</string>
|
<string name="settings_dark_mode">Dark mode</string>
|
||||||
<string name="settings_more_settings_title">More settings</string>
|
<string name="settings_more_settings_title">More settings</string>
|
||||||
<string name="settings_more_settings_text">Per-device settings can be found under \'Plugin settings\' from within a device.</string>
|
<string name="settings_more_settings_text">Per-device settings can be found under \'Plugin settings\' from within a device.</string>
|
||||||
|
<string name="setting_persistent_notification">Show persistent notification</string>
|
||||||
|
<string name="setting_persistent_notification_oreo_description">Required by Android since Android 8.0</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
@@ -138,10 +138,13 @@ public class BackgroundService extends Service {
|
|||||||
callback.onDeviceListChanged();
|
callback.onDeviceListChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (NotificationHelper.isPersistentNotificationEnabled(this)) {
|
||||||
//Update the foreground notification with the currently connected device list
|
//Update the foreground notification with the currently connected device list
|
||||||
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
nm.notify(FOREGROUND_NOTIFICATION_ID, createForegroundNotification());
|
nm.notify(FOREGROUND_NOTIFICATION_ID, createForegroundNotification());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void loadRememberedDevicesFromSettings() {
|
private void loadRememberedDevicesFromSettings() {
|
||||||
//Log.e("BackgroundService", "Loading remembered trusted devices");
|
//Log.e("BackgroundService", "Loading remembered trusted devices");
|
||||||
@@ -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() {
|
private Notification createForegroundNotification() {
|
||||||
|
|
||||||
//Why is this needed: https://developer.android.com/guide/components/services#Foreground
|
//Why is this needed: https://developer.android.com/guide/components/services#Foreground
|
||||||
@@ -364,7 +378,9 @@ public class BackgroundService extends Service {
|
|||||||
mutex.unlock();
|
mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (NotificationHelper.isPersistentNotificationEnabled(this)) {
|
||||||
startForeground(FOREGROUND_NOTIFICATION_ID, createForegroundNotification());
|
startForeground(FOREGROUND_NOTIFICATION_ID, createForegroundNotification());
|
||||||
|
}
|
||||||
return Service.START_STICKY;
|
return Service.START_STICKY;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -4,6 +4,9 @@ import android.app.Notification;
|
|||||||
import android.app.NotificationChannel;
|
import android.app.NotificationChannel;
|
||||||
import android.app.NotificationManager;
|
import android.app.NotificationManager;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
import android.os.Build;
|
||||||
|
import android.preference.PreferenceManager;
|
||||||
|
|
||||||
import org.kde.kdeconnect_tp.R;
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -6,11 +6,15 @@ import android.os.Build;
|
|||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
import android.support.v14.preference.SwitchPreference;
|
import android.support.v14.preference.SwitchPreference;
|
||||||
|
import android.support.v7.preference.CheckBoxPreference;
|
||||||
import android.support.v7.preference.Preference;
|
import android.support.v7.preference.Preference;
|
||||||
import android.support.v7.preference.PreferenceFragmentCompat;
|
import android.support.v7.preference.PreferenceFragmentCompat;
|
||||||
import android.support.v7.preference.PreferenceScreen;
|
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.DeviceHelper;
|
||||||
|
import org.kde.kdeconnect.Helpers.NotificationHelper;
|
||||||
import org.kde.kdeconnect_tp.R;
|
import org.kde.kdeconnect_tp.R;
|
||||||
|
|
||||||
public class SettingsFragment extends PreferenceFragmentCompat implements MainActivity.NameChangeCallback {
|
public class SettingsFragment extends PreferenceFragmentCompat implements MainActivity.NameChangeCallback {
|
||||||
@@ -24,6 +28,13 @@ public class SettingsFragment extends PreferenceFragmentCompat implements MainAc
|
|||||||
super.onDestroy();
|
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
|
@Override
|
||||||
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
|
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
|
||||||
|
|
||||||
@@ -53,7 +64,7 @@ public class SettingsFragment extends PreferenceFragmentCompat implements MainAc
|
|||||||
|
|
||||||
// Dark mode
|
// Dark mode
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
|
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.setPersistent(false);
|
||||||
darkThemeSwitch.setChecked(ThemeUtil.shouldUseDarkTheme(context));
|
darkThemeSwitch.setChecked(ThemeUtil.shouldUseDarkTheme(context));
|
||||||
darkThemeSwitch.setTitle(R.string.settings_dark_mode);
|
darkThemeSwitch.setTitle(R.string.settings_dark_mode);
|
||||||
@@ -71,9 +82,25 @@ public class SettingsFragment extends PreferenceFragmentCompat implements MainAc
|
|||||||
screen.addPreference(darkThemeSwitch);
|
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
|
// More settings text
|
||||||
Preference moreSettingsText = new Preference(context);
|
Preference moreSettingsText = new Preference(context);
|
||||||
@@ -92,4 +119,5 @@ public class SettingsFragment extends PreferenceFragmentCompat implements MainAc
|
|||||||
public void onNameChanged(String newName) {
|
public void onNameChanged(String newName) {
|
||||||
renameDevice.setSummary(newName);
|
renameDevice.setSummary(newName);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user