@Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      PreferenceScreen preferenceScreen =
          getPreferenceManager().createPreferenceScreen(getActivity());
      setPreferenceScreen(preferenceScreen);
      mSummaryPreference =
          new Preference(getActivity()) {
            @Override
            protected void onBindView(View view) {
              super.onBindView(view);
              TextView summaryView = (TextView) view.findViewById(R.id.summary);
              summaryView.setText(getSummary());
              sendAccessibilityEvent(summaryView);
            }

            private void sendAccessibilityEvent(View view) {
              // Since the view is still not attached we create, populate,
              // and send the event directly since we do not know when it
              // will be attached and posting commands is not as clean.
              AccessibilityManager accessibilityManager =
                  AccessibilityManager.getInstance(getActivity());
              if (accessibilityManager.isEnabled()) {
                AccessibilityEvent event = AccessibilityEvent.obtain();
                event.setEventType(AccessibilityEvent.TYPE_VIEW_FOCUSED);
                view.onInitializeAccessibilityEvent(event);
                view.dispatchPopulateAccessibilityEvent(event);
                accessibilityManager.sendAccessibilityEvent(event);
              }
            }
          };
      mSummaryPreference.setPersistent(false);
      mSummaryPreference.setLayoutResource(R.layout.text_description_preference);
      preferenceScreen.addPreference(mSummaryPreference);
    }
Пример #2
0
  private void addPluginPreferences(PreferenceScreen screen) {
    Intent queryIntent = new Intent(AstridApiConstants.ACTION_SETTINGS);
    PackageManager pm = getPackageManager();
    List<ResolveInfo> resolveInfoList =
        pm.queryIntentActivities(queryIntent, PackageManager.GET_META_DATA);
    int length = resolveInfoList.size();
    LinkedHashMap<String, ArrayList<Preference>> categoryPreferences =
        new LinkedHashMap<String, ArrayList<Preference>>();

    // Loop through a list of all packages (including plugins, addons)
    // that have a settings action
    for (int i = 0; i < length; i++) {
      ResolveInfo resolveInfo = resolveInfoList.get(i);
      Intent intent = new Intent(AstridApiConstants.ACTION_SETTINGS);
      intent.setClassName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);

      if (MilkPreferences.class.getName().equals(resolveInfo.activityInfo.name)
          && !MilkUtilities.INSTANCE.isLoggedIn()) continue;

      Preference preference = new Preference(this);
      preference.setTitle(resolveInfo.activityInfo.loadLabel(pm));
      preference.setIntent(intent);

      String category = MetadataHelper.resolveActivityCategoryName(resolveInfo, pm);

      if (!categoryPreferences.containsKey(category))
        categoryPreferences.put(category, new ArrayList<Preference>());
      ArrayList<Preference> arrayList = categoryPreferences.get(category);
      arrayList.add(preference);
    }

    for (Entry<String, ArrayList<Preference>> entry : categoryPreferences.entrySet()) {
      Preference header = new Preference(this);
      header.setLayoutResource(android.R.layout.preference_category);
      header.setTitle(entry.getKey());
      screen.addPreference(header);

      for (Preference preference : entry.getValue()) screen.addPreference(preference);
    }
  }
  private void updateServicesPreferences() {
    // 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.
    mServicesCategory.removeAll();

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

    List<AccessibilityServiceInfo> installedServices =
        accessibilityManager.getInstalledAccessibilityServiceList();
    Set<ComponentName> enabledServices = getEnabledServicesFromSettings(getActivity());

    final boolean accessibilityEnabled =
        Settings.Secure.getInt(getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED, 0) == 1;

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

      PreferenceScreen preference = getPreferenceManager().createPreferenceScreen(getActivity());
      String title = info.getResolveInfo().loadLabel(getPackageManager()).toString();

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

      preference.setKey(componentName.flattenToString());

      preference.setTitle(title);
      final boolean serviceEnabled =
          accessibilityEnabled && enabledServices.contains(componentName);
      if (serviceEnabled) {
        preference.setSummary(getString(R.string.accessibility_service_state_on));
      } else {
        preference.setSummary(getString(R.string.accessibility_service_state_off));
      }

      preference.setOrder(i);
      preference.setFragment(ToggleAccessibilityServiceFragment.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.getDescription();
      if (TextUtils.isEmpty(description)) {
        description = getString(R.string.accessibility_service_default_description);
      }
      extras.putString(EXTRA_SUMMARY, description);

      CharSequence applicationLabel = info.getResolveInfo().loadLabel(getPackageManager());

      extras.putString(
          EXTRA_ENABLE_WARNING_TITLE,
          getString(R.string.accessibility_service_security_warning_title, applicationLabel));
      extras.putString(
          EXTRA_ENABLE_WARNING_MESSAGE,
          getString(R.string.accessibility_service_security_warning_summary, applicationLabel));

      extras.putString(
          EXTRA_DISABLE_WARNING_TITLE,
          getString(R.string.accessibility_service_disable_warning_title, applicationLabel));
      extras.putString(
          EXTRA_DISABLE_WARNING_MESSAGE,
          getString(R.string.accessibility_service_disable_warning_summary, applicationLabel));

      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(info.getResolveInfo().serviceInfo.packageName, settingsClassName)
                .flattenToString());
      }

      mServicesCategory.addPreference(preference);
    }

    if (mServicesCategory.getPreferenceCount() == 0) {
      if (mNoServicesMessagePreference == null) {
        mNoServicesMessagePreference =
            new Preference(getActivity()) {
              @Override
              protected void onBindView(View view) {
                super.onBindView(view);

                LinearLayout containerView =
                    (LinearLayout) view.findViewById(R.id.message_container);
                containerView.setGravity(Gravity.CENTER);

                TextView summaryView = (TextView) view.findViewById(R.id.summary);
                String title = getString(R.string.accessibility_no_services_installed);
                summaryView.setText(title);
              }
            };
        mNoServicesMessagePreference.setPersistent(false);
        mNoServicesMessagePreference.setLayoutResource(R.layout.text_description_preference);
        mNoServicesMessagePreference.setSelectable(false);
      }
      mServicesCategory.addPreference(mNoServicesMessagePreference);
    }
  }
