HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Catching notifications using Accessibility Service Android app

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
accessibilityandroidappcatchingnotificationsserviceusing

Problem

I am currently working on an app in which one of the tasks is to catch the notifications of different applications (phone call, Facebook, Twitter, Whatsapp etc...). Now I will have some values pre-defined for all the pre-defined applications. Values will be of type string denoting R,G,B color values. I would have to send these color values via Bluetooth to another device. For this I have created an accessibility service.

Here is my main activity:

Main Activity displays a listview with applications name declared in the currentlyEnabled HashMap. The newFinalPackages HashMap stores the application names for all the pre defined applications. The apptoRBG HashMap stores these application names with user-given RGB values.

```
public class MainActivity extends AppCompatActivity {

private Intent mailAccessabilityIntent;
private ListView actioList;
private ActionCustomAdapter adapter;
private static final String DEFAULT_COLOR = "R:255,G:255,B:255";
private static HashMap newfinalpackages = new HashMap();
private static HashMap newappToRGB = new HashMap();

static {
newfinalpackages.put("dialer", "Calls");
newfinalpackages.put("whatsapp", "WhatsApp");
newfinalpackages.put("facebook.katana", "Facebook");
newfinalpackages.put("twitter", "Twitter");
newfinalpackages.put("instagram", "Instagram");
newfinalpackages.put("gmail", "GMail");
newfinalpackages.put("batterylow", "Low Battery");
newfinalpackages.put("messenger", "Text Message");

newappToRGB.put("dialer", DEFAULT_COLOR);
newappToRGB.put("whatsapp", DEFAULT_COLOR);
newappToRGB.put("facebook", DEFAULT_COLOR);
newappToRGB.put("twitter", DEFAULT_COLOR);
newappToRGB.put("instagram", DEFAULT_COLOR);
newappToRGB.put("gmail", null);
newappToRGB.put("batterylow", null);
newappToRGB.put("messenger", null);
}

private static HashMap currentlyEnabled = new

Solution

if(packagename.equals(selfname)==false && packagename.equals("com.android.providers.downloads")==false) {
        Toast.makeText(this, "Package: "+packagename,Toast.LENGTH_LONG).show();
}


You can use this check instead:

!packagename.equals(selfname) && !packagename.equals("com.android.providers.downloads")


Explicit equality checks with false seem out of place.

private void parsePackage (String pack) {
    String name = pack.substring(pack.indexOf(".")+1,pack.length());
    char[] array = pack.toCharArray();
    int prev=0;
    for(int i=0,n=pack.length() ; i<n ; i++) {
        String temp="";
        if(i==n-1) {
            temp = pack.substring(prev,i+1);
            Log.d("seetothis", "Name: " + temp +" has value= " + enabledActions.get(temp) );
        }
        else if(pack.charAt(i) == '.') {
            temp = pack.substring(prev,i);
            prev = i+1;
            Log.d("seetothis", "Name: " + temp +" has value= " + enabledActions.get(temp) );
        }
        if(enabledActions.get(temp) != null) {
            Log.d("printout", "Package grabbed is: "+temp+" With Value: "+enabledActions.get(temp));
        }
    }
}


Rather than using a complex loop here, you could make use of String.split, then iterate over the String array afterwards. As String.split takes a regex, you'll want to escape the delimiter:

(Escape method taken from How can I use “.” as the delimiter with String.split() in java)

String[] packageNames = pack.split(Pattern.quote("."));

Code Snippets

if(packagename.equals(selfname)==false && packagename.equals("com.android.providers.downloads")==false) {
        Toast.makeText(this, "Package: "+packagename,Toast.LENGTH_LONG).show();
}
!packagename.equals(selfname) && !packagename.equals("com.android.providers.downloads")
private void parsePackage (String pack) {
    String name = pack.substring(pack.indexOf(".")+1,pack.length());
    char[] array = pack.toCharArray();
    int prev=0;
    for(int i=0,n=pack.length() ; i<n ; i++) {
        String temp="";
        if(i==n-1) {
            temp = pack.substring(prev,i+1);
            Log.d("seetothis", "Name: " + temp +" has value= " + enabledActions.get(temp) );
        }
        else if(pack.charAt(i) == '.') {
            temp = pack.substring(prev,i);
            prev = i+1;
            Log.d("seetothis", "Name: " + temp +" has value= " + enabledActions.get(temp) );
        }
        if(enabledActions.get(temp) != null) {
            Log.d("printout", "Package grabbed is: "+temp+" With Value: "+enabledActions.get(temp));
        }
    }
}
String[] packageNames = pack.split(Pattern.quote("."));

Context

StackExchange Code Review Q#136233, answer score: 2

Revisions (0)

No revisions yet.