Skip to content

Commit 1fd7b74

Browse files
committed
Commit 3
1 parent 3067ac4 commit 1fd7b74

File tree

6 files changed

+157
-56
lines changed

6 files changed

+157
-56
lines changed

app/src/main/java/com/xncoder/advanceprotection/Home.java

+75-26
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,10 @@
33
import android.Manifest;
44
import android.app.ActivityManager;
55
import android.app.NotificationManager;
6-
import android.app.Service;
76
import android.content.Context;
8-
import android.content.DialogInterface;
97
import android.content.Intent;
10-
import android.content.IntentFilter;
118
import android.content.pm.PackageManager;
12-
import android.location.LocationManager;
139
import android.os.Bundle;
14-
import android.telephony.TelephonyManager;
1510
import android.view.Menu;
1611
import android.view.MenuItem;
1712
import android.widget.Button;
@@ -23,7 +18,6 @@
2318

2419
import androidx.activity.EdgeToEdge;
2520
import androidx.annotation.NonNull;
26-
import androidx.annotation.Nullable;
2721
import androidx.appcompat.app.AlertDialog;
2822
import androidx.appcompat.app.AppCompatActivity;
2923
import androidx.appcompat.widget.Toolbar;
@@ -32,13 +26,16 @@
3226
import androidx.core.view.ViewCompat;
3327
import androidx.core.view.WindowInsetsCompat;
3428

