Merge pull request #5809 from k9mail/new_ringtone_preference

Use system's ringtone picker activity for 'new mail ringtone'
This commit is contained in:
cketti 2021-12-07 19:50:58 +01:00 committed by GitHub
commit ec3cfce991
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 71 additions and 6 deletions

View file

@ -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}"

View file

@ -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())
}
}

View file

@ -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"