PageRenderTime 18ms CodeModel.GetById 2ms app.highlight 12ms RepoModel.GetById 2ms app.codeStats 0ms

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