Skip to content

Commit 71c029b

Browse files
authored
Merge pull request #24 from adgad/accessibility-service
Accessibility service for Whatsapp name
2 parents f1deb49 + 5b91359 commit 71c029b

File tree

6 files changed

+98
-23
lines changed

6 files changed

+98
-23
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,11 @@ Outputs a cat fact from a [Cat Fact API](https://alexwohlbruck.github.io/cat-fac
111111
`/dad joke!curl(https://icanhazdadjoke.com/)`
112112
Outputs a random lame joke.
113113

114+
## Accessibility Service (optional)
115+
116+
kboard provides an accessibilty service which can be used to populate a person's name from Whatsapp conversations.
117+
118+
This can be accessed with the keywords `$name`, `$fname` and `$lname`.
119+
120+
This service is optional, and the keyboard is fully functional without it.
114121

app/src/main/AndroidManifest.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@
2424
</intent-filter>
2525
</service>
2626

27+
<service android:name=".KboardAccessibilityService"
28+
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
29+
android:label="Kboard Accessibility">
30+
<intent-filter>
31+
<action android:name="android.accessibilityservice.AccessibilityService" />
32+
</intent-filter>
33+
<meta-data
34+
android:name="android.accessibilityservice"
35+
android:resource="@xml/accessibility_service_config" />
36+
37+
</service>
38+
39+
2740
<activity
2841
android:name=".PrefsActivity"
2942
android:label="@string/settings_name"

app/src/main/java/com/adgad/kboard/KCommands.java

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -222,23 +222,15 @@ public void sa(int n) {
222222
//insert text
223223
public void i(int n, String parameter) {
224224
for(int i=0;i<n;i++) {
225-
if (buffer != null) {
226-
commitText(parameter.replaceAll("\\$0", buffer));
227-
} else {
228-
commitText(parameter);
229-
}
225+
commitText(replaceDollarWords(parameter));
230226
}
231227

232228
}
233229

234230
//insert text raw (without autospace etc)
235231
public void iraw(int n, String parameter) {
236232
for(int i=0;i<n;i++) {
237-
if (buffer != null) {
238-
inputConnection.commitText(parameter.replaceAll("\\$0", buffer), 1);
239-
} else {
240233
inputConnection.commitText(parameter, 1);
241-
}
242234
}
243235
}
244236

@@ -292,24 +284,16 @@ public void pc(int n) {
292284
//make uppercase
293285
public void upper(int n, String parameter) {
294286
for(int i=0;i<n;i++) {
295-
String lastBufferWord = buffer;
296-
if (lastBufferWord != null) {
297-
inputConnection.commitText(parameter.replaceAll("\\$0", lastBufferWord).toUpperCase(), 1);
298-
} else {
299287
inputConnection.commitText(parameter.toUpperCase(), 1);
300-
}
301288
}
302289
}
303290

304291
//make lowercase
305292
public void lower(int n, String parameter) {
306293
for(int i=0;i<n;i++) {
307-
String lastBufferWord = buffer;
308-
if (lastBufferWord != null) {
309-
inputConnection.commitText(parameter.replaceAll("\\$0", lastBufferWord).toLowerCase(), 1);
310-
} else {
294+
311295
inputConnection.commitText(parameter.toLowerCase(), 1);
312-
}
296+
313297
}
314298
}
315299

@@ -457,7 +441,7 @@ public Map<String, String> getHeaders(){
457441
}
458442
}
459443

460-
public Uri getImageUri (Bitmap inImage) {
444+
private Uri getImageUri (Bitmap inImage) {
461445
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
462446
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
463447
String path = MediaStore.Images.Media.insertImage(mIme.getContentResolver(), inImage, "Title", null);
@@ -514,7 +498,7 @@ public void run() {
514498
commands = new String[1];
515499
commands[0] = cmd;
516500
int numberOfTimes = cmd.indexOf("e") > 0 ? Integer.parseInt(cmd.substring(0, cmd.indexOf("e"))) : 1;
517-
String parameter = cmd.substring(cmd.indexOf("(") + 1, cmd.lastIndexOf(")"));
501+
String parameter = replaceDollarWords(cmd.substring(cmd.indexOf("(") + 1, cmd.lastIndexOf(")")));
518502
e(numberOfTimes, parameter);
519503
} else {
520504
commands = cmd.split(",");
@@ -525,7 +509,8 @@ public void run() {
525509
String[] commandMethodParts = command.split("(\\((?!\\))|,|(?<!\\()\\))"); //split out parameter in brackets
526510
if(commandMethodParts.length > 1) { //has parameter
527511
commandMethod = commandMethodParts[0];
528-
parameter = commandMethodParts[1].replaceAll("\\$0", buffer);
512+
parameter = replaceDollarWords(commandMethodParts[1]);
513+
529514
} else {
530515
commandMethod = commandMethodParts[0];
531516
}
@@ -551,6 +536,27 @@ public void run() {
551536

552537
}
553538

539+
private String replaceDollarWords(String initial) {
540+
String newWord = initial;
541+
if(buffer != null) {
542+
newWord = newWord.replace("$0", buffer);
543+
}
544+
if(KboardAccessibilityService.getCurrentWhatsappName() != null) {
545+
String fullName = KboardAccessibilityService.getCurrentWhatsappName();
546+
String[] names = fullName.split(" ");
547+
548+
if(names.length > 1) {
549+
newWord = newWord.replace("$fname", names[0]);
550+
newWord = newWord.replace("$lname", names[1]);
551+
} else {
552+
newWord = newWord.replace("$fname", fullName);
553+
}
554+
555+
newWord = newWord.replace("$name", KboardAccessibilityService.getCurrentWhatsappName());
556+
}
557+
return newWord;
558+
}
559+
554560
private void execute(String cmd, int n, String parameter) {
555561
inputConnection.beginBatchEdit();
556562

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.adgad.kboard;
2+
3+
4+
import android.accessibilityservice.AccessibilityService;
5+
import android.util.Log;
6+
import android.view.accessibility.AccessibilityEvent;
7+
import android.view.accessibility.AccessibilityNodeInfo;
8+
9+
public class KboardAccessibilityService extends AccessibilityService {
10+
11+
private static String mWhatsappName;
12+
private AccessibilityNodeInfo titleNode;
13+
14+
@Override
15+
public void onAccessibilityEvent(AccessibilityEvent event) {
16+
try {
17+
mWhatsappName = "";
18+
String packageName = event.getPackageName().toString();
19+
if (packageName.equals("com.whatsapp")) {
20+
titleNode = event.getSource().findAccessibilityNodeInfosByViewId("com.whatsapp:id/conversation_contact_name").get(0);
21+
}
22+
if(titleNode != null) {
23+
mWhatsappName = titleNode.getText().toString();
24+
}
25+
} catch(Exception e) {
26+
e.printStackTrace();
27+
}
28+
}
29+
30+
@Override
31+
public void onInterrupt() {
32+
}
33+
34+
public static String getCurrentWhatsappName() {
35+
return mWhatsappName;
36+
}
37+
38+
39+
}

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
<string name="label_keyboard_key_next">Next</string>
1111
<string name="label_keyboard_key_enter">\u21b5</string>
1212
<string name="title_activity_macro_help">MacroHelpActivity</string>
13-
13+
<string name="accessibility_service_description">Kboard Accessibility - used for getting the current user name in chat apps</string>
1414
</resources>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:description="@string/accessibility_service_description"
3+
android:packageNames="com.whatsapp"
4+
android:accessibilityEventTypes="typeWindowStateChanged"
5+
android:accessibilityFlags="flagDefault"
6+
android:accessibilityFeedbackType="feedbackAllMask"
7+
android:notificationTimeout="100"
8+
android:canRetrieveWindowContent="true"
9+
android:settingsActivity="com.example.android.accessibility.ServiceSettingsActivity"
10+
/>

0 commit comments

Comments
 (0)