Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dynamic shortcuts for speed dial entries #246

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package org.fossify.phone.activities

import android.content.Intent
import android.content.pm.ShortcutInfo
import android.graphics.drawable.Icon
import android.net.Uri
import android.os.Build
import android.os.Bundle
import com.google.gson.Gson
import org.fossify.commons.dialogs.RadioGroupDialog
import org.fossify.commons.extensions.getMyContactsCursor
import org.fossify.commons.extensions.shortcutManager
import org.fossify.commons.extensions.updateTextColors
import org.fossify.commons.extensions.viewBinding
import org.fossify.commons.helpers.ContactsHelper
import org.fossify.commons.helpers.MyContactsContentProvider
import org.fossify.commons.helpers.NavigationIcon
import org.fossify.commons.helpers.*
import org.fossify.commons.models.PhoneNumber
import org.fossify.commons.models.RadioItem
import org.fossify.commons.models.contacts.Contact
Expand Down Expand Up @@ -80,31 +84,55 @@ class ManageSpeedDialActivity : SimpleActivity(), RemoveSpeedDialListener {
speedDialValues.first { it.id == clickedContact.id }.apply {
displayName = selectedContact.getNameToDisplay()
number = selectedNumber.normalizedNumber
photoUri = selectedContact.photoUri
}
updateAdapter()
}
} else {
speedDialValues.first { it.id == clickedContact.id }.apply {
displayName = selectedContact.getNameToDisplay()
number = selectedContact.phoneNumbers.first().normalizedNumber
photoUri = selectedContact.photoUri
}
updateAdapter()
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
speedDialValues.filter { it.number.isNotBlank() }.take(3).forEach {
this.handlePermission(PERMISSION_CALL_PHONE) { hasPermission ->
val action = if (hasPermission) Intent.ACTION_CALL else Intent.ACTION_DIAL
val intent = Intent(action).apply {
data = Uri.fromParts("tel", it.number, null)
}
SimpleContactsHelper(this).getShortcutImage(it.photoUri, it.displayName) { image ->
this.runOnUiThread {
val shortcut = ShortcutInfo.Builder(this, "sd${it.id}")
.setShortLabel(it.displayName)
.setIcon(Icon.createWithAdaptiveBitmap(image))
.setIntent(intent)
.build()
this.shortcutManager.pushDynamicShortcut(shortcut)
}
}
}
}
}
}
}.apply {
binding.speedDialList.adapter = this
}
}

override fun removeSpeedDial(ids: ArrayList<Int>) {
ids.forEach {
val dialId = it
ids.forEach { dialId ->
speedDialValues.first { it.id == dialId }.apply {
displayName = ""
number = ""
photoUri = ""
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
this.shortcutManager.removeDynamicShortcuts(ids.map { "sd$it" })
}
updateAdapter()
}
}
2 changes: 1 addition & 1 deletion app/src/main/kotlin/org/fossify/phone/helpers/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Config(context: Context) : BaseConfig(context) {
val speedDialValues = Gson().fromJson<ArrayList<SpeedDial>>(speedDial, speedDialType) ?: ArrayList(1)

for (i in 1..9) {
val speedDial = SpeedDial(i, "", "")
val speedDial = SpeedDial(i, "", "", "")
if (speedDialValues.firstOrNull { it.id == i } == null) {
speedDialValues.add(speedDial)
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/kotlin/org/fossify/phone/models/SpeedDial.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package org.fossify.phone.models

data class SpeedDial(val id: Int, var number: String, var displayName: String) {
data class SpeedDial(val id: Int, var number: String, var displayName: String, var photoUri: String) {
fun isValid() = number.trim().isNotEmpty()
}