fix #363, allow creating folder desktop shortcuts

This commit is contained in:
tibbi 2019-05-05 19:42:43 +02:00
parent f4bcae8086
commit e1834343fa
3 changed files with 76 additions and 0 deletions

View File

@ -1,9 +1,19 @@
package com.simplemobiletools.gallery.pro.adapters package com.simplemobiletools.gallery.pro.adapters
import android.annotation.SuppressLint
import android.content.Intent
import android.content.pm.ShortcutInfo
import android.content.pm.ShortcutManager
import android.graphics.drawable.Drawable
import android.graphics.drawable.Icon
import android.graphics.drawable.LayerDrawable
import android.view.Menu import android.view.Menu
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.google.gson.Gson import com.google.gson.Gson
import com.simplemobiletools.commons.activities.BaseSimpleActivity import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
@ -12,10 +22,12 @@ import com.simplemobiletools.commons.dialogs.PropertiesDialog
import com.simplemobiletools.commons.dialogs.RenameItemDialog import com.simplemobiletools.commons.dialogs.RenameItemDialog
import com.simplemobiletools.commons.dialogs.RenameItemsDialog import com.simplemobiletools.commons.dialogs.RenameItemsDialog
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.isNougatMR1Plus
import com.simplemobiletools.commons.models.FileDirItem import com.simplemobiletools.commons.models.FileDirItem
import com.simplemobiletools.commons.views.FastScroller import com.simplemobiletools.commons.views.FastScroller
import com.simplemobiletools.commons.views.MyRecyclerView import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.gallery.pro.R import com.simplemobiletools.gallery.pro.R
import com.simplemobiletools.gallery.pro.activities.MediaActivity
import com.simplemobiletools.gallery.pro.dialogs.ExcludeFolderDialog import com.simplemobiletools.gallery.pro.dialogs.ExcludeFolderDialog
import com.simplemobiletools.gallery.pro.dialogs.PickMediumDialog import com.simplemobiletools.gallery.pro.dialogs.PickMediumDialog
import com.simplemobiletools.gallery.pro.extensions.* import com.simplemobiletools.gallery.pro.extensions.*
@ -75,6 +87,8 @@ class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: ArrayList<Directo
findItem(R.id.cab_empty_recycle_bin).isVisible = isOneItemSelected && selectedPaths.first() == RECYCLE_BIN findItem(R.id.cab_empty_recycle_bin).isVisible = isOneItemSelected && selectedPaths.first() == RECYCLE_BIN
findItem(R.id.cab_empty_disable_recycle_bin).isVisible = isOneItemSelected && selectedPaths.first() == RECYCLE_BIN findItem(R.id.cab_empty_disable_recycle_bin).isVisible = isOneItemSelected && selectedPaths.first() == RECYCLE_BIN
findItem(R.id.cab_create_shortcut).isVisible = isNougatMR1Plus() && isOneItemSelected
checkHideBtnVisibility(this, selectedPaths) checkHideBtnVisibility(this, selectedPaths)
checkPinBtnVisibility(this, selectedPaths) checkPinBtnVisibility(this, selectedPaths)
} }
@ -98,6 +112,7 @@ class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: ArrayList<Directo
R.id.cab_copy_to -> copyMoveTo(true) R.id.cab_copy_to -> copyMoveTo(true)
R.id.cab_move_to -> moveFilesTo() R.id.cab_move_to -> moveFilesTo()
R.id.cab_select_all -> selectAll() R.id.cab_select_all -> selectAll()
R.id.cab_create_shortcut -> createShortcut()
R.id.cab_delete -> askConfirmDelete() R.id.cab_delete -> askConfirmDelete()
R.id.cab_select_photo -> changeAlbumCover(false) R.id.cab_select_photo -> changeAlbumCover(false)
R.id.cab_use_default -> changeAlbumCover(true) R.id.cab_use_default -> changeAlbumCover(true)
@ -340,6 +355,56 @@ class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: ArrayList<Directo
} }
} }
@SuppressLint("NewApi")
private fun createShortcut() {
val manager = activity.getSystemService(ShortcutManager::class.java)
if (manager.isRequestPinShortcutSupported) {
val dir = getFirstSelectedItem() ?: return
val path = dir.path
val drawable = resources.getDrawable(R.drawable.shortcut_image).mutate()
getShortcutImage(dir.tmb, drawable) {
val intent = Intent(activity, MediaActivity::class.java)
intent.action = Intent.ACTION_VIEW
intent.putExtra(DIRECTORY, path)
val shortcut = ShortcutInfo.Builder(activity, path)
.setShortLabel(dir.name)
.setIcon(Icon.createWithBitmap(drawable.convertToBitmap()))
.setIntent(intent)
.build()
manager.requestPinShortcut(shortcut, null)
}
}
}
private fun getShortcutImage(tmb: String, drawable: Drawable, callback: () -> Unit) {
Thread {
val options = RequestOptions()
.format(DecodeFormat.PREFER_ARGB_8888)
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.fitCenter()
val size = activity.resources.getDimension(R.dimen.shortcut_size).toInt()
val builder = Glide.with(activity)
.asDrawable()
.load(tmb)
.apply(options)
.centerCrop()
.into(size, size)
try {
(drawable as LayerDrawable).setDrawableByLayerId(R.id.shortcut_image, builder.get())
} catch (e: Exception) {
}
activity.runOnUiThread {
callback()
}
}.start()
}
private fun askConfirmDelete() { private fun askConfirmDelete() {
when { when {
config.isDeletePasswordProtectionOn -> activity.handleDeletePasswordProtection { config.isDeletePasswordProtectionOn -> activity.handleDeletePasswordProtection {

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/shortcut_image"
android:drawable="@mipmap/ic_launcher"/>
</layer-list>

View File

@ -61,6 +61,10 @@
android:icon="@drawable/ic_select_all" android:icon="@drawable/ic_select_all"
android:title="@string/select_all" android:title="@string/select_all"
app:showAsAction="ifRoom"/> app:showAsAction="ifRoom"/>
<item
android:id="@+id/cab_create_shortcut"
android:title="@string/create_shortcut"
app:showAsAction="never"/>
<item <item
android:id="@+id/cab_change_cover_image" android:id="@+id/cab_change_cover_image"
android:title="@string/change_cover_image" android:title="@string/change_cover_image"