use the new ringtone picker from the Edit screen too

This commit is contained in:
tibbi 2021-03-04 19:35:53 +01:00
parent 6de5aaeb94
commit 49ec8965af
3 changed files with 65 additions and 54 deletions

View File

@ -1,5 +1,6 @@
package com.simplemobiletools.contacts.pro.activities package com.simplemobiletools.contacts.pro.activities
import android.app.Activity
import android.content.Intent import android.content.Intent
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.Canvas import android.graphics.Canvas
@ -7,6 +8,7 @@ import android.graphics.Paint
import android.graphics.drawable.BitmapDrawable import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.ColorDrawable import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.media.RingtoneManager
import android.net.Uri import android.net.Uri
import android.provider.ContactsContract.CommonDataKinds.* import android.provider.ContactsContract.CommonDataKinds.*
import android.widget.ImageView import android.widget.ImageView
@ -33,6 +35,8 @@ import java.util.*
abstract class ContactActivity : SimpleActivity() { abstract class ContactActivity : SimpleActivity() {
protected val PICK_RINGTONE_INTENT_ID = 1500 protected val PICK_RINGTONE_INTENT_ID = 1500
protected val INTENT_SELECT_RINGTONE = 600
protected var contact: Contact? = null protected var contact: Contact? = null
protected var currentContactPhotoPath = "" protected var currentContactPhotoPath = ""
@ -40,11 +44,23 @@ abstract class ContactActivity : SimpleActivity() {
super.onActivityResult(requestCode, resultCode, resultData) super.onActivityResult(requestCode, resultCode, resultData)
if (requestCode == PICK_RINGTONE_INTENT_ID && resultCode == RESULT_OK && resultData != null && resultData.dataString != null) { if (requestCode == PICK_RINGTONE_INTENT_ID && resultCode == RESULT_OK && resultData != null && resultData.dataString != null) {
customRingtoneSelected(Uri.decode(resultData.dataString!!)) customRingtoneSelected(Uri.decode(resultData.dataString!!))
} else if (requestCode == INTENT_SELECT_RINGTONE && resultCode == Activity.RESULT_OK && resultData != null) {
val extras = resultData.extras
if (extras?.containsKey(RingtoneManager.EXTRA_RINGTONE_PICKED_URI) == true) {
val uri = extras.getParcelable<Uri>(RingtoneManager.EXTRA_RINGTONE_PICKED_URI) ?: return
try {
systemRingtoneSelected(uri)
} catch (e: Exception) {
showErrorToast(e)
}
}
} }
} }
abstract fun customRingtoneSelected(ringtonePath: String) abstract fun customRingtoneSelected(ringtonePath: String)
abstract fun systemRingtoneSelected(uri: Uri)
fun showPhotoPlaceholder(photoView: ImageView) { fun showPhotoPlaceholder(photoView: ImageView) {
val placeholder = BitmapDrawable(resources, getBigLetterPlaceholder(contact?.getNameToDisplay() ?: "A")) val placeholder = BitmapDrawable(resources, getBigLetterPlaceholder(contact?.getNameToDisplay() ?: "A"))
photoView.setImageDrawable(placeholder) photoView.setImageDrawable(placeholder)
@ -211,4 +227,16 @@ abstract class ContactActivity : SimpleActivity() {
view.draw(canvas) view.draw(canvas)
return bitmap return bitmap
} }
protected fun getDefaultRingtoneUri() = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE)
protected fun getRingtonePickerIntent(): Intent {
return Intent(RingtoneManager.ACTION_RINGTONE_PICKER).apply {
putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true)
putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true)
putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE)
putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, getDefaultRingtoneUri())
putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse(contact!!.ringtone))
}
}
} }

View File

