Skip to content

Commit

Permalink
Warn users if Draw over other apps permission is missing
Browse files Browse the repository at this point in the history
For android version >= 10(Q), a flash will be shown to users requesting them to grant the permission if they attempt to start a foreground terminal session command from background, like with the RUN_COMMAND intent. The flash will only be shown if "Plugin Error Notifications" toggle is enabled in settings.
  • Loading branch information
agnostic-apollo committed Mar 25, 2021
1 parent 8e80e88 commit d3ddb21
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
8 changes: 7 additions & 1 deletion app/src/main/java/com/termux/app/TermuxService.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.termux.app.terminal.TermuxSessionClientBase;
import com.termux.app.utils.Logger;
import com.termux.app.utils.NotificationUtils;
import com.termux.app.utils.PermissionUtils;
import com.termux.app.utils.ShellUtils;
import com.termux.app.utils.TextDataUtils;
import com.termux.models.ExecutionCommand;
Expand Down Expand Up @@ -513,7 +514,12 @@ private void handleSessionAction(int sessionAction, TerminalSession newTerminalS

/** Launch the {@link }TermuxActivity} to bring it to foreground. */
private void startTermuxActivity() {
TermuxActivity.startTermuxActivity(this);
// For android >= 10, apps require Display over other apps permission to start foreground activities
// from background (services). If it is not granted, then termux sessions that are started will
// show in Termux notification but will not run until user manually clicks the notification.
if(PermissionUtils.validateDisplayOverOtherAppsPermissionForPostAndroid10(this)) {
TermuxActivity.startTermuxActivity(this);
}
}


Expand Down
86 changes: 86 additions & 0 deletions app/src/main/java/com/termux/app/utils/PermissionUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.termux.app.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import androidx.core.content.ContextCompat;

import com.termux.R;
import com.termux.app.TermuxConstants;
import com.termux.app.settings.preferences.TermuxAppSharedPreferences;

import java.util.Arrays;

public class PermissionUtils {

public static final int ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE = 0;

private static final String LOG_TAG = "PluginUtils";

public static boolean checkPermissions(Context context, String[] permissions) {
int result;

for (String p:permissions) {
result = ContextCompat.checkSelfPermission(context,p);
if (result != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}

public static void askPermissions(Activity context, String[] permissions) {
if(context == null || permissions == null) return;

int result;
Logger.showToast(context, context.getString(R.string.message_sudo_please_grant_permissions), true);
try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}

for (String permission:permissions) {
result = ContextCompat.checkSelfPermission(context, permission);
if (result != PackageManager.PERMISSION_GRANTED) {
Logger.logDebug(LOG_TAG, "Requesting Permissions: " + Arrays.toString(permissions));
context.requestPermissions(new String[]{permission}, 0);
}
}
}



public static boolean checkDisplayOverOtherAppsPermission(Context context) {
boolean permissionGranted;

permissionGranted = Settings.canDrawOverlays(context);
if (!permissionGranted) {
Logger.logWarn(LOG_TAG, TermuxConstants.TERMUX_APP_NAME + " App does not have Display over other apps (SYSTEM_ALERT_WINDOW) permission");
return false;
} else {
Logger.logDebug(LOG_TAG, TermuxConstants.TERMUX_APP_NAME + " App already has Display over other apps (SYSTEM_ALERT_WINDOW) permission");
return true;
}
}

public static void askDisplayOverOtherAppsPermission(Activity context) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + context.getPackageName()));
context.startActivityForResult(intent, ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE);
}

public static boolean validateDisplayOverOtherAppsPermissionForPostAndroid10(Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) return true;

if(!PermissionUtils.checkDisplayOverOtherAppsPermission(context)) {
TermuxAppSharedPreferences preferences = new TermuxAppSharedPreferences(context);
if(preferences.getPluginErrorNotificationsEnabled())
Logger.showToast(context, context.getString(R.string.error_display_over_other_apps_permission_not_granted), true);
return false;
} else {
return true;
}
}

}
6 changes: 6 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@



<!-- Termux PermissionUtils -->
<string name="message_sudo_please_grant_permissions">Please grant permissions on next screen</string>
<string name="error_display_over_other_apps_permission_not_granted">&TERMUX_APP_NAME; requires \"Display over other apps\" permission to start terminal sessions from background on Android >= 10. Grants it from Settings -> Apps -> &TERMUX_APP_NAME; -> Advanced</string>



<!-- Termux File Receiver -->
<string name="title_file_received">Save file in ~/downloads/</string>
<string name="action_file_received_edit">Edit</string>
Expand Down

0 comments on commit d3ddb21

Please sign in to comment.