29+
import org.jetbrains.annotations.Nullable;
30+
3531
public 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 {

app/src/main/java/com/xncoder/advanceprotection/SmsReceiver.java

+31-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
import androidx.core.app.ActivityCompat;
2929

3030
import com.google.android.gms.location.FusedLocationProviderClient;
31+
import com.google.android.gms.location.LocationCallback;
32+
import com.google.android.gms.location.LocationRequest;
33+
import com.google.android.gms.location.LocationResult;
3134
import com.google.android.gms.location.LocationServices;
3235

3336
import java.io.IOException;
@@ -50,7 +53,6 @@ public class SmsReceiver extends BroadcastReceiver {
5053
@Override
5154
public void onReceive(Context context, Intent intent) {
5255
this.context = context;
53-
Ringtone ringtone = RingtoneManager.getRingtone(context, ringtoneUri);
5456

5557
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
5658
Bundle bundle = intent.getExtras();
@@ -65,14 +67,15 @@ public void onReceive(Context context, Intent intent) {
6567
SmsManager smsManager = SmsManager.getDefault();
6668
PendingIntent sentPendingIntent = PendingIntent.getBroadcast(context, 0, new Intent("SMS_SENT"), PendingIntent.FLAG_IMMUTABLE);
6769

70+
Ringtone ringtone = RingtoneManager.getRingtone(context, ringtoneUri);
6871
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
6972
ringtone.setLooping(true);
7073
}
7174

7275
int codeInd = messageBody.indexOf("-");
7376
if (messageBody.startsWith(code) && codeInd == -1) {
7477
String message1 = "Secure Mode Activated\nHelp: <Secure Code>-<Options>\nOptions:\n1. Get-Location";
75-
String message2 = "2. Ringer-Normal, Silent, Vibrate, DND\n3. Sound-Play, Stop\n4. Flash-On, Off\n5. Exit";
78+
String message2 = "2. Ringer-Normal, Silent, Vibrate, DND\n3. Sound-Play, Stop\n4. Flash-On, Off\n5. Secure-Deactivated";
7679

7780
smsManager.sendTextMessage(sender, null, message1, sentPendingIntent, null);
7881
smsManager.sendTextMessage(sender, null, message2, sentPendingIntent, null);
@@ -88,6 +91,31 @@ public void onReceive(Context context, Intent intent) {
8891
int modeInd = messageBody.indexOf("-");
8992
String mode = messageBody.substring(modeInd + 1);
9093
if (messageBody.substring(0, modeInd).equals("Get") && mode.startsWith("Location")) {
94+
FusedLocationProviderClient fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(context);
95+
LocationRequest locationRequest = LocationRequest.create();
96+
locationRequest.setInterval(600000);
97+
locationRequest.setFastestInterval(300000);
98+
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
99+
LocationCallback locationCallback = new LocationCallback() {
100+
@Override
101+
public void onLocationResult(LocationResult locationResult) {
102+
for (Location location : locationResult.getLocations()) {
103+
Log.d("Locations: ", location.toString());
104+
if (location != null) {
105+
try {
106+
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
107+
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
108+
new SaveLocation(context).updateLocation("https://www.google.com/maps?q=" + addresses.get(0).getLatitude() + "," + addresses.get(0).getLongitude());
109+
Toast.makeText(context, ""+addresses.get(0).getLatitude()+addresses.get(0).getLongitude(), Toast.LENGTH_SHORT).show();
110+
} catch (IOException e) {
111+
throw new RuntimeException(e);
112+
}
113+
}
114+
}
115+
}
116+
};
117+
// while(new SaveLocation(context).getLocationLink() == null)
118+
fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, null);
91119
String message = "Secure Mode get location\nLink : " + new SaveLocation(context).getLocationLink();
92120
smsManager.sendTextMessage(sender, null, message, sentPendingIntent, null);
93121
} else if (messageBody.substring(0, modeInd).equals("Ringer")) {
@@ -136,7 +164,7 @@ else if (mode.startsWith("DND")) {
136164
}
137165
}
138166
smsManager.sendTextMessage(sender, null, "Done " + messageBody.substring(0, modeInd) + " " + mode, sentPendingIntent, null);
139-
} else if (messageBody.substring(0, modeInd).equals("Exit")) {
167+
} else if (messageBody.substring(0, modeInd).equals("Secure") && mode.startsWith("Deactivated")) {
140168
smsManager.sendTextMessage(sender, null, "Secure Mode Deactivated", sentPendingIntent, null);
141169
secureMode = false;
142170
secureModeNumber = null;

app/src/main/java/com/xncoder/advanceprotection/SmsService.java

+5-25
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,13 @@
3434
import java.util.Locale;
3535
import java.util.Timer;
3636
import java.util.TimerTask;
37+
import java.util.concurrent.Executors;
38+
import java.util.concurrent.ScheduledExecutorService;
39+
import java.util.concurrent.TimeUnit;
3740

3841
public class SmsService extends Service {
42+
private ScheduledExecutorService scheduler;
43+
3944
@Override
4045
public void onCreate() {
4146
super.onCreate();
@@ -78,7 +83,6 @@ public int onStartCommand(Intent intent, int flags, int startId) {
7883
IntentFilter intentFilter = new IntentFilter();
7984
intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
8085
registerReceiver(smsReceiver, intentFilter);
81-
updateLocation();
8286
return START_STICKY;
8387
}
8488

@@ -96,28 +100,4 @@ public IBinder onBind(Intent intent) {
96100
return null;
97101
}
98102

99-
private void updateLocation() {
100-
FusedLocationProviderClient fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
101-
LocationRequest locationRequest = LocationRequest.create();
102-
locationRequest.setInterval(600000);
103-
locationRequest.setFastestInterval(300000);
104-
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
105-
LocationCallback locationCallback = new LocationCallback() {
106-
@Override
107-
public void onLocationResult(LocationResult locationResult) {
108-
for (Location location : locationResult.getLocations()) {
109-
if (location != null) {
110-
try {
111-
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
112-
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
113-
new SaveLocation(getApplicationContext()).updateLocation("https://www.google.com/maps?q=" + addresses.get(0).getLatitude() + "," + addresses.get(0).getLongitude());
114-
} catch (IOException e) {
115-
throw new RuntimeException(e);
116-
}
117-
}
118-
}
119-
}
120-
};
121-
fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, null);
122-
}
123103
}

0 commit comments

Comments
 (0)