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

/actionslib/src/com/google/android/marvin/commands/providers/CommandsContentProvider.java

http://eyes-free.googlecode.com/
Java | 221 lines | 183 code | 29 blank | 9 comment | 11 complexity | 0d2d50f2141429f3ae348ad878c387f1 MD5 | raw file
  1// Copyright 2010 Google Inc. All Rights Reserved.
  2
  3package com.google.android.marvin.commands.providers;
  4
  5import com.google.android.marvin.commands.CommandsManager;
  6import com.google.android.marvin.commands.UtilityCommands;
  7
  8import android.content.ContentProvider;
  9import android.content.ContentUris;
 10import android.content.ContentValues;
 11import android.content.Context;
 12import android.content.UriMatcher;
 13import android.database.Cursor;
 14import android.database.SQLException;
 15import android.database.sqlite.SQLiteDatabase;
 16import android.database.sqlite.SQLiteOpenHelper;
 17import android.database.sqlite.SQLiteQueryBuilder;
 18import android.net.Uri;
 19import android.util.Log;
 20
 21import java.util.HashMap;
 22import java.util.Map;
 23
 24/**
 25 * @author clsimon@google.com (Cheryl Simon)
 26 *
 27 */
 28public class CommandsContentProvider extends ContentProvider {
 29    
 30
 31    // debugging support
 32    private static final String TAG = "CommandsContentProvider";
 33    
 34    
 35    private static final String DATABASE_NAME = "commands.db";
 36    private static final String COMMANDS_TABLE = "commands";
 37    
 38   
 39    private static final int COMMANDS = 1;
 40    
 41    private static final UriMatcher sUriMatcher;
 42    private static final Map<String, String> sProjectionMap;
 43    
 44    static {
 45        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
 46        sUriMatcher.addURI(CommandsManager.AUTHORITY, COMMANDS_TABLE, COMMANDS);
 47        
 48        sProjectionMap = new HashMap<String, String>();
 49        sProjectionMap.put(CommandsManager.NAME_COLUMN, CommandsManager.NAME_COLUMN);
 50        sProjectionMap.put(CommandsManager.MODIFIER_KEY_COLUMN, 
 51                CommandsManager.MODIFIER_KEY_COLUMN);
 52        sProjectionMap.put(CommandsManager.KEY_CODE_COLUMN, CommandsManager.KEY_CODE_COLUMN);
 53        sProjectionMap.put(CommandsManager.ACTION_COLUMN, CommandsManager.ACTION_COLUMN);
 54    }
 55    
 56    private static class DatabaseHelper extends SQLiteOpenHelper {
 57        
 58        public DatabaseHelper(Context context) {
 59            super(context, DATABASE_NAME, null, 1);
 60        }
 61        
 62        @Override
 63        public void onCreate(SQLiteDatabase db) {
 64            StringBuilder builder = new StringBuilder();
 65            builder.append("CREATE TABLE ");
 66            builder.append(COMMANDS_TABLE);
 67            builder.append(" (");
 68            builder.append("_id INTEGER PRIMARY KEY AUTOINCREMENT");
 69            builder.append(", ");
 70            builder.append(CommandsManager.NAME_COLUMN);
 71            builder.append(" TEXT"); 
 72            builder.append(", ");
 73            builder.append(CommandsManager.MODIFIER_KEY_COLUMN);
 74            builder.append(" INTEGER");
 75            builder.append(", ");
 76            builder.append(CommandsManager.KEY_CODE_COLUMN);
 77            builder.append(" INTEGER");
 78            builder.append(", ");
 79            builder.append(CommandsManager.ACTION_COLUMN);
 80            builder.append(" TEXT)");
 81            db.execSQL(builder.toString());
 82            
 83            initContent(db);
 84        }
 85
 86        @Override
 87        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
 88            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
 89                    + newVersion + ", which will destroy all old data");
 90            db.execSQL("DROP TABLE IF EXISTS " + COMMANDS_TABLE);
 91            onCreate(db);
 92        }
 93        
 94        /**
 95         * Initialize the database with the command entries from UserCommands.
 96         */
 97        private void initContent(SQLiteDatabase db) {
 98            for (UtilityCommands command : UtilityCommands.values()) {
 99                ContentValues values = new ContentValues();
100                values.put(CommandsManager.NAME_COLUMN, command.getDisplayName());
101                values.put(CommandsManager.MODIFIER_KEY_COLUMN, command.getModifier());
102                values.put(CommandsManager.KEY_CODE_COLUMN, command.getKeyCode());
103                values.put(CommandsManager.ACTION_COLUMN, command.getAction());
104                db.insert(COMMANDS_TABLE, CommandsManager.NAME_COLUMN, values);
105            }
106        }
107    }
108    
109    private DatabaseHelper mDatabaseHelper;
110
111    @Override
112    public int delete(Uri uri, String selection, String[] selectionArgs) {
113        try {
114            SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
115            int count = 0;
116            switch (sUriMatcher.match(uri)) {
117                case COMMANDS:
118                    count = db.delete(COMMANDS_TABLE, selection, selectionArgs);
119                    break;
120                default:
121                    throw new IllegalArgumentException("Unknown Uri " + uri);
122            }
123            getContext().getContentResolver().notifyChange(uri, null);
124            return count; 
125        } catch (SQLException e) {
126            Log.e(TAG, "Failed to access database.", e);
127            return 0;
128        }
129    }
130
131    @Override
132    public String getType(Uri uri) {
133        switch (sUriMatcher.match(uri)) {
134            case COMMANDS:
135                return "vnd.android.cursor.item/vnd.google.commands";
136            default:
137                throw new IllegalArgumentException("Unknown Uri " + uri);
138        }
139    }
140
141    @Override
142    public Uri insert(Uri uri, ContentValues initialValues) {
143        try {
144            if (sUriMatcher.match(uri) != COMMANDS) { 
145                throw new IllegalArgumentException("Unknown URI " + uri); 
146            }
147            ContentValues values;
148            if (initialValues != null) {
149                values = new ContentValues(initialValues);
150            } else {
151                values = new ContentValues();
152            }
153     
154            SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
155            long rowId = db.insert(COMMANDS_TABLE, CommandsManager.NAME_COLUMN, values);
156            if (rowId > 0) {
157                Uri noteUri = ContentUris.withAppendedId(CommandsManager.CONTENT_URI, rowId);
158                getContext().getContentResolver().notifyChange(noteUri, null);
159                return noteUri;
160            }
161        } catch (SQLException e) {
162            Log.e(TAG, "Failed to insert row into " + uri, e);
163        }
164        return null;
165    }
166
167    @Override
168    public boolean onCreate() {
169        try {
170            mDatabaseHelper = new DatabaseHelper(getContext());
171        } catch (SQLException e) {
172            Log.e(TAG, "Failed to access database.", e);
173            return false;
174        }
175        return true;
176    }
177
178    @Override
179    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
180            String sortOrder) {
181        try {
182        SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
183            switch (sUriMatcher.match(uri)) {
184                case COMMANDS:
185                    builder.setTables(COMMANDS_TABLE);
186                    builder.setProjectionMap(sProjectionMap);
187                    break;
188                default:
189                    throw new IllegalArgumentException("Unknown Uri " + uri);
190            }
191            SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
192            Cursor c = builder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
193            c.setNotificationUri(getContext().getContentResolver(), uri);
194            return c;
195        } catch (SQLException e) {
196            Log.e(TAG, "Failed to access database.", e);
197            return null;
198        }
199    }
200
201    @Override
202    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
203        try {
204            SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
205            int count;
206            switch (sUriMatcher.match(uri)) {
207                case COMMANDS:
208                    count = db.update(COMMANDS_TABLE, values, selection, selectionArgs);
209                    break;
210                default:
211                    throw new IllegalArgumentException("Unknown Uri " + uri);
212            }
213            getContext().getContentResolver().notifyChange(uri, null);
214            return count;
215        } catch (SQLException e) {
216            Log.e(TAG, "Failed to access database.", e);
217            return 0;
218        }
219    }
220
221}