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 Jul 5, 2024
1 parent bdc86af commit a9a7265
Show file tree
Hide file tree
Showing 22 changed files with 1,071 additions and 35 deletions.
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="com.termux.permission.RUN_COMMAND" />
<uses-permission android:name="com.termoneplus.permission.RUN_SCRIPT" />
<uses-permission android:name="jackpal.androidterm.permission.RUN_SCRIPT" />

<uses-feature
android:name="android.hardware.touchscreen"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class AppsRecyclerAdapter(
private val isBottomSheet: Boolean,
private val adjustListViewCallback: AdjustListViewForTv<AppHolder>,
private val appDataParcelableList: MutableList<AppDataParcelable>,
private val onClickRowAction: ((AppDataParcelable) -> Unit)? = null,
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private val myChecked = SparseBooleanArray()
private var appDataListItem: MutableList<ListItem> = mutableListOf()
Expand Down Expand Up @@ -209,7 +210,11 @@ class AppsRecyclerAdapter(
holder.rl.isClickable = true
holder.rl.nextFocusRightId = holder.about.id
holder.rl.setOnClickListener {
startActivityForRowItem(rowItem)
if (onClickRowAction != null) {
onClickRowAction.invoke(rowItem)
} else {
startActivityForRowItem(rowItem)
}
}
}
if (myChecked[position]) {
Expand Down Expand Up @@ -508,7 +513,7 @@ class AppsRecyclerAdapter(
MaterialDialog.Builder(fragment.requireContext())
builder1
.theme(
themedActivity.appTheme.getMaterialDialogTheme(),
themedActivity.appTheme.materialDialogTheme,
)
.content(fragment.getString(R.string.unin_system_apk))
.title(fragment.getString(R.string.warning))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1435,13 +1435,15 @@ 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.compress).setVisible(true);
popupMenu.getMenu().findItem(R.id.open_in_terminal).setVisible(false);

if (description.endsWith(fileExtensionZip)
|| description.endsWith(fileExtensionJar)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,11 @@ public static void installApk(
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
&& !permissionsActivity.getPackageManager().canRequestPackageInstalls()) {
permissionsActivity.requestInstallApkPermission(
() -> installApk(f, permissionsActivity), true);
(input) -> {
installApk(f, permissionsActivity);
return null;
},
true);
}

Intent intent = new Intent(Intent.ACTION_VIEW);
Expand Down
4 changes: 4 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,6 +37,7 @@
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;
Expand Down Expand Up @@ -256,6 +257,9 @@ public void onButtonPressed(Intent intent, String password)
case R.id.return_select:
mainFragment.returnIntentResults(new HybridFileParcelable[] {rowItem.generateBaseFile()});
return true;
case R.id.open_in_terminal:
OpenFolderInTerminalFragment.Companion.openTerminalOrShow(rowItem.desc, mainActivity);
return true;
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import kotlin.Unit;
import kotlin.collections.ArraysKt;
import kotlin.jvm.functions.Function1;
import kotlin.text.Charsets;
Expand All @@ -220,7 +221,7 @@ public class MainActivity extends PermissionsActivity
CloudConnectionCallbacks,
LoaderManager.LoaderCallbacks<Cursor>,
FolderChooserDialog.FolderCallback,
PermissionsActivity.OnPermissionGranted {
Function<Unit, Unit> {

private static final Logger LOG = LoggerFactory.getLogger(MainActivity.class);

Expand Down Expand Up @@ -532,7 +533,7 @@ public void invalidateFragmentAndBundle(Bundle savedInstanceState, boolean isClo

@Override
@SuppressLint("CheckResult")
public void onPermissionGranted() {
public Unit apply(Unit input) {
drawer.refreshDrawer();
TabFragment tabFragment = getTabFragment();
boolean b = getBoolean(PREFERENCE_NEED_TO_SET_HOME);
Expand Down Expand Up @@ -561,6 +562,7 @@ public void onPermissionGranted() {
if (main1 != null) ((MainFragment) main1).updateList(false);
}
}
return null;
}

private void checkForExternalPermission() {
Expand Down Expand Up @@ -1120,6 +1122,7 @@ public boolean onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.hiddenitems).setVisible(true);
menu.findItem(R.id.view).setVisible(true);
menu.findItem(R.id.extract).setVisible(false);
menu.findItem(R.id.open_in_terminal).setVisible(true);
invalidatePasteSnackbar(true);
findViewById(R.id.buttonbarframe).setVisibility(View.VISIBLE);
} else if (fragment instanceof AppsListFragment
Expand All @@ -1133,6 +1136,7 @@ public boolean onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.home).setVisible(false);
menu.findItem(R.id.history).setVisible(false);
menu.findItem(R.id.extract).setVisible(false);
menu.findItem(R.id.open_in_terminal).setVisible(false);
if (fragment instanceof ProcessViewerFragment) {
menu.findItem(R.id.sort).setVisible(false);
} else if (fragment instanceof FtpServerFragment) {
Expand All @@ -1156,6 +1160,7 @@ public boolean onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.hiddenitems).setVisible(false);
menu.findItem(R.id.view).setVisible(false);
menu.findItem(R.id.extract).setVisible(true);
menu.findItem(R.id.open_in_terminal).setVisible(false);
invalidatePasteSnackbar(false);
}
return super.onPrepareOptionsMenu(menu);
Expand Down Expand Up @@ -1291,6 +1296,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
break;
case R.id.search:
getAppbar().getSearchView().revealSearchView();
break;
case R.id.open_in_terminal:
if (getFragmentAtFrame() instanceof MainFragment) {}

break;
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

package com.amaze.filemanager.ui.activities.superclasses;

import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.TIRAMISU;

Expand All @@ -35,7 +36,6 @@
import android.Manifest;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
Expand All @@ -45,21 +45,25 @@

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.arch.core.util.Function;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import kotlin.Unit;

public class PermissionsActivity extends ThemedActivity
implements ActivityCompat.OnRequestPermissionsResultCallback {

private static final String TAG = PermissionsActivity.class.getSimpleName();

public static final int PERMISSION_LENGTH = 4;
public static final int PERMISSION_LENGTH = 5;
public static final int STORAGE_PERMISSION = 0,
INSTALL_APK_PERMISSION = 1,
ALL_FILES_PERMISSION = 2,
NOTIFICATION_PERMISSION = 3;
NOTIFICATION_PERMISSION = 3,
TERMINAL_PERMISSION = 4;

private final OnPermissionGranted[] permissionCallbacks =
new OnPermissionGranted[PERMISSION_LENGTH];
private final Function<Unit, Unit>[] permissionCallbacks = new Function[PERMISSION_LENGTH];

@Override
public void onRequestPermissionsResult(
Expand All @@ -68,7 +72,7 @@ public void onRequestPermissionsResult(
if (requestCode == STORAGE_PERMISSION) {
if (isGranted(grantResults)) {
Utils.enableScreenRotation(this);
permissionCallbacks[STORAGE_PERMISSION].onPermissionGranted();
permissionCallbacks[STORAGE_PERMISSION].apply(null);
permissionCallbacks[STORAGE_PERMISSION] = null;
} else {
Toast.makeText(this, R.string.grantfailed, Toast.LENGTH_SHORT).show();
Expand All @@ -83,7 +87,7 @@ public void onRequestPermissionsResult(
}
} else if (requestCode == INSTALL_APK_PERMISSION) {
if (isGranted(grantResults)) {
permissionCallbacks[INSTALL_APK_PERMISSION].onPermissionGranted();
permissionCallbacks[INSTALL_APK_PERMISSION].apply(null);
permissionCallbacks[INSTALL_APK_PERMISSION] = null;
}
}
Expand All @@ -94,21 +98,21 @@ public boolean checkStoragePermission() {
if (SDK_INT >= Build.VERSION_CODES.R) {
return (ActivityCompat.checkSelfPermission(
this, Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
== PackageManager.PERMISSION_GRANTED)
== PERMISSION_GRANTED)
|| (ActivityCompat.checkSelfPermission(
this, Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION)
== PackageManager.PERMISSION_GRANTED)
== PERMISSION_GRANTED)
|| Environment.isExternalStorageManager();
} else {
return ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED;
== PERMISSION_GRANTED;
}
}

@RequiresApi(TIRAMISU)
public boolean checkNotificationPermission() {
return ActivityCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)
== PackageManager.PERMISSION_GRANTED;
== PERMISSION_GRANTED;
}

@RequiresApi(TIRAMISU)
Expand All @@ -128,14 +132,12 @@ public void requestNotificationPermission(boolean isInitialStart) {
Manifest.permission.POST_NOTIFICATIONS,
NOTIFICATION_PERMISSION,
materialDialog,
() -> {
// do nothing
},
input -> null,
isInitialStart);
}

public void requestStoragePermission(
@NonNull final OnPermissionGranted onPermissionGranted, boolean isInitialStart) {
@NonNull final Function<Unit, Unit> onPermissionGranted, boolean isInitialStart) {
Utils.disableScreenRotation(this);
final MaterialDialog materialDialog =
GeneralDialogCreation.showBasicDialog(
Expand All @@ -157,7 +159,7 @@ public void requestStoragePermission(

@RequiresApi(api = Build.VERSION_CODES.M)
public void requestInstallApkPermission(
@NonNull final OnPermissionGranted onPermissionGranted, boolean isInitialStart) {
@NonNull final Function<Unit, Unit> onPermissionGranted, boolean isInitialStart) {
final MaterialDialog materialDialog =
GeneralDialogCreation.showBasicDialog(
this,
Expand All @@ -178,6 +180,27 @@ public void requestInstallApkPermission(
isInitialStart);
}

public void requestTerminalPermission(
final String permission, @NonNull final Function<Unit, Unit> onPermissionGranted) {
if (ContextCompat.checkSelfPermission(this, permission) == PERMISSION_GRANTED) {
onPermissionGranted.apply(null);
} else {
final MaterialDialog materialDialog =
GeneralDialogCreation.showBasicDialog(
this,
R.string.grant_apkinstall_permission,
R.string.grantper,
R.string.grant,
R.string.cancel);
materialDialog
.getActionButton(DialogAction.NEGATIVE)
.setOnClickListener(v -> materialDialog.dismiss());
materialDialog.setCancelable(false);
requestPermission(
permission, TERMINAL_PERMISSION, materialDialog, onPermissionGranted, false);
}
}

/**
* Requests permission, overrides {@param rationale}'s POSITIVE button dialog action.
*
Expand All @@ -193,7 +216,7 @@ private void requestPermission(
final String permission,
final int code,
@NonNull final MaterialDialog rationale,
@NonNull final OnPermissionGranted onPermissionGranted,
@NonNull final Function<Unit, Unit> onPermissionGranted,
boolean isInitialStart) {
permissionCallbacks[code] = onPermissionGranted;

Expand Down Expand Up @@ -239,7 +262,7 @@ private void requestPermission(
*
* @param onPermissionGranted permission granted callback
*/
public void requestAllFilesAccess(@NonNull final OnPermissionGranted onPermissionGranted) {
public void requestAllFilesAccess(@NonNull final Function<Unit, Unit> onPermissionGranted) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && !Environment.isExternalStorageManager()) {
final MaterialDialog materialDialog =
GeneralDialogCreation.showBasicDialog(
Expand All @@ -263,7 +286,7 @@ public void requestAllFilesAccess(@NonNull final OnPermissionGranted onPermissio

@RequiresApi(api = Build.VERSION_CODES.R)
private void requestAllFilesAccessPermission(
@NonNull final OnPermissionGranted onPermissionGranted) {
@NonNull final Function<Unit, Unit> onPermissionGranted) {
Utils.disableScreenRotation(this);
permissionCallbacks[ALL_FILES_PERMISSION] = onPermissionGranted;
try {
Expand All @@ -288,10 +311,6 @@ private void requestAllFilesAccessPermission(
}

private boolean isGranted(int[] grantResults) {
return grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED;
}

public interface OnPermissionGranted {
void onPermissionGranted();
return grantResults.length == 1 && grantResults[0] == PERMISSION_GRANTED;
}
}
Loading

0 comments on commit a9a7265

Please sign in to comment.