2014-11-04 15:19:48 +01:00
|
|
|
package org.thoughtcrime.securesms.preferences;
|
|
|
|
|
2017-12-01 12:55:24 -08:00
|
|
|
import android.annotation.SuppressLint;
|
2014-11-04 15:19:48 +01:00
|
|
|
import android.content.Context;
|
2017-12-01 12:55:24 -08:00
|
|
|
import android.content.Intent;
|
2014-11-04 15:19:48 +01:00
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.media.Ringtone;
|
|
|
|
import android.media.RingtoneManager;
|
|
|
|
import android.net.Uri;
|
2015-07-31 15:05:24 -07:00
|
|
|
import android.os.AsyncTask;
|
2014-11-04 15:19:48 +01:00
|
|
|
import android.os.Bundle;
|
2017-09-20 18:10:44 -07:00
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.support.v7.preference.ListPreference;
|
|
|
|
import android.support.v7.preference.Preference;
|
|
|
|
import android.support.v7.preference.PreferenceManager;
|
2014-11-04 15:19:48 +01:00
|
|
|
import android.text.TextUtils;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.ApplicationPreferencesActivity;
|
|
|
|
import org.thoughtcrime.securesms.R;
|
2015-07-31 15:05:24 -07:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.notifications.MessageNotifier;
|
2014-11-04 15:19:48 +01:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
|
|
|
|
2017-12-01 12:55:24 -08:00
|
|
|
import static android.app.Activity.RESULT_OK;
|
|
|
|
|
2014-12-11 22:08:04 -08:00
|
|
|
public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragment {
|
2014-11-04 15:19:48 +01:00
|
|
|
|
2017-12-01 12:55:24 -08:00
|
|
|
@SuppressWarnings("unused")
|
2017-09-20 18:10:44 -07:00
|
|
|
private static final String TAG = NotificationsPreferenceFragment.class.getSimpleName();
|
|
|
|
|
2015-07-31 15:05:24 -07:00
|
|
|
private MasterSecret masterSecret;
|
|
|
|
|
2014-11-04 15:19:48 +01:00
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle paramBundle) {
|
|
|
|
super.onCreate(paramBundle);
|
2015-07-31 15:05:24 -07:00
|
|
|
masterSecret = getArguments().getParcelable("master_secret");
|
2014-11-04 15:19:48 +01:00
|
|
|
|
|
|
|
this.findPreference(TextSecurePreferences.LED_COLOR_PREF)
|
2014-12-11 19:36:46 -08:00
|
|
|
.setOnPreferenceChangeListener(new ListSummaryListener());
|
2014-11-04 15:19:48 +01:00
|
|
|
this.findPreference(TextSecurePreferences.LED_BLINK_PREF)
|
2014-12-11 19:36:46 -08:00
|
|
|
.setOnPreferenceChangeListener(new ListSummaryListener());
|
2014-11-04 15:19:48 +01:00
|
|
|
this.findPreference(TextSecurePreferences.RINGTONE_PREF)
|
2014-12-11 19:36:46 -08:00
|
|
|
.setOnPreferenceChangeListener(new RingtoneSummaryListener());
|
|
|
|
this.findPreference(TextSecurePreferences.REPEAT_ALERTS_PREF)
|
|
|
|
.setOnPreferenceChangeListener(new ListSummaryListener());
|
2015-07-31 15:05:24 -07:00
|
|
|
this.findPreference(TextSecurePreferences.NOTIFICATION_PRIVACY_PREF)
|
|
|
|
.setOnPreferenceChangeListener(new NotificationPrivacyListener());
|
2017-04-21 23:04:54 -07:00
|
|
|
this.findPreference(TextSecurePreferences.NOTIFICATION_PRIORITY_PREF)
|
|
|
|
.setOnPreferenceChangeListener(new ListSummaryListener());
|
2014-11-04 15:19:48 +01:00
|
|
|
|
2017-12-01 12:55:24 -08:00
|
|
|
this.findPreference(TextSecurePreferences.RINGTONE_PREF)
|
|
|
|
.setOnPreferenceClickListener(preference -> {
|
|
|
|
String current = TextSecurePreferences.getNotificationRingtone(getContext());
|
|
|
|
|
|
|
|
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
|
|
|
|
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
|
|
|
|
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true);
|
|
|
|
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
|
|
|
|
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, current == null ? null : Uri.parse(current));
|
|
|
|
|
|
|
|
startActivityForResult(intent, 1);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2014-11-04 15:19:48 +01:00
|
|
|
initializeListSummary((ListPreference) findPreference(TextSecurePreferences.LED_COLOR_PREF));
|
|
|
|
initializeListSummary((ListPreference) findPreference(TextSecurePreferences.LED_BLINK_PREF));
|
2014-12-11 19:36:46 -08:00
|
|
|
initializeListSummary((ListPreference) findPreference(TextSecurePreferences.REPEAT_ALERTS_PREF));
|
2015-07-31 15:05:24 -07:00
|
|
|
initializeListSummary((ListPreference) findPreference(TextSecurePreferences.NOTIFICATION_PRIVACY_PREF));
|
2017-04-21 23:04:54 -07:00
|
|
|
initializeListSummary((ListPreference) findPreference(TextSecurePreferences.NOTIFICATION_PRIORITY_PREF));
|
2017-12-01 12:55:24 -08:00
|
|
|
initializeRingtoneSummary(findPreference(TextSecurePreferences.RINGTONE_PREF));
|
2017-09-20 18:10:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreatePreferences(@Nullable Bundle savedInstanceState, String rootKey) {
|
|
|
|
addPreferencesFromResource(R.xml.preferences_notifications);
|
2014-11-04 15:19:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
((ApplicationPreferencesActivity) getActivity()).getSupportActionBar().setTitle(R.string.preferences__notifications);
|
|
|
|
}
|
|
|
|
|
2017-12-01 12:55:24 -08:00
|
|
|
@Override
|
|
|
|
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
|
if (requestCode == 1 && resultCode == RESULT_OK && data != null) {
|
|
|
|
Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
|
|
|
|
|
|
|
|
TextSecurePreferences.setNotificationRingtone(getContext(), uri != null ? uri.toString() : null);
|
|
|
|
initializeRingtoneSummary(findPreference(TextSecurePreferences.RINGTONE_PREF));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-04 15:19:48 +01:00
|
|
|
private class RingtoneSummaryListener implements Preference.OnPreferenceChangeListener {
|
|
|
|
@Override
|
|
|
|
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
2017-09-20 18:10:44 -07:00
|
|
|
Uri value = (Uri) newValue;
|
2014-11-04 15:19:48 +01:00
|
|
|
|
2017-09-20 18:10:44 -07:00
|
|
|
if (value == null) {
|
2015-11-07 18:56:32 +01:00
|
|
|
preference.setSummary(R.string.preferences__silent);
|
2014-11-04 15:19:48 +01:00
|
|
|
} else {
|
2017-09-20 18:10:44 -07:00
|
|
|
Ringtone tone = RingtoneManager.getRingtone(getActivity(), value);
|
2017-12-01 12:55:24 -08:00
|
|
|
|
2014-11-04 15:19:48 +01:00
|
|
|
if (tone != null) {
|
|
|
|
preference.setSummary(tone.getTitle(getActivity()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-01 12:55:24 -08:00
|
|
|
private void initializeRingtoneSummary(Preference pref) {
|
2017-09-20 18:10:44 -07:00
|
|
|
RingtoneSummaryListener listener = (RingtoneSummaryListener) pref.getOnPreferenceChangeListener();
|
|
|
|
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
|
|
|
|
String encodedUri = sharedPreferences.getString(pref.getKey(), null);
|
|
|
|
Uri uri = !TextUtils.isEmpty(encodedUri) ? Uri.parse(encodedUri) : null;
|
2014-11-04 15:19:48 +01:00
|
|
|
|
2017-09-20 18:10:44 -07:00
|
|
|
listener.onPreferenceChange(pref, uri);
|
2014-11-04 15:19:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static CharSequence getSummary(Context context) {
|
|
|
|
final int onCapsResId = R.string.ApplicationPreferencesActivity_On;
|
|
|
|
final int offCapsResId = R.string.ApplicationPreferencesActivity_Off;
|
|
|
|
|
|
|
|
return context.getString(TextSecurePreferences.isNotificationsEnabled(context) ? onCapsResId : offCapsResId);
|
|
|
|
}
|
2015-07-31 15:05:24 -07:00
|
|
|
|
|
|
|
private class NotificationPrivacyListener extends ListSummaryListener {
|
2017-12-01 12:55:24 -08:00
|
|
|
@SuppressLint("StaticFieldLeak")
|
2015-07-31 15:05:24 -07:00
|
|
|
@Override
|
|
|
|
public boolean onPreferenceChange(Preference preference, Object value) {
|
|
|
|
new AsyncTask<Void, Void, Void>() {
|
|
|
|
@Override
|
|
|
|
protected Void doInBackground(Void... params) {
|
|
|
|
MessageNotifier.updateNotification(getActivity(), masterSecret);
|
|
|
|
return null;
|
|
|
|
}
|
2017-10-23 13:03:32 -07:00
|
|
|
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2015-07-31 15:05:24 -07:00
|
|
|
|
|
|
|
return super.onPreferenceChange(preference, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-11-04 15:19:48 +01:00
|
|
|
}
|