3
3
import android .Manifest ;
4
4
import android .app .ActivityManager ;
5
5
import android .app .NotificationManager ;
6
- import android .app .Service ;
7
6
import android .content .Context ;
8
- import android .content .DialogInterface ;
9
7
import android .content .Intent ;
10
- import android .content .IntentFilter ;
11
8
import android .content .pm .PackageManager ;
12
- import android .location .LocationManager ;
13
9
import android .os .Bundle ;
14
- import android .telephony .TelephonyManager ;
15
10
import android .view .Menu ;
16
11
import android .view .MenuItem ;
17
12
import android .widget .Button ;
23
18
24
19
import androidx .activity .EdgeToEdge ;
25
20
import androidx .annotation .NonNull ;
26
- import androidx .annotation .Nullable ;
27
21
import androidx .appcompat .app .AlertDialog ;
28
22
import androidx .appcompat .app .AppCompatActivity ;
29
23
import androidx .appcompat .widget .Toolbar ;
32
26
import androidx .core .view .ViewCompat ;
33
27
import androidx .core .view .WindowInsetsCompat ;
34
28
29
+ import org .jetbrains .annotations .Nullable ;
30
+
35
31
public class Home extends AppCompatActivity {
36
32
37
33
private Button startAction ;
38
- private Switch dnd , sys , sms , code , camera , location ;
34
+ private Switch dnd , sys , read_sms , send_sms , code , camera , location ;
39
35
private final int REQUEST_CODE_WRITE_SETTINGS = 1 ;
40
36
private final int REQUEST_CODE_DND = 2 ;
41
- private final int REQUEST_CODE_SMS = 3 ;
37
+ private final int REQUEST_CODE_SMS_READ = 3 ;
38
+ private final int REQUEST_CODE_SMS_SEND = 3 ;
42
39
private final int REQUEST_CODE_CAMERA = 4 ;
43
40
private final int REQUEST_CODE_GPS_LOCATION = 5 ;
44
41
private NotificationManager notificationManager ;
@@ -76,12 +73,19 @@ protected void onCreate(Bundle savedInstanceState) {
76
73
if (Settings .System .canWrite (this ))
77
74
sys .setChecked (true );
78
75
79
- sms = findViewById (R .id .sms_switch );
76
+ read_sms = findViewById (R .id .sms_switch );
80
77
LinearLayout sms_lay = findViewById (R .id .sms_layout );
81
- sms .setOnClickListener (view -> smsPermission ());
82
- sms_lay .setOnClickListener (view -> smsPermission ());
78
+ read_sms .setOnClickListener (view -> smsReadPermission ());
79
+ sms_lay .setOnClickListener (view -> smsReadPermission ());
80
+ if (checkSelfPermission (android .Manifest .permission .RECEIVE_SMS ) == PackageManager .PERMISSION_GRANTED )
81
+ read_sms .setChecked (true );
82
+
83
+ send_sms = findViewById (R .id .sms_send_switch );
84
+ LinearLayout sms_send_lay = findViewById (R .id .sms_send_layout );
85
+ send_sms .setOnClickListener (view -> smsSendPermission ());
86
+ sms_lay .setOnClickListener (view -> smsSendPermission ());
83
87
if (checkSelfPermission (android .Manifest .permission .RECEIVE_SMS ) == PackageManager .PERMISSION_GRANTED )
84
- sms .setChecked (true );
88
+ send_sms .setChecked (true );
85
89
86
90
camera = findViewById (R .id .camera_switch );
87
91
LinearLayout camera_lay = findViewById (R .id .camera_layout );
@@ -106,13 +110,15 @@ protected void onCreate(Bundle savedInstanceState) {
106
110
107
111
startAction = findViewById (R .id .startAction );
108
112
startAction .setOnClickListener (view -> {
109
- if (dnd .isChecked () && sys .isChecked () && sms .isChecked () && camera .isChecked () && location .isChecked ()) {
113
+ if (dnd .isChecked () && sys .isChecked () && read_sms . isChecked () && send_sms .isChecked () && camera .isChecked () && location .isChecked ()) {
110
114
startProcess ();
111
115
} else if (!dnd .isChecked ()) {
112
116
Toast .makeText (this , "Please enable the DND mode permission" , Toast .LENGTH_SHORT ).show ();
113
117
} else if (!sys .isChecked ()) {
114
118
Toast .makeText (this , "Please enable the system setting permission" , Toast .LENGTH_SHORT ).show ();
115
- } else if (!sms .isChecked ()) {
119
+ } else if (!read_sms .isChecked ()) {
120
+ Toast .makeText (this , "Please enable the read sms permission" , Toast .LENGTH_SHORT ).show ();
121
+ } else if (!send_sms .isChecked ()) {
116
122
Toast .makeText (this , "Please enable the read sms permission" , Toast .LENGTH_SHORT ).show ();
117
123
} else if (!camera .isChecked ()) {
118
124
Toast .makeText (this , "Please enable the send sms permission" , Toast .LENGTH_SHORT ).show ();
@@ -194,11 +200,19 @@ private void dndPermission() {
194
200
}
195
201
}
196
202
197
- private void smsPermission () {
198
- if (checkSelfPermission (android .Manifest .permission .RECEIVE_SMS ) != PackageManager .PERMISSION_GRANTED ) {
199
- requestPermissions (new String []{android .Manifest .permission .RECEIVE_SMS }, REQUEST_CODE_SMS );
203
+ private void smsReadPermission () {
204
+ if (checkSelfPermission (Manifest .permission .RECEIVE_SMS ) != PackageManager .PERMISSION_GRANTED ) {
205
+ requestPermissions (new String []{Manifest .permission .RECEIVE_SMS }, REQUEST_CODE_SMS_READ );
206
+ } else {
207
+ read_sms .setChecked (true );
208
+ }
209
+ }
210
+
211
+ private void smsSendPermission () {
212
+ if (checkSelfPermission (Manifest .permission .SEND_SMS ) != PackageManager .PERMISSION_GRANTED ) {
213
+ requestPermissions (new String []{Manifest .permission .SEND_SMS }, REQUEST_CODE_SMS_SEND );
200
214
} else {
201
- sms .setChecked (true );
215
+ send_sms .setChecked (true );
202
216
}
203
217
}
204
218
@@ -212,22 +226,22 @@ private void cameraPermission() {
212
226
213
227
private void getGPSPermission () {
214
228
if (ActivityCompat .checkSelfPermission (this , Manifest .permission .ACCESS_COARSE_LOCATION ) != PackageManager .PERMISSION_GRANTED ) {
215
- ActivityCompat .requestPermissions (this , new String []{Manifest .permission .ACCESS_COARSE_LOCATION , Manifest .permission .ACCESS_FINE_LOCATION , Manifest .permission .ACCESS_COARSE_LOCATION }, REQUEST_CODE_GPS_LOCATION );
229
+ ActivityCompat .requestPermissions (this , new String []{Manifest .permission .ACCESS_COARSE_LOCATION , Manifest .permission .ACCESS_FINE_LOCATION , Manifest .permission .ACCESS_BACKGROUND_LOCATION }, REQUEST_CODE_GPS_LOCATION );
216
230
} else {
217
231
location .setChecked (true );
218
232
}
219
233
}
220
234
221
- private void showSMSPermission () {
235
+ private void showSMSReadPermission () {
222
236
AlertDialog .Builder builder = new AlertDialog .Builder (this );
223
237
builder .setTitle ("Permission Required" );
224
238
builder .setMessage ("This app needs to access your read sms permission to function properly." );
225
- builder .setPositiveButton ("OK" , (dialog , which ) -> requestPermissions (new String []{android .Manifest .permission .RECEIVE_SMS }, REQUEST_CODE_SMS ));
239
+ builder .setPositiveButton ("OK" , (dialog , which ) -> requestPermissions (new String []{android .Manifest .permission .RECEIVE_SMS }, REQUEST_CODE_SMS_READ ));
226
240
builder .setNegativeButton ("Cancel" , null );
227
241
builder .show ();
228
242
}
229
243
230
- private void showSMSPermissionSettingsDialog () {
244
+ private void showSMSReadPermissionSettingsDialog () {
231
245
AlertDialog .Builder builder = new AlertDialog .Builder (this );
232
246
builder .setTitle ("Permission Required" );
233
247
builder .setMessage ("This app needs access to your read sms. You can grant the permission in app settings." );
@@ -240,6 +254,28 @@ private void showSMSPermissionSettingsDialog() {
240
254
builder .show ();
241
255
}
242
256
257
+ private void showSMSSendPermission () {
258
+ AlertDialog .Builder builder = new AlertDialog .Builder (this );
259
+ builder .setTitle ("Permission Required" );
260
+ builder .setMessage ("This app needs to access your send sms permission to function properly." );
261
+ builder .setPositiveButton ("OK" , (dialog , which ) -> requestPermissions (new String []{Manifest .permission .SEND_SMS }, REQUEST_CODE_SMS_SEND ));
262
+ builder .setNegativeButton ("Cancel" , null );
263
+ builder .show ();
264
+ }
265
+
266
+ private void showSMSSendPermissionSettingsDialog () {
267
+ AlertDialog .Builder builder = new AlertDialog .Builder (this );
268
+ builder .setTitle ("Permission Required" );
269
+ builder .setMessage ("This app needs access to your send sms. You can grant the permission in app settings." );
270
+ builder .setPositiveButton ("Go to Settings" , (dialog , which ) -> {
271
+ Intent intent = new Intent (Settings .ACTION_APPLICATION_DETAILS_SETTINGS , Uri .fromParts ("package" , getPackageName (), null ));
272
+ intent .addFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
273
+ startActivity (intent );
274
+ });
275
+ builder .setNegativeButton ("Cancel" , null );
276
+ builder .show ();
277
+ }
278
+
243
279
private void showCameraPermission () {
244
280
AlertDialog .Builder builder = new AlertDialog .Builder (this );
245
281
builder .setTitle ("Permission Required" );
@@ -287,13 +323,22 @@ private void showLocationPermissionSettingsDialog() {
287
323
@ Override
288
324
public void onRequestPermissionsResult (int requestCode , @ NonNull String [] permissions , @ NonNull int [] grantResults ) {
289
325
super .onRequestPermissionsResult (requestCode , permissions , grantResults );
290
- if (requestCode == REQUEST_CODE_SMS ) {
326
+ if (requestCode == REQUEST_CODE_SMS_READ ) {
291
327
if (grantResults .length > 0 && grantResults [0 ] == PackageManager .PERMISSION_GRANTED ) {
292
- sms .setChecked (true );
328
+ read_sms .setChecked (true );
293
329
} else if (ActivityCompat .shouldShowRequestPermissionRationale (this , Manifest .permission .RECEIVE_SMS )) {
294
- showSMSPermission ();
330
+ showSMSReadPermission ();
331
+ } else {
332
+ showSMSReadPermissionSettingsDialog ();
333
+ }
334
+ }
335
+ if (requestCode == REQUEST_CODE_SMS_SEND ) {
336
+ if (grantResults .length > 0 && grantResults [0 ] == PackageManager .PERMISSION_GRANTED ) {
337
+ send_sms .setChecked (true );
338
+ } else if (ActivityCompat .shouldShowRequestPermissionRationale (this , Manifest .permission .SEND_SMS )) {
339
+ showSMSSendPermission ();
295
340
} else {
296
- showSMSPermissionSettingsDialog ();
341
+ showSMSSendPermissionSettingsDialog ();
297
342
}
298
343
}
299
344
if (requestCode == REQUEST_CODE_CAMERA ) {
@@ -308,6 +353,10 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
308
353
if (requestCode == REQUEST_CODE_GPS_LOCATION ) {
309
354
if (grantResults .length > 0 && grantResults [0 ] == PackageManager .PERMISSION_GRANTED ) {
310
355
location .setChecked (true );
356
+ Intent intent = new Intent (Settings .ACTION_APPLICATION_DETAILS_SETTINGS );
357
+ Uri uri = Uri .fromParts ("package" , this .getPackageName (), null );
358
+ intent .setData (uri );
359
+ startActivity (intent );
311
360
} else if (ActivityCompat .shouldShowRequestPermissionRationale (this , Manifest .permission .ACCESS_BACKGROUND_LOCATION )) {
312
361
showLocationPermission ();
313
362
} else {
0 commit comments