0

I am stuck in a small problem. I have text retrieved from the permission window.

Now I found out a way to retrieve the app name . This is how I did it:

private String Namer(String parse){
        if(parse.length() > 30) {
            parse = parse.substring(parse.indexOf("********") + 6, parse.length());
            parse = parse.substring(0, parse.indexOf("*******"));
            return parse;
        } else {
            return parse;
        }
}

and this is how i use it:

List<AccessibilityNodeInfo> NodeInfo;
    AccessibilityNodeInfo nodeInfo = event.getSource();
    for (Map.Entry<OriginalPermissions, String> permissions : 

detect.entrySet()) {
       NodeInfo = nodeInfo.findAccessibilityNodeInfosByText(permissions.getValue());


    Log.d("Information", Namer(Namer(String.valueOf(NodeInfo))));
}

But the problem is that the permission window text can be different. Is there any way in which I could just retrieve the bold text from the permission window?

Thanks

Thomas
  • 121
  • 2
  • 12

1 Answers1

3

As you see (source code), title of GrantPermissionsActivity is a Spanned text. Instead relying on the surrounding text you can get the value of first span (with bold text) using such method:

public String getFirstSpanValueFromTitle(Spanned text) {
    StyleSpan[] spans = text.getSpans(0, text.length(), StyleSpan.class);
    if (spans.length > 0) {
        int start = text.getSpanStart(spans[0]);
        int end = text.getSpanEnd(spans[0]);
        return String.valueOf(text.subSequence(start, end));
    }
    return "";
}

Otherwise, you will need to get the value of the permission_warning_template resource string for each language and hardcode it in you code.

UPDATE:

Example how to get title text from AccessibilityEvent object:

String pkgName = event.getPackageName().toString();
if ("com.google.android.packageinstaller".equals(pkgName)) {
    AccessibilityNodeInfo root = event.getSource();
    List<AccessibilityNodeInfo> nodes = 
            root.findAccessibilityNodeInfosByViewId("com.android.packageinstaller:id/permission_message");
    if (nodes.size() > 0) {
        Log.e(TAG, "First span value: " + getFirstSpanValueFromTitle((Spanned) nodes.get(0).getText()));
    }
}
Nikolay
  • 1,429
  • 1
  • 13
  • 24
  • 1
    SpannedText value for this method you can get from AccessibilityNodeInfo object that contais text "Allow Camera to user device storage." using AccessibilityNodeInfo.getText() method. It returns CharSequence type and you should cast it to SpannedText. – Nikolay Oct 25 '17 at 12:20
  • How it is not strange, but it is possible and very simple to realise. BTW I did not know about this possibility before you ask it and found a solution in this thread https://stackoverflow.com/questions/7205415/getting-resources-of-another-application. – Nikolay Oct 27 '17 at 07:04
  • In this thread, I saw that it is possible if both the apps are written by me. For example, can I retrieve the string resource named "permission_warning_template" for each language dynamically from the android aource code? – Thomas Oct 27 '17 at 07:34
  • 1
    Yes, you can. This is working code: Resources r = getActivity().getPackageManager() .getResourcesForApplication("com.google.android.packageinstaller"); int id = r.getIdentifier("com.android.packageinstaller:string/permission_warning_template", null, null); Log.e(TAG,"Value: " + r.getString(id)); – Nikolay Oct 27 '17 at 07:50
  • The code is correct and should work fine. Are you sure this method is being called? – Nikolay Oct 27 '17 at 09:23
  • I am able to retrieve strings from com.android.settings. but why not from com.android.packageinstaller? – Thomas Oct 27 '17 at 10:45
  • 1
    You can read resources of any installed apps in device. I tested it before post here. Please check may be error in another place of your code. – Nikolay Oct 27 '17 at 11:28