Skip to content
Advertisement

Is there any way for an Accessibility service to detect when the user is in the launcher or app drawer?

If you have an accessibility service that detects events like this:

@Override
public void onAccessibilityEvent(AccessibilityEvent event) { //Called whenever the accessibility service gets an accessibility event

    if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
        if (event.getPackageName() != null && event.getClassName() != null) {
            //Component can be an activity, service, BroadcastReceiver, ContentProvider
            ComponentName componentName = new ComponentName( //Component can be an activity, service, BroadcastReceiver, ContentProvider
                    event.getPackageName().toString(),
                    event.getClassName().toString()
            );

            ActivityInfo activityInfo = getPackageManager().getActivityInfo(componentName, 0);

            boolean isActivity = activityInfo != null;

            if (isActivity) {
                Log.i(TAG, "packageName for this activity is " + event.getPackageName().toString());
            }
        }
    }
}

I don’t actually know what the package name for the launcher is on many devices. Does it have a package name or something else that can be detected in an accessibility service?

Advertisement

Answer

You should be able to find what packages are launchers by looking for the “home” activity:

PackageManger pm = getPackageManager();
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);

List<ResolveInfo> launchers = pm.queryIntentActivities(homeIntent, PackageManager.MATCH_ALL);

String packages[] = new String[launchers.size()];

for (int i = 0; i < launchers.size(); i++) {
    packages[i] = laycnhers.get(i).activityInfo.packageName;
}

Do this when you accessibility service initializes, than check if the package for the accessibility event is in the array.

Normally, there should be only one such package, but if the user installed 3rd party launcher or the OEM included optional launcher there may be more.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement