/accessibilityPreferences/src/com/marvin/preferences/AccessibilityServicesListActivity.java

http://eyes-free.googlecode.com/ · Java · 146 lines · 84 code · 16 blank · 46 comment · 6 complexity · 653dc29ac41ad48463afcfd16401a9cc MD5 · raw file

  1. /*
  2. * Copyright 2010 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.marvin.preferences;
  17. import android.app.AlertDialog;
  18. import android.app.ListActivity;
  19. import android.content.ActivityNotFoundException;
  20. import android.content.DialogInterface;
  21. import android.content.Intent;
  22. import android.content.pm.PackageManager;
  23. import android.content.pm.ResolveInfo;
  24. import android.view.View;
  25. import android.widget.ArrayAdapter;
  26. import android.widget.ListView;
  27. import java.util.ArrayList;
  28. import java.util.List;
  29. /**
  30. * The main activity for the Accessibility Settings Manager. This class
  31. * essentially acts as a launcher for the preference activities for
  32. * accessibility services installed on the device. An activity that wishes to be
  33. * listed here should declare an intent filter with both of the following.
  34. * action: android.intent.action.MAIN category:
  35. * android.accessibilityservice.SERVICE_SETTINGS
  36. *
  37. * @author caseyburkhardt@google.com (Casey Burkhardt)
  38. */
  39. public class AccessibilityServicesListActivity extends ListActivity {
  40. public static final String ACTIVITY_ACCESSIBILITY_SERVICE_SETTINGS =
  41. "android.accessibilityservice.SERVICE_SETTINGS";
  42. private ArrayAdapter<AccessibilityServiceResolveInfo> mAppListAdapter;
  43. private List<AccessibilityServiceResolveInfo> mAppList;
  44. private PackageManager mPackageManager;
  45. private AccessibilityServicesListActivity self = this;
  46. @Override
  47. public void onResume() {
  48. super.onResume();
  49. mPackageManager = getPackageManager();
  50. mAppList = getCompatibleServicesList();
  51. mAppListAdapter = new ArrayAdapter<AccessibilityServiceResolveInfo>(
  52. this, R.layout.list_layout, R.id.list_item, mAppList);
  53. setListAdapter(mAppListAdapter);
  54. }
  55. @Override
  56. protected void onListItemClick(ListView l, View v, int position, long id) {
  57. AccessibilityServiceResolveInfo info = mAppListAdapter.getItem((int) id);
  58. Intent launchIntent = new Intent();
  59. launchIntent.setFlags(
  60. Intent.FLAG_ACTIVITY_NEW_TASK + Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
  61. launchIntent.setClassName(
  62. info.mResolveInfo.activityInfo.packageName, info.mResolveInfo.activityInfo.name);
  63. try {
  64. startActivity(launchIntent);
  65. } catch (ActivityNotFoundException e) {
  66. // If for some reason the activity can't be resolved, display an
  67. // error.
  68. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  69. builder.setMessage(getString(R.string.launch_failed_message)).setCancelable(true)
  70. .setPositiveButton(
  71. getString(R.string.ok_button), new DialogInterface.OnClickListener() {
  72. public void onClick(DialogInterface dialog, int id) {
  73. dialog.dismiss();
  74. }
  75. }).create().show();
  76. }
  77. }
  78. /**
  79. * Creates a List of ResolveInfo objects based on activities responding to
  80. * the following intent filter. action: android.intent.action.MAIN category:
  81. * android.accessibilityservice.SERVICE_SETTINGS
  82. *
  83. *@return an ArrayList containing ResolveInfo objects for each
  84. * accessibility service that has defined a settings activity.
  85. */
  86. private List<AccessibilityServiceResolveInfo> getCompatibleServicesList() {
  87. Intent targetIntent = new Intent(Intent.ACTION_MAIN);
  88. targetIntent.addCategory(ACTIVITY_ACCESSIBILITY_SERVICE_SETTINGS);
  89. return convertResolveInfoList(
  90. mPackageManager.queryIntentActivities(targetIntent, 0));
  91. }
  92. /**
  93. * Converts a given list of ResolveInfo objects to AccessibilityServiceInfo
  94. * objects
  95. *
  96. * @param list a list of ResolveInfo objects
  97. * @return a list of AccessibilityServiceObjects
  98. */
  99. public List<AccessibilityServiceResolveInfo> convertResolveInfoList(
  100. List<ResolveInfo> list) {
  101. ArrayList<AccessibilityServiceResolveInfo> result = new ArrayList<AccessibilityServiceResolveInfo>();
  102. for (ResolveInfo info : list) {
  103. result.add(new AccessibilityServiceResolveInfo(info));
  104. }
  105. return result;
  106. }
  107. /**
  108. * A simple wrapper of the ResolveInfo class used to override toString so our
  109. * list items appear correctly when using an ArrayAdapter.
  110. */
  111. private class AccessibilityServiceResolveInfo {
  112. private ResolveInfo mResolveInfo;
  113. public AccessibilityServiceResolveInfo(ResolveInfo info) {
  114. mResolveInfo = info;
  115. }
  116. @Override
  117. public String toString() {
  118. String result = mResolveInfo
  119. .loadLabel(self.getPackageManager()).toString();
  120. if (result.length() == 0) {
  121. if (mResolveInfo.activityInfo.labelRes != 0) {
  122. result = getString(mResolveInfo.activityInfo.labelRes);
  123. } else {
  124. result = mResolveInfo.activityInfo.name.toString();
  125. }
  126. }
  127. return String.format(self.getString(R.string.template_list_item_text), result);
  128. }
  129. }
  130. }