Improve shortcut handling by using ShortcutManagerCompat

supports shortcuts on newer versions
This commit is contained in:
ligi 2020-02-16 17:50:04 +01:00
parent 1da68a5706
commit 4e62614ef9
No known key found for this signature in database
GPG key ID: 8E81894010ABF23D
2 changed files with 23 additions and 33 deletions

View file

@ -2,14 +2,16 @@ package org.ligi.passandroid.ui
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import androidx.core.app.*
import androidx.viewpager.widget.ViewPager
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.WindowManager import android.view.WindowManager
import androidx.core.app.NavUtils
import androidx.core.app.TaskStackBuilder
import androidx.core.content.pm.ShortcutManagerCompat
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager.widget.ViewPager
import kotlinx.android.synthetic.main.activity_pass_view_base.* import kotlinx.android.synthetic.main.activity_pass_view_base.*
import org.ligi.kaxt.disableRotation import org.ligi.kaxt.disableRotation
import org.ligi.passandroid.R import org.ligi.passandroid.R
@ -91,7 +93,7 @@ class PassViewActivity : PassViewActivityBase() {
override fun onPrepareOptionsMenu(menu: Menu): Boolean { override fun onPrepareOptionsMenu(menu: Menu): Boolean {
menu.findItem(R.id.menu_map).isVisible = !currentPass.locations.isEmpty() menu.findItem(R.id.menu_map).isVisible = !currentPass.locations.isEmpty()
menu.findItem(R.id.menu_update).isVisible = PassViewActivityBase.mightPassBeAbleToUpdate(currentPass) menu.findItem(R.id.menu_update).isVisible = PassViewActivityBase.mightPassBeAbleToUpdate(currentPass)
menu.findItem(R.id.install_shortcut).isVisible = (23..25).contains(Build.VERSION.SDK_INT) menu.findItem(R.id.install_shortcut).isVisible = ShortcutManagerCompat.isRequestPinShortcutSupported(this)
return super.onPrepareOptionsMenu(menu) return super.onPrepareOptionsMenu(menu)
} }

View file

@ -3,22 +3,23 @@ package org.ligi.passandroid.ui
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.Dialog import android.app.Dialog
import android.app.ProgressDialog import android.app.ProgressDialog
import android.content.ComponentName
import android.content.Intent import android.content.Intent
import android.content.pm.ShortcutInfo
import android.content.pm.ShortcutManager
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import android.graphics.drawable.Icon
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import com.google.android.material.snackbar.Snackbar import android.view.Menu
import android.view.MenuItem
import android.view.ViewConfiguration
import android.view.WindowManager
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import android.view.* import androidx.core.content.pm.ShortcutInfoCompat
import androidx.core.content.pm.ShortcutManagerCompat
import androidx.core.graphics.drawable.IconCompat
import com.google.android.material.snackbar.Snackbar
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.Request import okhttp3.Request
import okhttp3.Response import okhttp3.Response
import org.ligi.passandroid.BuildConfig
import org.ligi.passandroid.R import org.ligi.passandroid.R
import org.ligi.passandroid.model.InputStreamWithSource import org.ligi.passandroid.model.InputStreamWithSource
import org.ligi.passandroid.model.PassBitmapDefinitions.BITMAP_ICON import org.ligi.passandroid.model.PassBitmapDefinitions.BITMAP_ICON
@ -28,7 +29,6 @@ import org.ligi.passandroid.ui.UnzipPassController.InputStreamUnzipControllerSpe
import permissions.dispatcher.NeedsPermission import permissions.dispatcher.NeedsPermission
import permissions.dispatcher.RuntimePermissions import permissions.dispatcher.RuntimePermissions
import java.io.IOException import java.io.IOException
import java.util.*
@SuppressLint("Registered") @SuppressLint("Registered")
@RuntimePermissions @RuntimePermissions
@ -150,34 +150,22 @@ open class PassViewActivityBase : PassAndroidActivity() {
@NeedsPermission("com.android.launcher.permission.INSTALL_SHORTCUT") @NeedsPermission("com.android.launcher.permission.INSTALL_SHORTCUT")
fun createShortcut() { fun createShortcut() {
val shortcutIntent = Intent()
shortcutIntent.putExtra(EXTRA_KEY_UUID, currentPass.id)
shortcutIntent.component = ComponentName(BuildConfig.APPLICATION_ID,
BuildConfig.APPLICATION_ID + ".ui.PassViewActivity")
val passBitmap = currentPass.getBitmap(passStore, BITMAP_ICON) val passBitmap = currentPass.getBitmap(passStore, BITMAP_ICON)
val shortcutIcon = if (passBitmap != null) { val shortcutIcon = if (passBitmap != null) {
Bitmap.createScaledBitmap(passBitmap, 128, 128, true) Bitmap.createScaledBitmap(passBitmap, 128, 128, true)
} else { } else {
BitmapFactory.decodeResource(resources, R.drawable.ic_launcher) BitmapFactory.decodeResource(resources, R.drawable.ic_launcher)
} }
val name: CharSequence = currentPass.description ?: "shortcut"
if (Build.VERSION.SDK_INT >= 25) { val targetIntent = Intent(this, PassViewActivity::class.java)
val shortcutManager = getSystemService<ShortcutManager>(ShortcutManager::class.java) .setAction(Intent.ACTION_MAIN)
val shortcut = ShortcutInfo.Builder(this, "id1") .putExtra(EXTRA_KEY_UUID, currentPass.id)
.setShortLabel(currentPass.description ?: "") val shortcutInfo = ShortcutInfoCompat.Builder(this, "shortcut$name")
.setIcon(Icon.createWithBitmap(shortcutIcon)) .setIntent(targetIntent)
.setIntent(shortcutIntent) .setShortLabel(name)
.build() .setIcon(IconCompat.createWithBitmap(shortcutIcon))
.build()
shortcutManager.dynamicShortcuts = Arrays.asList(shortcut) ShortcutManagerCompat.requestPinShortcut(this, shortcutInfo, null)
} else {
val intent = Intent("com.android.launcher.action.INSTALL_SHORTCUT")
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent)
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, currentPass.description)
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, shortcutIcon)
sendBroadcast(intent)
}
} }
inner class UpdateAsync : Runnable { inner class UpdateAsync : Runnable {