paint-brush
I Automated My Whatsapp Chats on Android and Here's How You Can Tooby@aditibhatnagar
621 reads
621 reads

I Automated My Whatsapp Chats on Android and Here's How You Can Too

by OffgridDecember 5th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Android Accessibility Service to Alter the Way You Use Your Mobile

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - I Automated My Whatsapp Chats on Android and Here's How You Can Too
Offgrid HackerNoon profile picture

Android Accessibility Service to Alter the Way You Use Your Mobile

I am a big fan of automation. Honestly, in this era of the attention economy, I try creative ways to save my time wherever I can.

So one fine day, when a friend of mine said, "Hey, can you please remind me to write that essay tomorrow", the inherent spirit kicked in and I ended up coding an Android app that can automatically send messages to her.

And think about that! How interesting the concept is! There is a difference between setting an alarm for yourself and getting reminded of stuff and someone texting you to remind you to do something. When a message is sent by a person, it has that special effect! So, I thought, why not use technology to intervene in human lives in a psychologically positive way?

And There it Comes: Automate Whatsapp Chats

Yeah, you can use it in A LOT of ways, in fact, once you have learned the technique, you can pretty much automate anything on Android - from automating sending emails, switching songs, clear data, and what not!

This is what the app looks like:

I have used Accessibility Service for it.

What is Accessibility Service, you may ask?

Accessibility services are a feature of the Android framework designed to provide alternative navigation feedback to the user on behalf of applications installed on Android devices. An accessibility service can communicate to the user on the application's behalf, for example by converting text to speech or providing haptic feedback when a user is hovering on an important area of the screen

Source: https://codelabs.developers.google.com/codelabs/developing-android-a11y-service/#0

I have used the Accessibility Service to create this automation for myself. The service enables us to not just view text but also, perform actions on the user's behalf. So, how can you create similar automation for yourself?

Follow these steps:

1. Define the service and XML config:

accessibility_service_config.xml

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:packageNames="com.whatsapp"
    android:accessibilityEventTypes="typeWindowStateChanged|typeWindowContentChanged"
    android:accessibilityFlags="flagReportViewIds|flagRetrieveInteractiveWindows"

    android:accessibilityFeedbackType="feedbackGeneric"
    android:notificationTimeout="1000"
    android:canRetrieveWindowContent="true"
    android:settingsActivity="infinite.hacks.aditi.autowhatsapp.ChatsAccService"/>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="infinite.hacks.aditi.autowhatsapp">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service
            android:name=".ChatsAccService"
            android:enabled="true"
            android:label="Automate Chats WhatsApp"
            android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
            <intent-filter>
                <action android:name="android.accessibilityservice.AccessibilityService" />
            </intent-filter>

            <meta-data
                android:name="android.accessibilityservice"
                android:resource="@xml/accessibility_service_config" />
        </service>
    </application>
</manifest>

2. Define the Accessibility Service, extends AccessibilityService:

Reference: https://developer.android.com/reference/android/accessibilityservice/AccessibilityService

3. Find out the IDs of UI elements that hold the:

  • Name of the person on WhatsApp
  • Edit Text (where you type the chat)
  • ID of the send button (note that the send button appears only once the user has typed the text)

You can use tools like UI Automator to find these IDs. And there you go!

Once you have the IDs, you can then define the implementation of the method,

public void onAccessibilityEvent (AccessibilityEvent event) { ... }

Get the root from the event nodes, traverse through the tree and find out the nodes having the above IDs acquired in step 3.

Once you have the IDs, you can then use the method performAction to simulate sending the chats:

 AccessibilityNodeInfo textBox = getNode(rootNode, chatBoxRefId);
                Bundle arguments = new Bundle();
                if (convIndex == convs.length -1) {
                    arguments.putString(AccessibilityNodeInfoCompat.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, "<3");

                } else {
                    arguments.putString(AccessibilityNodeInfoCompat.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, convs[convIndex % (convs.length - 1)]);
                    convIndex ++;
                }
                textBox.performAction(AccessibilityNodeInfoCompat.ACTION_SET_TEXT, arguments);
                AccessibilityNodeInfo sendButton = getNode(rootNode, sendButtonRefId);
                sendButton.performAction(AccessibilityNodeInfo.ACTION_CLICK);

So, that is all folks! Easy-peasy! Try it out and have fun! Stay curious and creative, I'll see you in the next post soon.

If you want an already coded app, checkout: https://www.aditi.fyi/whatsapp-hack

Previously published at: https://www.aditi.fyi/post/i-automated-my-whatsapp-chats