| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- package com.example.modifier;
- import android.accessibilityservice.AccessibilityServiceInfo;
- import android.accounts.AccountManager;
- import android.accounts.AuthenticatorDescription;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.content.pm.PackageManager;
- import android.content.pm.ServiceInfo;
- import android.content.res.AssetManager;
- import android.content.res.Resources;
- import android.content.res.XmlResourceParser;
- import android.os.Bundle;
- import android.provider.Settings;
- import android.text.TextUtils;
- import android.util.Log;
- import android.view.accessibility.AccessibilityManager;
- import com.example.modifier.service.ModifierService;
- import com.google.android.material.button.MaterialButton;
- import com.google.android.material.progressindicator.CircularProgressIndicatorSpec;
- import com.google.android.material.progressindicator.IndeterminateDrawable;
- import org.apache.commons.io.IOUtils;
- import org.apache.commons.lang3.RandomStringUtils;
- import org.apache.commons.lang3.StringUtils;
- import org.xmlpull.v1.XmlPullParser;
- import java.io.DataOutputStream;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.lang.reflect.Method;
- import java.nio.charset.StandardCharsets;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Objects;
- public class Utils {
- private static final String TAG = "Modifier";
- public static Context getContext() {
- try {
- Class<?> activityThreadClass = Class.forName("android.app.ActivityThread");
- Method currentActivityThreadMethod = activityThreadClass.getMethod("currentActivityThread");
- currentActivityThreadMethod.setAccessible(true);
- Object currentActivityThread = currentActivityThreadMethod.invoke(null);
- Method getApplicationMethod = activityThreadClass.getMethod("getApplication");
- getApplicationMethod.setAccessible(true);
- return (Context) getApplicationMethod.invoke(currentActivityThread);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- public static String runAsRoot(String... cmds) throws IOException, InterruptedException {
- Log.i(TAG, "Trying to run as root");
- Process p = new ProcessBuilder("su", "-M").start();
- StringBuilder res = new StringBuilder();
- StringBuilder err = new StringBuilder();
- new Thread(() -> {
- try {
- res.append(IOUtils.toString(p.getInputStream(), StandardCharsets.UTF_8));
- } catch (IOException ignored) {
- }
- }).start();
- new Thread(() -> {
- try {
- err.append(IOUtils.toString(p.getErrorStream(), StandardCharsets.UTF_8));
- } catch (IOException ignored) {
- }
- }).start();
- DataOutputStream outputStream = new DataOutputStream(p.getOutputStream());
- for (String cmd : cmds) {
- outputStream.writeBytes(cmd + "\n");
- outputStream.flush();
- Log.i(TAG, "Running command: " + cmd);
- }
- Thread.sleep(500);
- outputStream.writeBytes("exit\n");
- outputStream.flush();
- p.waitFor();
- Log.i(TAG, "Output: " + res);
- if (err.length() > 0) {
- Log.i(TAG, "Error: " + err);
- }
- return res.toString();
- }
- public static boolean isAccessibilityEnabled() {
- Context context = getContext();
- AccessibilityManager am = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
- List<AccessibilityServiceInfo> enabledServices = am.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_ALL_MASK);
- for (AccessibilityServiceInfo enabledService : enabledServices) {
- Log.i(TAG, "Enabled service: " + enabledService.getResolveInfo().serviceInfo.packageName + "/" + enabledService.getResolveInfo().serviceInfo.name);
- ServiceInfo enabledServiceInfo = enabledService.getResolveInfo().serviceInfo;
- if (enabledServiceInfo.packageName.equals(context.getPackageName()) && enabledServiceInfo.name.equals(ModifierService.NAME))
- return true;
- }
- return false;
- }
- public static boolean hasRootAccess() {
- boolean rootAccess = false;
- try {
- String res = runAsRoot("echo \"imrooted\"");
- if (res.contains("imrooted")) {
- rootAccess = true;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return rootAccess;
- }
- public static boolean enableAccessibility() {
- Context context = getContext();
- AccessibilityManager am = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
- List<AccessibilityServiceInfo> enabledServices = am.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_ALL_MASK);
- List<String> names = new ArrayList<>();
- for (AccessibilityServiceInfo enabledService : enabledServices) {
- names.add(enabledService.getResolveInfo().serviceInfo.packageName + "/" + enabledService.getResolveInfo().serviceInfo.name);
- }
- names.add(context.getPackageName() + "/" + ModifierService.NAME);
- try {
- runAsRoot("settings put secure enabled_accessibility_services " + TextUtils.join(":", names),
- "settings put secure accessibility_enabled 1");
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return false;
- }
- public static void enableOverlay() {
- try {
- runAsRoot("appops set " + BuildConfig.APPLICATION_ID + " SYSTEM_ALERT_WINDOW allow");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static String getUniqueID() {
- Context context = getContext();
- Objects.requireNonNull(context);
- SharedPreferences sharedPrefs = context.getSharedPreferences(BuildConfig.APPLICATION_ID, Context.MODE_PRIVATE);
- String uniqueID = sharedPrefs.getString("unique_id", null);
- if (StringUtils.isBlank(uniqueID)) {
- try {
- uniqueID = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- if (StringUtils.isBlank(uniqueID)) {
- uniqueID = java.util.UUID.randomUUID().toString();
- }
- sharedPrefs.edit().putString("unique_id", uniqueID).apply();
- return uniqueID;
- }
- public static String generateIMEI() {
- int pos;
- int[] str = Arrays.stream(("35684610" + RandomStringUtils.randomNumeric(7)).split("")).mapToInt(Integer::parseInt).toArray();
- int sum = 0;
- int final_digit;
- int t;
- int len_offset;
- int len = 15;
- String imei = "";
- len_offset = (len + 1) % 2;
- for (pos = 0; pos < len - 1; pos++) {
- if ((pos + len_offset) % 2 != 0) {
- t = str[pos] * 2;
- if (t > 9) {
- t -= 9;
- }
- sum += t;
- } else {
- sum += str[pos];
- }
- }
- final_digit = (10 - (sum % 10)) % 10;
- str[len - 1] = final_digit;
- for (int d : str) {
- imei += String.valueOf(d);
- }
- return imei;
- }
- public static String generateIMEI1() {
- int pos;
- int[] str = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
- int sum = 0;
- int final_digit;
- int t;
- int len_offset;
- int len = 15;
- String imei = "";
- String[] rbi = new String[]{"01", "10", "30", "33", "35", "44", "45", "49", "50", "51", "52", "53", "54", "86", "91", "98", "99"};
- String[] arr = rbi[(int) Math.floor(Math.random() * rbi.length)].split("");
- str[0] = Integer.parseInt(arr[0]);
- str[1] = Integer.parseInt(arr[1]);
- pos = 2;
- while (pos < len - 1) {
- str[pos++] = (int) (Math.floor(Math.random() * 10) % 10);
- }
- len_offset = (len + 1) % 2;
- for (pos = 0; pos < len - 1; pos++) {
- if ((pos + len_offset) % 2 != 0) {
- t = str[pos] * 2;
- if (t > 9) {
- t -= 9;
- }
- sum += t;
- } else {
- sum += str[pos];
- }
- }
- final_digit = (10 - (sum % 10)) % 10;
- str[len - 1] = final_digit;
- for (int d : str) {
- imei += String.valueOf(d);
- }
- return imei;
- }
- public static void makeLoadingButton(Context context, MaterialButton button) {
- CircularProgressIndicatorSpec spec = new CircularProgressIndicatorSpec(context, null, 0,
- com.google.android.material.R.style.Widget_Material3_CircularProgressIndicator_ExtraSmall);
- IndeterminateDrawable progressIndicatorDrawable = IndeterminateDrawable.createCircularDrawable(context, spec);
- button.setIcon(progressIndicatorDrawable);
- button.setEnabled(false);
- }
- public static boolean copyAssetFolder(AssetManager assetManager,
- String fromAssetPath, String toPath) {
- try {
- String[] files = assetManager.list(fromAssetPath);
- new File(toPath).mkdirs();
- boolean res = true;
- for (String file : files)
- if (file.contains("."))
- res &= copyAsset(assetManager,
- fromAssetPath + "/" + file,
- toPath + "/" + file);
- else
- res &= copyAssetFolder(assetManager,
- fromAssetPath + "/" + file,
- toPath + "/" + file);
- return res;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- public static boolean copyAsset(AssetManager assetManager,
- String fromAssetPath, String toPath) {
- if (new File(toPath).exists())
- return true;
- InputStream in = null;
- OutputStream out = null;
- try {
- in = assetManager.open(fromAssetPath);
- new File(toPath).createNewFile();
- out = Files.newOutputStream(Paths.get(toPath));
- IOUtils.copy(in, out);
- in.close();
- out.flush();
- out.close();
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- public static int dp2px(Context context, int dp) {
- float scale = context.getResources().getDisplayMetrics().density;
- return (int) (dp * scale + 0.5f);
- }
- public static void startPreferenceActivity(String type) {
- Context context = getContext();
- AccountManager accountManager = AccountManager.get(context);
- AuthenticatorDescription[] descriptions = accountManager.getAuthenticatorTypes();
- AuthenticatorDescription neededDescription = null;
- for (AuthenticatorDescription description : descriptions) {
- if (description.type.equals(type)) {
- neededDescription = description;
- break;
- }
- }
- if (neededDescription != null) {
- String packageName = neededDescription.packageName;
- int prefsId = neededDescription.accountPreferencesId;
- try {
- Resources resources = context.getPackageManager().getResourcesForApplication(packageName);
- XmlResourceParser xpp = resources.getLayout(prefsId);
- xpp.next();
- String action = null;
- String targetPackage = packageName; //default to the account pref package name...?
- String targetClass = null;
- int eventType = xpp.getEventType();
- while (eventType != XmlPullParser.END_DOCUMENT) {
- if (eventType == XmlPullParser.START_TAG) {
- if (xpp.getName().equals("intent")) {
- int count = xpp.getAttributeCount();
- for (int i = 0; i < count; i++) {
- String name = xpp.getAttributeName(i);
- if (name.equals("action")) {
- action = xpp.getAttributeValue(i);
- } else if (name.equals("targetPackage")) {
- targetPackage = xpp.getAttributeValue(i);
- } else if (name.equals("targetClass")) {
- targetClass = xpp.getAttributeValue(i);
- }
- }
- }
- }
- eventType = xpp.next();
- }
- if (action != null) {
- context.startActivity(new Intent(action));
- } else if (targetClass != null) {
- context.startActivity(new Intent(context.createPackageContext(targetPackage, Context.CONTEXT_IGNORE_SECURITY), Class.forName(targetClass)));
- }
- } catch (PackageManager.NameNotFoundException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
|