The selected answer works on API 18 and above, because it passes to findAccessibilityNodeInfosByViewId, which was added to API 18. I ended up writing this class to support API 17 and below.
ResourcesCompat class finds resources identified with this activity, which in our case should be active in the Android settings. You can get the ComponentName property from the settings by calling this function when processing an accessibility event in the accessibility service.
public static ComponentName getForegroundActivity(Context context) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1); ComponentName topActivity = taskInfo.get(0).topActivity; return topActivity; }
A good place to call init(...) is in onAccessibilityEvent when you first handle the TYPE_WINDOW_STATE_CHANGED event like thecr0w .
public class ResourcesCompat { private final static String RESOURCE_TYPE = "string"; private String mResourcesPackageName; private Resources mResources; public void init(Context context, String settingsPackageName, String settingsClassName) { try { mResourcesPackageName = settingsPackageName; ComponentName settingsComponentName = new ComponentName(settingsPackageName, settingsPackageName + settingsClassName); mResources = context.getPackageManager().getResourcesForActivity(settingsComponentName); } catch (PackageManager.NameNotFoundException e) { } } public String getString(String resourceName) { int resourceId = getIdentifier(resourceName); return resourceId > 0 ? mResources.getString(resourceId) : null; } private int getIdentifier(String resourceName) { return mResources.getIdentifier(resourceName, RESOURCE_TYPE, mResourcesPackageName); } }
Some manufacturers like to move classes and rename default lines (cough Samsung cough Xiomi cough). Therefore, make sure that you cover all cases and handle errors and exceptions.
Finally, find your opinion by name. here id can be "force_stop", for example
private List<AccessibilityNodeInfo> findAccessibilityNodeInfosByName(AccessibilityNodeInfo source, String id) { String nodeText = mResourcesCompat.getString(id); if (nodeText != null) { return source.findAccessibilityNodeInfosByText(nodeText); } return null; }
source share