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 starred contacts #249

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
50 changes: 50 additions & 0 deletions app/src/main/kotlin/org/fossify/phone/activities/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import android.content.res.Configuration
import android.graphics.drawable.Drawable
import android.graphics.drawable.Icon
import android.graphics.drawable.LayerDrawable
import android.net.Uri
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
import android.os.Bundle
import android.os.Handler
import android.provider.Settings
Expand All @@ -24,6 +27,7 @@ import org.fossify.commons.dialogs.RadioGroupDialog
import org.fossify.commons.extensions.*
import org.fossify.commons.helpers.*
import org.fossify.commons.models.FAQItem
import org.fossify.commons.models.PhoneNumber
import org.fossify.commons.models.RadioItem
import org.fossify.commons.models.contacts.Contact
import org.fossify.phone.BuildConfig
Expand Down Expand Up @@ -289,6 +293,51 @@ class MainActivity : SimpleActivity() {
} catch (ignored: Exception) {
}
}
pushStarredShortcuts()
}

private fun pushStarredShortcuts() {
if (VERSION.SDK_INT >= VERSION_CODES.R) {
val starred = cachedContacts.filter { it.starred == 1 && it.phoneNumbers.isNotEmpty() }
this.config.favoritesContactsOrder.let { order ->
if (!config.isCustomOrderSelected) {
starred.sorted()
} else {
val orderList = Converters().jsonToStringList(order).withIndex().associate { it.value to it.index }
starred.sortedBy { orderList[it.contactId.toString()] }
}
}.take(3).forEachIndexed { index, contact ->
var number = if (contact.phoneNumbers.size == 1) {
contact.phoneNumbers[0].normalizedNumber
} else {
contact.phoneNumbers.firstOrNull { it.isPrimary }?.normalizedNumber
}
if (number == null) {
val radioItems = contact.phoneNumbers.mapIndexed { index, item ->
RadioItem(index, item.normalizedNumber, item)
}
RadioGroupDialog(this, ArrayList(radioItems)) {
number = (it as PhoneNumber).normalizedNumber
}
}
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", number, null)
}
SimpleContactsHelper(this).getShortcutImage(contact.photoUri, contact.getNameToDisplay()) { image ->
this.runOnUiThread {
val shortcut = ShortcutInfo.Builder(this, "sd$index")
.setShortLabel(contact.getNameToDisplay())
.setIcon(Icon.createWithAdaptiveBitmap(image))
.setIntent(intent)
.build()
this.shortcutManager.pushDynamicShortcut(shortcut)
}
}
}
}
}
}

@SuppressLint("NewApi")
Expand Down Expand Up @@ -490,6 +539,7 @@ class MainActivity : SimpleActivity() {
getContactsFragment()?.refreshItems()
getFavoritesFragment()?.refreshItems()
getRecentsFragment()?.refreshItems()
pushStarredShortcuts()
}

private fun getAllFragments(): ArrayList<MyViewPagerFragment<*>?> {
Expand Down