1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-25 19:36:50 +01:00

Upgrade notification settings to androidx

This commit is contained in:
José Rebelo 2023-07-25 19:52:12 +01:00
parent 53c06b8c31
commit bac1d35479
7 changed files with 157 additions and 141 deletions

View File

@ -42,7 +42,6 @@ import androidx.preference.Preference;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.charts.AbstractPreferenceFragment;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceManager;
public class AboutUserPreferencesActivity extends AbstractGBActivity {

View File

@ -14,7 +14,7 @@
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.activities.charts;
package nodomain.freeyourgadget.gadgetbridge.activities;
import android.content.SharedPreferences;
import android.os.Bundle;

View File

@ -22,7 +22,6 @@ import android.os.Bundle;
import androidx.fragment.app.Fragment;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.charts.AbstractPreferenceFragment;
public class DiscoveryPairingPreferenceActivity extends AbstractGBActivity {
@Override

View File

@ -1,22 +1,5 @@
/* Copyright (C) 2015-2020 Andreas Shimokawa, Carsten Pfeiffer, Lem Dulfo,
vanous
This file is part of Gadgetbridge.
Gadgetbridge is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Gadgetbridge is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* Copyright (C) 2015-2020 Andreas Shimokawa, Carsten Pfeiffer, Lem Dulfo,
vanous
/* Copyright (C) 2015-2023 Andreas Shimokawa, Carsten Pfeiffer, Lem Dulfo,
vanous, José Rebelo
This file is part of Gadgetbridge.
@ -35,18 +18,14 @@
package nodomain.freeyourgadget.gadgetbridge.activities;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.widget.EditText;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -56,7 +35,7 @@ import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
public class NotificationManagementActivity extends AbstractSettingsActivity {
public class NotificationManagementActivity extends AbstractGBActivity {
private static final Logger LOG = LoggerFactory.getLogger(NotificationManagementActivity.class);
private static final int RINGTONE_REQUEST_CODE = 4712;
private static final String DEFAULT_RINGTONE_URI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE).toString();
@ -64,112 +43,114 @@ public class NotificationManagementActivity extends AbstractSettingsActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.notifications_preferences);
setContentView(R.layout.activity_device_settings);
if (savedInstanceState == null) {
Fragment fragment = getSupportFragmentManager().findFragmentByTag(NotificationPreferencesFragment.FRAGMENT_TAG);
if (fragment == null) {
fragment = new NotificationPreferencesFragment();
}
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings_container, fragment, NotificationPreferencesFragment.FRAGMENT_TAG)
.commit();
}
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
public static class NotificationPreferencesFragment extends AbstractPreferenceFragment {
static final String FRAGMENT_TAG = "NOTIFICATION_PREFERENCES_FRAGMENT";
Prefs prefs = GBApplication.getPrefs();
Preference pref = findPreference("notifications_generic");
pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
Intent enableIntent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
@Override
protected void onSharedPreferenceChanged(final Preference preference) {
if (GBPrefs.PING_TONE.equals(preference.getKey())) {
try {
final Prefs prefs = GBApplication.getPrefs();
// This fails on some ROMs. The actual implementation falls-back to an internal ping tone
preference.setSummary(RingtoneManager.getRingtone(requireContext(), Uri.parse(prefs.getString(GBPrefs.PING_TONE, DEFAULT_RINGTONE_URI))).getTitle(requireContext()));
} catch (final Exception e) {
LOG.error("Failed to find the configured ping ringtone", e);
preference.setSummary("-");
}
}
}
@Override
public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) {
addPreferencesFromResource(R.xml.notifications_preferences);
Preference pref = findPreference("notifications_generic");
pref.setOnPreferenceClickListener(preference -> {
final Intent enableIntent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
startActivity(enableIntent);
return true;
}
});
});
pref = findPreference(GBPrefs.PING_TONE);
pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
pref = findPreference(GBPrefs.PING_TONE);
pref.setOnPreferenceClickListener(preference -> {
final Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Set Ping tone");
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_ALL);
startActivityForResult(intent, RINGTONE_REQUEST_CODE);
return true;
}
});
});
try {
// This fails on some ROMs. The actual implementation falls-back to an internal ping tone
pref.setSummary(RingtoneManager.getRingtone(this, Uri.parse(prefs.getString(GBPrefs.PING_TONE, DEFAULT_RINGTONE_URI))).getTitle(this));
} catch (final Exception e) {
LOG.error("Failed to find the configured ping ringtone");
pref.setSummary("-");
}
pref = findPreference("pref_key_blacklist");
pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
Intent enableIntent = new Intent(NotificationManagementActivity.this, AppBlacklistActivity.class);
pref = findPreference("pref_key_blacklist");
pref.setOnPreferenceClickListener(preference -> {
final Intent enableIntent = new Intent(requireContext(), AppBlacklistActivity.class);
startActivity(enableIntent);
return true;
}
});
});
if (!GBApplication.isRunningMarshmallowOrLater()) {
pref = findPreference("notification_filter");
PreferenceCategory category = (PreferenceCategory) findPreference("pref_key_notifications");
category.removePreference(pref);
}
pref = findPreference("notifications_settings");
pref.setOnPreferenceClickListener(preference -> {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) {
LOG.warn("This preference should not be displayed in Android < O");
return true;
}
pref = findPreference("notifications_settings");
pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
final Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
intent.putExtra(Settings.EXTRA_APP_PACKAGE, requireActivity().getPackageName());
//This could open notification channel settings, if needed...:
//Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
//intent.putExtra(Settings.EXTRA_CHANNEL_ID, GB.NOTIFICATION_CHANNEL_ID_TRANSFER);
startActivity(intent);
return true;
});
final PreferenceCategory notificationsCategory = findPreference("pref_key_notifications");
if (!GBApplication.isRunningMarshmallowOrLater()) {
pref = findPreference("notification_filter");
notificationsCategory.removePreference(pref);
}
});
if (GBApplication.isRunningTenOrLater()) {
pref = findPreference("minimize_priority");
PreferenceCategory category = (PreferenceCategory) findPreference("pref_key_notifications");
category.removePreference(pref);
if (GBApplication.isRunningTenOrLater()) {
pref = findPreference("minimize_priority");
notificationsCategory.removePreference(pref);
}
if (!GBApplication.isRunningOreoOrLater()) {
pref = findPreference("notifications_settings");
notificationsCategory.removePreference(pref);
}
}
if (!GBApplication.isRunningOreoOrLater()) {
pref = findPreference("notifications_settings");
PreferenceCategory category = (PreferenceCategory) findPreference("pref_key_notifications");
category.removePreference(pref);
}
}
@Override
protected String[] getPreferenceKeysWithSummary() {
return new String[]{
};
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == RINGTONE_REQUEST_CODE && intent != null) {
if (intent.getExtras().getParcelable(RingtoneManager.EXTRA_RINGTONE_PICKED_URI) != null) {
Uri uri = intent.getExtras().getParcelable(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
Ringtone r = RingtoneManager.getRingtone(this, uri);
findPreference(GBPrefs.PING_TONE).setSummary(r.toString());
PreferenceManager
.getDefaultSharedPreferences(this)
.edit()
.putString(GBPrefs.PING_TONE, uri.toString())
.apply();
findPreference(GBPrefs.PING_TONE).setSummary(r.getTitle(this));
// TODO: Migrate this to ActivityResultContract
@Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent intent) {
if (requestCode == RINGTONE_REQUEST_CODE && intent != null) {
if (intent.getExtras().getParcelable(RingtoneManager.EXTRA_RINGTONE_PICKED_URI) != null) {
final Uri uri = intent.getExtras().getParcelable(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
GBApplication.getPrefs()
.getPreferences()
.edit()
.putString(GBPrefs.PING_TONE, uri.toString())
.apply();
}
}
}
}
}

View File

@ -27,6 +27,7 @@ import androidx.preference.Preference;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.AboutUserPreferencesActivity;
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractGBActivity;
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractPreferenceFragment;
import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs;
public class ChartsPreferencesActivity extends AbstractGBActivity {

View File

@ -40,7 +40,7 @@ import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.CalBlacklistActivity;
import nodomain.freeyourgadget.gadgetbridge.activities.ConfigureContacts;
import nodomain.freeyourgadget.gadgetbridge.activities.ConfigureWorldClocks;
import nodomain.freeyourgadget.gadgetbridge.activities.charts.AbstractPreferenceFragment;
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractPreferenceFragment;
import nodomain.freeyourgadget.gadgetbridge.activities.loyaltycards.LoyaltyCardsSettingsActivity;
import nodomain.freeyourgadget.gadgetbridge.activities.loyaltycards.LoyaltyCardsSettingsConst;
import nodomain.freeyourgadget.gadgetbridge.capabilities.HeartRateCapability;

View File

@ -1,76 +1,92 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory xmlns:app="http://schemas.android.com/apk/res-auto"
android:key="pref_key_notifications"
android:title="@string/pref_header_notifications">
android:title="@string/pref_header_notifications"
app:iconSpaceReserved="false">
<Preference
android:key="notifications_settings"
android:title="@string/pref_title_notifications_generic_settings" />
android:title="@string/pref_title_notifications_generic_settings"
app:iconSpaceReserved="false" />
<Preference
android:key="notifications_generic"
android:title="@string/pref_title_notifications_generic" />
android:title="@string/pref_title_notifications_generic"
app:iconSpaceReserved="false" />
<CheckBoxPreference
<SwitchPreference
android:defaultValue="false"
android:key="notifications_generic_whenscreenon"
android:layout="@layout/preference_checkbox"
android:title="@string/pref_title_whenscreenon" />
android:title="@string/pref_title_whenscreenon"
app:iconSpaceReserved="false" />
<CheckBoxPreference
<SwitchPreference
android:defaultValue="true"
android:key="notifications_ignore_low_priority"
android:layout="@layout/preference_checkbox"
android:summary="@string/pref_summary_notifications_ignore_low_priority"
android:title="@string/pref_title_notifications_ignore_low_priority"
android:summary="@string/pref_summary_notifications_ignore_low_priority" />
app:iconSpaceReserved="false" />
<CheckBoxPreference
<SwitchPreference
android:defaultValue="true"
android:key="notification_prefer_long_text"
android:layout="@layout/preference_checkbox"
android:summary="@string/pref_summary_notification_prefer_long_text"
android:title="@string/pref_title_notification_prefer_long_text"
android:summary="@string/pref_summary_notification_prefer_long_text" />
app:iconSpaceReserved="false" />
<CheckBoxPreference
<SwitchPreference
android:defaultValue="false"
android:key="notification_cache_while_disconnected"
android:layout="@layout/preference_checkbox"
android:summary="@string/pref_summary_notification_cache_while_disconnected"
android:title="@string/pref_title_notification_cache_while_disconnected"
android:summary="@string/pref_summary_notification_cache_while_disconnected" />
app:iconSpaceReserved="false" />
<CheckBoxPreference
<SwitchPreference
android:defaultValue="false"
android:key="minimize_priority"
android:layout="@layout/preference_checkbox"
android:summaryOff="@string/pref_summary_minimize_priority_off"
android:summaryOn="@string/pref_summary_minimize_priority_on"
android:title="@string/pref_title_minimize_priority" />
android:title="@string/pref_title_minimize_priority"
app:iconSpaceReserved="false" />
<Preference
android:id="@+id/preference"
android:key="ping_tone"
android:title="@string/pref_title_ping_tone"
app:iconSpaceReserved="false"
app:summary="%s" />
<ListPreference
android:defaultValue="always"
android:entries="@array/notification_mode_toggle"
android:entryValues="@array/notification_mode_values_toggle"
android:key="notification_mode_calls"
android:summary="%s"
android:title="@string/pref_title_notifications_call" />
<CheckBoxPreference
android:title="@string/pref_title_notifications_call"
app:iconSpaceReserved="false" />
<SwitchPreference
android:defaultValue="false"
android:key="notification_support_voip_calls"
android:layout="@layout/preference_checkbox"
android:title="@string/pref_title_support_voip_calls" />
android:title="@string/pref_title_support_voip_calls"
app:iconSpaceReserved="false" />
<EditTextPreference
android:defaultValue="0"
android:inputType="number"
android:digits="0123456789"
android:inputType="number"
android:key="notification_delay_calls"
android:maxLength="2"
android:summary="@string/pref_summary_notification_delay_calls"
android:title="@string/pref_title_notification_delay_calls"
android:summary="@string/pref_summary_notification_delay_calls" />
app:iconSpaceReserved="false" />
<ListPreference
android:defaultValue="when_screen_off"
@ -78,7 +94,8 @@
android:entryValues="@array/notification_mode_values"
android:key="notification_mode_sms"
android:summary="%s"
android:title="@string/pref_title_notifications_sms" />
android:title="@string/pref_title_notifications_sms"
app:iconSpaceReserved="false" />
<ListPreference
android:defaultValue="when_screen_off"
@ -86,7 +103,8 @@
android:entryValues="@array/notification_mode_values"
android:key="notification_mode_pebblemsg"
android:summary="@string/pref_summary_notifications_pebblemsg"
android:title="@string/pref_title_notifications_pebblemsg" />
android:title="@string/pref_title_notifications_pebblemsg"
app:iconSpaceReserved="false" />
<ListPreference
android:defaultValue="0"
@ -94,48 +112,66 @@
android:entryValues="@array/notifications_timeout_values"
android:key="notifications_timeout"
android:summary="%s"
android:title="@string/pref_title_notifications_timeout" />
android:title="@string/pref_title_notifications_timeout"
app:iconSpaceReserved="false" />
<CheckBoxPreference
<SwitchPreference
android:defaultValue="false"
android:key="notification_filter"
android:layout="@layout/preference_checkbox"
android:summary="@string/pref_summary_notification_filter"
android:title="@string/pref_title_notification_filter" />
android:title="@string/pref_title_notification_filter"
app:iconSpaceReserved="false" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/pref_header_notification_application_settings">
<PreferenceCategory
android:title="@string/pref_header_notification_application_settings"
app:iconSpaceReserved="false">
<ListPreference
android:defaultValue="true"
android:entries="@array/notification_list_is_blacklist_names"
android:entryValues="@array/notification_list_is_blacklist_values"
android:key="notification_list_is_blacklist"
android:summary="%s"
android:title="@string/pref_title_notification_use_as" />
<CheckBoxPreference
android:title="@string/pref_title_notification_use_as"
app:iconSpaceReserved="false" />
<SwitchPreference
android:defaultValue="false"
android:key="notification_media_ignores_application_list"
android:layout="@layout/preference_checkbox"
android:summary="@string/pref_summary_notification_media_ignores_application_list"
android:title="@string/pref_title_notification_media_ignores_application_list" />
android:title="@string/pref_title_notification_media_ignores_application_list"
app:iconSpaceReserved="false" />
<Preference
android:key="pref_key_blacklist"
android:title="@string/pref_applications_settings" />
android:title="@string/pref_applications_settings"
app:iconSpaceReserved="false" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/pref_header_privacy">
<PreferenceCategory
android:title="@string/pref_header_privacy"
app:iconSpaceReserved="false">
<ListPreference
android:defaultValue="@string/p_call_privacy_mode_off"
android:entries="@array/pref_call_privacy_mode"
android:entryValues="@array/pref_call_privacy_mode_values"
android:key="pref_call_privacy_mode"
android:summary="%s"
android:title="@string/pref_title_call_privacy_mode" />
android:title="@string/pref_title_call_privacy_mode"
app:iconSpaceReserved="false" />
<ListPreference
android:defaultValue="off"
android:entries="@array/pref_message_privacy_mode"
android:entryValues="@array/pref_message_privacy_mode_values"
android:key="pref_message_privacy_mode"
android:summary="%s"
android:title="@string/pref_title_message_privacy_mode" />
android:title="@string/pref_title_message_privacy_mode"
app:iconSpaceReserved="false" />
</PreferenceCategory>
</PreferenceScreen>