/commandslib/src/com/google/android/marvin/commands/CommandsManager.java

http://eyes-free.googlecode.com/ · Java · 132 lines · 91 code · 21 blank · 20 comment · 10 complexity · 15ae44d3afee68ad51ac5ed988744d1e MD5 · raw file

  1. // Copyright 2010 Google Inc. All Rights Reserved.
  2. package com.google.android.marvin.commands;
  3. import android.content.ContentResolver;
  4. import android.content.ContentValues;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.database.Cursor;
  8. import android.net.Uri;
  9. import android.util.Log;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. /**
  13. * A utility class for accessing the collection of available commands.
  14. *
  15. * @author clsimon@google.com (Cheryl Simon)
  16. *
  17. */
  18. public class CommandsManager {
  19. /**
  20. * The action for a broadcast that will announce changes to the available commands or command
  21. * shortcuts.
  22. */
  23. public static final String COMMAND_UPDATE_ACTION = "com.google.android.marvin.commands.UPDATE";
  24. public static final String NAME_COLUMN = "name";
  25. public static final String MODIFIER_KEY_COLUMN = "modifierKey";
  26. public static final String KEY_CODE_COLUMN = "keyCode";
  27. public static final String ACTION_COLUMN = "action";
  28. public static final String AUTHORITY =
  29. "com.google.android.marvin.commands.providers.CommandsContentProvider";
  30. public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/commands");
  31. private static final String TAG = "CommandsAccessor";
  32. /**
  33. * Get a list of all available commands.
  34. */
  35. public List<Command> getAvailableCommands(Context context) {
  36. List<Command> availableCommands = new ArrayList<Command>();
  37. ContentResolver resolver = context.getContentResolver();
  38. Cursor cursor = resolver.query(CONTENT_URI, null, null, null, null);
  39. if (cursor == null) {
  40. Log.d(TAG, "No commands content provider found.");
  41. return availableCommands;
  42. }
  43. if (cursor.moveToFirst()) {
  44. int nameColumn = cursor.getColumnIndex("name");
  45. int modifierKeyColumn = cursor.getColumnIndex("modifierKey");
  46. int keyCodeColumn = cursor.getColumnIndex("keyCode");
  47. int actionColumn = cursor.getColumnIndex("action");
  48. do {
  49. Command command = new CommandImpl(
  50. cursor.getString(nameColumn),
  51. cursor.getInt(modifierKeyColumn),
  52. cursor.getInt(keyCodeColumn),
  53. cursor.getString(actionColumn));
  54. availableCommands.add(command);
  55. } while (cursor.moveToNext());
  56. }
  57. cursor.close();
  58. Log.d(TAG, availableCommands.toString());
  59. return availableCommands;
  60. }
  61. /**
  62. * For each Command in {@code commands}, add to the content provider if it does not already
  63. * have it.
  64. * @param commands
  65. */
  66. public void addCommands(List<Command> commands, Context context) {
  67. for (Command command : commands) {
  68. addCommand(context, command, false);
  69. }
  70. }
  71. protected void addCommand(Context context, Command command, boolean update) {
  72. ContentResolver resolver = context.getContentResolver();
  73. String[] projection = new String[] { NAME_COLUMN };
  74. StringBuilder selection = new StringBuilder()
  75. .append(NAME_COLUMN)
  76. .append("=\"")
  77. .append(command.getDisplayName())
  78. .append("\" AND ")
  79. .append(ACTION_COLUMN)
  80. .append("=\"")
  81. .append(command.getAction())
  82. .append("\"");
  83. Cursor cursor = resolver.query(
  84. CONTENT_URI,
  85. projection,
  86. selection.toString(),
  87. null,
  88. null);
  89. if (cursor == null) {
  90. Log.d(TAG, "No commands content provider found.");
  91. return;
  92. }
  93. ContentValues values = new ContentValues();
  94. values.put(NAME_COLUMN, command.getDisplayName());
  95. values.put(MODIFIER_KEY_COLUMN, command.getModifier());
  96. values.put(KEY_CODE_COLUMN, command.getKeyCode());
  97. values.put(ACTION_COLUMN, command.getAction());
  98. if (!cursor.moveToNext()) {
  99. // need to add command
  100. resolver.insert(CONTENT_URI, values);
  101. } else if (update) {
  102. resolver.update(CONTENT_URI, values, selection.toString(), new String[] { });
  103. }
  104. cursor.close();
  105. }
  106. public void updateCommandShortcut(Context context, Command command) {
  107. addCommand(context, command, true);
  108. Intent updateIntent = new Intent(COMMAND_UPDATE_ACTION);
  109. context.sendBroadcast(updateIntent);
  110. }
  111. }