| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452 |
- /* _____ _
- * |_ _| |_ _ _ ___ ___ _ __ __ _
- * | | | ' \| '_/ -_) -_) ' \/ _` |_
- * |_| |_||_|_| \___\___|_|_|_\__,_(_)
- *
- * Threema for Android
- * Copyright (c) 2013-2021 Threema GmbH
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * This program 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 <https://www.gnu.org/licenses/>.
- */
- package ch.threema.app.preference;
- import android.app.Activity;
- import android.content.ActivityNotFoundException;
- import android.content.ComponentName;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.media.RingtoneManager;
- import android.net.Uri;
- import android.os.Build;
- import android.os.Bundle;
- import android.provider.Settings;
- import android.text.format.DateFormat;
- import android.view.View;
- import com.google.android.material.timepicker.MaterialTimePicker;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import java.text.DateFormatSymbols;
- import java.util.Arrays;
- import java.util.Locale;
- import java.util.Set;
- import androidx.activity.result.ActivityResultLauncher;
- import androidx.activity.result.contract.ActivityResultContracts;
- import androidx.annotation.Nullable;
- import androidx.annotation.StringRes;
- import androidx.core.app.NotificationManagerCompat;
- import androidx.preference.CheckBoxPreference;
- import androidx.preference.MultiSelectListPreference;
- import androidx.preference.Preference;
- import androidx.preference.PreferenceCategory;
- import androidx.preference.PreferenceScreen;
- import ch.threema.app.BuildConfig;
- import ch.threema.app.R;
- import ch.threema.app.ThreemaApplication;
- import ch.threema.app.dialogs.GenericAlertDialog;
- import ch.threema.app.dialogs.RingtoneSelectorDialog;
- import ch.threema.app.dialogs.ShowOnceDialog;
- import ch.threema.app.utils.AppRestrictionUtil;
- import ch.threema.app.utils.ConfigUtils;
- import ch.threema.app.utils.RingtoneUtil;
- import static com.google.android.material.timepicker.TimeFormat.CLOCK_12H;
- import static com.google.android.material.timepicker.TimeFormat.CLOCK_24H;
- public class SettingsNotificationsFragment extends ThreemaPreferenceFragment implements GenericAlertDialog.DialogClickListener, RingtoneSelectorDialog.RingtoneSelectorDialogClickListener {
- private static final Logger logger = LoggerFactory.getLogger(SettingsNotificationsFragment.class);
- private static final String DIALOG_TAG_NOTIFICATIONS_DISABLED = "ndd";
- private static final String DIALOG_TAG_CONTACT_NOTIFICATION = "cn";
- private static final String DIALOG_TAG_GROUP_NOTIFICATION = "gn";
- private static final String DIALOG_TAG_VOIP_NOTIFICATION = "vn";
- private static final String DIALOG_TAG_MIUI_NOTICE = "miui10_channel_notice";
- private static final int INTENT_SYSTEM_NOTIFICATION_SETTINGS = 5199;
- private SharedPreferences sharedPreferences;
- private NotificationManagerCompat notificationManagerCompat;
- // Weekdays used for work-life balance prefs
- private final String[] weekdays = new String[7];
- private final String[] shortWeekdays = new String[7];
- private final String[] weekday_values = new String[]{"0", "1", "2", "3", "4", "5", "6"};
- private Preference startPreference, endPreference;
- private Preference ringtonePreference, groupRingtonePreference, voiceRingtonePreference;
- private final ActivityResultLauncher<Intent> voipRingtonePickerLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
- result -> {
- if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) {
- Uri uri = result.getData().getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
- onRingtoneSelected(DIALOG_TAG_VOIP_NOTIFICATION, uri);
- }
- });
- private final ActivityResultLauncher<Intent> contactTonePickerLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
- result -> {
- if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) {
- Uri uri = result.getData().getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
- onRingtoneSelected(DIALOG_TAG_CONTACT_NOTIFICATION, uri);
- }
- });
- private final ActivityResultLauncher<Intent> groupTonePickerLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
- result -> {
- if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) {
- Uri uri = result.getData().getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
- onRingtoneSelected(DIALOG_TAG_GROUP_NOTIFICATION, uri);
- }
- });
- private void initWorkingTimePrefs() {
- if (!ConfigUtils.isWorkBuild()) {
- // remove preferences
- PreferenceScreen preferenceScreen = findPreference("pref_key_notifications");
- PreferenceCategory preferenceCategory = findPreference("pref_key_work_life_balance");
- preferenceScreen.removePreference(preferenceCategory);
- return;
- }
- DateFormatSymbols dfs = new DateFormatSymbols(getResources().getConfiguration().locale);
- System.arraycopy(dfs.getWeekdays(), 1, weekdays, 0, 7);
- System.arraycopy(dfs.getShortWeekdays(), 1, shortWeekdays, 0, 7);
- MultiSelectListPreference multiSelectListPreference = findPreference(getString(R.string.preferences__working_days));
- multiSelectListPreference.setEntries(weekdays);
- multiSelectListPreference.setOnPreferenceChangeListener((preference, newValue) -> {
- updateWorkingDaysSummary(preference, (Set<String>)newValue);
- return true;
- });
- updateWorkingDaysSummary(multiSelectListPreference, multiSelectListPreference.getValues());
- startPreference = findPreference(getString(R.string.preferences__work_time_start));
- updateTimeSummary(startPreference, R.string.prefs_work_time_start_sum);
- startPreference.setOnPreferenceClickListener(preference -> {
- int[] startTime = splitDateFromPrefs(R.string.preferences__work_time_start);
- final MaterialTimePicker timePicker = new MaterialTimePicker.Builder()
- .setTitleText(R.string.prefs_work_time_start)
- .setHour(startTime != null ? startTime[0] : 0)
- .setMinute(startTime != null ? startTime[1] : 0)
- .setTimeFormat(DateFormat.is24HourFormat(getContext()) ? CLOCK_24H : CLOCK_12H)
- .build();
- timePicker.addOnPositiveButtonClickListener(v1 -> {
- int[] endTime = splitDateFromPrefs(R.string.preferences__work_time_end);
- if (endTime != null) {
- int newTimeStamp = timePicker.getHour() * 60 + timePicker.getMinute();
- int endTimeStamp = endTime[0] * 60 + endTime[1];
- if (newTimeStamp >= endTimeStamp) {
- return;
- }
- }
- String newValue = String.format(Locale.US, "%02d:%02d", timePicker.getHour(), timePicker.getMinute());
- sharedPreferences.edit().putString(getResources().getString(R.string.preferences__work_time_start), newValue).apply();
- updateTimeSummary(startPreference, R.string.prefs_work_time_start_sum);
- });
- if (isAdded()) {
- timePicker.show(getParentFragmentManager(), "startt");
- }
- return true;
- });
- endPreference = findPreference(getString(R.string.preferences__work_time_end));
- updateTimeSummary(endPreference, R.string.prefs_work_time_end_sum);
- endPreference.setOnPreferenceClickListener(preference -> {
- int[] endTime = splitDateFromPrefs(R.string.preferences__work_time_end);
- final MaterialTimePicker timePicker = new MaterialTimePicker.Builder()
- .setTitleText(R.string.prefs_work_time_end)
- .setHour(endTime != null ? endTime[0] : 0)
- .setMinute(endTime != null ? endTime[1] : 0)
- .setTimeFormat(DateFormat.is24HourFormat(getContext()) ? CLOCK_24H : CLOCK_12H)
- .build();
- timePicker.addOnPositiveButtonClickListener(v1 -> {
- int[] startTime = splitDateFromPrefs(R.string.preferences__work_time_start);
- if (startTime != null) {
- int newTimeStamp = timePicker.getHour() * 60 + timePicker.getMinute();
- int startTimeStamp = startTime[0] * 60 + startTime[1];
- if (newTimeStamp <= startTimeStamp) {
- return;
- }
- }
- String newValue = String.format(Locale.US, "%02d:%02d", timePicker.getHour(), timePicker.getMinute());
- sharedPreferences.edit().putString(getResources().getString(R.string.preferences__work_time_end), newValue).apply();
- updateTimeSummary(endPreference, R.string.prefs_work_time_end_sum);
- });
- if (isAdded()) {
- timePicker.show(getParentFragmentManager(), "endt");
- }
- return true;
- });
- }
- private void updateWorkingDaysSummary(Preference preference, Set<String> values) {
- StringBuilder summary = new StringBuilder();
- for (String value : values) {
- int index = Arrays.asList(weekday_values).indexOf(value);
- if (summary.length() > 0) {
- summary.append(", ");
- }
- summary.append(shortWeekdays[index]);
- }
- if (summary.length() == 0) {
- summary = new StringBuilder(getString(R.string.prefs_working_days_sum));
- }
- preference.setSummary(summary);
- }
- @Nullable
- private int[] splitDateFromPrefs(@StringRes int key) {
- String value = sharedPreferences.getString(getString(key), null);
- if (value == null) {
- return null;
- }
- try {
- String[] hourMinuteString = value.split(":");
- int[] hourMinuteInt = new int[2];
- hourMinuteInt[0] = Integer.parseInt(hourMinuteString[0]);
- hourMinuteInt[1] = Integer.parseInt(hourMinuteString[1]);
- return hourMinuteInt;
- } catch (Exception e) {
- return null;
- }
- }
- @Override
- public void onCreatePreferencesFix(@Nullable Bundle savedInstanceState, String rootKey) {
- sharedPreferences = getPreferenceManager().getSharedPreferences();
- addPreferencesFromResource(R.xml.preference_notifications);
- int miuiVersion = ConfigUtils.getMIUIVersion();
- if (miuiVersion < 10) {
- PreferenceScreen preferenceScreen = findPreference("pref_key_notifications");
- preferenceScreen.removePreference(findPreference("pref_key_miui"));
- }
- notificationManagerCompat = NotificationManagerCompat.from(getActivity());
- initWorkingTimePrefs();
- // setup defaults and callbacks
- ringtonePreference = findPreference(getResources().getString(R.string.preferences__notification_sound));
- updateRingtoneSummary(ringtonePreference, sharedPreferences.getString(getResources().getString(R.string.preferences__notification_sound), ""));
- groupRingtonePreference = findPreference(getResources().getString(R.string.preferences__group_notification_sound));
- updateRingtoneSummary(groupRingtonePreference, sharedPreferences.getString(getResources().getString(R.string.preferences__group_notification_sound), ""));
- voiceRingtonePreference = findPreference(getResources().getString(R.string.preferences__voip_ringtone));
- updateRingtoneSummary(voiceRingtonePreference, sharedPreferences.getString(getResources().getString(R.string.preferences__voip_ringtone), ""));
- ringtonePreference.setOnPreferenceClickListener(preference -> {
- chooseRingtone(RingtoneManager.TYPE_NOTIFICATION,
- getRingtoneFromRingtonePref(R.string.preferences__notification_sound),
- null,
- getString(R.string.prefs_notification_sound),
- DIALOG_TAG_CONTACT_NOTIFICATION);
- return true;
- });
- groupRingtonePreference.setOnPreferenceClickListener(preference -> {
- chooseRingtone(RingtoneManager.TYPE_NOTIFICATION,
- getRingtoneFromRingtonePref(R.string.preferences__group_notification_sound),
- null,
- getString(R.string.prefs_notification_sound),
- DIALOG_TAG_GROUP_NOTIFICATION);
- return true;
- });
- voiceRingtonePreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
- @Override
- public boolean onPreferenceClick(Preference preference) {
- chooseRingtone(RingtoneManager.TYPE_RINGTONE, getRingtoneFromRingtonePref(R.string.preferences__voip_ringtone), RingtoneUtil.THREEMA_CALL_RINGTONE_URI, getString(R.string.prefs_voice_call_sound), DIALOG_TAG_VOIP_NOTIFICATION);
- return true;
- }
- });
- if (ConfigUtils.isWorkRestricted()) {
- CheckBoxPreference notificationPreview = findPreference(getString(R.string.preferences__notification_preview));
- Boolean value = AppRestrictionUtil.getBooleanRestriction(getString(R.string.restriction__disable_message_preview));
- if (value != null) {
- notificationPreview.setEnabled(false);
- notificationPreview.setSelectable(false);
- }
- }
- if (miuiVersion >= 10) {
- ShowOnceDialog.newInstance(
- R.string.miui_notification_title,
- miuiVersion >= 12 ?
- R.string.miui12_notification_body:
- R.string.miui_notification_body).show(getFragmentManager(), DIALOG_TAG_MIUI_NOTICE);
- Preference miuiPreference = findPreference("pref_key_miui");
- miuiPreference.setOnPreferenceClickListener(preference -> {
- openMIUINotificationSettings();
- return true;
- });
- }
- }
- private void chooseRingtone(final int type, final Uri currentUri, final Uri defaultUri, final String title, final String tag) {
- try {
- Intent intent = RingtoneUtil.getRingtonePickerIntent(type, currentUri, defaultUri);
- switch (tag) {
- case DIALOG_TAG_VOIP_NOTIFICATION:
- voipRingtonePickerLauncher.launch(intent);
- break;
- case DIALOG_TAG_CONTACT_NOTIFICATION:
- contactTonePickerLauncher.launch(intent);
- break;
- case DIALOG_TAG_GROUP_NOTIFICATION:
- groupTonePickerLauncher.launch(intent);
- break;
- }
- } catch (ActivityNotFoundException e) {
- RingtoneSelectorDialog dialog = RingtoneSelectorDialog.newInstance(
- title,
- type,
- currentUri,
- defaultUri,
- true,
- true);
- dialog.setTargetFragment(SettingsNotificationsFragment.this, 0);
- dialog.show(getFragmentManager(), tag);
- }
- }
- @Override
- public void onViewCreated(View view, Bundle savedInstanceState) {
- preferenceFragmentCallbackInterface.setToolbarTitle(R.string.prefs_notifications);
- super.onViewCreated(view, savedInstanceState);
- if (!notificationManagerCompat.areNotificationsEnabled()) {
- showNotificationsDisabledDialog();
- }
- }
- private void showNotificationsDisabledDialog() {
- GenericAlertDialog dialog = GenericAlertDialog.newInstance(R.string.notifications_disabled_title, R.string.notifications_disabled_text, R.string.notifications_disabled_settings, R.string.cancel);
- dialog.setTargetFragment(this, 0);
- dialog.show(getFragmentManager(), DIALOG_TAG_NOTIFICATIONS_DISABLED);
- }
- private Uri getRingtoneFromRingtonePref(@StringRes int preference) {
- String uriString = sharedPreferences.getString(getResources().getString(preference), null);
- if (uriString == null) {
- // silent
- uriString = "";
- }
- return Uri.parse(uriString);
- }
- private void updateRingtoneSummary(Preference preference, String value) {
- String summary = null;
- if (value == null || value.length() == 0) {
- summary = getString(R.string.ringtone_none);
- } else {
- summary = RingtoneUtil.getRingtoneNameFromUri(getContext(), Uri.parse(value));
- }
- preference.setSummary(summary);
- }
- private void updateTimeSummary(Preference preference, @StringRes int defaultSummary) {
- preference.setSummary(sharedPreferences.getString(preference.getKey(), getString(defaultSummary)));
- }
- private void openMIUINotificationSettings() {
- ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.Settings$NotificationFilterActivity");
- Bundle bundle = new Bundle();
- bundle.putString("appName", getContext().getResources().getString(getContext().getApplicationInfo().labelRes));
- bundle.putString("packageName", BuildConfig.APPLICATION_ID);
- bundle.putString(":android:show_fragment", "NotificationAccessSettings");
- Intent intent = new Intent();
- intent.putExtras(bundle);
- intent.setComponent(cn);
- try {
- startActivity(intent);
- } catch (Exception e) {
- logger.error("Exception", e);
- }
- }
- @Override
- public void onYes(String tag, Object data) {
- Intent intent = new Intent();
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
- intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
- // for Android 5-7
- intent.putExtra("app_package", getActivity().getPackageName());
- intent.putExtra("app_uid", getActivity().getApplicationInfo().uid);
- // for Android O
- intent.putExtra("android.provider.extra.APP_PACKAGE", getActivity().getPackageName());
- } else {
- intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
- intent.addCategory(Intent.CATEGORY_DEFAULT);
- intent.setData(Uri.parse("package:" + getActivity().getPackageName()));
- }
- startActivityForResult(intent, INTENT_SYSTEM_NOTIFICATION_SETTINGS);
- }
- @Override
- public void onNo(String tag, Object data) {
- // ignore disabled notifications
- }
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if (requestCode == INTENT_SYSTEM_NOTIFICATION_SETTINGS && !notificationManagerCompat.areNotificationsEnabled()) {
- // return from system settings but notifications still disabled
- showNotificationsDisabledDialog();
- }
- }
- @Override
- public void onRingtoneSelected(String tag, Uri ringtone) {
- String toneString = ringtone != null ? ringtone.toString() : "";
- switch (tag) {
- case DIALOG_TAG_CONTACT_NOTIFICATION:
- sharedPreferences.edit().putString(ThreemaApplication.getAppContext().getString(R.string.preferences__notification_sound), toneString).apply();
- updateRingtoneSummary(ringtonePreference, sharedPreferences.getString(getResources().getString(R.string.preferences__notification_sound), ""));
- break;
- case DIALOG_TAG_GROUP_NOTIFICATION:
- sharedPreferences.edit().putString(ThreemaApplication.getAppContext().getString(R.string.preferences__group_notification_sound), toneString).apply();
- updateRingtoneSummary(groupRingtonePreference, sharedPreferences.getString(getResources().getString(R.string.preferences__group_notification_sound), ""));
- break;
- case DIALOG_TAG_VOIP_NOTIFICATION:
- sharedPreferences.edit().putString(ThreemaApplication.getAppContext().getString(R.string.preferences__voip_ringtone), toneString).apply();
- updateRingtoneSummary(voiceRingtonePreference, sharedPreferences.getString(getResources().getString(R.string.preferences__voip_ringtone), ""));
- break;
- }
- }
- @Override
- public void onCancel(String tag) {}
- }
|