@Override public Uri insert(Uri uri, ContentValues initialValues) { if (sUriMatcher.match(uri) != DEVICES) { throw new IllegalArgumentException("Unknown URI " + uri); } ContentValues values; if (initialValues != null) { values = new ContentValues(initialValues); } else { values = new ContentValues(); } String json = DeviceProcessor.toJSON(values); String response = RESTMethod.post(URL + "/devices/create", json); if (!"".equals(response)) { values = DeviceProcessor.parseJSON(response); } SQLiteDatabase db = dbHelper.getWritableDatabase(); long rowId = db.insert(DEVICES_TABLE_NAME, Devices.LOCATION, values); if (rowId > 0) { Uri noteUri = ContentUris.withAppendedId(Devices.CONTENT_URI, rowId); getContext().getContentResolver().notifyChange(noteUri, null); return noteUri; } throw new SQLException("Failed to insert row into " + uri); }
@Override public void onCreate(SQLiteDatabase db) { db.execSQL(DATABASE_CREATE); String result = RESTMethod.get(URL + "/devices.json"); ArrayList<ContentValues> devices = DeviceProcessor.parseJSONArray(result); if (devices == null) return; // server has no data Iterator<ContentValues> it = devices.iterator(); do { String sql = getInsertSQL(it.next()); db.execSQL(sql); } while (it.hasNext()); }
@Override public int update(Uri uri, ContentValues values, String where, String[] whereArgs) { SQLiteDatabase db = dbHelper.getWritableDatabase(); int count; String json = DeviceProcessor.toJSONParameters(values); int restId = getRestId(uri, where); // String response = RESTMethod.put(URL+"/devices/" + restId, json); RESTMethod.get(URL + "/devices/" + restId + "/power"); /* if(!"".equals(response)){ values = DeviceProcessor.parseJSON(response); } */ switch (sUriMatcher.match(uri)) { case DEVICES: count = db.update(DEVICES_TABLE_NAME, values, where, whereArgs); break; default: throw new IllegalArgumentException("Unknown URI " + uri); } getContext().getContentResolver().notifyChange(uri, null); return count; }