SettingsFragment.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. package com.example.modifier.fragments;
  2. import android.annotation.SuppressLint;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.os.Handler;
  6. import android.os.Looper;
  7. import android.util.Log;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.Toast;
  12. import androidx.annotation.NonNull;
  13. import androidx.core.content.ContextCompat;
  14. import androidx.fragment.app.Fragment;
  15. import com.android.volley.Request;
  16. import com.android.volley.RequestQueue;
  17. import com.android.volley.toolbox.JsonObjectRequest;
  18. import com.android.volley.toolbox.RequestFuture;
  19. import com.android.volley.toolbox.Volley;
  20. import com.example.modifier.model.TelephonyConfig;
  21. import com.example.modifier.Global;
  22. import com.example.modifier.MainActivity;
  23. import com.example.modifier.ModifierService;
  24. import com.example.modifier.R;
  25. import com.example.modifier.Utils;
  26. import com.example.modifier.databinding.FragmentSettingsBinding;
  27. import com.google.android.material.dialog.MaterialAlertDialogBuilder;
  28. import com.google.gson.Gson;
  29. import org.apache.commons.lang3.RandomStringUtils;
  30. import org.apache.commons.lang3.StringUtils;
  31. import org.json.JSONObject;
  32. import java.io.File;
  33. import java.io.FileWriter;
  34. import java.time.Instant;
  35. import java.util.Objects;
  36. import java.util.Optional;
  37. import java.util.concurrent.ScheduledThreadPoolExecutor;
  38. import java.util.concurrent.TimeUnit;
  39. import java.util.regex.Matcher;
  40. import java.util.regex.Pattern;
  41. public class SettingsFragment extends Fragment {
  42. private FragmentSettingsBinding binding;
  43. Handler handler = new Handler(Looper.getMainLooper());
  44. ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(8);
  45. public SettingsFragment() {
  46. Log.i("SettingsFragment", "SettingsFragment");
  47. }
  48. @Override
  49. public void onCreate(Bundle savedInstanceState) {
  50. super.onCreate(savedInstanceState);
  51. }
  52. @SuppressLint("SetTextI18n")
  53. @Override
  54. public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
  55. Bundle savedInstanceState) {
  56. if (binding != null) {
  57. return binding.getRoot();
  58. }
  59. binding = FragmentSettingsBinding.inflate(inflater, container, false);
  60. binding.tlIccid.setEndIconOnClickListener(v -> {
  61. binding.etIccid.setText(RandomStringUtils.randomNumeric(20));
  62. });
  63. binding.tlImsi.setEndIconOnClickListener(v -> {
  64. String mcc = Optional.ofNullable(binding.etMcc.getText()).map(Objects::toString).orElse("");
  65. String mnc = Optional.ofNullable(binding.etMnc.getText()).map(Objects::toString).orElse("");
  66. if (StringUtils.isEmpty(mcc) || StringUtils.isEmpty(mnc)) {
  67. Toast.makeText(getContext(), "MCC and MNC are required", Toast.LENGTH_SHORT).show();
  68. return;
  69. }
  70. binding.etImsi.setText(mcc + mnc + RandomStringUtils.randomNumeric(15 - mcc.length() - mnc.length()));
  71. });
  72. binding.tlImei.setEndIconOnClickListener(v -> {
  73. binding.etImei.setText(Utils.generateIMEI());
  74. });
  75. binding.btnSave.setOnClickListener(v -> {
  76. onSave();
  77. });
  78. binding.etServer.setThreshold(1000);
  79. binding.btnServer.setOnClickListener(v -> {
  80. String server = binding.etServer.getText().toString();
  81. if (StringUtils.isEmpty(server)) {
  82. Toast.makeText(getContext(), "Server is required", Toast.LENGTH_SHORT).show();
  83. return;
  84. }
  85. Global.saveServer(server, binding.etDeviceLabel.getText().toString());
  86. binding.etServer.setSimpleItems(Global.getServers().toArray(new String[0]));
  87. ModifierService modifierService = ModifierService.getInstance();
  88. if (modifierService != null) {
  89. modifierService.connect();
  90. }
  91. Utils.makeLoadingButton(getContext(), binding.btnServer);
  92. binding.btnServer.setEnabled(false);
  93. handler.postDelayed(() -> {
  94. binding.btnServer.setIconResource(R.drawable.ic_done);
  95. binding.btnServer.setText("OK");
  96. handler.postDelayed(() -> {
  97. binding.btnServer.setEnabled(true);
  98. binding.btnServer.setIcon(null);
  99. binding.btnServer.setText("Save");
  100. }, 1500);
  101. }, 500);
  102. });
  103. binding.btnRequest.setOnClickListener(v -> {
  104. Utils.makeLoadingButton(getContext(), binding.btnRequest);
  105. binding.btnRequest.setEnabled(false);
  106. executor.submit(() -> {
  107. try {
  108. RequestQueue queue = Volley.newRequestQueue(getContext());
  109. RequestFuture<JSONObject> future = RequestFuture.newFuture();
  110. JsonObjectRequest request = new JsonObjectRequest(Request.Method.PUT, Global.serverUrl + "/api/rcs-number", null, future, future);
  111. queue.add(request);
  112. JSONObject jsonObject = future.get(60, TimeUnit.SECONDS);
  113. Integer id = jsonObject.getInt("id");
  114. String expiryTimeStr = jsonObject.getString("expiryTime");
  115. Instant expiryTime = Instant.parse(expiryTimeStr);
  116. String number = jsonObject.getString("number");
  117. String mcc = jsonObject.getString("mcc");
  118. String mnc = jsonObject.getString("mnc");
  119. String country = jsonObject.getString("country");
  120. String iccid = RandomStringUtils.randomNumeric(20);
  121. String imsi = mcc + mnc + RandomStringUtils.randomNumeric(15 - mcc.length() - mnc.length());
  122. String imei = Utils.generateIMEI();
  123. Global.save(new TelephonyConfig(number, mcc, mnc, iccid, imsi, imei, country));
  124. Global.stop(false, true, true);
  125. handler.post(() -> {
  126. TelephonyConfig telephonyConfig = Global.telephonyConfig;
  127. binding.etNumber.setText(telephonyConfig.getNumber());
  128. binding.etMcc.setText(telephonyConfig.getMcc());
  129. binding.etMnc.setText(telephonyConfig.getMnc());
  130. binding.etIccid.setText(telephonyConfig.getIccid());
  131. binding.etImsi.setText(telephonyConfig.getImsi());
  132. binding.etImei.setText(telephonyConfig.getImei());
  133. binding.etCountry.setText(telephonyConfig.getCountry());
  134. binding.btnRequest.setText("Waiting for OTP...");
  135. });
  136. try {
  137. Utils.runAsRoot("am start -a android.intent.action.MAIN -c android.intent.category.LAUNCHER com.google.android.apps.messaging");
  138. Thread.sleep(1000);
  139. Utils.runAsRoot("am start com.google.android.apps.messaging/com.google.android.apps.messaging.ui.appsettings.SettingsActivity;");
  140. Utils.runAsRoot("am start com.google.android.apps.messaging/com.google.android.apps.messaging.ui.appsettings.RcsSettingsActivity;");
  141. Thread.sleep(1000);
  142. Intent intent = new Intent(getContext(), MainActivity.class);
  143. intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
  144. startActivity(intent);
  145. } catch (Exception e) {
  146. e.printStackTrace();
  147. }
  148. boolean success = false;
  149. while (Instant.now().isBefore(expiryTime)) {
  150. try {
  151. Log.i("SettingsFragment", "Waiting for OTP...");
  152. RequestFuture<JSONObject> future1 = RequestFuture.newFuture();
  153. JsonObjectRequest request1 = new JsonObjectRequest(Request.Method.GET, Global.serverUrl + "/api/rcs-number/" + id, null, future1, future1);
  154. queue.add(request1);
  155. JSONObject jsonObject1 = null;
  156. try {
  157. jsonObject1 = future1.get(60, TimeUnit.SECONDS);
  158. } catch (Exception e) {
  159. e.printStackTrace();
  160. }
  161. if (jsonObject1 == null) {
  162. continue;
  163. }
  164. String status = jsonObject1.optString("status");
  165. if ("success".equals(status)) {
  166. String message = jsonObject1.optString("message");
  167. Matcher matcher = Pattern.compile("Your Messenger verification code is G-(\\d{6})")
  168. .matcher(message);
  169. if (matcher.find()) {
  170. String otp = matcher.group(1);
  171. Intent intent = new Intent();
  172. intent.setAction("com.example.modifier.sms");
  173. intent.putExtra("sender", "3538");
  174. intent.putExtra("message", "Your Messenger verification code is G-" + otp);
  175. getContext().sendBroadcast(intent);
  176. success = true;
  177. break;
  178. }
  179. }
  180. Thread.sleep(2000);
  181. } catch (Exception e) {
  182. e.printStackTrace();
  183. }
  184. }
  185. if (!success) {
  186. handler.post(() -> {
  187. new MaterialAlertDialogBuilder(getContext())
  188. .setTitle("Error")
  189. .setMessage("Failed to get OTP")
  190. .setPositiveButton("OK", (dialog, which) -> {
  191. dialog.dismiss();
  192. })
  193. .show();
  194. });
  195. } else {
  196. handler.post(() -> {
  197. new MaterialAlertDialogBuilder(getContext())
  198. .setTitle("Success")
  199. .setMessage("OTP sent successfully")
  200. .setPositiveButton("OK", (dialog, which) -> {
  201. dialog.dismiss();
  202. })
  203. .show();
  204. });
  205. }
  206. } catch (Exception e) {
  207. e.printStackTrace();
  208. }
  209. handler.post(() -> {
  210. binding.btnRequest.setEnabled(true);
  211. binding.btnRequest.setIcon(null);
  212. binding.btnRequest.setText("Request");
  213. });
  214. });
  215. });
  216. executor.execute(() -> {
  217. Global.load();
  218. handler.post(() -> {
  219. binding.etServer.setText(Global.serverUrl);
  220. binding.etServer.setSimpleItems(Global.getServers().toArray(new String[0]));
  221. binding.etDeviceLabel.setText(Global.name);
  222. TelephonyConfig telephonyConfig = Global.telephonyConfig;
  223. binding.etNumber.setText(telephonyConfig.getNumber());
  224. binding.etMcc.setText(telephonyConfig.getMcc());
  225. binding.etMnc.setText(telephonyConfig.getMnc());
  226. binding.etIccid.setText(telephonyConfig.getIccid());
  227. binding.etImsi.setText(telephonyConfig.getImsi());
  228. binding.etImei.setText(telephonyConfig.getImei());
  229. binding.etCountry.setText(telephonyConfig.getCountry());
  230. });
  231. });
  232. return binding.getRoot();
  233. }
  234. private void onSave() {
  235. Utils.makeLoadingButton(getContext(), binding.btnSave);
  236. binding.btnSave.setEnabled(false);
  237. executor.execute(() -> {
  238. save();
  239. handler.post(() -> {
  240. binding.btnSave.setIconResource(R.drawable.ic_done);
  241. binding.btnSave.setText("OK");
  242. handler.postDelayed(() -> {
  243. binding.btnSave.setEnabled(true);
  244. binding.btnSave.setIcon(null);
  245. binding.btnSave.setText("Save");
  246. }, 1500);
  247. });
  248. });
  249. }
  250. private void save() {
  251. try {
  252. TelephonyConfig telephonyConfig = new TelephonyConfig(binding.etNumber.getText().toString(),
  253. binding.etMcc.getText().toString(),
  254. binding.etMnc.getText().toString(),
  255. binding.etIccid.getText().toString(),
  256. binding.etImsi.getText().toString(),
  257. binding.etImei.getText().toString(),
  258. binding.etCountry.getText().toString());
  259. File file = new File(ContextCompat.getDataDir(getContext()), "config.json");
  260. Gson gson = new Gson();
  261. String json = gson.toJson(telephonyConfig);
  262. try {
  263. FileWriter writer = new FileWriter(file);
  264. writer.write(json);
  265. writer.close();
  266. } catch (Exception e) {
  267. e.printStackTrace();
  268. }
  269. Utils.runAsRoot(
  270. "cp " + file.getPath() + " /data/data/com.android.phone/rcsConfig.json",
  271. "echo 'copied to phone'",
  272. "chmod 777 /data/data/com.android.phone/rcsConfig.json");
  273. } catch (Exception e) {
  274. e.printStackTrace();
  275. }
  276. }
  277. }