Using ShortcutManagerCompat, got rid of Compatibility layer
This commit is contained in:
parent
4218c5b2a9
commit
363322485e
8 changed files with 41 additions and 69 deletions
|
@ -64,6 +64,7 @@ import org.linphone.databinding.MainActivityBinding
|
||||||
import org.linphone.utils.AppUtils
|
import org.linphone.utils.AppUtils
|
||||||
import org.linphone.utils.Event
|
import org.linphone.utils.Event
|
||||||
import org.linphone.utils.FileUtils
|
import org.linphone.utils.FileUtils
|
||||||
|
import org.linphone.utils.ShortcutsHelper
|
||||||
|
|
||||||
class MainActivity : GenericActivity(), SnackBarActivity, NavController.OnDestinationChangedListener {
|
class MainActivity : GenericActivity(), SnackBarActivity, NavController.OnDestinationChangedListener {
|
||||||
private lateinit var binding: MainActivityBinding
|
private lateinit var binding: MainActivityBinding
|
||||||
|
@ -74,9 +75,9 @@ class MainActivity : GenericActivity(), SnackBarActivity, NavController.OnDestin
|
||||||
override fun onContactsUpdated() {
|
override fun onContactsUpdated() {
|
||||||
Log.i("[Main Activity] Contact(s) updated, update shortcuts")
|
Log.i("[Main Activity] Contact(s) updated, update shortcuts")
|
||||||
if (corePreferences.contactsShortcuts) {
|
if (corePreferences.contactsShortcuts) {
|
||||||
Compatibility.createShortcutsToContacts(this@MainActivity)
|
ShortcutsHelper.createShortcutsToContacts(this@MainActivity)
|
||||||
} else if (corePreferences.chatRoomShortcuts) {
|
} else if (corePreferences.chatRoomShortcuts) {
|
||||||
Compatibility.createShortcutsToChatRooms(this@MainActivity)
|
ShortcutsHelper.createShortcutsToChatRooms(this@MainActivity)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,10 +29,10 @@ import org.linphone.R
|
||||||
import org.linphone.activities.main.chat.viewmodels.ChatRoomsListViewModel
|
import org.linphone.activities.main.chat.viewmodels.ChatRoomsListViewModel
|
||||||
import org.linphone.activities.main.settings.viewmodels.ChatSettingsViewModel
|
import org.linphone.activities.main.settings.viewmodels.ChatSettingsViewModel
|
||||||
import org.linphone.activities.navigateToEmptySetting
|
import org.linphone.activities.navigateToEmptySetting
|
||||||
import org.linphone.compatibility.Compatibility
|
|
||||||
import org.linphone.databinding.SettingsChatFragmentBinding
|
import org.linphone.databinding.SettingsChatFragmentBinding
|
||||||
import org.linphone.mediastream.Version
|
import org.linphone.mediastream.Version
|
||||||
import org.linphone.utils.Event
|
import org.linphone.utils.Event
|
||||||
|
import org.linphone.utils.ShortcutsHelper
|
||||||
|
|
||||||
class ChatSettingsFragment : GenericSettingFragment<SettingsChatFragmentBinding>() {
|
class ChatSettingsFragment : GenericSettingFragment<SettingsChatFragmentBinding>() {
|
||||||
private lateinit var viewModel: ChatSettingsViewModel
|
private lateinit var viewModel: ChatSettingsViewModel
|
||||||
|
@ -55,9 +55,9 @@ class ChatSettingsFragment : GenericSettingFragment<SettingsChatFragmentBinding>
|
||||||
) {
|
) {
|
||||||
it.consume { newValue ->
|
it.consume { newValue ->
|
||||||
if (newValue) {
|
if (newValue) {
|
||||||
Compatibility.createShortcutsToChatRooms(requireContext())
|
ShortcutsHelper.createShortcutsToChatRooms(requireContext())
|
||||||
} else {
|
} else {
|
||||||
Compatibility.removeShortcuts(requireContext())
|
ShortcutsHelper.removeShortcuts(requireContext())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,11 +30,11 @@ import org.linphone.activities.main.settings.SettingListenerStub
|
||||||
import org.linphone.activities.main.settings.viewmodels.ContactsSettingsViewModel
|
import org.linphone.activities.main.settings.viewmodels.ContactsSettingsViewModel
|
||||||
import org.linphone.activities.navigateToEmptySetting
|
import org.linphone.activities.navigateToEmptySetting
|
||||||
import org.linphone.activities.navigateToLdapSettings
|
import org.linphone.activities.navigateToLdapSettings
|
||||||
import org.linphone.compatibility.Compatibility
|
|
||||||
import org.linphone.core.tools.Log
|
import org.linphone.core.tools.Log
|
||||||
import org.linphone.databinding.SettingsContactsFragmentBinding
|
import org.linphone.databinding.SettingsContactsFragmentBinding
|
||||||
import org.linphone.utils.Event
|
import org.linphone.utils.Event
|
||||||
import org.linphone.utils.PermissionHelper
|
import org.linphone.utils.PermissionHelper
|
||||||
|
import org.linphone.utils.ShortcutsHelper
|
||||||
|
|
||||||
class ContactsSettingsFragment : GenericSettingFragment<SettingsContactsFragmentBinding>() {
|
class ContactsSettingsFragment : GenericSettingFragment<SettingsContactsFragmentBinding>() {
|
||||||
private lateinit var viewModel: ContactsSettingsViewModel
|
private lateinit var viewModel: ContactsSettingsViewModel
|
||||||
|
@ -57,11 +57,11 @@ class ContactsSettingsFragment : GenericSettingFragment<SettingsContactsFragment
|
||||||
) {
|
) {
|
||||||
it.consume { newValue ->
|
it.consume { newValue ->
|
||||||
if (newValue) {
|
if (newValue) {
|
||||||
Compatibility.createShortcutsToContacts(requireContext())
|
ShortcutsHelper.createShortcutsToContacts(requireContext())
|
||||||
} else {
|
} else {
|
||||||
Compatibility.removeShortcuts(requireContext())
|
ShortcutsHelper.removeShortcuts(requireContext())
|
||||||
if (corePreferences.chatRoomShortcuts) {
|
if (corePreferences.chatRoomShortcuts) {
|
||||||
Compatibility.createShortcutsToChatRooms(requireContext())
|
ShortcutsHelper.createShortcutsToChatRooms(requireContext())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,6 @@ package org.linphone.compatibility
|
||||||
|
|
||||||
import android.Manifest
|
import android.Manifest
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
import android.annotation.TargetApi
|
|
||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
import android.app.Notification
|
import android.app.Notification
|
||||||
import android.app.PendingIntent
|
import android.app.PendingIntent
|
||||||
|
|
|
@ -25,7 +25,6 @@ import android.bluetooth.BluetoothAdapter
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.provider.Settings
|
import android.provider.Settings
|
||||||
import org.linphone.utils.ShortcutsHelper
|
|
||||||
|
|
||||||
@TargetApi(25)
|
@TargetApi(25)
|
||||||
class Api25Compatibility {
|
class Api25Compatibility {
|
||||||
|
@ -50,17 +49,5 @@ class Api25Compatibility {
|
||||||
}
|
}
|
||||||
return name
|
return name
|
||||||
}
|
}
|
||||||
|
|
||||||
fun createShortcutsToContacts(context: Context) {
|
|
||||||
ShortcutsHelper.createShortcutsToContacts(context)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun createShortcutsToChatRooms(context: Context) {
|
|
||||||
ShortcutsHelper.createShortcutsToChatRooms(context)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun removeShortcuts(context: Context) {
|
|
||||||
ShortcutsHelper.removeShortcuts(context)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -273,28 +273,8 @@ class Compatibility {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Contacts */
|
|
||||||
|
|
||||||
fun createShortcutsToContacts(context: Context) {
|
|
||||||
if (Version.sdkAboveOrEqual(Version.API25_NOUGAT_71)) {
|
|
||||||
Api25Compatibility.createShortcutsToContacts(context)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun removeShortcuts(context: Context) {
|
|
||||||
if (Version.sdkAboveOrEqual(Version.API25_NOUGAT_71)) {
|
|
||||||
Api25Compatibility.removeShortcuts(context)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Chat */
|
/* Chat */
|
||||||
|
|
||||||
fun createShortcutsToChatRooms(context: Context) {
|
|
||||||
if (Version.sdkAboveOrEqual(Version.API25_NOUGAT_71)) {
|
|
||||||
Api25Compatibility.createShortcutsToChatRooms(context)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun removeChatRoomShortcut(context: Context, chatRoom: ChatRoom) {
|
fun removeChatRoomShortcut(context: Context, chatRoom: ChatRoom) {
|
||||||
if (Version.sdkAboveOrEqual(Version.API30_ANDROID_11)) {
|
if (Version.sdkAboveOrEqual(Version.API30_ANDROID_11)) {
|
||||||
Api30Compatibility.removeChatRoomShortcut(context, chatRoom)
|
Api30Compatibility.removeChatRoomShortcut(context, chatRoom)
|
||||||
|
|
|
@ -49,10 +49,7 @@ import org.linphone.contact.getPerson
|
||||||
import org.linphone.contact.getThumbnailUri
|
import org.linphone.contact.getThumbnailUri
|
||||||
import org.linphone.core.*
|
import org.linphone.core.*
|
||||||
import org.linphone.core.tools.Log
|
import org.linphone.core.tools.Log
|
||||||
import org.linphone.utils.AppUtils
|
import org.linphone.utils.*
|
||||||
import org.linphone.utils.FileUtils
|
|
||||||
import org.linphone.utils.ImageUtils
|
|
||||||
import org.linphone.utils.LinphoneUtils
|
|
||||||
|
|
||||||
class Notifiable(val notificationId: Int) {
|
class Notifiable(val notificationId: Int) {
|
||||||
val messages: ArrayList<NotifiableMessage> = arrayListOf()
|
val messages: ArrayList<NotifiableMessage> = arrayListOf()
|
||||||
|
@ -163,8 +160,12 @@ class NotificationsManager(private val context: Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (corePreferences.chatRoomShortcuts) {
|
if (corePreferences.chatRoomShortcuts) {
|
||||||
Log.i("[Notifications Manager] Ensure chat room shortcut exists for bubble notification")
|
if (ShortcutsHelper.isShortcutToChatRoomAlreadyCreated(context, room)) {
|
||||||
Compatibility.createShortcutsToChatRooms(context)
|
Log.i("[Notifications Manager] Chat room shortcut already exists")
|
||||||
|
} else {
|
||||||
|
Log.i("[Notifications Manager] Ensure chat room shortcut exists for bubble notification")
|
||||||
|
ShortcutsHelper.createShortcutsToChatRooms(context)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
displayIncomingChatNotification(room, message)
|
displayIncomingChatNotification(room, message)
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,12 +23,12 @@ import android.annotation.TargetApi
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.pm.ShortcutInfo
|
import android.content.pm.ShortcutInfo
|
||||||
import android.content.pm.ShortcutManager
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import androidx.collection.ArraySet
|
import androidx.collection.ArraySet
|
||||||
import androidx.core.app.Person
|
import androidx.core.app.Person
|
||||||
import androidx.core.content.LocusIdCompat
|
import androidx.core.content.LocusIdCompat
|
||||||
import androidx.core.content.pm.ShortcutInfoCompat
|
import androidx.core.content.pm.ShortcutInfoCompat
|
||||||
|
import androidx.core.content.pm.ShortcutManagerCompat
|
||||||
import androidx.core.graphics.drawable.IconCompat
|
import androidx.core.graphics.drawable.IconCompat
|
||||||
import kotlin.math.min
|
import kotlin.math.min
|
||||||
import org.linphone.LinphoneApplication.Companion.coreContext
|
import org.linphone.LinphoneApplication.Companion.coreContext
|
||||||
|
@ -45,14 +45,13 @@ import org.linphone.mediastream.Version
|
||||||
class ShortcutsHelper(val context: Context) {
|
class ShortcutsHelper(val context: Context) {
|
||||||
companion object {
|
companion object {
|
||||||
fun createShortcutsToContacts(context: Context) {
|
fun createShortcutsToContacts(context: Context) {
|
||||||
val shortcuts = ArrayList<ShortcutInfo>()
|
val shortcuts = ArrayList<ShortcutInfoCompat>()
|
||||||
val shortcutManager = context.getSystemService(ShortcutManager::class.java)
|
if (ShortcutManagerCompat.isRateLimitingActive(context)) {
|
||||||
if (shortcutManager.isRateLimitingActive) {
|
|
||||||
Log.e("[Shortcut Helper] Rate limiting is active, aborting")
|
Log.e("[Shortcut Helper] Rate limiting is active, aborting")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val maxShortcuts = shortcutManager.maxShortcutCountPerActivity
|
val maxShortcuts = min(ShortcutManagerCompat.getMaxShortcutCountPerActivity(context), 5)
|
||||||
var count = 0
|
var count = 0
|
||||||
val processedAddresses = arrayListOf<String>()
|
val processedAddresses = arrayListOf<String>()
|
||||||
for (room in coreContext.core.chatRooms) {
|
for (room in coreContext.core.chatRooms) {
|
||||||
|
@ -82,7 +81,7 @@ class ShortcutsHelper(val context: Context) {
|
||||||
coreContext.contactsManager.findContactByAddress(address)
|
coreContext.contactsManager.findContactByAddress(address)
|
||||||
|
|
||||||
if (contact != null && contact.refKey != null) {
|
if (contact != null && contact.refKey != null) {
|
||||||
val shortcut: ShortcutInfo? = createContactShortcut(context, contact)
|
val shortcut: ShortcutInfoCompat? = createContactShortcut(context, contact)
|
||||||
if (shortcut != null) {
|
if (shortcut != null) {
|
||||||
Log.i("[Shortcut Helper] Creating launcher shortcut for ${shortcut.shortLabel}")
|
Log.i("[Shortcut Helper] Creating launcher shortcut for ${shortcut.shortLabel}")
|
||||||
shortcuts.add(shortcut)
|
shortcuts.add(shortcut)
|
||||||
|
@ -94,10 +93,10 @@ class ShortcutsHelper(val context: Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
shortcutManager.dynamicShortcuts = shortcuts
|
ShortcutManagerCompat.setDynamicShortcuts(context, shortcuts)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun createContactShortcut(context: Context, contact: Friend): ShortcutInfo? {
|
private fun createContactShortcut(context: Context, contact: Friend): ShortcutInfoCompat? {
|
||||||
try {
|
try {
|
||||||
val categories: ArraySet<String> = ArraySet()
|
val categories: ArraySet<String> = ArraySet()
|
||||||
categories.add(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION)
|
categories.add(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION)
|
||||||
|
@ -116,7 +115,7 @@ class ShortcutsHelper(val context: Context) {
|
||||||
.setPerson(person)
|
.setPerson(person)
|
||||||
.setCategories(categories)
|
.setCategories(categories)
|
||||||
.setIntent(intent)
|
.setIntent(intent)
|
||||||
.build().toShortcutInfo()
|
.build()
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e("[Shortcuts Helper] createContactShortcut for contact [${contact.name}] exception: $e")
|
Log.e("[Shortcuts Helper] createContactShortcut for contact [${contact.name}] exception: $e")
|
||||||
}
|
}
|
||||||
|
@ -125,15 +124,13 @@ class ShortcutsHelper(val context: Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun createShortcutsToChatRooms(context: Context) {
|
fun createShortcutsToChatRooms(context: Context) {
|
||||||
val shortcuts = ArrayList<ShortcutInfo>()
|
val shortcuts = ArrayList<ShortcutInfoCompat>()
|
||||||
val shortcutManager = context.getSystemService(ShortcutManager::class.java)
|
if (ShortcutManagerCompat.isRateLimitingActive(context)) {
|
||||||
if (shortcutManager.isRateLimitingActive) {
|
|
||||||
Log.e("[Shortcut Helper] Rate limiting is active, aborting")
|
Log.e("[Shortcut Helper] Rate limiting is active, aborting")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.i("[Shortcut Helper] Creating launcher shortcuts for chat rooms")
|
Log.i("[Shortcut Helper] Creating launcher shortcuts for chat rooms")
|
||||||
val maxShortcuts = min(shortcutManager.maxShortcutCountPerActivity, 5)
|
val maxShortcuts = min(ShortcutManagerCompat.getMaxShortcutCountPerActivity(context), 5)
|
||||||
var count = 0
|
var count = 0
|
||||||
for (room in coreContext.core.chatRooms) {
|
for (room in coreContext.core.chatRooms) {
|
||||||
// Android can usually only have around 4-5 shortcuts at a time
|
// Android can usually only have around 4-5 shortcuts at a time
|
||||||
|
@ -142,18 +139,18 @@ class ShortcutsHelper(val context: Context) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
val shortcut: ShortcutInfo? = createChatRoomShortcut(context, room)
|
val shortcut: ShortcutInfoCompat? = createChatRoomShortcut(context, room)
|
||||||
if (shortcut != null) {
|
if (shortcut != null) {
|
||||||
Log.i("[Shortcut Helper] Created launcher shortcut for ${shortcut.shortLabel}")
|
Log.i("[Shortcut Helper] Created launcher shortcut for ${shortcut.shortLabel}")
|
||||||
shortcuts.add(shortcut)
|
shortcuts.add(shortcut)
|
||||||
count += 1
|
count += 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
shortcutManager.dynamicShortcuts = shortcuts
|
ShortcutManagerCompat.setDynamicShortcuts(context, shortcuts)
|
||||||
Log.i("[Shortcut Helper] Created $count launcher shortcuts")
|
Log.i("[Shortcut Helper] Created $count launcher shortcuts")
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun createChatRoomShortcut(context: Context, chatRoom: ChatRoom): ShortcutInfo? {
|
private fun createChatRoomShortcut(context: Context, chatRoom: ChatRoom): ShortcutInfoCompat? {
|
||||||
val localAddress = chatRoom.localAddress.asStringUriOnly()
|
val localAddress = chatRoom.localAddress.asStringUriOnly()
|
||||||
val peerAddress = chatRoom.peerAddress.asStringUriOnly()
|
val peerAddress = chatRoom.peerAddress.asStringUriOnly()
|
||||||
val id = LinphoneUtils.getChatRoomId(chatRoom.localAddress, chatRoom.peerAddress)
|
val id = LinphoneUtils.getChatRoomId(chatRoom.localAddress, chatRoom.peerAddress)
|
||||||
|
@ -219,7 +216,7 @@ class ShortcutsHelper(val context: Context) {
|
||||||
.setIntent(intent)
|
.setIntent(intent)
|
||||||
.setLongLived(Version.sdkAboveOrEqual(Version.API30_ANDROID_11))
|
.setLongLived(Version.sdkAboveOrEqual(Version.API30_ANDROID_11))
|
||||||
.setLocusId(LocusIdCompat(id))
|
.setLocusId(LocusIdCompat(id))
|
||||||
.build().toShortcutInfo()
|
.build()
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e("[Shortcuts Helper] createChatRoomShortcut for id [$id] exception: $e")
|
Log.e("[Shortcuts Helper] createChatRoomShortcut for id [$id] exception: $e")
|
||||||
}
|
}
|
||||||
|
@ -229,8 +226,15 @@ class ShortcutsHelper(val context: Context) {
|
||||||
|
|
||||||
fun removeShortcuts(context: Context) {
|
fun removeShortcuts(context: Context) {
|
||||||
Log.w("[Shortcut Helper] Removing all contacts shortcuts")
|
Log.w("[Shortcut Helper] Removing all contacts shortcuts")
|
||||||
val shortcutManager = context.getSystemService(ShortcutManager::class.java)
|
ShortcutManagerCompat.removeAllDynamicShortcuts(context)
|
||||||
shortcutManager.removeAllDynamicShortcuts()
|
}
|
||||||
|
|
||||||
|
fun isShortcutToChatRoomAlreadyCreated(context: Context, chatRoom: ChatRoom): Boolean {
|
||||||
|
val id = LinphoneUtils.getChatRoomId(chatRoom.localAddress, chatRoom.peerAddress)
|
||||||
|
val found = ShortcutManagerCompat.getDynamicShortcuts(context).find {
|
||||||
|
it.id == id
|
||||||
|
}
|
||||||
|
return found != null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue