javaandroidaccessibilitywhatsapppopupwindow

How Using Android's accessibility feature to auto-click and close Whats App pop-up notifications and then return to the main app


This is a part of my college project which I have to complete.In this project app i am sending whats app message automatically by using Accessibility feature of android through following java code.

    numberlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

        try {
            Toast.makeText(getBaseContext(), (String) numberlist.getItemAtPosition(i), Toast.LENGTH_SHORT).show();
            txtnumber.setText(numberlist.getItemAtPosition(i).toString());
           

            PackageManager packageManager = MainActivity.this.getPackageManager();
            Intent saif = new Intent(Intent.ACTION_VIEW);
            String message;
            message = txtmessage.getText().toString() + suffix.toString();
           
          
            saif.setData(Uri.parse(url));
            if (saif.resolveActivity(packageManager) != null) {
                MainActivity.this.startActivityForResult(saif, 1);
            }
        } catch (Exception e) {
            Log.e("ERROR WHATSAPP",e.toString());
            Toast.makeText(getApplicationContext(),
                    "Contact not found",
                    Toast.LENGTH_LONG).show();

        }
    }

});

If the user's mobile number is created in the WhatsApp app, the message goes easily. If there is a mobile number on which WhatsApp is not created, then the following message appears. enter image description here

I have using following Accessibility node info ....

    List<AccessibilityNodeInfoCompat> sendMessageNodeInfoList = rootInActiveWindow.findAccessibilityNodeInfosByViewId("com.whatsapp:id/send");
    String actname = event.getClassName().toString();
    switch (actname) {
        case "com.whatsapp.Conversation":
            sendMessageNodeInfoList = rootInActiveWindow.findAccessibilityNodeInfosByViewId("com.whatsapp:id/send");
            if (sendMessageNodeInfoList.size() > 0) {
                sendMessageNodeInfoList.get(0).performAction(ACTION_CLICK);
            }
            break;
        case "com.whatsapp.HomeActivity":
            sendNext();
            break;
        case "com.whatsapp.contact.picker.ContactPicker":
            Toast.makeText(this, "Unable to find contacts in your list! Skipping!!!", Toast.LENGTH_SHORT).show();
            performGlobalAction(GLOBAL_ACTION_BACK);
            break;
    }
    assert sendMessageNodeInfoList != null;
    AccessibilityNodeInfoCompat sendMessageButton = sendMessageNodeInfoList.get(0);
    if (!sendMessageButton.isVisibleToUser()) {

        return;
    }
    sendMessageButton.performAction(ACTION_CLICK);

My question is, how can we close this notification by auto clicking on the OK or Cancel button using the accessibility feature of Android? I want that when a Whats App message is sent to a user's mobile number, it should first be checked whether his mobile number is available on Whats App or not. If Whats App is created on the mobile number, then a message should be sent to him and if Whats App is not created on his mobile number, then auto-click on this pop-up notification to close it and return to the main screen. This is the last project of my college which I have to complete please help me to complete it. Thanks


Solution

  • I have solved this issue with following code: Someone has truly said that " Try Try again ".

        if (accessibilityEvent.toString().contains("ClassName: com.whatsapp.contact.picker.ContactPicker")) {
                        Thread.sleep(100);
                        MainActivity.V0++;
                        performGlobalAction(GLOBAL_ACTION_BACK);
                        Intent intent2 = new Intent("NextNumber");
                        intent2.setPackage(getPackageName());
                        getApplicationContext().sendBroadcast(intent2);
                        return;
                    }
                    AccessibilityNodeInfo source = accessibilityEvent.getSource();
                    if (source != null) {
                        List<AccessibilityNodeInfo> findAccessibilityNodeInfosByViewId = source.findAccessibilityNodeInfosByViewId(MainActivity.R0 + ":id/send");
                        if (findAccessibilityNodeInfosByViewId == null) {
                            return;
                        }
                        if (!findAccessibilityNodeInfosByViewId.isEmpty()) {
                            AccessibilityNodeInfo accessibilityNodeInfo = findAccessibilityNodeInfosByViewId.get(0);
                            if (accessibilityNodeInfo.isVisibleToUser()) {
                                accessibilityNodeInfo.performAction(ACTION_CLICK);
                                Thread.sleep(900);
                                MainActivity.V0++;
                                performGlobalAction(GLOBAL_ACTION_BACK);
                                intent = new Intent("NextNumber");
                                packageName = getPackageName();
                            } else {
                                return;
                            }
                        } else {
                            return;
                        }
                    } else {
                        return;
                    }
                } else if (getRootInActiveWindow() != null) {
                    AccessibilityNodeInfo rootInActiveWindow = getRootInActiveWindow();
                     i iVar = new i(rootInActiveWindow, 0);
                    AccessibilityNodeInfo child = rootInActiveWindow.getChild(0);
                    String iVar2 = (child != null ? new i(child, 0) : null).toString();
                    if (!iVar2.contains("text: The phone number +" +MainActivity.txtnumber.getText().toString()+"isn't on WhatsApp ")) {
                        if (!iVar2.contains("text: The phone number +" +MainActivity.txtnumber.getText().toString()+"isn't on WhatsApp")) {
                            ArrayList d7 = iVar.d(MainActivity.R0 + ":id/entry");
                            if (!d7.isEmpty()) {
                                i iVar3 = (i) d7.get(0);
                                if (!(iVar3.f() == null && iVar3.f().length() == 0) && iVar3.f().toString().endsWith(getApplicationContext().getString(R.string.whatsapp_suffix))) {
                                    ArrayList d8 = iVar.d(MainActivity.R0 + ":id/send");
                                    if (!d8.isEmpty()) {
                                        i iVar4 = (i) d8.get(0);
                                        if (iVar4.f2707a.isVisibleToUser()) {
                                            MainActivity.V0++;
                                            iVar4.f2707a.performAction(ACTION_CLICK);
                                            Thread.sleep(700);
                                            performGlobalAction(GLOBAL_ACTION_BACK);
                                            Thread.sleep(700);
                                            performGlobalAction(GLOBAL_ACTION_BACK);
                                            intent = new Intent("NextNumber");
                                            packageName = getPackageName();
                                        } else {
                                            return;
                                        }
                                    } else {
                                        return;
                                    }
                                } else {
                                    return;
                                }
                            } else {
                                return;
                            }
                        }
                    }
                    MainActivity.V0++;
                    performGlobalAction(GLOBAL_ACTION_BACK);
                    Thread.sleep(400);
                    performGlobalAction(GLOBAL_ACTION_BACK);
                    Intent intent3 = new Intent("NextNumber");
                    intent3.setPackage(getPackageName());
                    getApplicationContext().sendBroadcast(intent3);
                    return;