Пример #4
0
  private void addPluginPreferences(PreferenceScreen screen) {
    Intent queryIntent = new Intent(AstridApiConstants.ACTION_SETTINGS);
    PackageManager pm = getPackageManager();
    List<ResolveInfo> resolveInfoList =
        pm.queryIntentActivities(queryIntent, PackageManager.GET_META_DATA);
    int length = resolveInfoList.size();
    LinkedHashMap<String, ArrayList<Preference>> categoryPreferences =
        new LinkedHashMap<String, ArrayList<Preference>>();

    // Loop through a list of all packages (including plugins, addons)
    // that have a settings action
    String labsTitle = getString(R.string.EPr_labs_header);
    for (int i = 0; i < length; i++) {
      ResolveInfo resolveInfo = resolveInfoList.get(i);
      final Intent intent = new Intent(AstridApiConstants.ACTION_SETTINGS);
      intent.setClassName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);

      if (MilkPreferences.class.getName().equals(resolveInfo.activityInfo.name)
          && !MilkUtilities.INSTANCE.isLoggedIn()) continue;

      if (GtasksPreferences.class.getName().equals(resolveInfo.activityInfo.name)
          && AmazonMarketStrategy.isKindleFire()) continue;

      if (ProducteevPreferences.class.getName().equals(resolveInfo.activityInfo.name)
          && !Preferences.getBoolean(R.string.p_third_party_addons, false)
          && !ProducteevUtilities.INSTANCE.isLoggedIn()) continue;

      Preference preference = new Preference(this);
      preference.setTitle(resolveInfo.activityInfo.loadLabel(pm));
      if (labsTitle.equals(preference.getTitle())) preference.setSummary(R.string.EPr_labs_desc);
      try {
        Class<?> intentComponent = Class.forName(intent.getComponent().getClassName());
        if (intentComponent.getSuperclass().equals(SyncProviderPreferences.class))
          intentComponent = SyncProviderPreferences.class;
        if (PREFERENCE_REQUEST_CODES.containsKey(intentComponent)) {
          final int code = PREFERENCE_REQUEST_CODES.get(intentComponent);
          preference.setOnPreferenceClickListener(
              new OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference pref) {
                  startActivityForResult(intent, code);
                  return true;
                }
              });
        } else {
          preference.setIntent(intent);
        }
      } catch (ClassNotFoundException e) {
        preference.setIntent(intent);
      }

      String category = MetadataHelper.resolveActivityCategoryName(resolveInfo, pm);

      if (!categoryPreferences.containsKey(category))
        categoryPreferences.put(category, new ArrayList<Preference>());
      ArrayList<Preference> arrayList = categoryPreferences.get(category);
      arrayList.add(preference);
    }

    for (Entry<String, ArrayList<Preference>> entry : categoryPreferences.entrySet()) {
      Preference header = new Preference(this);
      header.setLayoutResource(android.R.layout.preference_category);
      header.setTitle(entry.getKey());
      screen.addPreference(header);

      for (Preference preference : entry.getValue()) screen.addPreference(preference);
    }
  }
  private void updateServicesPreferences() {
    // 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.
    mServicesCategory.removeAll();

    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;

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

      PreferenceScreen preference = getPreferenceManager().createPreferenceScreen(getActivity());
      String title = info.getResolveInfo().loadLabel(getPackageManager()).toString();

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

      preference.setKey(componentName.flattenToString());

      preference.setTitle(title);
      final boolean serviceEnabled =
          accessibilityEnabled && enabledServices.contains(componentName);
      String serviceEnabledString;
      if (serviceEnabled) {
        serviceEnabledString = getString(R.string.accessibility_feature_state_on);
      } else {
        serviceEnabledString = getString(R.string.accessibility_feature_state_off);
      }

      // Disable all accessibility services that are not permitted.
      String packageName = serviceInfo.packageName;
      boolean serviceAllowed = permittedServices == null || permittedServices.contains(packageName);
      preference.setEnabled(serviceAllowed || serviceEnabled);

      String summaryString;
      if (serviceAllowed) {
        summaryString = serviceEnabledString;
      } else {
        summaryString = getString(R.string.accessibility_feature_or_input_method_not_allowed);
      }
      preference.setSummary(summaryString);

      preference.setOrder(i);
      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(info.getResolveInfo().serviceInfo.packageName, settingsClassName)
                .flattenToString());
      }

      extras.putParcelable(EXTRA_COMPONENT_NAME, componentName);

      mServicesCategory.addPreference(preference);
    }

    if (mServicesCategory.getPreferenceCount() == 0) {
      if (mNoServicesMessagePreference == null) {
        mNoServicesMessagePreference = new Preference(getActivity());
        mNoServicesMessagePreference.setPersistent(false);
        mNoServicesMessagePreference.setLayoutResource(R.layout.text_description_preference);
        mNoServicesMessagePreference.setSelectable(false);
        mNoServicesMessagePreference.setSummary(
            getString(R.string.accessibility_no_services_installed));
      }
      mServicesCategory.addPreference(mNoServicesMessagePreference);
    }
  }
Пример #6
0
 private void markFieldValid(Preference field) {
   field.setLayoutResource(R.layout.valid_preference_row);
 }