1
1
package com .termux .shared .packages ;
2
2
3
+ import android .annotation .SuppressLint ;
3
4
import android .app .Activity ;
4
5
import android .content .Context ;
5
6
import android .content .Intent ;
6
7
import android .content .pm .PackageManager ;
7
8
import android .net .Uri ;
8
9
import android .os .Build ;
10
+ import android .os .PowerManager ;
9
11
import android .provider .Settings ;
10
12
11
13
import androidx .core .content .ContextCompat ;
19
21
20
22
public class PermissionUtils {
21
23
22
- public static final int ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE = 0 ;
24
+ public static final int REQUEST_GRANT_STORAGE_PERMISSION = 1000 ;
25
+
26
+ public static final int REQUEST_DISABLE_BATTERY_OPTIMIZATIONS = 2000 ;
27
+ public static final int REQUEST_GRANT_DISPLAY_OVER_OTHER_APPS_PERMISSION = 2001 ;
23
28
24
29
private static final String LOG_TAG = "PermissionUtils" ;
25
30
31
+
32
+ public static boolean checkPermission (Context context , String permission ) {
33
+ if (permission == null ) return false ;
34
+ return checkPermissions (context , new String []{permission });
35
+ }
36
+
26
37
public static boolean checkPermissions (Context context , String [] permissions ) {
27
- int result ;
38
+ if ( permissions == null ) return false ;
28
39
40
+ int result ;
29
41
for (String p :permissions ) {
30
42
result = ContextCompat .checkSelfPermission (context ,p );
31
43
if (result != PackageManager .PERMISSION_GRANTED ) {
@@ -35,55 +47,70 @@ public static boolean checkPermissions(Context context, String[] permissions) {
35
47
return true ;
36
48
}
37
49
38
- public static void askPermissions (Activity context , String [] permissions ) {
39
- if (context == null || permissions == null ) return ;
50
+
51
+
52
+ public static void requestPermission (Activity activity , String permission , int requestCode ) {
53
+ if (permission == null ) return ;
54
+ requestPermissions (activity , new String []{permission }, requestCode );
55
+ }
56
+
57
+ public static void requestPermissions (Activity activity , String [] permissions , int requestCode ) {
58
+ if (activity == null || permissions == null ) return ;
40
59
41
60
int result ;
42
- Logger .showToast (context , context .getString (R .string .message_sudo_please_grant_permissions ), true );
61
+ Logger .showToast (activity , activity .getString (R .string .message_sudo_please_grant_permissions ), true );
43
62
try {Thread .sleep (1000 );} catch (InterruptedException e ) {e .printStackTrace ();}
44
63
45
64
for (String permission :permissions ) {
46
- result = ContextCompat .checkSelfPermission (context , permission );
65
+ result = ContextCompat .checkSelfPermission (activity , permission );
47
66
if (result != PackageManager .PERMISSION_GRANTED ) {
48
67
Logger .logDebug (LOG_TAG , "Requesting Permissions: " + Arrays .toString (permissions ));
49
- context .requestPermissions (new String []{permission }, 0 );
68
+ activity .requestPermissions (new String []{permission }, requestCode );
50
69
}
51
70
}
52
71
}
53
72
54
73
55
74
56
75
public static boolean checkDisplayOverOtherAppsPermission (Context context ) {
57
- boolean permissionGranted ;
58
-
59
- permissionGranted = Settings .canDrawOverlays (context );
60
- if (!permissionGranted ) {
61
- Logger .logWarn (LOG_TAG , TermuxConstants .TERMUX_APP_NAME + " App does not have Display over other apps (SYSTEM_ALERT_WINDOW) permission" );
62
- return false ;
63
- } else {
64
- Logger .logDebug (LOG_TAG , TermuxConstants .TERMUX_APP_NAME + " App already has Display over other apps (SYSTEM_ALERT_WINDOW) permission" );
65
- return true ;
66
- }
76
+ return Settings .canDrawOverlays (context );
67
77
}
68
78
69
- public static void askDisplayOverOtherAppsPermission (Activity context ) {
79
+ public static void requestDisplayOverOtherAppsPermission (Activity context , int requestCode ) {
70
80
Intent intent = new Intent (Settings .ACTION_MANAGE_OVERLAY_PERMISSION , Uri .parse ("package:" + context .getPackageName ()));
71
- context .startActivityForResult (intent , ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE );
81
+ context .startActivityForResult (intent , requestCode );
72
82
}
73
83
74
84
public static boolean validateDisplayOverOtherAppsPermissionForPostAndroid10 (Context context ) {
75
85
if (Build .VERSION .SDK_INT < Build .VERSION_CODES .Q ) return true ;
76
86
77
87
if (!PermissionUtils .checkDisplayOverOtherAppsPermission (context )) {
88
+ Logger .logWarn (LOG_TAG , TermuxConstants .TERMUX_APP_NAME + " App does not have Display over other apps (SYSTEM_ALERT_WINDOW) permission" );
89
+
78
90
TermuxAppSharedPreferences preferences = TermuxAppSharedPreferences .build (context );
79
91
if (preferences == null ) return false ;
80
92
81
93
if (preferences .arePluginErrorNotificationsEnabled ())
82
94
Logger .showToast (context , context .getString (R .string .error_display_over_other_apps_permission_not_granted ), true );
83
95
return false ;
84
96
} else {
97
+ Logger .logDebug (LOG_TAG , TermuxConstants .TERMUX_APP_NAME + " App already has Display over other apps (SYSTEM_ALERT_WINDOW) permission" );
85
98
return true ;
86
99
}
87
100
}
88
101
102
+
103
+
104
+ public static boolean checkIfBatteryOptimizationsDisabled (Context context ) {
105
+ PowerManager powerManager = (PowerManager ) context .getSystemService (Context .POWER_SERVICE );
106
+ return powerManager .isIgnoringBatteryOptimizations (context .getPackageName ());
107
+ }
108
+
109
+ @ SuppressLint ("BatteryLife" )
110
+ public static void requestDisableBatteryOptimizations (Activity activity , int requestCode ) {
111
+ Intent intent = new Intent (Settings .ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS );
112
+ intent .setData (Uri .parse ("package:" + activity .getPackageName ()));
113
+ activity .startActivityForResult (intent , requestCode );
114
+ }
115
+
89
116
}
0 commit comments