5
5
import android .os .Environment ;
6
6
import android .view .Menu ;
7
7
import android .view .MenuItem ;
8
+ import android .widget .Button ;
8
9
import android .widget .TextView ;
9
10
10
11
import androidx .appcompat .app .AppCompatActivity ;
11
12
13
+ import com .termux .api .util .ViewUtils ;
12
14
import com .termux .shared .activities .ReportActivity ;
13
15
import com .termux .shared .activity .ActivityUtils ;
14
16
import com .termux .shared .activity .media .AppCompatActivityUtils ;
15
17
import com .termux .shared .android .AndroidUtils ;
18
+ import com .termux .shared .android .PermissionUtils ;
19
+ import com .termux .shared .data .IntentUtils ;
16
20
import com .termux .shared .file .FileUtils ;
17
21
import com .termux .shared .logger .Logger ;
18
22
import com .termux .shared .models .ReportInfo ;
24
28
25
29
public class TermuxAPIActivity extends AppCompatActivity {
26
30
31
+ private TextView mBatteryOptimizationNotDisabledWarning ;
32
+ private TextView mDisplayOverOtherAppsPermissionNotGrantedWarning ;
33
+
34
+ private Button mDisableBatteryOptimization ;
35
+ private Button mGrantDisplayOverOtherAppsPermission ;
36
+
27
37
private static final String LOG_TAG = "TermuxAPIActivity" ;
28
38
29
39
@ Override
@@ -44,8 +54,23 @@ protected void onCreate(Bundle savedInstanceState) {
44
54
pluginInfo .setText (getString (R .string .plugin_info , TermuxConstants .TERMUX_GITHUB_REPO_URL ,
45
55
TermuxConstants .TERMUX_API_GITHUB_REPO_URL , TermuxConstants .TERMUX_API_APT_PACKAGE_NAME ,
46
56
TermuxConstants .TERMUX_API_APT_GITHUB_REPO_URL ));
57
+
58
+ mBatteryOptimizationNotDisabledWarning = findViewById (R .id .textview_battery_optimization_not_disabled_warning );
59
+ mDisableBatteryOptimization = findViewById (R .id .btn_disable_battery_optimizations );
60
+ mDisableBatteryOptimization .setOnClickListener (v -> requestDisableBatteryOptimizations ());
61
+
62
+ mDisplayOverOtherAppsPermissionNotGrantedWarning = findViewById (R .id .textview_display_over_other_apps_not_granted_warning );
63
+ mGrantDisplayOverOtherAppsPermission = findViewById (R .id .btn_grant_display_over_other_apps_permission );
64
+ mGrantDisplayOverOtherAppsPermission .setOnClickListener (v -> requestDisplayOverOtherAppsPermission ());
47
65
}
48
66
67
+ @ Override
68
+ protected void onResume () {
69
+ super .onResume ();
70
+
71
+ checkIfBatteryOptimizationNotDisabled ();
72
+ checkIfDisplayOverOtherAppsPermissionNotGranted ();
73
+ }
49
74
50
75
@ Override
51
76
public boolean onCreateOptionsMenu (Menu menu ) {
@@ -92,6 +117,73 @@ public void run() {
92
117
}.start ();
93
118
}
94
119
120
+
121
+
122
+ private void checkIfBatteryOptimizationNotDisabled () {
123
+ if (mBatteryOptimizationNotDisabledWarning == null ) return ;
124
+
125
+ // If battery optimizations not disabled
126
+ if (!PermissionUtils .checkIfBatteryOptimizationsDisabled (this )) {
127
+ ViewUtils .setWarningTextViewAndButtonState (this , mBatteryOptimizationNotDisabledWarning ,
128
+ mDisableBatteryOptimization , true , getString (R .string .action_disable_battery_optimizations ));
129
+ } else {
130
+ ViewUtils .setWarningTextViewAndButtonState (this , mBatteryOptimizationNotDisabledWarning ,
131
+ mDisableBatteryOptimization , false , getString (R .string .action_already_disabled ));
132
+ }
133
+ }
134
+
135
+ private void requestDisableBatteryOptimizations () {
136
+ Logger .logDebug (LOG_TAG , "Requesting to disable battery optimizations" );
137
+ PermissionUtils .requestDisableBatteryOptimizations (this , PermissionUtils .REQUEST_DISABLE_BATTERY_OPTIMIZATIONS );
138
+ }
139
+
140
+
141
+
142
+ private void checkIfDisplayOverOtherAppsPermissionNotGranted () {
143
+ if (mDisplayOverOtherAppsPermissionNotGrantedWarning == null ) return ;
144
+
145
+ // If display over other apps permission not granted
146
+ if (!PermissionUtils .checkDisplayOverOtherAppsPermission (this )) {
147
+ ViewUtils .setWarningTextViewAndButtonState (this , mDisplayOverOtherAppsPermissionNotGrantedWarning ,
148
+ mGrantDisplayOverOtherAppsPermission , true , getString (R .string .action_grant_display_over_other_apps_permission ));
149
+ } else {
150
+ ViewUtils .setWarningTextViewAndButtonState (this , mDisplayOverOtherAppsPermissionNotGrantedWarning ,
151
+ mGrantDisplayOverOtherAppsPermission , false , getString (R .string .action_already_granted ));
152
+ }
153
+ }
154
+
155
+ private void requestDisplayOverOtherAppsPermission () {
156
+ Logger .logDebug (LOG_TAG , "Requesting to grant display over other apps permission" );
157
+ PermissionUtils .requestDisplayOverOtherAppsPermission (this , PermissionUtils .REQUEST_GRANT_DISPLAY_OVER_OTHER_APPS_PERMISSION );
158
+ }
159
+
160
+
161
+
162
+ @ Override
163
+ protected void onActivityResult (int requestCode , int resultCode , Intent data ) {
164
+ super .onActivityResult (requestCode , resultCode , data );
165
+ Logger .logVerbose (LOG_TAG , "onActivityResult: requestCode: " + requestCode + ", resultCode: " + resultCode + ", data: " + IntentUtils .getIntentString (data ));
166
+
167
+ switch (requestCode ) {
168
+ case PermissionUtils .REQUEST_DISABLE_BATTERY_OPTIMIZATIONS :
169
+ if (PermissionUtils .checkIfBatteryOptimizationsDisabled (this ))
170
+ Logger .logDebug (LOG_TAG , "Battery optimizations disabled by user on request." );
171
+ else
172
+ Logger .logDebug (LOG_TAG , "Battery optimizations not disabled by user on request." );
173
+ break ;
174
+ case PermissionUtils .REQUEST_GRANT_DISPLAY_OVER_OTHER_APPS_PERMISSION :
175
+ if (PermissionUtils .checkDisplayOverOtherAppsPermission (this ))
176
+ Logger .logDebug (LOG_TAG , "Display over other apps granted by user on request." );
177
+ else
178
+ Logger .logDebug (LOG_TAG , "Display over other apps denied by user on request." );
179
+ break ;
180
+ default :
181
+ Logger .logError (LOG_TAG , "Unknown request code \" " + requestCode + "\" passed to onRequestPermissionsResult" );
182
+ }
183
+ }
184
+
185
+
186
+
95
187
private void openSettings () {
96
188
ActivityUtils .startActivity (this , new Intent ().setClassName (TermuxConstants .TERMUX_PACKAGE_NAME , TermuxConstants .TERMUX_APP .TERMUX_SETTINGS_ACTIVITY_NAME ));
97
189
}
0 commit comments