#848 - App shortcuts support

This commit is contained in:
Moez Bhatti
2018-09-03 20:03:03 -04:00
parent 14399e057d
commit d27575240c
17 changed files with 200 additions and 12 deletions
-1
View File
@@ -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')
@@ -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)
}
@@ -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<Conversation> {
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 {
@@ -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<ReceiveSms.Params>() {
class Params(val subId: Int, val messages: Array<SmsMessage>)
@@ -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
}
@@ -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<Unit>() {
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() }
}
}
@@ -0,0 +1,27 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.manager
interface ShortcutManager {
fun updateBadge()
fun updateShortcuts()
}
@@ -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()
}
@@ -28,6 +28,11 @@ interface ConversationRepository {
fun getConversationsSnapshot(): List<Conversation>
/**
* Returns the top conversations that were active in the last week
*/
fun getTopConversations(): List<Conversation>
fun setConversationName(id: Long, name: String)
fun searchConversations(query: String): List<SearchResult>
+1
View File
@@ -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")
@@ -50,6 +50,9 @@
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
</activity>
<activity
android:name=".feature.compose.ComposeActivity"
@@ -0,0 +1,92 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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()
}
}
@@ -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
Binary file not shown.

After

Width:  |  Height:  |  Size: 928 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

@@ -20,6 +20,10 @@
<string name="app_name" translatable="false">QKSMS</string>
<string name="shortcut_compose_long_label">New conversation</string>
<string name="shortcut_compose_short_label">Compose</string>
<string name="shortcut_compose_disabled_message">Shortcut disabled</string>
<string name="title_archived">Archived</string>
<string name="title_settings">Settings</string>
<string name="title_notification_prefs">Notifications</string>
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
~
~ 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 <http://www.gnu.org/licenses/>.
-->
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:enabled="true"
android:icon="@mipmap/ic_shortcut_compose"
android:shortcutDisabledMessage="@string/shortcut_compose_disabled_message"
android:shortcutId="compose"
android:shortcutLongLabel="@string/shortcut_compose_long_label"
android:shortcutShortLabel="@string/shortcut_compose_short_label">
<intent
android:action="com.moez.QKSMS.START"
android:targetClass="com.moez.QKSMS.feature.compose.ComposeActivity"
android:targetPackage="com.moez.QKSMS" />
<categories android:name="android.shortcut.conversation" />
</shortcut>
</shortcuts>