@ -488,13 +488,18 @@ class EditContactActivity : ContactActivity() {
private fun setupRingtone() { private fun setupRingtone() {
contact_ringtone.setOnClickListener { contact_ringtone.setOnClickListener {
val currentRingtone = contact!!.ringtone ?: getDefaultAlarmSound(RingtoneManager.TYPE_RINGTONE).uri val ringtonePickerIntent = getRingtonePickerIntent()
SelectAlarmSoundDialog(this, currentRingtone, AudioManager.STREAM_RING, PICK_RINGTONE_INTENT_ID, RingtoneManager.TYPE_RINGTONE, true, if (ringtonePickerIntent.resolveActivity(packageManager) != null) {
onAlarmPicked = { startActivityForResult(ringtonePickerIntent, INTENT_SELECT_RINGTONE)
contact!!.ringtone = it?.uri } else {
contact_ringtone.text = it?.title val currentRingtone = contact!!.ringtone ?: getDefaultAlarmSound(RingtoneManager.TYPE_RINGTONE).uri
}, onAlarmSoundDeleted = {} SelectAlarmSoundDialog(this, currentRingtone, AudioManager.STREAM_RING, PICK_RINGTONE_INTENT_ID, RingtoneManager.TYPE_RINGTONE, true,
) onAlarmPicked = {
contact!!.ringtone = it?.uri
contact_ringtone.text = it?.title
}, onAlarmSoundDeleted = {}
)
}
} }
val ringtone = contact!!.ringtone val ringtone = contact!!.ringtone
@ -502,8 +507,7 @@ class EditContactActivity : ContactActivity() {
if (ringtone == SILENT) { if (ringtone == SILENT) {
contact_ringtone.text = getString(R.string.no_sound) contact_ringtone.text = getString(R.string.no_sound)
} else { } else {
val contactRingtone = RingtoneManager.getRingtone(this, Uri.parse(ringtone)) systemRingtoneSelected(Uri.parse(ringtone))
contact_ringtone.text = contactRingtone.getTitle(this)
} }
} else { } else {
val default = getDefaultAlarmSound(RingtoneManager.TYPE_RINGTONE) val default = getDefaultAlarmSound(RingtoneManager.TYPE_RINGTONE)
@ -1247,6 +1251,12 @@ class EditContactActivity : ContactActivity() {
contact_ringtone.text = ringtonePath.getFilenameFromPath() contact_ringtone.text = ringtonePath.getFilenameFromPath()
} }
override fun systemRingtoneSelected(uri: Uri) {
contact!!.ringtone = uri.toString()
val contactRingtone = RingtoneManager.getRingtone(this, uri)
contact_ringtone.text = contactRingtone.getTitle(this)
}
private fun getPhoneNumberTypeId(value: String) = when (value) { private fun getPhoneNumberTypeId(value: String) = when (value) {
getString(R.string.mobile) -> Phone.TYPE_MOBILE getString(R.string.mobile) -> Phone.TYPE_MOBILE
getString(R.string.home) -> Phone.TYPE_HOME getString(R.string.home) -> Phone.TYPE_HOME

View File

@ -1,6 +1,5 @@
package com.simplemobiletools.contacts.pro.activities package com.simplemobiletools.contacts.pro.activities
import android.app.Activity
import android.content.ContentUris import android.content.ContentUris
import android.content.Intent import android.content.Intent
import android.media.AudioManager import android.media.AudioManager
@ -40,7 +39,6 @@ class ViewContactActivity : ContactActivity() {
private var fullContact: Contact? = null // contact with all fields filled from duplicates private var fullContact: Contact? = null // contact with all fields filled from duplicates
private val COMPARABLE_PHONE_NUMBER_LENGTH = 9 private val COMPARABLE_PHONE_NUMBER_LENGTH = 9
private val INTENT_SELECT_RINGTONE = 600
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
showTransparentTop = true showTransparentTop = true
@ -77,22 +75,6 @@ class ViewContactActivity : ContactActivity() {
} }
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
super.onActivityResult(requestCode, resultCode, resultData)
if (requestCode == INTENT_SELECT_RINGTONE && resultCode == Activity.RESULT_OK && resultData != null) {
val extras = resultData.extras
if (extras?.containsKey(RingtoneManager.EXTRA_RINGTONE_PICKED_URI) == true) {
val uri = extras.getParcelable<Uri>(RingtoneManager.EXTRA_RINGTONE_PICKED_URI) ?: return
try {
updateRingtoneText(uri)
ringtoneUpdated(uri.toString())
} catch (e: Exception) {
showErrorToast(e)
}
}
}
}
private fun setupMenu() { private fun setupMenu() {
(contact_appbar.layoutParams as RelativeLayout.LayoutParams).topMargin = statusBarHeight (contact_appbar.layoutParams as RelativeLayout.LayoutParams).topMargin = statusBarHeight
contact_toolbar.menu.apply { contact_toolbar.menu.apply {
@ -601,7 +583,7 @@ class ViewContactActivity : ContactActivity() {
if (ringtone == SILENT) { if (ringtone == SILENT) {
contact_ringtone.text = getString(R.string.no_sound) contact_ringtone.text = getString(R.string.no_sound)
} else { } else {
updateRingtoneText(Uri.parse(ringtone)) systemRingtoneSelected(Uri.parse(ringtone))
} }
} else { } else {
contact_ringtone_image.beGone() contact_ringtone_image.beGone()
@ -612,24 +594,17 @@ class ViewContactActivity : ContactActivity() {
contact_ringtone.copyOnLongClick(contact_ringtone.text.toString()) contact_ringtone.copyOnLongClick(contact_ringtone.text.toString())
contact_ringtone.setOnClickListener { contact_ringtone.setOnClickListener {
Intent(RingtoneManager.ACTION_RINGTONE_PICKER).apply { val ringtonePickerIntent = getRingtonePickerIntent()
putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true) if (ringtonePickerIntent.resolveActivity(packageManager) != null) {
putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true) startActivityForResult(ringtonePickerIntent, INTENT_SELECT_RINGTONE)
putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE) } else {
putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, getDefaultRingtoneUri()) val currentRingtone = contact!!.ringtone ?: getDefaultAlarmSound(RingtoneManager.TYPE_RINGTONE).uri
putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse(contact!!.ringtone)) SelectAlarmSoundDialog(this@ViewContactActivity, currentRingtone, AudioManager.STREAM_RING, PICK_RINGTONE_INTENT_ID, RingtoneManager.TYPE_RINGTONE, true,
onAlarmPicked = {
if (resolveActivity(packageManager) != null) { contact_ringtone.text = it?.title
startActivityForResult(this, INTENT_SELECT_RINGTONE) ringtoneUpdated(it?.uri)
} else { }, onAlarmSoundDeleted = {}
val currentRingtone = contact!!.ringtone ?: getDefaultAlarmSound(RingtoneManager.TYPE_RINGTONE).uri )
SelectAlarmSoundDialog(this@ViewContactActivity, currentRingtone, AudioManager.STREAM_RING, PICK_RINGTONE_INTENT_ID, RingtoneManager.TYPE_RINGTONE, true,
onAlarmPicked = {
contact_ringtone.text = it?.title
ringtoneUpdated(it?.uri)
}, onAlarmSoundDeleted = {}
)
}
} }
} }
} else { } else {
@ -638,14 +613,6 @@ class ViewContactActivity : ContactActivity() {
} }
} }
private fun getDefaultRingtoneUri() = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE)
private fun updateRingtoneText(uri: Uri) {
val contactRingtone = RingtoneManager.getRingtone(this, uri)
val ringtoneTitle = contactRingtone.getTitle(this)
contact_ringtone.text = ringtoneTitle
}
private fun setupOrganization() { private fun setupOrganization() {
val organization = contact!!.organization val organization = contact!!.organization
if (organization.isNotEmpty() && showFields and SHOW_ORGANIZATION_FIELD != 0) { if (organization.isNotEmpty() && showFields and SHOW_ORGANIZATION_FIELD != 0) {
@ -688,6 +655,12 @@ class ViewContactActivity : ContactActivity() {
ringtoneUpdated(ringtonePath) ringtoneUpdated(ringtonePath)
} }
override fun systemRingtoneSelected(uri: Uri) {
val contactRingtone = RingtoneManager.getRingtone(this, uri)
contact_ringtone.text = contactRingtone.getTitle(this)
ringtoneUpdated(uri.toString())
}
private fun ringtoneUpdated(path: String?) { private fun ringtoneUpdated(path: String?) {
contact!!.ringtone = path contact!!.ringtone = path