Reworked preferences

Making sure the summary was displaye properly for drop down preferences
This commit is contained in:
ThomasSession 2024-09-03 13:37:36 +10:00 committed by fanchao
parent ea24ff67b3
commit 97db1fe4e6
9 changed files with 134 additions and 219 deletions

View File

@ -9,7 +9,7 @@ import org.thoughtcrime.securesms.permissions.Permissions;
import network.loki.messenger.R;
public class ChatsPreferenceFragment extends ListSummaryPreferenceFragment {
public class ChatsPreferenceFragment extends CorrectedPreferenceFragment {
private static final String TAG = ChatsPreferenceFragment.class.getSimpleName();
@Override

View File

@ -1,29 +0,0 @@
package org.thoughtcrime.securesms.preferences;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import java.util.Arrays;
import network.loki.messenger.R;
public abstract class ListSummaryPreferenceFragment extends CorrectedPreferenceFragment {
protected class ListSummaryListener implements Preference.OnPreferenceChangeListener {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
ListPreference listPref = (ListPreference) preference;
int entryIndex = Arrays.asList(listPref.getEntryValues()).indexOf(value);
listPref.setSummary(entryIndex >= 0 && entryIndex < listPref.getEntries().length
? listPref.getEntries()[entryIndex]
: getString(R.string.unknown));
return true;
}
}
protected void initializeListSummary(ListPreference pref) {
pref.setSummary(pref.getEntry());
}
}

View File

@ -2,7 +2,6 @@ package org.thoughtcrime.securesms.preferences
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.media.RingtoneManager
import android.net.Uri
@ -11,7 +10,6 @@ import android.os.Bundle
import android.provider.Settings
import android.text.TextUtils
import androidx.lifecycle.lifecycleScope
import androidx.preference.ListPreference
import androidx.preference.Preference
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
@ -19,17 +17,19 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import network.loki.messenger.R
import org.session.libsession.utilities.TextSecurePreferences
import org.session.libsession.utilities.TextSecurePreferences.Companion.isNotificationsEnabled
import org.thoughtcrime.securesms.ApplicationContext
import org.thoughtcrime.securesms.components.SwitchPreferenceCompat
import org.thoughtcrime.securesms.notifications.NotificationChannels
import org.thoughtcrime.securesms.notifications.PushRegistry
import org.thoughtcrime.securesms.preferences.widgets.DropDownPreference
import java.util.Arrays
import javax.inject.Inject
@AndroidEntryPoint
class NotificationsPreferenceFragment : ListSummaryPreferenceFragment() {
class NotificationsPreferenceFragment : CorrectedPreferenceFragment() {
@Inject
lateinit var pushRegistry: PushRegistry
@Inject
lateinit var prefs: TextSecurePreferences
@ -41,22 +41,22 @@ class NotificationsPreferenceFragment : ListSummaryPreferenceFragment() {
val fcmPreference: SwitchPreferenceCompat = findPreference(fcmKey)!!
fcmPreference.isChecked = prefs.isPushEnabled()
fcmPreference.setOnPreferenceChangeListener { _: Preference, newValue: Any ->
prefs.setPushEnabled(newValue as Boolean)
val job = pushRegistry.refresh(true)
prefs.setPushEnabled(newValue as Boolean)
val job = pushRegistry.refresh(true)
fcmPreference.isEnabled = false
fcmPreference.isEnabled = false
lifecycleScope.launch(Dispatchers.IO) {
job.join()
lifecycleScope.launch(Dispatchers.IO) {
job.join()
withContext(Dispatchers.Main) {
fcmPreference.isEnabled = true
}
withContext(Dispatchers.Main) {
fcmPreference.isEnabled = true
}
true
}
true
}
prefs.setNotificationRingtone(
NotificationChannels.getMessageRingtone(requireContext()).toString()
)
@ -64,8 +64,16 @@ class NotificationsPreferenceFragment : ListSummaryPreferenceFragment() {
NotificationChannels.getMessageVibrate(requireContext())
)
findPreference<Preference>(TextSecurePreferences.RINGTONE_PREF)!!.onPreferenceChangeListener = RingtoneSummaryListener()
findPreference<Preference>(TextSecurePreferences.NOTIFICATION_PRIVACY_PREF)!!.onPreferenceChangeListener = NotificationPrivacyListener()
findPreference<DropDownPreference>(TextSecurePreferences.RINGTONE_PREF)?.apply {
setOnViewReady { updateRingtonePref() }
onPreferenceChangeListener = RingtoneSummaryListener()
}
findPreference<DropDownPreference>(TextSecurePreferences.NOTIFICATION_PRIVACY_PREF)?.apply {
setOnViewReady { setDropDownLabel(entry) }
onPreferenceChangeListener = NotificationPrivacyListener()
}
findPreference<Preference>(TextSecurePreferences.VIBRATE_PREF)!!.onPreferenceChangeListener =
Preference.OnPreferenceChangeListener { _: Preference?, newValue: Any ->
NotificationChannels.updateMessageVibrate(requireContext(), newValue as Boolean)
@ -91,28 +99,18 @@ class NotificationsPreferenceFragment : ListSummaryPreferenceFragment() {
true
}
findPreference<Preference>(TextSecurePreferences.NOTIFICATION_PRIVACY_PREF)!!.onPreferenceClickListener =
Preference.OnPreferenceClickListener { preference: Preference ->
val listPreference = preference as ListPreference
listPreferenceDialog(requireContext(), listPreference) {
initializeListSummary(findPreference(TextSecurePreferences.NOTIFICATION_PRIVACY_PREF))
}
true
}
initializeListSummary(findPreference<Preference>(TextSecurePreferences.NOTIFICATION_PRIVACY_PREF) as ListPreference?)
findPreference<Preference>(TextSecurePreferences.NOTIFICATION_PRIORITY_PREF)!!.onPreferenceClickListener =
Preference.OnPreferenceClickListener {
val intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS)
intent.putExtra(
Settings.EXTRA_CHANNEL_ID, NotificationChannels.getMessagesChannel(requireContext())
Settings.EXTRA_CHANNEL_ID,
NotificationChannels.getMessagesChannel(requireContext())
)
intent.putExtra(Settings.EXTRA_APP_PACKAGE, requireContext().packageName)
startActivity(intent)
true
}
initializeRingtoneSummary(findPreference(TextSecurePreferences.RINGTONE_PREF))
initializeMessageVibrateSummary(findPreference<Preference>(TextSecurePreferences.VIBRATE_PREF) as SwitchPreferenceCompat?)
}
@ -131,54 +129,63 @@ class NotificationsPreferenceFragment : ListSummaryPreferenceFragment() {
NotificationChannels.updateMessageRingtone(requireContext(), uri)
prefs.setNotificationRingtone(uri.toString())
}
initializeRingtoneSummary(findPreference(TextSecurePreferences.RINGTONE_PREF))
updateRingtonePref()
}
}
private inner class RingtoneSummaryListener : Preference.OnPreferenceChangeListener {
override fun onPreferenceChange(preference: Preference, newValue: Any): Boolean {
val pref = preference as? DropDownPreference ?: return false
val value = newValue as? Uri
if (value == null || TextUtils.isEmpty(value.toString())) {
preference.setSummary(R.string.none)
pref.setDropDownLabel(context?.getString(R.string.none))
} else {
RingtoneManager.getRingtone(activity, value)
?.getTitle(activity)
?.let { preference.summary = it }
?.let { pref.setDropDownLabel(it) }
}
return true
}
}
private fun initializeRingtoneSummary(pref: Preference?) {
val listener = pref!!.onPreferenceChangeListener as RingtoneSummaryListener?
private fun updateRingtonePref() {
val pref = findPreference<Preference>(TextSecurePreferences.RINGTONE_PREF)
val listener: RingtoneSummaryListener =
(pref?.onPreferenceChangeListener) as? RingtoneSummaryListener
?: return
val uri = prefs.getNotificationRingtone()
listener!!.onPreferenceChange(pref, uri)
listener.onPreferenceChange(pref, uri)
}
private fun initializeMessageVibrateSummary(pref: SwitchPreferenceCompat?) {
pref!!.isChecked = prefs.isNotificationVibrateEnabled()
}
private inner class NotificationPrivacyListener : ListSummaryListener() {
private inner class NotificationPrivacyListener : Preference.OnPreferenceChangeListener {
@SuppressLint("StaticFieldLeak")
override fun onPreferenceChange(preference: Preference, value: Any): Boolean {
// update drop down
val pref = preference as? DropDownPreference ?: return false
val entryIndex = Arrays.asList(*pref.entryValues).indexOf(value)
pref.setDropDownLabel(
if (entryIndex >= 0 && entryIndex < pref.entries.size
) pref.entries[entryIndex]
else getString(R.string.unknown)
)
// update notification
object : AsyncTask<Void?, Void?, Void?>() {
override fun doInBackground(vararg params: Void?): Void? {
ApplicationContext.getInstance(activity).messageNotifier.updateNotification(activity!!)
ApplicationContext.getInstance(activity).messageNotifier.updateNotification(
activity!!
)
return null
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
return super.onPreferenceChange(preference, value)
return true
}
}
companion object {
@Suppress("unused")
private val TAG = NotificationsPreferenceFragment::class.java.simpleName
fun getSummary(context: Context): CharSequence = when (isNotificationsEnabled(context)) {
true -> R.string.on
false -> R.string.off
}.let(context::getString)
}
}

View File

@ -25,7 +25,7 @@ import org.thoughtcrime.securesms.util.CallNotificationBuilder.Companion.areNoti
import org.thoughtcrime.securesms.util.IntentUtils
@AndroidEntryPoint
class PrivacySettingsPreferenceFragment : ListSummaryPreferenceFragment() {
class PrivacySettingsPreferenceFragment : CorrectedPreferenceFragment() {
@Inject lateinit var configFactory: ConfigFactory

View File

@ -0,0 +1,70 @@
package org.thoughtcrime.securesms.preferences.widgets
import android.content.Context
import android.util.AttributeSet
import android.widget.TextView
import androidx.preference.ListPreference
import androidx.preference.PreferenceViewHolder
import network.loki.messenger.R
class DropDownPreference : ListPreference {
private var dropDownLabel: TextView? = null
private var clickListener: OnPreferenceClickListener? = null
private var onViewReady: (()->Unit)? = null
constructor(
context: Context?,
attrs: AttributeSet?,
defStyleAttr: Int,
defStyleRes: Int
) : super(
context!!, attrs, defStyleAttr, defStyleRes
) {
initialize()
}
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
context!!, attrs, defStyleAttr
) {
initialize()
}
constructor(context: Context?, attrs: AttributeSet?) : super(
context!!, attrs
) {
initialize()
}
constructor(context: Context?) : super(context!!) {
initialize()
}
private fun initialize() {
widgetLayoutResource = R.layout.preference_drop_down
}
override fun onBindViewHolder(view: PreferenceViewHolder) {
super.onBindViewHolder(view)
this.dropDownLabel = view.findViewById(R.id.drop_down_label) as TextView
onViewReady?.invoke()
}
override fun setOnPreferenceClickListener(onPreferenceClickListener: OnPreferenceClickListener?) {
this.clickListener = onPreferenceClickListener
}
fun setOnViewReady(init: (()->Unit)){
this.onViewReady = init
}
override fun onClick() {
if (clickListener == null || !clickListener!!.onPreferenceClick(this)) {
super.onClick()
}
}
fun setDropDownLabel(label: CharSequence?){
dropDownLabel?.text = label
}
}

View File

@ -1,70 +0,0 @@
package org.thoughtcrime.securesms.preferences.widgets;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
import androidx.preference.ListPreference;
import androidx.preference.PreferenceViewHolder;
import network.loki.messenger.R;
public class SignalListPreference extends ListPreference {
private TextView rightSummary;
private CharSequence summary;
private OnPreferenceClickListener clickListener;
public SignalListPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initialize();
}
public SignalListPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize();
}
public SignalListPreference(Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
}
public SignalListPreference(Context context) {
super(context);
initialize();
}
private void initialize() {
setWidgetLayoutResource(R.layout.preference_right_summary_widget);
}
@Override
public void onBindViewHolder(PreferenceViewHolder view) {
super.onBindViewHolder(view);
this.rightSummary = (TextView)view.findViewById(R.id.right_summary);
setSummary(this.summary);
}
@Override
public void setSummary(CharSequence summary) {
super.setSummary(null);
this.summary = summary;
if (this.rightSummary != null) {
this.rightSummary.setText(summary);
}
}
@Override
public void setOnPreferenceClickListener(OnPreferenceClickListener onPreferenceClickListener) {
this.clickListener = onPreferenceClickListener;
}
@Override
protected void onClick() {
if (clickListener == null || !clickListener.onPreferenceClick(this)) {
super.onClick();
}
}
}

View File

@ -1,60 +0,0 @@
package org.thoughtcrime.securesms.preferences.widgets;
import android.content.Context;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;
import android.util.AttributeSet;
import android.widget.TextView;
import network.loki.messenger.R;
public class SignalPreference extends Preference {
private TextView rightSummary;
private CharSequence summary;
public SignalPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initialize();
}
public SignalPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize();
}
public SignalPreference(Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
}
public SignalPreference(Context context) {
super(context);
initialize();
}
private void initialize() {
setWidgetLayoutResource(R.layout.preference_right_summary_widget);
}
@Override
public void onBindViewHolder(PreferenceViewHolder view) {
super.onBindViewHolder(view);
this.rightSummary = (TextView)view.findViewById(R.id.right_summary);
setSummary(this.summary);
}
@Override
public void setSummary(CharSequence summary) {
super.setSummary(null);
this.summary = summary;
if (this.rightSummary != null) {
this.rightSummary.setText(summary);
}
}
}

View File

@ -2,11 +2,12 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:drawableTint="?android:textColorPrimary"
android:id="@+id/left_summary"
app:drawableTint="?android:textColorPrimary"
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start|center_vertical"
@ -16,9 +17,9 @@
tools:text="Whatever subtitle-type string you want" />
<TextView
android:drawableTint="?android:textColorPrimary"
android:drawableLeft="@drawable/ic_baseline_arrow_drop_down_24"
android:id="@+id/right_summary"
app:drawableStartCompat="@drawable/ic_baseline_arrow_drop_down_24"
app:drawableTint="?android:textColorPrimary"
android:id="@+id/drop_down_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|center_vertical"

View File

@ -17,7 +17,7 @@
<PreferenceCategory android:title="@string/notificationsStyle">
<org.thoughtcrime.securesms.preferences.widgets.SignalPreference
<org.thoughtcrime.securesms.preferences.widgets.DropDownPreference
android:key="pref_key_ringtone"
android:title="@string/notificationsSound"
android:persistent="false"
@ -37,11 +37,7 @@
</PreferenceCategory>
<PreferenceCategory android:title="@string/notificationsContent" >
<!-- android:summary="@string/notificationsContentDescription" -->
<!-- android:layout="@layout/preference_right_summary_widget" -->
<!-- android:summary="@string/notificationsContentDescription" -->
<org.thoughtcrime.securesms.preferences.widgets.SignalListPreference
<org.thoughtcrime.securesms.preferences.widgets.DropDownPreference
android:key="pref_notification_privacy"
android:title="@string/notificationsContent"
android:summary="@string/notificationsContentDescription"