Example usage for android.view.accessibility AccessibilityManager getInstance

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

Introduction

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

Prototype

@UnsupportedAppUsage
public static AccessibilityManager getInstance(Context context) 

Source Link

Document

Get an AccessibilityManager instance (create one if necessary).

Usage

From source file:android.webkit.AccessibilityInjector.java

/**
 * Creates an instance of the AccessibilityInjector based on
 * {@code webViewClassic}.//from   w  w  w  . j a  v a2  s.c  o m
 *
 * @param webViewClassic The WebViewClassic that this AccessibilityInjector
 *            manages.
 */
public AccessibilityInjector(WebViewClassic webViewClassic) {
    mWebViewClassic = webViewClassic;
    mWebView = webViewClassic.getWebView();
    mContext = webViewClassic.getContext();
    mAccessibilityManager = AccessibilityManager.getInstance(mContext);
}

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 v a 2  s.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);
    }
}

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

private void updateAccessibilityShortcut(Preference preference) {
    if (AccessibilityManager.getInstance(getActivity()).getInstalledAccessibilityServiceList().isEmpty()) {
        mAccessibilityShortcutPreferenceScreen
                .setSummary(getString(R.string.accessibility_no_services_installed));
        mAccessibilityShortcutPreferenceScreen.setEnabled(false);
    } else {//w  w w.  j  a v a2s  . c om
        mAccessibilityShortcutPreferenceScreen.setEnabled(true);
        boolean shortcutEnabled = AccessibilityUtils.isShortcutEnabled(getContext(), UserHandle.myUserId());
        CharSequence summary = shortcutEnabled
                ? AccessibilityShortcutPreferenceFragment.getServiceName(getContext())
                : getString(R.string.accessibility_feature_state_off);
        mAccessibilityShortcutPreferenceScreen.setSummary(summary);
    }
}