List of usage examples for android.content ContentResolver notifyChange
public void notifyChange(@NonNull Uri uri, @Nullable ContentObserver observer, @NotifyFlags int flags)
From source file:pl.selvin.android.syncframework.content.BaseContentProvider.java
@Override public Uri insert(Uri uri, ContentValues values) { final int code = contentHelper.matchUri(uri); if (code != UriMatcher.NO_MATCH) { if (code == ContentHelper.uriSyncCode) { throw new IllegalArgumentException("Can not insert with Sync Uri."); }/*from w w w . j ava 2 s . c o m*/ final TableInfo tab = contentHelper.getTableFromCode(code & ContentHelper.uriCode); if (tab.readonly) { throw new IllegalArgumentException("Table " + tab.name + " is readonly."); } if (isItemCode(code)) { throw new IllegalArgumentException("Can not delete with Item type Uri."); } /*-String tempId = UUID.randomUUID().toString(); if (tab.primaryKey.length == 1 && tab.primaryKey[0].type == ColumnType.guid) { tempId = values.getAsString(tab.primaryKey[0].name); }*/ boolean syncToNetwork = checkSyncToNetwork(uri); values.put("tempId", UUID.randomUUID().toString()); values.put("isDirty", 1); values.put("isDeleted", 0); long rowId = getWritableDatabase().insert(tab.name, null, values); if (rowId > 0) { Uri ret_uri = contentHelper.getItemUri(tab.name, syncToNetwork, rowId); final ContentResolver cr = getContext().getContentResolver(); cr.notifyChange(ret_uri, null, syncToNetwork); for (String n : tab.notifyUris) { cr.notifyChange(Uri.parse(n), null, syncToNetwork); } return ret_uri; } } throw new SQLException("Failed to insert row into " + uri); }
From source file:pl.selvin.android.syncframework.content.BaseContentProvider.java
@Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { final int code = contentHelper.matchUri(uri); if (code != UriMatcher.NO_MATCH) { if (code == ContentHelper.uriSyncCode) { if (DEBUG) { Log.d(TAG, "CP-update-sync: " + uri.toString()); }//from ww w . ja v a 2s.c o m return (Sync(uri.getPathSegments().get(1), uri.getPathSegments().get(2), selection) ? 1 : 0); } final TableInfo tab = contentHelper.getTableFromCode(code & ContentHelper.uriCode); if (tab.readonly) { throw new IllegalArgumentException("Table " + tab.name + " is readonly."); } if (isItemCode(code)) { if (isItemRowIDCode(code)) { selection = "isDeleted=0 AND ROWID=" + uri.getPathSegments().get(2) + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""); } else { selection = "isDeleted=0" + tab.getSelection() + (!TextUtils.isEmpty(selection) ? "(" + selection + ") AND " : ""); int i = 0; final String[] old = selectionArgs; final int len = (old == null) ? 0 : old.length; selectionArgs = new String[len + tab.primaryKey.length]; for (; i < tab.primaryKey.length; i++) { selectionArgs[i] = uri.getPathSegments().get(i); } if (len > 0) { for (; i < old.length; i++) { selectionArgs[i] = old[i - tab.primaryKey.length]; } } } } else { selection = "isDeleted=0" + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""); } boolean syncToNetwork = checkSyncToNetwork(uri); values.put("isDirty", 1); int ret = getWritableDatabase().update(tab.name, values, selection, selectionArgs); if (ret > 0) { final ContentResolver cr = getContext().getContentResolver(); cr.notifyChange(uri, null, syncToNetwork); for (String n : tab.notifyUris) { cr.notifyChange(Uri.parse(n), null, syncToNetwork); } } return ret; } throw new IllegalArgumentException("Unknown Uri " + uri); }
From source file:pl.selvin.android.syncframework.content.BaseContentProvider.java
@Override public int delete(Uri uri, String selection, String[] selectionArgs) { final int code = contentHelper.matchUri(uri); if (code != UriMatcher.NO_MATCH) { if (code == ContentHelper.uriClearCode) { if (DEBUG) { Log.d("delete", "uriClearCode"); }/* w w w. j a va 2 s . c o m*/ mDB.onUpgrade(getWritableDatabase(), 1, contentHelper.DATABASE_VERSION); return 0; } if (code == ContentHelper.uriSyncCode) { throw new IllegalArgumentException("Can not delete with Sync Uri."); } boolean syncToNetwork = checkSyncToNetwork(uri); final TableInfo tab = contentHelper.getTableFromCode(code & ContentHelper.uriCode); if (tab.readonly) { throw new IllegalArgumentException("Table " + tab.name + " is readonly."); } if (isItemCode(code)) { if (isItemRowIDCode(code)) { selection = "isDeleted=0 AND ROWID=" + uri.getPathSegments().get(2) + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""); } else { selection = "isDeleted=0" + tab.getSelection() + (!TextUtils.isEmpty(selection) ? "(" + selection + ") AND " : ""); int i = 0; final String[] old = selectionArgs; final int len = (old == null) ? 0 : old.length; selectionArgs = new String[len + tab.primaryKey.length]; for (; i < tab.primaryKey.length; i++) { selectionArgs[i] = uri.getPathSegments().get(i); } if (len > 0) { for (; i < old.length; i++) { selectionArgs[i] = old[i - tab.primaryKey.length]; } } } } else { selection = "isDeleted=0" + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""); } int ret = 0; int cascaderet = 0; if (tab.cascadeDelete != null) { for (CascadeInfo info : tab.cascadeDelete) { final Cursor c = query(contentHelper.getDirUri(tab.name), info.pk, selection, selectionArgs, null); if (c.moveToFirst()) { do { final String[] args = new String[info.pk.length]; final StringBuilder sb = new StringBuilder(); for (int i = 0; i < info.pk.length; i++) { if (i > 0) sb.append("AND "); sb.append(info.fk[i]); sb.append("=? "); args[i] = c.getString(i); } cascaderet += delete(contentHelper.getDirUri(info.table, syncToNetwork), sb.toString(), args); } while (c.moveToNext()); } c.close(); } } ContentValues values = new ContentValues(2); values.put("isDirty", 1); values.put("isDeleted", 1); ret = getWritableDatabase().update(tab.name, values, "tempId IS NULL AND " + selection, selectionArgs); ret += getWritableDatabase().delete(tab.name, "tempId IS NOT NULL AND " + selection, selectionArgs); if (ret > 0) { final ContentResolver cr = getContext().getContentResolver(); cr.notifyChange(uri, null, syncToNetwork); for (String n : tab.notifyUris) { cr.notifyChange(Uri.parse(n), null, syncToNetwork); } } return ret + cascaderet; } throw new IllegalArgumentException("Unknown Uri " + uri); }
From source file:pl.selvin.android.syncframework.content.BaseContentProvider.java
protected boolean Sync(String service, String scope, String params) { final Date start = new Date(); boolean hasError = false; if (params == null) params = ""; final SQLiteDatabase db = mDB.getWritableDatabase(); final ArrayList<TableInfo> notifyTableInfo = new ArrayList<TableInfo>(); final String download = String.format(contentHelper.DOWNLOAD_SERVICE_URI, service, scope, params); final String upload = String.format(contentHelper.UPLOAD_SERVICE_URI, service, scope, params); final String scopeServerBlob = String.format("%s.%s.%s", service, scope, _.serverBlob); String serverBlob = null;// ww w. j av a 2 s. c o m Cursor cur = db.query(BlobsTable.NAME, new String[] { BlobsTable.C_VALUE }, BlobsTable.C_NAME + "=?", new String[] { scopeServerBlob }, null, null, null); final String originalBlob; if (cur.moveToFirst()) { originalBlob = serverBlob = cur.getString(0); } else { originalBlob = null; } cur.close(); db.beginTransaction(); try { boolean nochanges = false; if (serverBlob != null) { nochanges = !contentHelper.hasDirtTable(db, scope); } boolean resolve = false; final Metadata meta = new Metadata(); final HashMap<String, Object> vals = new HashMap<String, Object>(); final ContentValues cv = new ContentValues(2); JsonFactory jsonFactory = new JsonFactory(); JsonToken current = null; String name = null; boolean moreChanges = false; boolean forceMoreChanges = false; do { final int requestMethod; final String serviceRequestUrl; final ContentProducer contentProducer; if (serverBlob != null) { requestMethod = HTTP_POST; if (nochanges) { serviceRequestUrl = download; } else { serviceRequestUrl = upload; forceMoreChanges = true; } contentProducer = new SyncContentProducer(jsonFactory, db, scope, serverBlob, !nochanges, notifyTableInfo, contentHelper); nochanges = true; } else { requestMethod = HTTP_GET; serviceRequestUrl = download; contentProducer = null; } if (moreChanges) { db.beginTransaction(); } Result result = executeRequest(requestMethod, serviceRequestUrl, contentProducer); if (result.getStatus() == HttpStatus.SC_OK) { final JsonParser jp = jsonFactory.createParser(result.getInputStream()); jp.nextToken(); // skip ("START_OBJECT(d) expected"); jp.nextToken(); // skip ("FIELD_NAME(d) expected"); if (jp.nextToken() != JsonToken.START_OBJECT) throw new Exception("START_OBJECT(d - object) expected"); while (jp.nextToken() != JsonToken.END_OBJECT) { name = jp.getCurrentName(); if (_.__sync.equals(name)) { current = jp.nextToken(); while (jp.nextToken() != JsonToken.END_OBJECT) { name = jp.getCurrentName(); current = jp.nextToken(); if (_.serverBlob.equals(name)) { serverBlob = jp.getText(); } else if (_.moreChangesAvailable.equals(name)) { moreChanges = jp.getBooleanValue() || forceMoreChanges; forceMoreChanges = false; } else if (_.resolveConflicts.equals(name)) { resolve = jp.getBooleanValue(); } } } else if (_.results.equals(name)) { if (jp.nextToken() != JsonToken.START_ARRAY) throw new Exception("START_ARRAY(results) expected"); while (jp.nextToken() != JsonToken.END_ARRAY) { meta.isDeleted = false; meta.tempId = null; vals.clear(); while (jp.nextToken() != JsonToken.END_OBJECT) { name = jp.getCurrentName(); current = jp.nextToken(); if (current == JsonToken.VALUE_STRING) { vals.put(name, jp.getText()); } else if (current == JsonToken.VALUE_NUMBER_INT) { vals.put(name, jp.getLongValue()); } else if (current == JsonToken.VALUE_NUMBER_FLOAT) { vals.put(name, jp.getDoubleValue()); } else if (current == JsonToken.VALUE_FALSE) { vals.put(name, 0L); } else if (current == JsonToken.VALUE_TRUE) { vals.put(name, 1L); } else if (current == JsonToken.VALUE_NULL) { vals.put(name, null); } else { if (current == JsonToken.START_OBJECT) { if (_.__metadata.equals(name)) { while (jp.nextToken() != JsonToken.END_OBJECT) { name = jp.getCurrentName(); jp.nextToken(); if (_.uri.equals(name)) { meta.uri = jp.getText(); } else if (_.type.equals(name)) { meta.type = jp.getText(); } else if (_.isDeleted.equals(name)) { meta.isDeleted = jp.getBooleanValue(); } else if (_.tempId.equals(name)) { meta.tempId = jp.getText(); } } } else if (_.__syncConflict.equals(name)) { while (jp.nextToken() != JsonToken.END_OBJECT) { name = jp.getCurrentName(); jp.nextToken(); if (_.isResolved.equals(name)) { } else if (_.conflictResolution.equals(name)) { } else if (_.conflictingChange.equals(name)) { while (jp.nextToken() != JsonToken.END_OBJECT) { name = jp.getCurrentName(); current = jp.nextToken(); if (current == JsonToken.START_OBJECT) { if (_.__metadata.equals(name)) { while (jp.nextToken() != JsonToken.END_OBJECT) { } } } } } } // resolve conf } else if (_.__syncError.equals(name)) { while (jp.nextToken() != JsonToken.END_OBJECT) { name = jp.getCurrentName(); jp.nextToken(); } } } } } TableInfo tab = contentHelper.getTableFromType(meta.type); if (meta.isDeleted) { tab.DeleteWithUri(meta.uri, db); } else { tab.SyncJSON(vals, meta, db); } if (!notifyTableInfo.contains(tab)) notifyTableInfo.add(tab); } } } jp.close(); if (!hasError) { cv.clear(); cv.put(BlobsTable.C_NAME, scopeServerBlob); cv.put(BlobsTable.C_VALUE, serverBlob); cv.put(BlobsTable.C_DATE, Calendar.getInstance().getTimeInMillis()); cv.put(BlobsTable.C_STATE, 0); db.replace(BlobsTable.NAME, null, cv); db.setTransactionSuccessful(); db.endTransaction(); if (DEBUG) { Log.d(TAG, "CP-Sync: commit changes"); } final ContentResolver cr = getContext().getContentResolver(); for (TableInfo t : notifyTableInfo) { final Uri nu = contentHelper.getDirUri(t.name, false); cr.notifyChange(nu, null, false); // false - do not force sync cause we are in sync if (DEBUG) { Log.d(TAG, "CP-Sync: notifyChange table: " + t.name + ", uri: " + nu); } for (String n : t.notifyUris) { cr.notifyChange(Uri.parse(n), null, false); if (DEBUG) { Log.d(TAG, "+uri: " + n); } } } notifyTableInfo.clear(); } } else { if (DEBUG) { Log.e(TAG, "Server error in fetching remote contacts: " + result.getStatus()); } hasError = true; break; } } while (moreChanges); } catch (final ConnectTimeoutException e) { hasError = true; if (DEBUG) { Log.e(TAG, "ConnectTimeoutException", e); } } catch (final IOException e) { hasError = true; if (DEBUG) { Log.e(TAG, Log.getStackTraceString(e)); } } catch (final ParseException e) { hasError = true; if (DEBUG) { Log.e(TAG, "ParseException", e); } } catch (final Exception e) { hasError = true; if (DEBUG) { Log.e(TAG, "ParseException", e); } } if (hasError) { db.endTransaction(); ContentValues cv = new ContentValues(); cv.put(BlobsTable.C_NAME, scopeServerBlob); cv.put(BlobsTable.C_VALUE, originalBlob); cv.put(BlobsTable.C_DATE, Calendar.getInstance().getTimeInMillis()); cv.put(BlobsTable.C_STATE, -1); db.replace(BlobsTable.NAME, null, cv); } /*-if (!hasError) { final ContentValues cv = new ContentValues(2); cv.put(BlobsTable.C_NAME, scopeServerBlob); cv.put(BlobsTable.C_VALUE, serverBlob); db.replace(BlobsTable.NAME, null, cv); db.setTransactionSuccessful(); } db.endTransaction(); if (!hasError) { for (String t : notifyTableInfo) { getContext().getContentResolver().notifyChange(getDirUri(t), null); } }*/ if (DEBUG) { Helpers.LogInfo(start); } return !hasError; }