Commit a6ae97af authored by Piyush Aggarwal's avatar Piyush Aggarwal 🎮 Committed by Albert Astals Cid

Allow disabling persistent notification in Android 8.0 and up

Summary:
`Show persistent notification` toggle is disabled from Android O and up
This patch aims to enable the toggle with link to Notifications Settings, so that user can itself disable the persistent notification if desired

Test Plan: Apply patch, use on Android O or up device and use `Show persistent notification` toggle. It should take you to Notifications settings page of KDE Connect

Reviewers: #kde_connect, albertvaka

Reviewed By: #kde_connect, albertvaka

Subscribers: albertvaka, eduisters, nicolasfella, kdeconnect, #kde_connect

Tags: #kde_connect

Differential Revision: https://phabricator.kde.org/D18181
parent 8466a6f8
......@@ -314,8 +314,8 @@
<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>
<string name="setting_persistent_notification_pie_description">Since Android 9.0, this notification can only be minimized by long tapping on it</string>
<string name="setting_persistent_notification_oreo">Persistent notification</string>
<string name="setting_persistent_notification_description">Tap to enable/disable in Notification settings</string>
<string name="extra_options">Extra options</string>
<string name="privacy_options">Privacy options</string>
......
package org.kde.kdeconnect.UserInterface;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
......@@ -73,26 +74,40 @@ public class SettingsFragment extends PreferenceFragmentCompat implements MainAc
});
screen.addPreference(darkThemeSwitch);
// Persistent notification toggle
final TwoStatePreference notificationSwitch = new SwitchPreferenceCompat(context);
notificationSwitch.setPersistent(false);
notificationSwitch.setChecked(NotificationHelper.isPersistentNotificationEnabled(context));
notificationSwitch.setTitle(R.string.setting_persistent_notification);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
notificationSwitch.setSummary(R.string.setting_persistent_notification_pie_description);
notificationSwitch.setEnabled(false);
} else 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));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
return true;
});
screen.addPreference(notificationSwitch);
Preference persistentNotif = new Preference(context);
persistentNotif.setTitle(R.string.setting_persistent_notification_oreo);
persistentNotif.setSummary(R.string.setting_persistent_notification_description);
persistentNotif.setOnPreferenceClickListener(preference -> {
Intent intent = new Intent();
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
intent.putExtra("android.provider.extra.APP_PACKAGE", context.getPackageName());
context.startActivity(intent);
return true;
});
screen.addPreference(persistentNotif);
} else {
// Persistent notification toggle for Android Versions below Oreo
final TwoStatePreference notificationSwitch = new SwitchPreferenceCompat(context);
notificationSwitch.setPersistent(false);
notificationSwitch.setChecked(NotificationHelper.isPersistentNotificationEnabled(context));
notificationSwitch.setTitle(R.string.setting_persistent_notification);
notificationSwitch.setOnPreferenceChangeListener((preference, newValue) -> {
final boolean isChecked = (Boolean) newValue;
NotificationHelper.setPersistentNotificationEnabled(context, isChecked);
BackgroundService.RunCommand(context,
service -> service.changePersistentNotificationVisibility(isChecked));
NotificationHelper.setPersistentNotificationEnabled(context, isChecked);
return true;
});
screen.addPreference(notificationSwitch);
}
// More settings text
Preference moreSettingsText = new Preference(context);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment