Skip to content

Commit db5fdcd

Browse files
committed
add a shortcut for creating new tasks
1 parent 09ecb2c commit db5fdcd

5 files changed

Lines changed: 76 additions & 16 deletions

File tree

app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import android.app.SearchManager
66
import android.content.Context
77
import android.content.Intent
88
import android.content.pm.ShortcutInfo
9-
import android.content.pm.ShortcutManager
109
import android.graphics.drawable.ColorDrawable
1110
import android.graphics.drawable.Icon
1211
import android.graphics.drawable.LayerDrawable
@@ -387,29 +386,54 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
387386
private fun checkShortcuts() {
388387
val appIconColor = config.appIconColor
389388
if (isNougatMR1Plus() && config.lastHandledShortcutColor != appIconColor) {
390-
val newEvent = getString(R.string.new_event)
391-
val manager = getSystemService(ShortcutManager::class.java)
392-
val drawable = resources.getDrawable(R.drawable.shortcut_plus, theme)
393-
(drawable as LayerDrawable).findDrawableByLayerId(R.id.shortcut_plus_background).applyColorFilter(appIconColor)
394-
val bmp = drawable.convertToBitmap()
395-
396-
val intent = Intent(this, SplashActivity::class.java)
397-
intent.action = SHORTCUT_NEW_EVENT
398-
val shortcut = ShortcutInfo.Builder(this, "new_event")
399-
.setShortLabel(newEvent)
400-
.setLongLabel(newEvent)
401-
.setIcon(Icon.createWithBitmap(bmp))
402-
.setIntent(intent)
403-
.build()
389+
val newEvent = getNewEventShortcut(appIconColor)
390+
val shortcuts = arrayListOf(newEvent)
391+
392+
if (config.allowCreatingTasks) {
393+
shortcuts.add(getNewTaskShortcut(appIconColor))
394+
}
404395

405396
try {
406-
manager.dynamicShortcuts = Arrays.asList(shortcut)
397+
shortcutManager.dynamicShortcuts = shortcuts
407398
config.lastHandledShortcutColor = appIconColor
408399
} catch (ignored: Exception) {
409400
}
410401
}
411402
}
412403

404+
@SuppressLint("NewApi")
405+
private fun getNewEventShortcut(appIconColor: Int): ShortcutInfo {
406+
val newEvent = getString(R.string.new_event)
407+
val newEventDrawable = resources.getDrawable(R.drawable.shortcut_event, theme)
408+
(newEventDrawable as LayerDrawable).findDrawableByLayerId(R.id.shortcut_event_background).applyColorFilter(appIconColor)
409+
val newEventBitmap = newEventDrawable.convertToBitmap()
410+
411+
val newEventIntent = Intent(this, SplashActivity::class.java)
412+
newEventIntent.action = SHORTCUT_NEW_EVENT
413+
return ShortcutInfo.Builder(this, "new_event")
414+
.setShortLabel(newEvent)
415+
.setLongLabel(newEvent)
416+
.setIcon(Icon.createWithBitmap(newEventBitmap))
417+
.setIntent(newEventIntent)
418+
.build()
419+
}
420+
421+
@SuppressLint("NewApi")
422+
private fun getNewTaskShortcut(appIconColor: Int): ShortcutInfo {
423+
val newTask = getString(R.string.new_task)
424+
val newTaskDrawable = resources.getDrawable(R.drawable.shortcut_task, theme)
425+
(newTaskDrawable as LayerDrawable).findDrawableByLayerId(R.id.shortcut_task_background).applyColorFilter(appIconColor)
426+
val newTaskBitmap = newTaskDrawable.convertToBitmap()
427+
val newTaskIntent = Intent(this, SplashActivity::class.java)
428+
newTaskIntent.action = SHORTCUT_NEW_TASK
429+
return ShortcutInfo.Builder(this, "new_task")
430+
.setShortLabel(newTask)
431+
.setLongLabel(newTask)
432+
.setIcon(Icon.createWithBitmap(newTaskBitmap))
433+
.setIntent(newTaskIntent)
434+
.build()
435+
}
436+
413437
private fun checkIsOpenIntent(): Boolean {
414438
val dayCodeToOpen = intent.getStringExtra(DAY_CODE) ?: ""
415439
val viewToOpen = intent.getIntExtra(VIEW_TO_OPEN, DAILY_VIEW)

app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/SplashActivity.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ class SplashActivity : BaseSplashActivity() {
2626
startActivity(this)
2727
}
2828
}
29+
intent.action == SHORTCUT_NEW_TASK -> {
30+
val dayCode = Formatter.getDayCodeFromDateTime(DateTime())
31+
Intent(this, TaskActivity::class.java).apply {
32+
putExtra(NEW_EVENT_START_TS, getNewEventTimestampFromCode(dayCode))
33+
startActivity(this)
34+
}
35+
}
2936
else -> startActivity(Intent(this, MainActivity::class.java))
3037
}
3138
finish()

app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Constants.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const val YEAR_TO_OPEN = "year_to_open"
2121
const val CALDAV = "Caldav"
2222
const val VIEW_TO_OPEN = "view_to_open"
2323
const val SHORTCUT_NEW_EVENT = "shortcut_new_event"
24+
const val SHORTCUT_NEW_TASK = "shortcut_new_task"
2425
const val REGULAR_EVENT_TYPE_ID = 1L
2526
const val TIME_ZONE = "time_zone"
2627
const val CURRENT_TIME_ZONE = "current_time_zone"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
3+
<item android:id="@+id/shortcut_event_background">
4+
<shape android:shape="oval">
5+
<solid android:color="@color/color_primary" />
6+
</shape>
7+
</item>
8+
<item
9+
android:bottom="@dimen/medium_margin"
10+
android:drawable="@drawable/ic_today_vector"
11+
android:left="@dimen/medium_margin"
12+
android:right="@dimen/medium_margin"
13+
android:top="@dimen/medium_margin" />
14+
</layer-list>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
3+
<item android:id="@+id/shortcut_task_background">
4+
<shape android:shape="oval">
5+
<solid android:color="@color/color_primary" />
6+
</shape>
7+
</item>
8+
<item
9+
android:bottom="@dimen/medium_margin"
10+
android:drawable="@drawable/ic_task_vector"
11+
android:left="@dimen/medium_margin"
12+
android:right="@dimen/medium_margin"
13+
android:top="@dimen/medium_margin" />
14+
</layer-list>

0 commit comments

Comments
 (0)