Skip to content

Commit e7839bc

Browse files
committed
allow creating shortcuts of files too
1 parent 253db88 commit e7839bc

File tree

2 files changed

+76
-38
lines changed

2 files changed

+76
-38
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ android {
5151
}
5252

5353
dependencies {
54-
implementation 'com.simplemobiletools:commons:5.12.11'
54+
implementation 'com.simplemobiletools:commons:5.12.12'
5555
implementation 'com.github.Stericson:RootTools:df729dcb13'
5656
implementation 'com.github.Stericson:RootShell:1.6'
5757
implementation 'com.alexvasilkov:gesture-views:2.5.2'

app/src/main/kotlin/com/simplemobiletools/filemanager/pro/adapters/ItemsAdapter.kt

Lines changed: 75 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import android.view.Menu
1717
import android.view.View
1818
import android.view.ViewGroup
1919
import com.bumptech.glide.Glide
20+
import com.bumptech.glide.load.DecodeFormat
2021
import com.bumptech.glide.load.engine.DiskCacheStrategy
2122
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
2223
import com.bumptech.glide.request.RequestOptions
@@ -233,29 +234,63 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem
233234
val manager = activity.getSystemService(ShortcutManager::class.java)
234235
if (manager.isRequestPinShortcutSupported) {
235236
val path = getFirstSelectedItemPath()
237+
val drawable = resources.getDrawable(R.drawable.shortcut_folder).mutate()
238+
getShortcutImage(path, drawable) {
239+
val intent = Intent(activity, SplashActivity::class.java)
240+
intent.action = Intent.ACTION_VIEW
241+
intent.data = Uri.fromFile(File(path))
236242

237-
val appIconColor = baseConfig.appIconColor
238-
val drawable = resources.getDrawable(R.drawable.shortcut_folder)
239-
(drawable as LayerDrawable).findDrawableByLayerId(R.id.shortcut_folder_background).applyColorFilter(appIconColor)
240-
val bmp = drawable.convertToBitmap()
243+
val shortcut = ShortcutInfo.Builder(activity, path)
244+
.setShortLabel(path.getFilenameFromPath())
245+
.setIcon(Icon.createWithBitmap(drawable.convertToBitmap()))
246+
.setIntent(intent)
247+
.build()
241248

242-
val intent = Intent(activity, SplashActivity::class.java)
243-
intent.action = Intent.ACTION_VIEW
244-
intent.data = Uri.fromFile(File(path))
249+
manager.dynamicShortcuts = Arrays.asList(shortcut)
245250

246-
val shortcut = ShortcutInfo.Builder(activity, path)
247-
.setShortLabel(path.getFilenameFromPath())
248-
.setIcon(Icon.createWithBitmap(bmp))
249-
.setIntent(intent)
250-
.build()
251+
val pinShortcutInfo = ShortcutInfo.Builder(activity, path).build()
252+
val pinnedShortcutCallbackIntent = manager.createShortcutResultIntent(pinShortcutInfo)
251253

252-
manager.dynamicShortcuts = Arrays.asList(shortcut)
254+
val successCallback = PendingIntent.getBroadcast(activity, 0, pinnedShortcutCallbackIntent, 0)
255+
manager.requestPinShortcut(pinShortcutInfo, successCallback.intentSender)
256+
}
257+
}
258+
}
253259

