Skip to content

Commit

Permalink
[WIP] Add context menu "Open in Terminal" option
Browse files Browse the repository at this point in the history
  • Loading branch information
TranceLove committed May 2, 2023
1 parent b5199ff commit bd3e8c8
Show file tree
Hide file tree
Showing 12 changed files with 518 additions and 73 deletions.
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />

<uses-permission android:name="jackpal.androidterm.permission.RUN_SCRIPT" />
<uses-permission android:name="com.termoneplus.permission.RUN_SCRIPT" />
<uses-permission android:name="yarolegovich.materialterminal.permission.RUN_SCRIPT" />

<uses-feature
android:name="android.hardware.touchscreen"
android:required="false" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ import com.amaze.filemanager.utils.AnimUtils.marqueeAfterDelay
import com.amaze.filemanager.utils.Utils
import com.amaze.filemanager.utils.safeLet
import java.io.File
import kotlin.collections.ArrayList
import kotlin.math.roundToInt

class AppsRecyclerAdapter(
private val fragment: Fragment,
private val modelProvider: AppsAdapterPreloadModel,
private val isBottomSheet: Boolean,
private val adjustListViewCallback: AdjustListViewForTv<AppHolder>,
private val appDataParcelableList: MutableList<AppDataParcelable>
private val appDataParcelableList: MutableList<AppDataParcelable>,
private val customRowAction: ((AppDataParcelable) -> Unit)? = null,
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

private val myChecked = SparseBooleanArray()
Expand Down Expand Up @@ -135,7 +135,7 @@ class AppsRecyclerAdapter(
SpecialViewHolder.HEADER_SYSTEM_APP
} else {
SpecialViewHolder.HEADER_USER_APP
}
},
)
}
EMPTY_LAST_ITEM -> {
Expand Down Expand Up @@ -172,15 +172,15 @@ class AppsRecyclerAdapter(
if (holder.about != null && !isBottomSheet) {
if ((fragment.requireActivity() as MainActivity).appTheme == AppTheme.LIGHT) {
holder.about.setColorFilter(
Color.parseColor("#ff666666")
Color.parseColor("#ff666666"),
)
}
showPopup(holder.about, rowItem)
}
holder.rl.setOnFocusChangeListener { _, _ ->
adjustListViewCallback.adjustListViewForTv(
holder,
fragment.requireActivity() as MainActivity
fragment.requireActivity() as MainActivity,
)
}
holder.txtTitle.text = rowItem.label
Expand Down Expand Up @@ -208,7 +208,7 @@ class AppsRecyclerAdapter(
}
if (myChecked[position]) {
holder.rl.setBackgroundColor(
Utils.getColor(fragment.context, R.color.appsadapter_background)
Utils.getColor(fragment.context, R.color.appsadapter_background),
)
} else {
if ((fragment.requireActivity() as MainActivity).appTheme == AppTheme.LIGHT) {
Expand Down Expand Up @@ -240,61 +240,61 @@ class AppsRecyclerAdapter(

private fun startActivityForRowItem(rowItem: AppDataParcelable?) {
rowItem?.run {
if (isBottomSheet) {
val openFileParcelable = rowItem.openFileParcelable
openFileParcelable?.let {
safeLet(
openFileParcelable.uri,
openFileParcelable.mimeType,
openFileParcelable.useNewStack
) {
uri, mimeType, useNewStack ->
val intent = buildIntent(
uri,
mimeType,
useNewStack,
openFileParcelable.className,
openFileParcelable.packageName
)
setLastOpenedApp(
rowItem,
fragment.requireActivity() as PreferenceActivity
)
fragment.requireContext().startActivityCatchingSecurityException(intent)
customRowAction?.invoke(rowItem)
?: if (isBottomSheet) {
val openFileParcelable = rowItem.openFileParcelable
openFileParcelable?.let {
safeLet(
openFileParcelable.uri,
openFileParcelable.mimeType,
openFileParcelable.useNewStack,
) { uri, mimeType, useNewStack ->
val intent = buildIntent(
uri,
mimeType,
useNewStack,
openFileParcelable.className,
openFileParcelable.packageName,
)
setLastOpenedApp(
rowItem,
fragment.requireActivity() as PreferenceActivity,
)
fragment.requireContext().startActivityCatchingSecurityException(intent)
}
}
}
} else {
val i1 = fragment.requireContext().packageManager.getLaunchIntentForPackage(
rowItem.packageName
)
if (i1 != null) {
fragment.startActivity(i1)
} else {
Toast.makeText(
fragment.context,
fragment.getString(R.string.not_allowed),
Toast.LENGTH_LONG
val i1 = fragment.requireContext().packageManager.getLaunchIntentForPackage(
rowItem.packageName,
)
.show()
// TODO: Implement this method
if (i1 != null) {
fragment.startActivity(i1)
} else {
Toast.makeText(
fragment.context,
fragment.getString(R.string.not_allowed),
Toast.LENGTH_LONG,
)
.show()
// TODO: Implement this method
}
}
}
}
}

private fun showPopup(v: View, rowItem: AppDataParcelable?) {
v.setOnClickListener { view: View? ->
var context = fragment.context
if ((
fragment.requireActivity()
as MainActivity
).appTheme.getSimpleTheme(fragment.requireContext()) == AppTheme.BLACK
fragment.requireActivity()
as MainActivity
).appTheme.getSimpleTheme(fragment.requireContext()) == AppTheme.BLACK
) {
context = ContextThemeWrapper(context, R.style.overflow_black)
}
val popupMenu = PopupMenu(
context,
view
view,
)
popupMenu.setOnMenuItemClickListener { item: MenuItem ->
val themedActivity: MainActivity = fragment.requireActivity() as MainActivity
Expand Down Expand Up @@ -331,10 +331,10 @@ class AppsRecyclerAdapter(
Uri.parse(
String.format(
"package:%s",
rowItem!!.packageName
)
)
)
rowItem!!.packageName,
),
),
),
)
return@setOnMenuItemClickListener true
}
Expand All @@ -357,17 +357,21 @@ class AppsRecyclerAdapter(
.context
?.packageManager
?.getLaunchIntentForPackage(appDataParcelable.packageName)
if (i1 != null) fragment.startActivity(i1) else Toast.makeText(
fragment.context,
fragment.getString(R.string.not_allowed),
Toast.LENGTH_LONG
).show()
if (i1 != null) {
fragment.startActivity(i1)
} else {
Toast.makeText(
fragment.context,
fragment.getString(R.string.not_allowed),
Toast.LENGTH_LONG,
).show()
}
}

private fun popupShare(
appDataParcelable: AppDataParcelable,
themedActivity: ThemedActivity,
colorAccent: Int
colorAccent: Int,
) {
val arrayList2 =
ArrayList<File>()
Expand All @@ -377,36 +381,36 @@ class AppsRecyclerAdapter(
arrayList2,
fragment.activity,
themedActivity.utilsProvider.appTheme,
colorAccent
colorAccent,
)
}

private fun popupUninstall(
appDataParcelable: AppDataParcelable,
themedActivity: ThemedActivity,
colorAccent: Int
colorAccent: Int,
) {
val f1 = HybridFileParcelable(appDataParcelable.path)
f1.mode = OpenMode.ROOT
if (appDataParcelable.isSystemApp) {
// system package
if ((fragment.requireActivity() as MainActivity).getBoolean(
PreferencesConstants.PREFERENCE_ROOTMODE
PreferencesConstants.PREFERENCE_ROOTMODE,
)
) {
showDeleteSystemAppDialog(themedActivity, colorAccent, f1)
} else {
Toast.makeText(
fragment.context,
fragment.getString(R.string.enablerootmde),
Toast.LENGTH_SHORT
Toast.LENGTH_SHORT,
)
.show()
}
} else {
FileUtils.uninstallPackage(
appDataParcelable.packageName,
fragment.context
fragment.context,
)
}
}
Expand All @@ -418,16 +422,16 @@ class AppsRecyclerAdapter(
intent1.data = Uri.parse(
String.format(
"market://details?id=%s",
appDataParcelable.packageName
)
appDataParcelable.packageName,
),
)
fragment.startActivity(intent1)
} catch (ifPlayStoreNotInstalled: ActivityNotFoundException) {
intent1.data = Uri.parse(
String.format(
"https://play.google.com/store/apps/details?id=%s",
appDataParcelable.packageName
)
appDataParcelable.packageName,
),
)
fragment.startActivity(intent1)
}
Expand All @@ -438,12 +442,12 @@ class AppsRecyclerAdapter(
val filesToCopyList = ArrayList<HybridFileParcelable>()
val dst = File(
Environment.getExternalStorageDirectory()
.path + "/app_backup"
.path + "/app_backup",
)
if (!dst.exists() || !dst.isDirectory) dst.mkdirs()
val intent = Intent(
fragment.context,
CopyService::class.java
CopyService::class.java,
)
val mainApkFile = RootHelper.generateBaseFile(baseApkFile, true)
val startIndex = appDataParcelable.packageName.indexOf("_")
Expand Down Expand Up @@ -474,7 +478,7 @@ class AppsRecyclerAdapter(
Toast.makeText(
fragment.context,
fragment.getString(R.string.copyingapks, filesToCopyList.size, dst.path),
Toast.LENGTH_LONG
Toast.LENGTH_LONG,
)
.show()

Expand All @@ -484,13 +488,13 @@ class AppsRecyclerAdapter(
private fun showDeleteSystemAppDialog(
themedActivity: ThemedActivity,
colorAccent: Int,
f1: HybridFileParcelable
f1: HybridFileParcelable,
) {
val builder1 =
MaterialDialog.Builder(fragment.requireContext())
builder1
.theme(
themedActivity.appTheme.getMaterialDialogTheme(fragment.requireContext())
themedActivity.appTheme.getMaterialDialogTheme(fragment.requireContext()),
)
.content(fragment.getString(R.string.unin_system_apk))
.title(fragment.getString(R.string.warning))
Expand All @@ -508,12 +512,14 @@ class AppsRecyclerAdapter(
if (parent != "app" && parent != "priv-app") {
val baseFile =
HybridFileParcelable(
f1.getParent(fragment.context)
f1.getParent(fragment.context),
)
baseFile.mode =
OpenMode.ROOT
files.add(baseFile)
} else files.add(f1)
} else {
files.add(f1)
}
} else {
files.add(f1)
}
Expand All @@ -528,13 +534,13 @@ class AppsRecyclerAdapter(
TYPE_ITEM,
TYPE_HEADER_SYSTEM,
TYPE_HEADER_THIRD_PARTY,
EMPTY_LAST_ITEM
EMPTY_LAST_ITEM,
)
annotation class ListItemType

data class ListItem(
var appDataParcelable: AppDataParcelable?,
var listItemType: @ListItemType Int = TYPE_ITEM
var listItemType: @ListItemType Int = TYPE_ITEM,
) {
constructor(listItemType: @ListItemType Int) : this(null, listItemType)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1386,12 +1386,14 @@ private void showPopup(@NonNull View view, @NonNull final LayoutElementParcelabl
if (rowItem.isDirectory) {
popupMenu.getMenu().findItem(R.id.open_with).setVisible(false);
popupMenu.getMenu().findItem(R.id.share).setVisible(false);
popupMenu.getMenu().findItem(R.id.open_in_terminal).setVisible(true);

if (mainFragment.getMainActivity().mReturnIntent) {
popupMenu.getMenu().findItem(R.id.return_select).setVisible(true);
}
} else {
popupMenu.getMenu().findItem(R.id.book).setVisible(false);
popupMenu.getMenu().findItem(R.id.open_in_terminal).setVisible(false);

if (description.endsWith(fileExtensionZip)
|| description.endsWith(fileExtensionJar)
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/amaze/filemanager/ui/ItemPopupMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@
import com.amaze.filemanager.ui.dialogs.EncryptAuthenticateDialog;
import com.amaze.filemanager.ui.dialogs.EncryptWithPresetPasswordSaveAsDialog;
import com.amaze.filemanager.ui.dialogs.GeneralDialogCreation;
import com.amaze.filemanager.ui.dialogs.OpenFolderInTerminalFragment;
import com.amaze.filemanager.ui.fragments.MainFragment;
import com.amaze.filemanager.ui.fragments.preferencefragments.PreferencesConstants;
import com.amaze.filemanager.ui.provider.UtilitiesProvider;
import com.amaze.filemanager.utils.DataUtils;
import com.amaze.filemanager.utils.OpenTerminalUtilsExtKt;

import android.content.Context;
import android.content.Intent;
Expand Down Expand Up @@ -237,6 +239,9 @@ public void onButtonPressed(Intent intent, String password)
case R.id.return_select:
mainFragment.returnIntentResults(rowItem.generateBaseFile());
return true;
case R.id.open_in_terminal:
OpenFolderInTerminalFragment.Companion.openTerminalOrShow(rowItem.desc, mainActivity);
return true;
}
return false;
}
Expand Down
Loading

0 comments on commit bd3e8c8

Please sign in to comment.