-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Warn users if Draw over other apps permission is missing
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
1 parent
8e80e88
commit d3ddb21
Showing
3 changed files
with
99 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
app/src/main/java/com/termux/app/utils/PermissionUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters