Example usage for android.view.accessibility AccessibilityManager getInstalledAccessibilityServiceList

List of usage examples for android.view.accessibility AccessibilityManager getInstalledAccessibilityServiceList

Introduction

In this page you can find the example usage for android.view.accessibility AccessibilityManager getInstalledAccessibilityServiceList.

Prototype

public List<AccessibilityServiceInfo> getInstalledAccessibilityServiceList() 

Source Link

Document

Returns the AccessibilityServiceInfo s of the installed accessibility services.

Usage

From source file:com.android.settings.accessibility.AccessibilitySettings.java

private void updateServicePreferences() {
    // Since services category is auto generated we have to do a pass
    // to generate it since services can come and go and then based on
    // the global accessibility state to decided whether it is enabled.

    // Generate./*from w w  w.  j  a va  2s  .c o m*/
    ArrayList<Preference> servicePreferences = new ArrayList<>(
            mServicePreferenceToPreferenceCategoryMap.keySet());
    for (int i = 0; i < servicePreferences.size(); i++) {
        Preference service = servicePreferences.get(i);
        PreferenceCategory category = mServicePreferenceToPreferenceCategoryMap.get(service);
        category.removePreference(service);
    }

    initializePreBundledServicesMapFromArray(CATEGORY_SCREEN_READER,
            R.array.config_preinstalled_screen_reader_services);
    initializePreBundledServicesMapFromArray(CATEGORY_AUDIO_AND_CAPTIONS,
            R.array.config_preinstalled_audio_and_caption_services);
    initializePreBundledServicesMapFromArray(CATEGORY_DISPLAY, R.array.config_preinstalled_display_services);
    initializePreBundledServicesMapFromArray(CATEGORY_INTERACTION_CONTROL,
            R.array.config_preinstalled_interaction_control_services);

    AccessibilityManager accessibilityManager = AccessibilityManager.getInstance(getActivity());

    List<AccessibilityServiceInfo> installedServices = accessibilityManager
            .getInstalledAccessibilityServiceList();
    Set<ComponentName> enabledServices = AccessibilityUtils.getEnabledServicesFromSettings(getActivity());
    List<String> permittedServices = mDpm.getPermittedAccessibilityServices(UserHandle.myUserId());
    final boolean accessibilityEnabled = Settings.Secure.getInt(getContentResolver(),
            Settings.Secure.ACCESSIBILITY_ENABLED, 0) == 1;

    PreferenceCategory downloadedServicesCategory = mCategoryToPrefCategoryMap
            .get(CATEGORY_DOWNLOADED_SERVICES);
    // Temporarily add the downloaded services category back if it was previously removed.
    if (findPreference(CATEGORY_DOWNLOADED_SERVICES) == null) {
        getPreferenceScreen().addPreference(downloadedServicesCategory);
    }

    for (int i = 0, count = installedServices.size(); i < count; ++i) {
        AccessibilityServiceInfo info = installedServices.get(i);

        RestrictedPreference preference = new RestrictedPreference(downloadedServicesCategory.getContext());
        String title = info.getResolveInfo().loadLabel(getPackageManager()).toString();

        Drawable icon;
        if (info.getResolveInfo().getIconResource() == 0) {
            icon = ContextCompat.getDrawable(getContext(), R.mipmap.ic_accessibility_generic);
        } else {
            icon = info.getResolveInfo().loadIcon(getPackageManager());
        }

        ServiceInfo serviceInfo = info.getResolveInfo().serviceInfo;
        String packageName = serviceInfo.packageName;
        ComponentName componentName = new ComponentName(packageName, serviceInfo.name);
        String componentNameKey = componentName.flattenToString();

        preference.setKey(componentName.flattenToString());

        preference.setTitle(title);
        preference.setIcon(icon);
        final boolean serviceEnabled = accessibilityEnabled && enabledServices.contains(componentName);
        final String serviceState = serviceEnabled ? getString(R.string.accessibility_summary_state_enabled)
                : getString(R.string.accessibility_summary_state_disabled);
        final CharSequence serviceSummary = info.loadSummary(getPackageManager());
        final String stateSummaryCombo = getString(R.string.accessibility_summary_default_combination,
                serviceState, serviceSummary);
        preference.setSummary((TextUtils.isEmpty(serviceSummary)) ? serviceState : stateSummaryCombo);

        // Disable all accessibility services that are not permitted.
        boolean serviceAllowed = permittedServices == null || permittedServices.contains(packageName);
        if (!serviceAllowed && !serviceEnabled) {
            EnforcedAdmin admin = RestrictedLockUtils.checkIfAccessibilityServiceDisallowed(getActivity(),
                    packageName, UserHandle.myUserId());
            if (admin != null) {
                preference.setDisabledByAdmin(admin);
            } else {
                preference.setEnabled(false);
            }
        } else {
            preference.setEnabled(true);
        }

        preference.setFragment(ToggleAccessibilityServicePreferenceFragment.class.getName());
        preference.setPersistent(true);

        Bundle extras = preference.getExtras();
        extras.putString(EXTRA_PREFERENCE_KEY, preference.getKey());
        extras.putBoolean(EXTRA_CHECKED, serviceEnabled);
        extras.putString(EXTRA_TITLE, title);

        String description = info.loadDescription(getPackageManager());
        if (TextUtils.isEmpty(description)) {
            description = getString(R.string.accessibility_service_default_description);
        }
        extras.putString(EXTRA_SUMMARY, description);

        String settingsClassName = info.getSettingsActivityName();
        if (!TextUtils.isEmpty(settingsClassName)) {
            extras.putString(EXTRA_SETTINGS_TITLE, getString(R.string.accessibility_menu_item_settings));
            extras.putString(EXTRA_SETTINGS_COMPONENT_NAME,
                    new ComponentName(packageName, settingsClassName).flattenToString());
        }
        extras.putParcelable(EXTRA_COMPONENT_NAME, componentName);

        PreferenceCategory prefCategory = downloadedServicesCategory;
        // Set the appropriate category if the service comes pre-installed.
        if (mPreBundledServiceComponentToCategoryMap.containsKey(componentName)) {
            prefCategory = mPreBundledServiceComponentToCategoryMap.get(componentName);
        }
        preference.setOrder(FIRST_PREFERENCE_IN_CATEGORY_INDEX);
        prefCategory.addPreference(preference);
        mServicePreferenceToPreferenceCategoryMap.put(preference, prefCategory);
    }

    // If the user has not installed any additional services, hide the category.
    if (downloadedServicesCategory.getPreferenceCount() == 0) {
        PreferenceScreen screen = getPreferenceScreen();
        screen.removePreference(downloadedServicesCategory);
    }
}