Merge pull request #5809 from k9mail/new_ringtone_preference
Use system's ringtone picker activity for 'new mail ringtone'
This commit is contained in:
commit
ec3cfce991
3 changed files with 71 additions and 6 deletions
|
@ -21,7 +21,6 @@ dependencies {
|
|||
implementation "com.takisoft.preferencex:preferencex:${versions.preferencesFix}"
|
||||
implementation "com.takisoft.preferencex:preferencex-datetimepicker:${versions.preferencesFix}"
|
||||
implementation "com.takisoft.preferencex:preferencex-colorpicker:${versions.preferencesFix}"
|
||||
implementation "com.takisoft.preferencex:preferencex-ringtone:${versions.preferencesFix}"
|
||||
implementation "androidx.recyclerview:recyclerview:${versions.androidxRecyclerView}"
|
||||
implementation "androidx.lifecycle:lifecycle-runtime-ktx:${versions.androidxLifecycle}"
|
||||
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:${versions.androidxLifecycle}"
|
||||
|
|
|
@ -0,0 +1,69 @@
|
|||
package com.fsck.k9.ui.settings.account
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.media.RingtoneManager
|
||||
import android.net.Uri
|
||||
import android.util.AttributeSet
|
||||
import androidx.core.content.res.TypedArrayUtils
|
||||
import androidx.preference.Preference
|
||||
import com.takisoft.preferencex.PreferenceActivityResultListener
|
||||
import com.takisoft.preferencex.PreferenceFragmentCompat
|
||||
|
||||
private const val REQUEST_CODE_RINGTONE = 1
|
||||
|
||||
@SuppressLint("RestrictedApi")
|
||||
class NotificationSoundPreference
|
||||
@JvmOverloads
|
||||
constructor(
|
||||
context: Context,
|
||||
attrs: AttributeSet? = null,
|
||||
defStyleAttr: Int = TypedArrayUtils.getAttr(
|
||||
context,
|
||||
androidx.preference.R.attr.preferenceStyle,
|
||||
android.R.attr.preferenceStyle
|
||||
),
|
||||
defStyleRes: Int = 0
|
||||
) : Preference(context, attrs, defStyleAttr, defStyleRes), PreferenceActivityResultListener {
|
||||
|
||||
override fun onPreferenceClick(fragment: PreferenceFragmentCompat, preference: Preference) {
|
||||
launchRingtonePicker(fragment, onRestoreRingtone())
|
||||
}
|
||||
|
||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||
if (requestCode != REQUEST_CODE_RINGTONE || resultCode != Activity.RESULT_OK) return
|
||||
|
||||
val uri = data?.getParcelableExtra<Uri>(RingtoneManager.EXTRA_RINGTONE_PICKED_URI)
|
||||
|
||||
if (callChangeListener(uri?.toString().orEmpty())) {
|
||||
onSaveRingtone(uri)
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
private fun launchRingtonePicker(fragment: PreferenceFragmentCompat, selectedRingtone: Uri?) {
|
||||
val intent = Intent(RingtoneManager.ACTION_RINGTONE_PICKER)
|
||||
.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION)
|
||||
.putExtra(
|
||||
RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI,
|
||||
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
|
||||
)
|
||||
.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, title)
|
||||
.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true)
|
||||
.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true)
|
||||
.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, selectedRingtone)
|
||||
|
||||
fragment.startActivityForResult(intent, REQUEST_CODE_RINGTONE)
|
||||
}
|
||||
|
||||
private fun onRestoreRingtone(): Uri? {
|
||||
val uriString = getPersistedString(null)?.takeIf { it.isNotEmpty() }
|
||||
return uriString?.let { Uri.parse(it) }
|
||||
}
|
||||
|
||||
private fun onSaveRingtone(ringtoneUri: Uri?) {
|
||||
persistString(ringtoneUri?.toString().orEmpty())
|
||||
}
|
||||
}
|
|
@ -329,14 +329,11 @@
|
|||
android:summary="@string/account_settings_ignore_chat_messages_summary"
|
||||
android:title="@string/account_settings_ignore_chat_messages_label" />
|
||||
|
||||
<com.takisoft.preferencex.RingtonePreference
|
||||
<com.fsck.k9.ui.settings.account.NotificationSoundPreference
|
||||
android:defaultValue="content://settings/system/notification_sound"
|
||||
android:dependency="account_notify"
|
||||
android:key="account_ringtone"
|
||||
android:ringtoneType="notification"
|
||||
android:showSilent="true"
|
||||
android:title="@string/account_settings_ringtone"
|
||||
app:pref_summaryHasRingtone="%s" />
|
||||
android:title="@string/account_settings_ringtone" />
|
||||
|
||||
<CheckBoxPreference
|
||||
android:defaultValue="false"
|
||||
|
|
Loading…
Reference in a new issue