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