Skip to content

Commit

Permalink
add context menu to apps
Browse files Browse the repository at this point in the history
  • Loading branch information
newhinton committed Aug 23, 2024
1 parent 60a55de commit e3fec61
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
16 changes: 16 additions & 0 deletions app/src/main/java/de/felixnuesse/disky/extensions/packageinfo.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package de.felixnuesse.disky.extensions

import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager.NameNotFoundException
import android.graphics.drawable.Drawable
import android.net.Uri
import android.provider.Settings
import androidx.core.content.ContextCompat


fun Any.getAppname(packagename: String, context: Context): String {
Expand All @@ -25,4 +29,16 @@ fun Any.getAppIcon(packagename: String, context: Context): Drawable? {
} catch (e: NameNotFoundException) {
return null
}
}

fun Any.startApp(packagename: String, context: Context) {
val packageManager = context.packageManager
val intent = packageManager.getLaunchIntentForPackage(packagename)
intent?.let { ContextCompat.startActivity(context, it, null) }
}

fun Any.startAppSettings(packagename: String, context: Context) {
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
intent.data = Uri.fromParts("package", packagename, null)
intent.let { ContextCompat.startActivity(context, it, null) }
}
27 changes: 22 additions & 5 deletions app/src/main/java/de/felixnuesse/disky/ui/RecyclerViewAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ import android.content.Context
import android.content.Intent
import android.net.Uri
import android.provider.DocumentsContract
import android.util.Log
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.ViewGroup
import android.webkit.MimeTypeMap
import androidx.appcompat.content.res.AppCompatResources
import androidx.appcompat.widget.PopupMenu
import androidx.core.content.ContextCompat.startActivity
import androidx.recyclerview.widget.RecyclerView
import de.felixnuesse.disky.R
import de.felixnuesse.disky.databinding.ItemFolderEntryBinding
import de.felixnuesse.disky.databinding.ItemLeafEntryBinding
import de.felixnuesse.disky.extensions.getAppIcon
import de.felixnuesse.disky.extensions.getAppname
import de.felixnuesse.disky.extensions.readableFileSize
import de.felixnuesse.disky.extensions.startApp
import de.felixnuesse.disky.extensions.startAppSettings
import de.felixnuesse.disky.model.StorageBranch
import de.felixnuesse.disky.model.StorageLeaf
import de.felixnuesse.disky.model.StoragePrototype
Expand Down Expand Up @@ -50,6 +52,7 @@ class RecyclerViewAdapter(private var mContext: Context, private val folders: Li
if(holder is FolderView) {
with(holder) {
with(folders[position]){
val branch = folders[position] as StorageBranch
binding.title.text = name
binding.size.text = readableFileSize(getCalculatedSize())
binding.progressBar.progress = percent
Expand All @@ -62,10 +65,11 @@ class RecyclerViewAdapter(private var mContext: Context, private val folders: Li
binding.title.text = getAppname(name, mContext)
binding.imageView.imageTintList = null
binding.imageView.setImageDrawable(getAppIcon(name, mContext))
setMenu(R.menu.context_folder_app_menu, branch)
}

if(storageType == StorageType.FOLDER) {
holder.leafFolder = folders[position] as StorageBranch
holder.leafFolder = branch
enableDeletion()
}
setChangeFolderCallbackTarget(this)
Expand Down Expand Up @@ -122,15 +126,20 @@ class RecyclerViewAdapter(private var mContext: Context, private val folders: Li
private var popupMenuEnableDeletion = false

init {
setMenu(R.menu.context_folder_menu, null)
}

fun setMenu(menu: Int, branch: StorageBranch?) {
branch.let{leafFolder = it}
binding.root.setOnLongClickListener {
if(leafFolder == null) {
return@setOnLongClickListener true
}
val context = binding.root.context
var popup = PopupMenu(context, it)
val popup = PopupMenu(context, it)
popup.setOnMenuItemClickListener(this)
popup.menuInflater.inflate(R.menu.context_folder_menu, popup.menu)
popup.menu.findItem(R.id.action_folder_delete).setVisible(popupMenuEnableDeletion)
popup.menuInflater.inflate(menu, popup.menu)
popup.menu.findItem(R.id.action_folder_delete)?.setVisible(popupMenuEnableDeletion)
popup.setForceShowIcon(true)
popup.show()
true
Expand All @@ -154,6 +163,14 @@ class RecyclerViewAdapter(private var mContext: Context, private val folders: Li
DeleteDialog(mContext, File(leafFolder!!.getParentPath())).askDelete()
true
}
R.id.action_folder_app_open -> {
leafFolder?.let { startApp(it.name, mContext) }
true
}
R.id.action_folder_app_settings -> {
leafFolder?.let { startAppSettings(it.name, mContext) }
true
}
else -> false
}
}
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/icon_open.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:autoMirrored="true" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/white" android:pathData="M18,19H6c-0.55,0 -1,-0.45 -1,-1V6c0,-0.55 0.45,-1 1,-1h5c0.55,0 1,-0.45 1,-1s-0.45,-1 -1,-1H5c-1.11,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2v-6c0,-0.55 -0.45,-1 -1,-1s-1,0.45 -1,1v5c0,0.55 -0.45,1 -1,1zM14,4c0,0.55 0.45,1 1,1h2.59l-9.13,9.13c-0.39,0.39 -0.39,1.02 0,1.41 0.39,0.39 1.02,0.39 1.41,0L19,6.41V9c0,0.55 0.45,1 1,1s1,-0.45 1,-1V4c0,-0.55 -0.45,-1 -1,-1h-5c-0.55,0 -1,0.45 -1,1z"/>

</vector>
17 changes: 17 additions & 0 deletions app/src/main/res/menu/context_folder_app_menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:id="@+id/action_folder_app_open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/icon_open"
android:title="@string/contextmenu_folder_app_open" />

<item
android:id="@+id/action_folder_app_settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/icon_settings"
android:title="@string/contextmenu_app_open_settings" />
</menu>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,6 @@
<string name="error_notification_channel_message">The Scan-Service encountered an error.</string>
<string name="error_notification_title">Scanning was not successful</string>
<string name="error_notification_action_copy">Copy Error</string>
<string name="contextmenu_folder_app_open">Open</string>
<string name="contextmenu_app_open_settings">Settings</string>
</resources>

0 comments on commit e3fec61

Please sign in to comment.