/talkback_preics/src/com/google/android/marvin/talkback/Utils.java

http://eyes-free.googlecode.com/ · Java · 157 lines · 84 code · 20 blank · 53 comment · 17 complexity · 14a37b3ec475bd9953a0b7cb5e19caa0 MD5 · raw file

  1. /*
  2. * Copyright (C) 2010 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of 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,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.google.android.marvin.talkback;
  17. import android.app.ActivityManager;
  18. import android.app.Service;
  19. import android.content.Context;
  20. import android.content.pm.PackageInfo;
  21. import android.content.pm.PackageManager;
  22. import android.content.pm.PackageManager.NameNotFoundException;
  23. import android.util.Log;
  24. import android.view.accessibility.AccessibilityEvent;
  25. import android.widget.CompoundButton;
  26. import java.util.List;
  27. /**
  28. * This class contains utility methods.
  29. *
  30. * @author svetoslavganov@google.com (Svetoslav R. Ganov)
  31. */
  32. public class Utils {
  33. /** Tag for logging. */
  34. private static final String LOG_TAG = Utils.class.getSimpleName();
  35. /** Invalid version code for a package. */
  36. public static final int INVALID_VERSION_CODE = -1;
  37. /** String constant. */
  38. private static final String SPACE = " ";
  39. // this is need as a workaround for the adding of CompoundButton state by
  40. // the framework
  41. private static String sValueChecked;
  42. private static String sValueNotChecked;
  43. /**
  44. * @return The name of the current {@link android.app.Activity} given
  45. * through the current <code>context</code>.
  46. */
  47. public static String getCurrentActivityName(Context context) {
  48. ActivityManager activityManager = (ActivityManager) context
  49. .getSystemService(Service.ACTIVITY_SERVICE);
  50. List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(1);
  51. if (!tasks.isEmpty()) {
  52. return tasks.get(0).topActivity.getClassName();
  53. }
  54. return null;
  55. }
  56. /**
  57. * @return The package version code or {@link #INVALID_VERSION_CODE}
  58. * if the package does not exist.
  59. */
  60. public static int getVersionCode(Context context, String packageName) {
  61. PackageManager packageManager = context.getPackageManager();
  62. try {
  63. PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
  64. return packageInfo.versionCode;
  65. } catch (NameNotFoundException e) {
  66. Log.e(LOG_TAG, "Could not find package: " + packageName, e);
  67. return INVALID_VERSION_CODE;
  68. }
  69. }
  70. /**
  71. * @return The package version name or <code>null</code> if the package
  72. * does not exist.
  73. */
  74. public static String getVersionName(Context context, String packageName) {
  75. PackageManager packageManager = context.getPackageManager();
  76. try {
  77. PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
  78. return packageInfo.versionName;
  79. } catch (NameNotFoundException e) {
  80. Log.e(LOG_TAG, "Could not find package: " + packageName, e);
  81. return null;
  82. }
  83. }
  84. /**
  85. * This is a workaround for the addition of checked/not checked message
  86. * generated by the framework which should actually happen the the
  87. * accessibility service.
  88. */
  89. private static void ensureCompoundButtonWorkaround() {
  90. if (sValueChecked == null || sValueNotChecked == null) {
  91. Context context = TalkBackService.asContext();
  92. sValueChecked = context.getString(R.string.value_checked);
  93. sValueNotChecked = context.getString(R.string.value_not_checked);
  94. }
  95. }
  96. /**
  97. * Gets the text of an <code>event</code> by concatenating the text members
  98. * (regardless of their priority) using space as a delimiter.
  99. *
  100. * @param context The context from which to load required resources.
  101. * @param event The event.
  102. * @return The event text.
  103. */
  104. public static StringBuilder getEventText(Context context, AccessibilityEvent event) {
  105. ensureCompoundButtonWorkaround();
  106. StringBuilder aggregator = new StringBuilder();
  107. List<CharSequence> eventText = event.getText();
  108. if (context == null) {
  109. String s = "";
  110. }
  111. Class<?> eventClass = ClassLoadingManager.getInstance().loadOrGetCachedClass(context,
  112. event.getClassName().toString(), event.getPackageName().toString());
  113. // here we have a special case since the framework is adding
  114. // the string for the state of a CompoundButton but we also get the
  115. // isChecked attribute
  116. int stateStringIndex = -1;
  117. if (eventClass != null && CompoundButton.class.isAssignableFrom(eventClass)) {
  118. for (int i = 0, count = eventText.size(); i < count; i++) {
  119. CharSequence next = eventText.get(i);
  120. if (sValueChecked.equals(next) || sValueNotChecked.equals(next)) {
  121. stateStringIndex = i;
  122. break;
  123. }
  124. }
  125. }
  126. for (int i = 0, count = eventText.size(); i < count; i++) {
  127. if (i != stateStringIndex) {
  128. aggregator.append(eventText.get(i));
  129. aggregator.append(SPACE);
  130. }
  131. }
  132. if (aggregator.length() > 0) {
  133. aggregator.deleteCharAt(aggregator.length() - 1);
  134. }
  135. return aggregator;
  136. }
  137. }