Utils.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package com.example.modifier;
  2. import android.accessibilityservice.AccessibilityServiceInfo;
  3. import android.content.Context;
  4. import android.content.SharedPreferences;
  5. import android.content.pm.ServiceInfo;
  6. import android.content.res.AssetManager;
  7. import android.provider.Settings;
  8. import android.text.TextUtils;
  9. import android.util.Log;
  10. import android.view.accessibility.AccessibilityManager;
  11. import androidx.annotation.DrawableRes;
  12. import com.google.android.material.button.MaterialButton;
  13. import com.google.android.material.progressindicator.CircularProgressIndicatorSpec;
  14. import com.google.android.material.progressindicator.IndeterminateDrawable;
  15. import org.apache.commons.io.IOUtils;
  16. import org.apache.commons.lang3.StringUtils;
  17. import java.io.DataOutputStream;
  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.OutputStream;
  22. import java.lang.reflect.Method;
  23. import java.nio.charset.StandardCharsets;
  24. import java.nio.file.Files;
  25. import java.nio.file.Paths;
  26. import java.util.ArrayList;
  27. import java.util.List;
  28. import java.util.Objects;
  29. public class Utils {
  30. private static final String TAG = "Modifier";
  31. public static Context getContext() {
  32. try {
  33. Class<?> activityThreadClass = Class.forName("android.app.ActivityThread");
  34. Method currentActivityThreadMethod = activityThreadClass.getMethod("currentActivityThread");
  35. currentActivityThreadMethod.setAccessible(true);
  36. Object currentActivityThread = currentActivityThreadMethod.invoke(null);
  37. Method getApplicationMethod = activityThreadClass.getMethod("getApplication");
  38. getApplicationMethod.setAccessible(true);
  39. return (Context) getApplicationMethod.invoke(currentActivityThread);
  40. } catch (Exception e) {
  41. e.printStackTrace();
  42. }
  43. return null;
  44. }
  45. public static String runAsRoot(String... cmds) throws IOException, InterruptedException {
  46. Log.i(TAG, "Trying to run as root");
  47. Process p = new ProcessBuilder("su", "-M").start();
  48. StringBuilder res = new StringBuilder();
  49. StringBuilder err = new StringBuilder();
  50. new Thread(() -> {
  51. try {
  52. res.append(IOUtils.toString(p.getInputStream(), StandardCharsets.UTF_8));
  53. } catch (IOException ignored) {
  54. }
  55. }).start();
  56. new Thread(() -> {
  57. try {
  58. err.append(IOUtils.toString(p.getErrorStream(), StandardCharsets.UTF_8));
  59. } catch (IOException ignored) {
  60. }
  61. }).start();
  62. DataOutputStream outputStream = new DataOutputStream(p.getOutputStream());
  63. for (String cmd : cmds) {
  64. outputStream.writeBytes(cmd + "\n");
  65. outputStream.flush();
  66. Log.i(TAG, "Running command: " + cmd);
  67. }
  68. Thread.sleep(500);
  69. outputStream.writeBytes("exit\n");
  70. outputStream.flush();
  71. p.waitFor();
  72. Log.i(TAG, "Output: " + res);
  73. if (err.length() > 0) {
  74. Log.i(TAG, "Error: " + err);
  75. }
  76. return res.toString();
  77. }
  78. public static boolean isAccessibilityEnabled() {
  79. Context context = getContext();
  80. AccessibilityManager am = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
  81. List<AccessibilityServiceInfo> enabledServices = am.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_ALL_MASK);
  82. for (AccessibilityServiceInfo enabledService : enabledServices) {
  83. Log.i(TAG, "Enabled service: " + enabledService.getResolveInfo().serviceInfo.packageName + "/" + enabledService.getResolveInfo().serviceInfo.name);
  84. ServiceInfo enabledServiceInfo = enabledService.getResolveInfo().serviceInfo;
  85. if (enabledServiceInfo.packageName.equals(context.getPackageName()) && enabledServiceInfo.name.equals(ModifierService.NAME))
  86. return true;
  87. }
  88. return false;
  89. }
  90. public static boolean hasRootAccess() {
  91. boolean rootAccess = false;
  92. try {
  93. String res = runAsRoot("echo \"imrooted\"");
  94. if (res.contains("imrooted")) {
  95. rootAccess = true;
  96. }
  97. } catch (Exception e) {
  98. e.printStackTrace();
  99. }
  100. return rootAccess;
  101. }
  102. public static boolean enableAccessibility() {
  103. Context context = getContext();
  104. AccessibilityManager am = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
  105. List<AccessibilityServiceInfo> enabledServices = am.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_ALL_MASK);
  106. List<String> names = new ArrayList<>();
  107. for (AccessibilityServiceInfo enabledService : enabledServices) {
  108. names.add(enabledService.getResolveInfo().serviceInfo.packageName + "/" + enabledService.getResolveInfo().serviceInfo.name);
  109. }
  110. names.add(context.getPackageName() + "/" + ModifierService.NAME);
  111. try {
  112. runAsRoot("settings put secure enabled_accessibility_services " + TextUtils.join(":", names),
  113. "settings put secure accessibility_enabled 1");
  114. return true;
  115. } catch (Exception e) {
  116. e.printStackTrace();
  117. }
  118. return false;
  119. }
  120. public static void enableOverlay() {
  121. try {
  122. runAsRoot("appops set " + BuildConfig.APPLICATION_ID + " SYSTEM_ALERT_WINDOW allow");
  123. } catch (Exception e) {
  124. e.printStackTrace();
  125. }
  126. }
  127. public static String getUniqueID() {
  128. Context context = getContext();
  129. Objects.requireNonNull(context);
  130. SharedPreferences sharedPrefs = context.getSharedPreferences(BuildConfig.APPLICATION_ID, Context.MODE_PRIVATE);
  131. String uniqueID = sharedPrefs.getString("unique_id", null);
  132. if (StringUtils.isBlank(uniqueID)) {
  133. try {
  134. uniqueID = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
  135. } catch (Exception e) {
  136. e.printStackTrace();
  137. }
  138. }
  139. if (StringUtils.isBlank(uniqueID)) {
  140. uniqueID = java.util.UUID.randomUUID().toString();
  141. }
  142. sharedPrefs.edit().putString("unique_id", uniqueID).apply();
  143. return uniqueID;
  144. }
  145. public static String generateIMEI() {
  146. int pos;
  147. int[] str = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  148. int sum = 0;
  149. int final_digit;
  150. int t;
  151. int len_offset;
  152. int len = 15;
  153. String imei = "";
  154. String[] rbi = new String[]{"01", "10", "30", "33", "35", "44", "45", "49", "50", "51", "52", "53", "54", "86", "91", "98", "99"};
  155. String[] arr = rbi[(int) Math.floor(Math.random() * rbi.length)].split("");
  156. str[0] = Integer.parseInt(arr[0]);
  157. str[1] = Integer.parseInt(arr[1]);
  158. pos = 2;
  159. while (pos < len - 1) {
  160. str[pos++] = (int) (Math.floor(Math.random() * 10) % 10);
  161. }
  162. len_offset = (len + 1) % 2;
  163. for (pos = 0; pos < len - 1; pos++) {
  164. if ((pos + len_offset) % 2 != 0) {
  165. t = str[pos] * 2;
  166. if (t > 9) {
  167. t -= 9;
  168. }
  169. sum += t;
  170. } else {
  171. sum += str[pos];
  172. }
  173. }
  174. final_digit = (10 - (sum % 10)) % 10;
  175. str[len - 1] = final_digit;
  176. for (int d : str) {
  177. imei += String.valueOf(d);
  178. }
  179. return imei;
  180. }
  181. public static void makeLoadingButton(Context context, MaterialButton button) {
  182. CircularProgressIndicatorSpec spec = new CircularProgressIndicatorSpec(context, null, 0,
  183. com.google.android.material.R.style.Widget_Material3_CircularProgressIndicator_ExtraSmall);
  184. IndeterminateDrawable progressIndicatorDrawable = IndeterminateDrawable.createCircularDrawable(context, spec);
  185. button.setIcon(progressIndicatorDrawable);
  186. button.setEnabled(false);
  187. }
  188. public static boolean copyAssetFolder(AssetManager assetManager,
  189. String fromAssetPath, String toPath) {
  190. try {
  191. String[] files = assetManager.list(fromAssetPath);
  192. new File(toPath).mkdirs();
  193. boolean res = true;
  194. for (String file : files)
  195. if (file.contains("."))
  196. res &= copyAsset(assetManager,
  197. fromAssetPath + "/" + file,
  198. toPath + "/" + file);
  199. else
  200. res &= copyAssetFolder(assetManager,
  201. fromAssetPath + "/" + file,
  202. toPath + "/" + file);
  203. return res;
  204. } catch (Exception e) {
  205. e.printStackTrace();
  206. return false;
  207. }
  208. }
  209. public static boolean copyAsset(AssetManager assetManager,
  210. String fromAssetPath, String toPath) {
  211. InputStream in = null;
  212. OutputStream out = null;
  213. try {
  214. in = assetManager.open(fromAssetPath);
  215. new File(toPath).createNewFile();
  216. out = Files.newOutputStream(Paths.get(toPath));
  217. IOUtils.copy(in, out);
  218. in.close();
  219. out.flush();
  220. out.close();
  221. return true;
  222. } catch (Exception e) {
  223. e.printStackTrace();
  224. return false;
  225. }
  226. }
  227. public static int dp2px(Context context, int dp) {
  228. float scale = context.getResources().getDisplayMetrics().density;
  229. return (int) (dp * scale + 0.5f);
  230. }
  231. }