33import android .Manifest ;
44import android .app .ActivityManager ;
55import android .app .NotificationManager ;
6- import android .app .Service ;
76import android .content .Context ;
8- import android .content .DialogInterface ;
97import android .content .Intent ;
10- import android .content .IntentFilter ;
118import android .content .pm .PackageManager ;
12- import android .location .LocationManager ;
139import android .os .Bundle ;
14- import android .telephony .TelephonyManager ;
1510import android .view .Menu ;
1611import android .view .MenuItem ;
1712import android .widget .Button ;
2318
2419import androidx .activity .EdgeToEdge ;
2520import androidx .annotation .NonNull ;
26- import androidx .annotation .Nullable ;
2721import androidx .appcompat .app .AlertDialog ;
2822import androidx .appcompat .app .AppCompatActivity ;
2923import androidx .appcompat .widget .Toolbar ;
3226import androidx .core .view .ViewCompat ;
3327import androidx .core .view .WindowInsetsCompat ;
3428
29+ import org .jetbrains .annotations .Nullable ;
30+
3531public class Home extends AppCompatActivity {
3632
3733 private Button startAction ;
38- private Switch dnd , sys , sms , code , camera , location ;
34+ private Switch dnd , sys , read_sms , send_sms , code , camera , location ;
3935 private final int REQUEST_CODE_WRITE_SETTINGS = 1 ;
4036 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 ;
4239 private final int REQUEST_CODE_CAMERA = 4 ;
4340 private final int REQUEST_CODE_GPS_LOCATION = 5 ;
4441 private NotificationManager notificationManager ;
@@ -76,12 +73,19 @@ protected void onCreate(Bundle savedInstanceState) {
7673 if (Settings .System .canWrite (this ))
7774 sys .setChecked (true );
7875
79- sms = findViewById (R .id .sms_switch );
76+ read_sms = findViewById (R .id .sms_switch );
8077 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 ());
8387 if (checkSelfPermission (android .Manifest .permission .RECEIVE_SMS ) == PackageManager .PERMISSION_GRANTED )
84- sms .setChecked (true );
88+ send_sms .setChecked (true );
8589
8690 camera = findViewById (R .id .camera_switch );
8791 LinearLayout camera_lay = findViewById (R .id .camera_layout );
@@ -106,13 +110,15 @@ protected void onCreate(Bundle savedInstanceState) {
106110
107111 startAction = findViewById (R .id .startAction );
108112 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 ()) {
110114 startProcess ();
111115 } else if (!dnd .isChecked ()) {
112116 Toast .makeText (this , "Please enable the DND mode permission" , Toast .LENGTH_SHORT ).show ();
113117 } else if (!sys .isChecked ()) {
114118 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 ()) {
116122 Toast .makeText (this , "Please enable the read sms permission" , Toast .LENGTH_SHORT ).show ();
117123 } else if (!camera .isChecked ()) {
118124 Toast .makeText (this , "Please enable the send sms permission" , Toast .LENGTH_SHORT ).show ();
@@ -194,11 +200,19 @@ private void dndPermission() {
194200 }
195201 }
196202
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 );
200214 } else {
201- sms .setChecked (true );
215+ send_sms .setChecked (true );
202216 }
203217 }
204218
@@ -212,22 +226,22 @@ private void cameraPermission() {
212226
213227 private void getGPSPermission () {
214228 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 );
216230 } else {
217231 location .setChecked (true );
218232 }
219233 }
220234
221- private void showSMSPermission () {
235+ private void showSMSReadPermission () {
222236 AlertDialog .Builder builder = new AlertDialog .Builder (this );
223237 builder .setTitle ("Permission Required" );
224238 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 ));
226240 builder .setNegativeButton ("Cancel" , null );
227241 builder .show ();
228242 }
229243
230- private void showSMSPermissionSettingsDialog () {
244+ private void showSMSReadPermissionSettingsDialog () {
231245 AlertDialog .Builder builder = new AlertDialog .Builder (this );
232246 builder .setTitle ("Permission Required" );
233247 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() {
240254 builder .show ();
241255 }
242256
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+
243279 private void showCameraPermission () {
244280 AlertDialog .Builder builder = new AlertDialog .Builder (this );
245281 builder .setTitle ("Permission Required" );
@@ -287,13 +323,22 @@ private void showLocationPermissionSettingsDialog() {
287323 @ Override
288324 public void onRequestPermissionsResult (int requestCode , @ NonNull String [] permissions , @ NonNull int [] grantResults ) {
289325 super .onRequestPermissionsResult (requestCode , permissions , grantResults );
290- if (requestCode == REQUEST_CODE_SMS ) {
326+ if (requestCode == REQUEST_CODE_SMS_READ ) {
291327 if (grantResults .length > 0 && grantResults [0 ] == PackageManager .PERMISSION_GRANTED ) {
292- sms .setChecked (true );
328+ read_sms .setChecked (true );
293329 } 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 ();
295340 } else {
296- showSMSPermissionSettingsDialog ();
341+ showSMSSendPermissionSettingsDialog ();
297342 }
298343 }
299344 if (requestCode == REQUEST_CODE_CAMERA ) {
@@ -308,6 +353,10 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
308353 if (requestCode == REQUEST_CODE_GPS_LOCATION ) {
309354 if (grantResults .length > 0 && grantResults [0 ] == PackageManager .PERMISSION_GRANTED ) {
310355 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 );
311360 } else if (ActivityCompat .shouldShowRequestPermissionRationale (this , Manifest .permission .ACCESS_BACKGROUND_LOCATION )) {
312361 showLocationPermission ();
313362 } else {
0 commit comments