Utils.java 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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").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 String getUniqueID() {
  121. Context context = getContext();
  122. Objects.requireNonNull(context);
  123. SharedPreferences sharedPrefs = context.getSharedPreferences(BuildConfig.APPLICATION_ID, Context.MODE_PRIVATE);
  124. String uniqueID = sharedPrefs.getString("unique_id", null);
  125. if (StringUtils.isBlank(uniqueID)) {
  126. try {
  127. uniqueID = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
  128. } catch (Exception e) {
  129. e.printStackTrace();
  130. }
  131. }
  132. if (StringUtils.isBlank(uniqueID)) {
  133. uniqueID = java.util.UUID.randomUUID().toString();
  134. }
  135. sharedPrefs.edit().putString("unique_id", uniqueID).apply();
  136. return uniqueID;
  137. }
  138. public static String generateIMEI() {
  139. int pos;
  140. int[] str = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  141. int sum = 0;
  142. int final_digit;
  143. int t;
  144. int len_offset;
  145. int len = 15;
  146. String imei = "";
  147. String[] rbi = new String[]{"01", "10", "30", "33", "35", "44", "45", "49", "50", "51", "52", "53", "54", "86", "91", "98", "99"};
  148. String[] arr = rbi[(int) Math.floor(Math.random() * rbi.length)].split("");
  149. str[0] = Integer.parseInt(arr[0]);
  150. str[1] = Integer.parseInt(arr[1]);
  151. pos = 2;
  152. while (pos < len - 1) {
  153. str[pos++] = (int) (Math.floor(Math.random() * 10) % 10);
  154. }
  155. len_offset = (len + 1) % 2;
  156. for (pos = 0; pos < len - 1; pos++) {
  157. if ((pos + len_offset) % 2 != 0) {
  158. t = str[pos] * 2;
  159. if (t > 9) {
  160. t -= 9;
  161. }
  162. sum += t;
  163. } else {
  164. sum += str[pos];
  165. }
  166. }
  167. final_digit = (10 - (sum % 10)) % 10;
  168. str[len - 1] = final_digit;
  169. for (int d : str) {
  170. imei += String.valueOf(d);
  171. }
  172. return imei;
  173. }
  174. public static void makeLoadingButton(Context context, MaterialButton button) {
  175. CircularProgressIndicatorSpec spec = new CircularProgressIndicatorSpec(context, null, 0,
  176. com.google.android.material.R.style.Widget_Material3_CircularProgressIndicator_ExtraSmall);
  177. IndeterminateDrawable progressIndicatorDrawable = IndeterminateDrawable.createCircularDrawable(context, spec);
  178. button.setIcon(progressIndicatorDrawable);
  179. button.setEnabled(false);
  180. }
  181. public static boolean copyAssetFolder(AssetManager assetManager,
  182. String fromAssetPath, String toPath) {
  183. try {
  184. String[] files = assetManager.list(fromAssetPath);
  185. new File(toPath).mkdirs();
  186. boolean res = true;
  187. for (String file : files)
  188. if (file.contains("."))
  189. res &= copyAsset(assetManager,
  190. fromAssetPath + "/" + file,
  191. toPath + "/" + file);
  192. else
  193. res &= copyAssetFolder(assetManager,
  194. fromAssetPath + "/" + file,
  195. toPath + "/" + file);
  196. return res;
  197. } catch (Exception e) {
  198. e.printStackTrace();
  199. return false;
  200. }
  201. }
  202. public static boolean copyAsset(AssetManager assetManager,
  203. String fromAssetPath, String toPath) {
  204. InputStream in = null;
  205. OutputStream out = null;
  206. try {
  207. in = assetManager.open(fromAssetPath);
  208. new File(toPath).createNewFile();
  209. out = Files.newOutputStream(Paths.get(toPath));
  210. IOUtils.copy(in, out);
  211. in.close();
  212. out.flush();
  213. out.close();
  214. return true;
  215. } catch (Exception e) {
  216. e.printStackTrace();
  217. return false;
  218. }
  219. }
  220. public static int dp2px(Context context, int dp) {
  221. float scale = context.getResources().getDisplayMetrics().density;
  222. return (int) (dp * scale + 0.5f);
  223. }
  224. }