From 7d95415ebe1b1e0a812f4ae9585af51cd975d3ce Mon Sep 17 00:00:00 2001
From: Agnieszka C <85929121+Aga-C@users.noreply.github.com>
Date: Fri, 3 Mar 2023 20:27:40 +0100
Subject: [PATCH] Added create shortcut option (#598)
---
.../notes/pro/activities/MainActivity.kt | 27 +++++++++++++++++++
app/src/main/res/drawable/shortcut_note.xml | 9 +++++++
app/src/main/res/menu/menu.xml | 4 +++
3 files changed, 40 insertions(+)
create mode 100644 app/src/main/res/drawable/shortcut_note.xml
diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt
index 1f6fd880..1c2a52f5 100644
--- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt
+++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt
@@ -6,6 +6,7 @@ import android.content.ActivityNotFoundException
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.graphics.drawable.LayerDrawable
import android.net.Uri
@@ -43,6 +44,7 @@ import com.simplemobiletools.notes.pro.fragments.TextFragment
import com.simplemobiletools.notes.pro.helpers.*
import com.simplemobiletools.notes.pro.models.Note
import kotlinx.android.synthetic.main.activity_main.*
+import kotlinx.android.synthetic.main.item_checklist.*
import java.io.File
import java.io.FileOutputStream
import java.io.OutputStream
@@ -214,6 +216,7 @@ class MainActivity : SimpleActivity() {
R.id.new_note -> displayNewNoteDialog()
R.id.rename_note -> fragment?.handleUnlocking { displayRenameDialog() }
R.id.share -> fragment?.handleUnlocking { shareText() }
+ R.id.cab_create_shortcut -> createShortcut()
R.id.lock_note -> lockNote()
R.id.unlock_note -> unlockNote()
R.id.open_file -> tryOpenFile()
@@ -1320,6 +1323,30 @@ class MainActivity : SimpleActivity() {
}
}
+ @SuppressLint("NewApi")
+ private fun createShortcut() {
+ val manager = getSystemService(ShortcutManager::class.java)
+ if (manager.isRequestPinShortcutSupported) {
+ val note = mCurrentNote
+ val drawable = resources.getDrawable(R.drawable.shortcut_note).mutate()
+ val appIconColor = baseConfig.appIconColor
+ (drawable as LayerDrawable).findDrawableByLayerId(R.id.shortcut_plus_background).applyColorFilter(appIconColor)
+
+ val intent = Intent(this, SplashActivity::class.java)
+ intent.action = Intent.ACTION_VIEW
+ intent.putExtra(OPEN_NOTE_ID, note.id)
+ intent.flags = intent.flags or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NO_HISTORY
+
+ val shortcut = ShortcutInfo.Builder(this, note.hashCode().toString())
+ .setShortLabel(mCurrentNote.title)
+ .setIcon(Icon.createWithBitmap(drawable.convertToBitmap()))
+ .setIntent(intent)
+ .build()
+
+ manager.requestPinShortcut(shortcut, null)
+ }
+ }
+
private fun lockNote() {
ConfirmationDialog(this, "", R.string.locking_warning, R.string.ok, R.string.cancel) {
SecurityDialog(this, "", SHOW_ALL_TABS) { hash, type, success ->
diff --git a/app/src/main/res/drawable/shortcut_note.xml b/app/src/main/res/drawable/shortcut_note.xml
new file mode 100644
index 00000000..3e96b5c7
--- /dev/null
+++ b/app/src/main/res/drawable/shortcut_note.xml
@@ -0,0 +1,9 @@
+
+
+ -
+
+
+
+
+
+
diff --git a/app/src/main/res/menu/menu.xml b/app/src/main/res/menu/menu.xml
index d2b0ef87..50516e8d 100644
--- a/app/src/main/res/menu/menu.xml
+++ b/app/src/main/res/menu/menu.xml
@@ -50,6 +50,10 @@
android:icon="@drawable/ic_share_vector"
android:title="@string/share"
app:showAsAction="ifRoom" />
+