replacing static shortcuts with dynamic ones

This commit is contained in:
tibbi 2019-03-07 12:25:08 +01:00
parent 7f87cc3412
commit 394f047bb9
7 changed files with 34 additions and 28 deletions

View File

@ -57,7 +57,7 @@ android {
} }
dependencies { dependencies {
implementation 'com.simplemobiletools:commons:5.10.0' implementation 'com.simplemobiletools:commons:5.10.5'
implementation 'joda-time:joda-time:2.10.1' implementation 'joda-time:joda-time:2.10.1'
implementation 'androidx.multidex:multidex:2.0.1' implementation 'androidx.multidex:multidex:2.0.1'

View File

@ -391,10 +391,6 @@
<action android:name="android.intent.action.MAIN"/> <action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/> <category android:name="android.intent.category.LAUNCHER"/>
</intent-filter> </intent-filter>
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts"/>
</activity-alias> </activity-alias>
<activity-alias <activity-alias

View File

@ -1,11 +1,16 @@
package com.simplemobiletools.calendar.pro.activities package com.simplemobiletools.calendar.pro.activities
import android.annotation.SuppressLint
import android.app.SearchManager import android.app.SearchManager
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.ActivityInfo import android.content.pm.ActivityInfo
import android.content.pm.ShortcutInfo
import android.content.pm.ShortcutManager
import android.database.Cursor import android.database.Cursor
import android.graphics.drawable.ColorDrawable import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Icon
import android.graphics.drawable.LayerDrawable
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.provider.ContactsContract import android.provider.ContactsContract
@ -130,6 +135,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
calendar_fab.setColors(config.textColor, getAdjustedPrimaryColor(), config.backgroundColor) calendar_fab.setColors(config.textColor, getAdjustedPrimaryColor(), config.backgroundColor)
search_holder.background = ColorDrawable(config.backgroundColor) search_holder.background = ColorDrawable(config.backgroundColor)
checkSwipeRefreshAvailability() checkSwipeRefreshAvailability()
checkShortcuts()
} }
override fun onPause() { override fun onPause() {
@ -259,6 +265,30 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
mSearchMenuItem?.collapseActionView() mSearchMenuItem?.collapseActionView()
} }
@SuppressLint("NewApi")
private fun checkShortcuts() {
val appIconColor = config.appIconColor
if (isNougatMR1Plus() && config.lastHandledShortcutColor != appIconColor) {
val newEvent = getString(R.string.new_event)
val manager = getSystemService(ShortcutManager::class.java)
val drawable = resources.getDrawable(R.drawable.shortcut_plus_orange)
(drawable as LayerDrawable).findDrawableByLayerId(R.id.shortcut_plus_background).applyColorFilter(appIconColor)
val bmp = drawable.convertToBitmap()
val intent = Intent(this, SplashActivity::class.java)
intent.action = SHORTCUT_NEW_EVENT
val shortcut = ShortcutInfo.Builder(this, "new_event")
.setShortLabel(newEvent)
.setLongLabel(newEvent)
.setIcon(Icon.createWithBitmap(bmp))
.setIntent(intent)
.build()
manager.dynamicShortcuts = Arrays.asList(shortcut)
config.lastHandledShortcutColor = appIconColor
}
}
private fun checkIsOpenIntent(): Boolean { private fun checkIsOpenIntent(): Boolean {
val dayCodeToOpen = intent.getStringExtra(DAY_CODE) ?: "" val dayCodeToOpen = intent.getStringExtra(DAY_CODE) ?: ""
val viewToOpen = intent.getIntExtra(VIEW_TO_OPEN, DAILY_VIEW) val viewToOpen = intent.getIntExtra(VIEW_TO_OPEN, DAILY_VIEW)

View File

@ -21,7 +21,7 @@ class SplashActivity : BaseSplashActivity() {
putExtra(EVENT_OCCURRENCE_TS, intent.getLongExtra(EVENT_OCCURRENCE_TS, 0L)) putExtra(EVENT_OCCURRENCE_TS, intent.getLongExtra(EVENT_OCCURRENCE_TS, 0L))
startActivity(this) startActivity(this)
} }
intent.action == "shortcut_new_event" -> { intent.action == SHORTCUT_NEW_EVENT -> {
val dayCode = Formatter.getDayCodeFromDateTime(DateTime()) val dayCode = Formatter.getDayCodeFromDateTime(DateTime())
Intent(this, EventActivity::class.java).apply { Intent(this, EventActivity::class.java).apply {
putExtra(NEW_EVENT_START_TS, getNewEventTimestampFromCode(dayCode)) putExtra(NEW_EVENT_START_TS, getNewEventTimestampFromCode(dayCode))

View File

@ -15,6 +15,7 @@ const val NEW_EVENT_SET_HOUR_DURATION = "new_event_set_hour_duration"
const val WEEK_START_DATE_TIME = "week_start_date_time" const val WEEK_START_DATE_TIME = "week_start_date_time"
const val CALDAV = "Caldav" const val CALDAV = "Caldav"
const val VIEW_TO_OPEN = "view_to_open" const val VIEW_TO_OPEN = "view_to_open"
const val SHORTCUT_NEW_EVENT = "shortcut_new_event"
const val REGULAR_EVENT_TYPE_ID = 1L const val REGULAR_EVENT_TYPE_ID = 1L
const val CHOPPED_LIST_DEFAULT_SIZE = 100 const val CHOPPED_LIST_DEFAULT_SIZE = 100

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item> <item android:id="@+id/shortcut_plus_background">
<shape android:shape="oval"> <shape android:shape="oval">
<solid android:color="@color/color_primary"/> <solid android:color="@color/color_primary"/>
</shape> </shape>

View File

@ -1,21 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<shortcuts
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<shortcut
android:enabled="true"
android:icon="@drawable/shortcut_plus_orange"
android:shortcutDisabledMessage="@string/new_event"
android:shortcutId="new_event"
android:shortcutLongLabel="@string/new_event"
android:shortcutShortLabel="@string/new_event"
tools:targetApi="n_mr1">
<intent
android:action="shortcut_new_event"
android:targetClass="com.simplemobiletools.calendar.pro.activities.SplashActivity"
android:targetPackage="${applicationId}"/>
</shortcut>
</shortcuts>