diff --git a/data/build.gradle b/data/build.gradle index dfc56ede..489e1428 100644 --- a/data/build.gradle +++ b/data/build.gradle @@ -76,7 +76,6 @@ dependencies { implementation "com.jakewharton.timber:timber:$timber_version" implementation "com.squareup.moshi:moshi:$moshi_version" implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" - implementation "me.leolin:ShortcutBadger:1.1.21" implementation project(":android-smsmms") implementation project(':common') implementation project(':domain') diff --git a/data/src/main/java/com/moez/QKSMS/manager/WidgetManagerImpl.kt b/data/src/main/java/com/moez/QKSMS/manager/WidgetManagerImpl.kt index 70cc6368..e7b063a8 100644 --- a/data/src/main/java/com/moez/QKSMS/manager/WidgetManagerImpl.kt +++ b/data/src/main/java/com/moez/QKSMS/manager/WidgetManagerImpl.kt @@ -21,14 +21,11 @@ package com.moez.QKSMS.manager import android.content.Context import android.content.Intent import com.klinker.android.send_message.BroadcastUtils -import me.leolin.shortcutbadger.ShortcutBadger import javax.inject.Inject class WidgetManagerImpl @Inject constructor(private val context: Context) : WidgetManager { - override fun updateUnreadCount(count: Int) { - ShortcutBadger.applyCount(context, count) - + override fun updateUnreadCount() { BroadcastUtils.sendExplicitBroadcast(context, Intent(), WidgetManager.ACTION_NOTIFY_DATASET_CHANGED) } diff --git a/data/src/main/java/com/moez/QKSMS/repository/ConversationRepositoryImpl.kt b/data/src/main/java/com/moez/QKSMS/repository/ConversationRepositoryImpl.kt index e376e15d..a057ca91 100644 --- a/data/src/main/java/com/moez/QKSMS/repository/ConversationRepositoryImpl.kt +++ b/data/src/main/java/com/moez/QKSMS/repository/ConversationRepositoryImpl.kt @@ -37,6 +37,7 @@ import io.realm.Case import io.realm.Realm import io.realm.RealmResults import io.realm.Sort +import java.util.concurrent.TimeUnit import javax.inject.Inject class ConversationRepositoryImpl @Inject constructor( @@ -70,6 +71,19 @@ class ConversationRepositoryImpl @Inject constructor( .findAll()) } + override fun getTopConversations(): List { + val realm = Realm.getDefaultInstance() + return realm.copyFromRealm(realm.where(Conversation::class.java) + .notEqualTo("id", 0L) + .greaterThan("count", 0) + .greaterThan("date", System.currentTimeMillis() - TimeUnit.DAYS.toMillis(7)) + .equalTo("archived", false) + .equalTo("blocked", false) + .isNotEmpty("recipients") + .sort("pinned", Sort.DESCENDING, "count", Sort.DESCENDING) + .findAll()) + } + override fun setConversationName(id: Long, name: String) { Realm.getDefaultInstance().use { realm -> realm.executeTransaction { diff --git a/domain/src/main/java/com/moez/QKSMS/interactor/ReceiveSms.kt b/domain/src/main/java/com/moez/QKSMS/interactor/ReceiveSms.kt index f8298ee5..4e22c241 100644 --- a/domain/src/main/java/com/moez/QKSMS/interactor/ReceiveSms.kt +++ b/domain/src/main/java/com/moez/QKSMS/interactor/ReceiveSms.kt @@ -22,6 +22,7 @@ import android.telephony.SmsMessage import com.moez.QKSMS.extensions.mapNotNull import com.moez.QKSMS.manager.ExternalBlockingManager import com.moez.QKSMS.manager.NotificationManager +import com.moez.QKSMS.manager.ShortcutManager import com.moez.QKSMS.repository.ConversationRepository import com.moez.QKSMS.repository.MessageRepository import io.reactivex.Flowable @@ -33,7 +34,8 @@ class ReceiveSms @Inject constructor( private val externalBlockingManager: ExternalBlockingManager, private val messageRepo: MessageRepository, private val notificationManager: NotificationManager, - private val updateBadge: UpdateBadge + private val updateBadge: UpdateBadge, + private val shortcutManager: ShortcutManager ) : Interactor() { class Params(val subId: Int, val messages: Array) @@ -63,6 +65,7 @@ class ReceiveSms @Inject constructor( .map { conversation -> conversation.id } // Map to the id because [delay] will put us on the wrong thread .delay(1, TimeUnit.SECONDS) // Wait one second before trying to notify, in case the foreground app marks it as read first .doOnNext { threadId -> notificationManager.update(threadId) } // Update the notification + .doOnNext { shortcutManager.updateShortcuts() } // Update shortcuts .flatMap { updateBadge.buildObservable(Unit) } // Update the badge } diff --git a/domain/src/main/java/com/moez/QKSMS/interactor/UpdateBadge.kt b/domain/src/main/java/com/moez/QKSMS/interactor/UpdateBadge.kt index a5aefcd6..7051b97a 100644 --- a/domain/src/main/java/com/moez/QKSMS/interactor/UpdateBadge.kt +++ b/domain/src/main/java/com/moez/QKSMS/interactor/UpdateBadge.kt @@ -18,21 +18,20 @@ */ package com.moez.QKSMS.interactor +import com.moez.QKSMS.manager.ShortcutManager import com.moez.QKSMS.manager.WidgetManager -import com.moez.QKSMS.repository.MessageRepository import io.reactivex.Flowable import javax.inject.Inject class UpdateBadge @Inject constructor( - private val messageRepo: MessageRepository, + private val shortcutManager: ShortcutManager, private val widgetManager: WidgetManager ) : Interactor() { override fun buildObservable(params: Unit): Flowable<*> { return Flowable.just(params) - .map { messageRepo.getUnreadCount() } - .map { count -> count.toInt() } - .doOnNext { count -> widgetManager.updateUnreadCount(count) } + .doOnNext { shortcutManager.updateBadge() } + .doOnNext { widgetManager.updateUnreadCount() } } } \ No newline at end of file diff --git a/domain/src/main/java/com/moez/QKSMS/manager/ShortcutManager.kt b/domain/src/main/java/com/moez/QKSMS/manager/ShortcutManager.kt new file mode 100644 index 00000000..7f57f378 --- /dev/null +++ b/domain/src/main/java/com/moez/QKSMS/manager/ShortcutManager.kt @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2017 Moez Bhatti + * + * This file is part of QKSMS. + * + * QKSMS is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * QKSMS is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with QKSMS. If not, see . + */ +package com.moez.QKSMS.manager + +interface ShortcutManager { + + fun updateBadge() + + fun updateShortcuts() + +} \ No newline at end of file diff --git a/domain/src/main/java/com/moez/QKSMS/manager/WidgetManager.kt b/domain/src/main/java/com/moez/QKSMS/manager/WidgetManager.kt index aac1ad13..86840d51 100644 --- a/domain/src/main/java/com/moez/QKSMS/manager/WidgetManager.kt +++ b/domain/src/main/java/com/moez/QKSMS/manager/WidgetManager.kt @@ -24,6 +24,6 @@ interface WidgetManager { const val ACTION_NOTIFY_DATASET_CHANGED = "com.moez.QKSMS.intent.action.ACTION_NOTIFY_DATASET_CHANGED" } - fun updateUnreadCount(count: Int) + fun updateUnreadCount() } \ No newline at end of file diff --git a/domain/src/main/java/com/moez/QKSMS/repository/ConversationRepository.kt b/domain/src/main/java/com/moez/QKSMS/repository/ConversationRepository.kt index a7275762..bf07859f 100644 --- a/domain/src/main/java/com/moez/QKSMS/repository/ConversationRepository.kt +++ b/domain/src/main/java/com/moez/QKSMS/repository/ConversationRepository.kt @@ -28,6 +28,11 @@ interface ConversationRepository { fun getConversationsSnapshot(): List + /** + * Returns the top conversations that were active in the last week + */ + fun getTopConversations(): List + fun setConversationName(id: Long, name: String) fun searchConversations(query: String): List diff --git a/presentation/build.gradle b/presentation/build.gradle index 3af3d254..915eeed6 100644 --- a/presentation/build.gradle +++ b/presentation/build.gradle @@ -181,6 +181,7 @@ dependencies { implementation "com.google.android:flexbox:0.3.1" implementation "com.jakewharton.timber:timber:$timber_version" implementation "com.squareup.moshi:moshi:$moshi_version" + implementation "me.leolin:ShortcutBadger:1.1.21" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation project(":android-smsmms") implementation project(":common") diff --git a/presentation/src/main/AndroidManifest.xml b/presentation/src/main/AndroidManifest.xml index eeeefb9f..df510701 100644 --- a/presentation/src/main/AndroidManifest.xml +++ b/presentation/src/main/AndroidManifest.xml @@ -50,6 +50,9 @@ + + * + * This file is part of QKSMS. + * + * QKSMS is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * QKSMS is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with QKSMS. If not, see . + */ +package com.moez.QKSMS.common.util + +import android.annotation.TargetApi +import android.content.Context +import android.content.Intent +import android.content.pm.ShortcutInfo +import android.content.pm.ShortcutManager +import android.graphics.drawable.Icon +import android.os.Build +import android.telephony.PhoneNumberUtils +import com.moez.QKSMS.R +import com.moez.QKSMS.feature.compose.ComposeActivity +import com.moez.QKSMS.model.Conversation +import com.moez.QKSMS.repository.ConversationRepository +import com.moez.QKSMS.repository.MessageRepository +import com.moez.QKSMS.util.GlideApp +import com.moez.QKSMS.util.tryOrNull +import me.leolin.shortcutbadger.ShortcutBadger +import javax.inject.Inject + +class ShortcutManagerImpl @Inject constructor( + private val context: Context, + private val conversationRepo: ConversationRepository, + private val messageRepo: MessageRepository +) : com.moez.QKSMS.manager.ShortcutManager { + + override fun updateBadge() { + val count = messageRepo.getUnreadCount().toInt() + ShortcutBadger.applyCount(context, count) + } + + override fun updateShortcuts() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { + val shortcutManager = context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager + if (shortcutManager.isRateLimitingActive) return + + shortcutManager.dynamicShortcuts = conversationRepo.getTopConversations() + .take(shortcutManager.maxShortcutCountPerActivity - shortcutManager.manifestShortcuts.size) + .map { conversation -> createShortcutForConversation(conversation, shortcutManager) } + } + } + + @TargetApi(25) + private fun createShortcutForConversation(conversation: Conversation, shortcutManager: ShortcutManager): ShortcutInfo { + val icon = when { + conversation.recipients.size == 1 -> { + val address = conversation.recipients.first()!!.address + val request = GlideApp.with(context) + .asBitmap() + .circleCrop() + .load(PhoneNumberUtils.stripSeparators(address)) + .submit(shortcutManager.iconMaxWidth, shortcutManager.iconMaxHeight) + val bitmap = tryOrNull(false) { request.get() } + + if (bitmap != null) Icon.createWithBitmap(bitmap) + else Icon.createWithResource(context, R.mipmap.ic_shortcut_person) + } + + else -> Icon.createWithResource(context, R.mipmap.ic_shortcut_people) + } + + val intent = Intent(context, ComposeActivity::class.java) + .setAction(Intent.ACTION_VIEW) + .putExtra("threadId", conversation.id) + + return ShortcutInfo.Builder(context, "${conversation.id}") + .setShortLabel(conversation.getTitle()) + .setLongLabel(conversation.getTitle()) + .setIcon(icon) + .setIntent(intent) + .build() + } + +} \ No newline at end of file diff --git a/presentation/src/main/java/com/moez/QKSMS/injection/AppModule.kt b/presentation/src/main/java/com/moez/QKSMS/injection/AppModule.kt index 27591c61..e9cc3122 100644 --- a/presentation/src/main/java/com/moez/QKSMS/injection/AppModule.kt +++ b/presentation/src/main/java/com/moez/QKSMS/injection/AppModule.kt @@ -26,6 +26,7 @@ import androidx.lifecycle.ViewModelProvider import com.f2prateek.rx.preferences2.RxSharedPreferences import com.moez.QKSMS.common.ViewModelFactory import com.moez.QKSMS.common.util.NotificationManagerImpl +import com.moez.QKSMS.common.util.ShortcutManagerImpl import com.moez.QKSMS.feature.conversationinfo.injection.ConversationInfoComponent import com.moez.QKSMS.feature.themepicker.injection.ThemePickerComponent import com.moez.QKSMS.listener.ContactAddedListener @@ -42,6 +43,7 @@ import com.moez.QKSMS.manager.NotificationManager import com.moez.QKSMS.manager.PermissionManager import com.moez.QKSMS.manager.PermissionManagerImpl import com.moez.QKSMS.manager.RatingManager +import com.moez.QKSMS.manager.ShortcutManager import com.moez.QKSMS.manager.WidgetManager import com.moez.QKSMS.manager.WidgetManagerImpl import com.moez.QKSMS.mapper.CursorToContact @@ -130,6 +132,9 @@ class AppModule(private var application: Application) { @Provides fun provideRatingManager(manager: RatingManagerImpl): RatingManager = manager + @Provides + fun provideShortcutManager(manager: ShortcutManagerImpl): ShortcutManager = manager + @Provides fun provideWidgetManager(manager: WidgetManagerImpl): WidgetManager = manager diff --git a/presentation/src/main/res/mipmap-xxxhdpi/ic_shortcut_compose.png b/presentation/src/main/res/mipmap-xxxhdpi/ic_shortcut_compose.png new file mode 100755 index 00000000..52a0483d Binary files /dev/null and b/presentation/src/main/res/mipmap-xxxhdpi/ic_shortcut_compose.png differ diff --git a/presentation/src/main/res/mipmap-xxxhdpi/ic_shortcut_people.png b/presentation/src/main/res/mipmap-xxxhdpi/ic_shortcut_people.png new file mode 100755 index 00000000..1f783dff Binary files /dev/null and b/presentation/src/main/res/mipmap-xxxhdpi/ic_shortcut_people.png differ diff --git a/presentation/src/main/res/mipmap-xxxhdpi/ic_shortcut_person.png b/presentation/src/main/res/mipmap-xxxhdpi/ic_shortcut_person.png new file mode 100755 index 00000000..1968a047 Binary files /dev/null and b/presentation/src/main/res/mipmap-xxxhdpi/ic_shortcut_person.png differ diff --git a/presentation/src/main/res/values/strings.xml b/presentation/src/main/res/values/strings.xml index c55a361e..e078fdfb 100644 --- a/presentation/src/main/res/values/strings.xml +++ b/presentation/src/main/res/values/strings.xml @@ -20,6 +20,10 @@ QKSMS + New conversation + Compose + Shortcut disabled + Archived Settings Notifications diff --git a/presentation/src/main/res/xml/shortcuts.xml b/presentation/src/main/res/xml/shortcuts.xml new file mode 100644 index 00000000..8cc5bb9f --- /dev/null +++ b/presentation/src/main/res/xml/shortcuts.xml @@ -0,0 +1,39 @@ + + + + + + + + + + + + + \ No newline at end of file