2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-29 13:17:43 +00:00

Added a setting to disable the persistent notification pre-oreo

This commit is contained in:
Albert Vaca 2018-10-25 16:53:10 +02:00
parent e90bd904e9
commit fcf3032965
4 changed files with 70 additions and 7 deletions

View File

@ -297,5 +297,7 @@
<string name="settings_dark_mode">Dark mode</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="setting_persistent_notification">Show persistent notification</string>
<string name="setting_persistent_notification_oreo_description">Required by Android since Android 8.0</string>
</resources>

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -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);
}
}