254-
val pinShortcutInfo = ShortcutInfo.Builder(activity, path).build()
255-
val pinnedShortcutCallbackIntent = manager.createShortcutResultIntent(pinShortcutInfo)
260+
private fun getShortcutImage(path: String, drawable: Drawable, callback: () -> Unit) {
261+
val appIconColor = baseConfig.appIconColor
262+
(drawable as LayerDrawable).findDrawableByLayerId(R.id.shortcut_folder_background).applyColorFilter(appIconColor)
263+
if (File(path).isDirectory) {
264+
callback()
265+
} else {
266+
Thread {
267+
val options = RequestOptions()
268+
.format(DecodeFormat.PREFER_ARGB_8888)
269+
.skipMemoryCache(true)
270+
.diskCacheStrategy(DiskCacheStrategy.NONE)
271+
.fitCenter()
272+
273+
val size = activity.resources.getDimension(R.dimen.shortcut_size).toInt()
274+
val builder = Glide.with(activity)
275+
.asDrawable()
276+
.load(getImagePathToLoad(path))
277+
.apply(options)
278+
.centerCrop()
279+
.into(size, size)
280+
281+
try {
282+
val bitmap = builder.get()
283+
drawable.findDrawableByLayerId(R.id.shortcut_folder_background).applyColorFilter(0)
284+
drawable.setDrawableByLayerId(R.id.shortcut_folder_image, bitmap)
285+
} catch (e: Exception) {
286+
val fileIcon = activity.resources.getDrawable(R.drawable.ic_file)
287+
drawable.setDrawableByLayerId(R.id.shortcut_folder_image, fileIcon)
288+
}
256289

257-
val successCallback = PendingIntent.getBroadcast(activity, 0, pinnedShortcutCallbackIntent, 0)
258-
manager.requestPinShortcut(pinShortcutInfo, successCallback.intentSender)
290+
activity.runOnUiThread {
291+
callback()
292+
}
293+
}.start()
259294
}
260295
}
261296

@@ -632,31 +667,12 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem
632667
item_details.text = getChildrenCnt(listItem)
633668
} else {
634669
item_details.text = listItem.size.formatSize()
635-
val path = listItem.path
636670
val options = RequestOptions()
637671
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
638672
.error(fileDrawable)
639673
.centerCrop()
640674

641-
var itemToLoad = if (listItem.name.endsWith(".apk", true)) {
642-
val packageInfo = context.packageManager.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES)
643-
if (packageInfo != null) {
644-
val appInfo = packageInfo.applicationInfo
645-
appInfo.sourceDir = path
646-
appInfo.publicSourceDir = path
647-
appInfo.loadIcon(context.packageManager)
648-
} else {
649-
path
650-
}
651-
} else {
652-
path
653-
}
654-
655-
656-
if (hasOTGConnected && itemToLoad is String && activity.isPathOnOTG(itemToLoad) && baseConfig.OTGTreeUri.isNotEmpty() && baseConfig.OTGPartition.isNotEmpty()) {
657-
itemToLoad = getOTGPublicPath(itemToLoad)
658-
}
659-
675+
val itemToLoad = getImagePathToLoad(listItem.path)
660676
if (!activity.isDestroyed) {
661677
Glide.with(activity).load(itemToLoad).transition(DrawableTransitionOptions.withCrossFade()).apply(options).into(item_icon)
662678
}
@@ -671,4 +687,26 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem
671687
}
672688

673689
private fun getOTGPublicPath(itemToLoad: String) = "${baseConfig.OTGTreeUri}/document/${baseConfig.OTGPartition}%3A${itemToLoad.substring(baseConfig.OTGPath.length).replace("/", "%2F")}"
690+
691+
private fun getImagePathToLoad(path: String): Any {
692+
var itemToLoad = if (path.endsWith(".apk", true)) {
693+
val packageInfo = activity.packageManager.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES)
694+
if (packageInfo != null) {
695+
val appInfo = packageInfo.applicationInfo
696+
appInfo.sourceDir = path
697+
appInfo.publicSourceDir = path
698+
appInfo.loadIcon(activity.packageManager)
699+
} else {
700+
path
701+
}
702+
} else {
703+
path
704+
}
705+
706+
if (hasOTGConnected && itemToLoad is String && activity.isPathOnOTG(itemToLoad) && baseConfig.OTGTreeUri.isNotEmpty() && baseConfig.OTGPartition.isNotEmpty()) {
707+
itemToLoad = getOTGPublicPath(itemToLoad)
708+
}
709+
710+
return itemToLoad
711+
}
674712
}

0 commit comments

Comments
 (0)