/actionslib/src/com/google/android/marvin/commands/UtilityCommandsBroadcastReceiver.java

http://eyes-free.googlecode.com/ · Java · 92 lines · 66 code · 16 blank · 10 comment · 8 complexity · 744319073e717c7ea9a2f9165baa53e7 MD5 · raw file

  1. // Copyright 2010 Google Inc. All Rights Reserved.
  2. package com.google.android.marvin.commands;
  3. import com.google.android.marvin.commands.impls.BatteryLevelCommand;
  4. import com.google.android.marvin.commands.impls.ConnectivityCommand;
  5. import com.google.android.marvin.commands.impls.TimeAndDateCommand;
  6. import android.app.Service;
  7. import android.content.BroadcastReceiver;
  8. import android.content.Context;
  9. import android.content.Intent;
  10. import android.util.Log;
  11. import android.view.accessibility.AccessibilityEvent;
  12. import android.view.accessibility.AccessibilityManager;
  13. import java.util.Date;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. /**
  17. * The broadcast receiver that will process the commands defined in {@link UtilityCommands}.
  18. *
  19. * @author clsimon@google.com (Cheryl Simon)
  20. *
  21. */
  22. public class UtilityCommandsBroadcastReceiver extends BroadcastReceiver {
  23. private static final String TAG = "UserEventBroadcastReceiver";
  24. private Map<String, UtilityCommands> mIdToUserCommands;
  25. private Map<String, CommandExecutor> mIdToExecutor;
  26. private AccessibilityManager mAccessibilityManager;
  27. public UtilityCommandsBroadcastReceiver() {
  28. mIdToUserCommands = new HashMap<String, UtilityCommands>();
  29. for (UtilityCommands command : UtilityCommands.values()) {
  30. mIdToUserCommands.put(command.getDisplayName(), command);
  31. }
  32. }
  33. @Override
  34. public void onReceive(Context context, Intent intent) {
  35. if (mAccessibilityManager == null){
  36. mAccessibilityManager =
  37. (AccessibilityManager)context.getSystemService(Service.ACCESSIBILITY_SERVICE);
  38. }
  39. if (!mAccessibilityManager.isEnabled()) {
  40. return;
  41. }
  42. if (mIdToExecutor == null) {
  43. Log.e(TAG, "Creating new command handlers");
  44. mIdToExecutor = new HashMap<String, CommandExecutor>();
  45. mIdToExecutor.put(
  46. UtilityCommands.BATTERY.getDisplayName(),
  47. new BatteryLevelCommand());
  48. mIdToExecutor.put(
  49. UtilityCommands.TIME.getDisplayName(),
  50. new TimeAndDateCommand());
  51. // mIdToExecutor.put(
  52. // UtilityCommands.LOCATION.getDisplayName(),
  53. // new LocationCommand());
  54. mIdToExecutor.put(
  55. UtilityCommands.CONNECTIVITY.getDisplayName(),
  56. new ConnectivityCommand());
  57. }
  58. String userEventName = intent.getStringExtra(CommandConstants.EXTRA_COMMAND_NAME);
  59. Log.i(TAG, "got event " + userEventName);
  60. CommandExecutor command = mIdToExecutor.get(userEventName);
  61. if (command == null) {
  62. Log.e(TAG, "Unimplemented command " + userEventName);
  63. }
  64. String text = command.executeCommand(context);
  65. speakText(context, text);
  66. }
  67. private void speakText(Context context, String text) {
  68. Log.d(TAG, text);
  69. AccessibilityEvent event = AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
  70. event.setClassName(getClass().getCanonicalName());
  71. event.setPackageName(getClass().getPackage().getName());
  72. event.setEventTime(new Date().getTime());
  73. event.getText().add(text);
  74. mAccessibilityManager.sendAccessibilityEvent(event);
  75. }
  76. }