Created
October 18, 2016 14:51
-
-
Save uchidev/604fbb7da5021f70d2706fac10f96bc3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class TargetProvider extends ContentProvider { | |
| final private String tag; | |
| final private String providerName; | |
| final private String dbName; | |
| final private String tableName; | |
| private SQLiteOpenHelper dbHelper = null; | |
| private UriMatcher uriMatcher = null; | |
| public TargetProvider(String providerName, String dbName, String tableName, String tag) { | |
| super(); | |
| this.providerName = providerName; | |
| this.dbName = dbName; | |
| this.tableName = tableName; | |
| this.tag = tag; | |
| } | |
| @Override | |
| public boolean onCreate() { | |
| Log.d(tag, "PROVIDER CREATE"); | |
| dbHelper = new TargetDBHelper(getContext(), dbName, TargetTable.Version, tag); | |
| uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); | |
| uriMatcher.addURI(providerName, "main", 1); | |
| uriMatcher.addURI(providerName, "main/#", 2); | |
| return false; | |
| } | |
| @Override | |
| public Cursor query(@NonNull Uri uri, String[] columns, | |
| String selection, String[] selectionArgs, String orderBy) { | |
| if (uriMatcher.match(uri) == 2) { | |
| long id; | |
| List<String> result = uri.getPathSegments(); | |
| if (result != null) { | |
| id = Long.parseLong(result.get(1)); | |
| selection = BaseColumns._ID + "=" + Long.toString(id) + | |
| (selection == null ? "" : "AND (" + selection + ")"); | |
| } | |
| } | |
| SQLiteDatabase db = dbHelper.getReadableDatabase(); | |
| if (db == null) { | |
| return null; | |
| } else { | |
| Cursor cursor = db.query(tableName, columns, selection, selectionArgs, null, null, orderBy); | |
| Log.v(tag, "query result=" + cursor.getCount()); | |
| Context context = getContext(); | |
| if (context != null) { | |
| cursor.setNotificationUri(context.getContentResolver(), uri); | |
| } | |
| return cursor; | |
| } | |
| } | |
| @Override | |
| public String getType(@NonNull Uri uri) { | |
| return null; | |
| } | |
| @Override | |
| public Uri insert(@NonNull Uri uri, ContentValues values) { | |
| SQLiteDatabase db = dbHelper.getWritableDatabase(); | |
| if (db == null) { | |
| return null; | |
| } else { | |
| long newId = db.insert(tableName, null, values); | |
| final Uri appendedUri = Uri.parse(uri + "/" + newId); | |
| Log.v(tag, "inserted id=" + newId); | |
| Context context = getContext(); | |
| if (context != null) { | |
| context.getContentResolver().notifyChange(appendedUri, null); | |
| } | |
| return appendedUri; | |
| } | |
| } | |
| @Override | |
| public int delete(@NonNull Uri uri, String selection, String[] selectionArgs) { | |
| SQLiteDatabase db = dbHelper.getWritableDatabase(); | |
| if (db == null) { | |
| return 0; | |
| } else { | |
| final int count = db.delete(tableName, selection, selectionArgs); | |
| Log.v(tag, "deleted count=" + count); | |
| Context context = getContext(); | |
| if (context != null) { | |
| context.getContentResolver().notifyChange(uri, null); | |
| } | |
| return count; | |
| } | |
| } | |
| @Override | |
| public int update(@NonNull Uri uri, ContentValues values, String selection, String[] selectionArgs) { | |
| SQLiteDatabase db = dbHelper.getWritableDatabase(); | |
| if (db == null) { | |
| return 0; | |
| } else { | |
| final int count = db.update(tableName, values, selection, selectionArgs); | |
| Log.v(tag, "updated count=" + count); | |
| Context context = getContext(); | |
| if (context != null) { | |
| context.getContentResolver().notifyChange(uri, null); | |
| } | |
| return count; | |
| } | |
| } | |
| @Override | |
| public ParcelFileDescriptor openFile (@NonNull Uri uri, @NonNull String mode) throws FileNotFoundException { | |
| return openFileHelper(uri, mode); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment