2024-01-10 18:54:00 +01:00
|
|
|
/* Copyright (C) 2021-2024 José Rebelo, Petr Vaněk
|
2021-09-26 21:59:11 +02:00
|
|
|
|
|
|
|
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
|
2024-01-10 18:54:00 +01:00
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
2021-09-26 21:59:11 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.activities;
|
|
|
|
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.media.RingtoneManager;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Bundle;
|
2022-02-23 10:25:10 +01:00
|
|
|
import android.provider.Settings;
|
2022-06-06 01:38:34 +02:00
|
|
|
|
2023-07-25 20:52:12 +02:00
|
|
|
import androidx.fragment.app.Fragment;
|
|
|
|
import androidx.preference.Preference;
|
|
|
|
import androidx.preference.PreferenceCategory;
|
2023-07-27 00:08:13 +02:00
|
|
|
import androidx.preference.PreferenceFragmentCompat;
|
2021-09-26 21:59:11 +02:00
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
|
|
|
|
2023-07-27 00:08:13 +02:00
|
|
|
public class NotificationManagementActivity extends AbstractSettingsActivityV2 {
|
2021-09-26 21:59:11 +02:00
|
|
|
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();
|
|
|
|
|
|
|
|
@Override
|
2023-07-27 00:08:13 +02:00
|
|
|
protected String fragmentTag() {
|
|
|
|
return NotificationPreferencesFragment.FRAGMENT_TAG;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected PreferenceFragmentCompat newFragment() {
|
|
|
|
return new NotificationPreferencesFragment();
|
2021-09-26 21:59:11 +02:00
|
|
|
}
|
|
|
|
|
2023-07-25 20:52:12 +02:00
|
|
|
public static class NotificationPreferencesFragment extends AbstractPreferenceFragment {
|
|
|
|
static final String FRAGMENT_TAG = "NOTIFICATION_PREFERENCES_FRAGMENT";
|
|
|
|
|
|
|
|
@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("-");
|
|
|
|
}
|
2021-09-26 21:59:11 +02:00
|
|
|
}
|
2023-07-25 20:52:12 +02:00
|
|
|
}
|
2021-09-26 21:59:11 +02:00
|
|
|
|
2023-07-25 20:52:12 +02:00
|
|
|
@Override
|
|
|
|
public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) {
|
2023-07-27 00:08:13 +02:00
|
|
|
setPreferencesFromResource(R.xml.notifications_preferences, rootKey);
|
2021-09-26 21:59:11 +02:00
|
|
|
|
2023-07-25 20:52:12 +02:00
|
|
|
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(preference -> {
|
|
|
|
final Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
|
2021-09-26 21:59:11 +02:00
|
|
|
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;
|
2023-07-25 20:52:12 +02:00
|
|
|
});
|
2021-09-26 21:59:11 +02:00
|
|
|
|
2023-07-25 20:52:12 +02:00
|
|
|
pref = findPreference("pref_key_blacklist");
|
|
|
|
pref.setOnPreferenceClickListener(preference -> {
|
|
|
|
final Intent enableIntent = new Intent(requireContext(), AppBlacklistActivity.class);
|
2021-09-26 21:59:11 +02:00
|
|
|
startActivity(enableIntent);
|
|
|
|
return true;
|
2023-07-25 20:52:12 +02:00
|
|
|
});
|
2021-09-26 21:59:11 +02:00
|
|
|
|
2023-07-25 20:52:12 +02:00
|
|
|
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;
|
|
|
|
}
|
2021-09-26 21:59:11 +02:00
|
|
|
|
2023-07-25 20:52:12 +02:00
|
|
|
final Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
|
2022-02-23 10:25:10 +01:00
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
2023-07-25 20:52:12 +02:00
|
|
|
intent.putExtra(Settings.EXTRA_APP_PACKAGE, requireActivity().getPackageName());
|
2022-02-23 10:25:10 +01:00
|
|
|
//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;
|
2023-07-25 20:52:12 +02:00
|
|
|
});
|
2022-02-23 10:25:10 +01:00
|
|
|
|
2023-07-25 20:52:12 +02:00
|
|
|
final PreferenceCategory notificationsCategory = findPreference("pref_key_notifications");
|
2022-02-23 10:25:10 +01:00
|
|
|
|
2023-07-25 20:52:12 +02:00
|
|
|
if (!GBApplication.isRunningMarshmallowOrLater()) {
|
|
|
|
pref = findPreference("notification_filter");
|
|
|
|
notificationsCategory.removePreference(pref);
|
|
|
|
}
|
2021-09-26 21:59:11 +02:00
|
|
|
|
2023-07-25 20:52:12 +02:00
|
|
|
if (GBApplication.isRunningTenOrLater()) {
|
|
|
|
pref = findPreference("minimize_priority");
|
|
|
|
notificationsCategory.removePreference(pref);
|
|
|
|
}
|
2021-09-26 21:59:11 +02:00
|
|
|
|
2023-07-25 20:52:12 +02:00
|
|
|
if (!GBApplication.isRunningOreoOrLater()) {
|
|
|
|
pref = findPreference("notifications_settings");
|
|
|
|
notificationsCategory.removePreference(pref);
|
|
|
|
}
|
|
|
|
}
|
2021-09-26 21:59:11 +02:00
|
|
|
|
2023-07-25 20:52:12 +02:00
|
|
|
// 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();
|
|
|
|
}
|
2021-09-26 21:59:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|