From c38ee53de90ac54410b5e4f7a1af098fecd47b9b Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 13 May 2020 21:06:46 +0200 Subject: [PATCH] allow creating a dialpad shortcut of the app --- .../dialer/activities/MainActivity.kt | 38 +++++++++++++++++++ .../main/res/drawable/shortcut_dialpad.xml | 16 ++++++++ 2 files changed, 54 insertions(+) create mode 100644 app/src/main/res/drawable/shortcut_dialpad.xml diff --git a/app/src/main/kotlin/com/simplemobiletools/dialer/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/dialer/activities/MainActivity.kt index 7e4b4ba4..12b36c53 100644 --- a/app/src/main/kotlin/com/simplemobiletools/dialer/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/dialer/activities/MainActivity.kt @@ -1,10 +1,15 @@ package com.simplemobiletools.dialer.activities +import android.annotation.SuppressLint import android.app.SearchManager import android.content.Context import android.content.Intent +import android.content.pm.ShortcutInfo +import android.content.pm.ShortcutManager import android.graphics.drawable.ColorDrawable import android.graphics.drawable.Drawable +import android.graphics.drawable.Icon +import android.graphics.drawable.LayerDrawable import android.os.Bundle import android.os.Handler import android.view.Menu @@ -24,6 +29,7 @@ import com.simplemobiletools.dialer.helpers.tabsList import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.fragment_contacts.* import kotlinx.android.synthetic.main.fragment_recents.* +import java.util.* class MainActivity : SimpleActivity() { private var storedTextColor = 0 @@ -80,6 +86,7 @@ class MainActivity : SimpleActivity() { refreshItems() } + checkShortcuts() isFirstResume = false } @@ -178,6 +185,37 @@ class MainActivity : SimpleActivity() { }) } + @SuppressLint("NewApi") + private fun checkShortcuts() { + val appIconColor = config.appIconColor + if (isNougatMR1Plus() && config.lastHandledShortcutColor != appIconColor) { + val launchDialpad = getLaunchDialpadShortcut(appIconColor) + + try { + getSystemService(ShortcutManager::class.java)!!.dynamicShortcuts = listOf(launchDialpad) + config.lastHandledShortcutColor = appIconColor + } catch (ignored: Exception) { + } + } + } + + @SuppressLint("NewApi") + private fun getLaunchDialpadShortcut(appIconColor: Int): ShortcutInfo { + val newEvent = getString(R.string.dialpad) + val drawable = resources.getDrawable(R.drawable.shortcut_dialpad) + (drawable as LayerDrawable).findDrawableByLayerId(R.id.shortcut_dialpad_background).applyColorFilter(appIconColor) + val bmp = drawable.convertToBitmap() + + val intent = Intent(this, DialpadActivity::class.java) + intent.action = Intent.ACTION_VIEW + return ShortcutInfo.Builder(this, "launch_dialpad") + .setShortLabel(newEvent) + .setLongLabel(newEvent) + .setIcon(Icon.createWithBitmap(bmp)) + .setIntent(intent) + .build() + } + private fun setupTabColors() { val lastUsedPage = config.lastUsedViewPagerPage main_tabs_holder.apply { diff --git a/app/src/main/res/drawable/shortcut_dialpad.xml b/app/src/main/res/drawable/shortcut_dialpad.xml new file mode 100644 index 00000000..2f2225b7 --- /dev/null +++ b/app/src/main/res/drawable/shortcut_dialpad.xml @@ -0,0 +1,16 @@ + + + + + + + + + + +