Last active
April 17, 2016 16:09
-
-
Save Evin1-/42d61dabe35e90e97cf10205fff08a4b to your computer and use it in GitHub Desktop.
Content Provider simple app.
This file contains 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
<provider | |
android:name=".provider.SimpleProvider" | |
android:authorities="com.example.simplecontentprovider" | |
android:enabled="true" | |
android:exported="false"> | |
</provider> |
This file contains 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 DatabaseContract { | |
public static final String PATH_USERS = "users"; | |
public static final String PATH_COMPANIES = "companies"; | |
public static class UsersEntry implements BaseColumns { | |
public static final Uri CONTENT_URI = SimpleProvider.BASE_URI | |
.buildUpon() | |
.appendPath(PATH_USERS) | |
.build(); | |
public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + | |
"/" + SimpleProvider.AUTHORITY + | |
"/" + PATH_USERS; | |
public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + | |
"/" + SimpleProvider.AUTHORITY + | |
"/" + PATH_USERS; | |
public static final String TABLE_NAME = "users"; | |
public static final String KEY_NAME = "name"; | |
public static final String KEY_AGE = "age"; | |
public static final String CREATE_TABLE_QUERY = "CREATE TABLE " + TABLE_NAME + | |
"(" + | |
_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + | |
KEY_NAME + " TEXT," + | |
KEY_AGE + " INTEGER" + | |
")"; | |
} | |
public static class CompaniesEntry implements BaseColumns { | |
public static final Uri CONTENT_URI = SimpleProvider.BASE_URI | |
.buildUpon() | |
.appendPath(PATH_COMPANIES) | |
.build(); | |
public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + | |
"/" + SimpleProvider.AUTHORITY + | |
"/" + PATH_COMPANIES; | |
public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + | |
"/" + SimpleProvider.AUTHORITY + | |
"/" + PATH_COMPANIES; | |
public static final String TABLE_NAME = "companies"; | |
public static final String KEY_NAME = "name"; | |
public static final String KEY_EMPLOYEES = "employees"; | |
public static final String KEY_NET_WORTH = "net_worth"; | |
public static final String CREATE_TABLE_QUERY = "CREATE TABLE " + TABLE_NAME + | |
"(" + | |
_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + | |
KEY_NAME + " TEXT," + | |
KEY_EMPLOYEES + " INTEGER," + | |
KEY_NET_WORTH + " INTEGER" + | |
")"; | |
} | |
} |
This file contains 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 DatabaseHelper extends SQLiteOpenHelper { | |
private static final String DATABASE_NAME = "simpleDB"; | |
private static final int DATABASE_VERSION = 2; | |
public DatabaseHelper(Context context) { | |
super(context, DATABASE_NAME, null, DATABASE_VERSION); | |
} | |
@Override | |
public void onCreate(SQLiteDatabase db) { | |
db.execSQL(UsersEntry.CREATE_TABLE_QUERY); | |
db.execSQL(CompaniesEntry.CREATE_TABLE_QUERY); | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | |
db.execSQL("DROP TABLE IF EXISTS " + UsersEntry.TABLE_NAME); | |
db.execSQL("DROP TABLE IF EXISTS " + CompaniesEntry.TABLE_NAME); | |
onCreate(db); | |
} | |
} |
This file contains 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 MainActivity extends AppCompatActivity { | |
private static final String TAG = "MainActivityTAG_"; | |
private static final String SAMPLE_NAME = "Edwin"; | |
private static final int SAMPLE_AGE = 63; | |
int mCounter = 0; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
} | |
public void insertRows(View view) { | |
Uri usersUri = DatabaseContract.UsersEntry.CONTENT_URI; | |
ContentValues contentValues = new ContentValues(); | |
contentValues.put(DatabaseContract.UsersEntry.KEY_NAME, SAMPLE_NAME); | |
contentValues.put(DatabaseContract.UsersEntry.KEY_AGE, SAMPLE_AGE + mCounter++); | |
Uri resultUri = getContentResolver().insert(usersUri, contentValues); | |
Log.d(TAG, "insertRows: " + resultUri); | |
} | |
public void logRows(View view) { | |
Uri usersUri = DatabaseContract.UsersEntry.CONTENT_URI; | |
Cursor resultCursor = getContentResolver().query(usersUri, | |
null, | |
null, | |
null, | |
null); | |
resultCursor.moveToFirst(); | |
do { | |
Log.d(TAG, "logRows: " + resultCursor.getString(resultCursor.getColumnIndex(DatabaseContract.UsersEntry.KEY_NAME))); | |
Log.d(TAG, "logRows: " + resultCursor.getInt(resultCursor.getColumnIndex(DatabaseContract.UsersEntry.KEY_AGE))); | |
} while (resultCursor.moveToNext()); | |
resultCursor.close(); | |
} | |
} |
This file contains 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 SimpleProvider extends ContentProvider { | |
public static final String AUTHORITY = "com.example.simplecontentprovider"; | |
public static final Uri BASE_URI = Uri.parse("content://" + AUTHORITY); | |
private static final UriMatcher sUriMatcher = buildUriMatcher(); | |
private static final int MATCH_USERS = 100; | |
private static final int MATCH_COMPANIES = 200; | |
private DatabaseHelper mDatabaseHelper; | |
public SimpleProvider() { | |
} | |
public static UriMatcher buildUriMatcher() { | |
UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); | |
uriMatcher.addURI(AUTHORITY, DatabaseContract.PATH_USERS, MATCH_USERS); | |
uriMatcher.addURI(AUTHORITY, DatabaseContract.PATH_COMPANIES, MATCH_COMPANIES); | |
return uriMatcher; | |
} | |
@Override | |
public boolean onCreate() { | |
mDatabaseHelper = new DatabaseHelper(getContext()); | |
return true; | |
} | |
@Override | |
public String getType(Uri uri) { | |
final int match = sUriMatcher.match(uri); | |
switch (match) { | |
case MATCH_USERS: | |
return DatabaseContract.UsersEntry.CONTENT_TYPE; | |
case MATCH_COMPANIES: | |
return DatabaseContract.CompaniesEntry.CONTENT_TYPE; | |
default: | |
throw new UnsupportedOperationException("Unknown uri: " + uri); | |
} | |
} | |
@Override | |
public Cursor query(Uri uri, String[] projection, String selection, | |
String[] selectionArgs, String sortOrder) { | |
final SQLiteDatabase sqLiteDatabase = mDatabaseHelper.getReadableDatabase(); | |
final int match = sUriMatcher.match(uri); | |
String tableName; | |
switch (match) { | |
case MATCH_USERS: | |
tableName = DatabaseContract.UsersEntry.TABLE_NAME; | |
break; | |
case MATCH_COMPANIES: | |
tableName = DatabaseContract.CompaniesEntry.TABLE_NAME; | |
break; | |
default: | |
throw new UnsupportedOperationException("Unknown uri: " + uri); | |
} | |
return sqLiteDatabase.query(tableName, | |
projection, | |
selection, | |
selectionArgs, | |
null, | |
null, | |
sortOrder); | |
} | |
@Override | |
public Uri insert(Uri uri, ContentValues values) { | |
final SQLiteDatabase sqLiteDatabase = mDatabaseHelper.getWritableDatabase(); | |
final int match = sUriMatcher.match(uri); | |
long resultId; | |
Uri returnUri; | |
switch (match) { | |
case MATCH_USERS: | |
resultId = sqLiteDatabase.insert(DatabaseContract.UsersEntry.TABLE_NAME, null, values); | |
if (resultId > 0) { | |
returnUri = ContentUris.withAppendedId(DatabaseContract.UsersEntry.CONTENT_URI, resultId); | |
} else { | |
throw new android.database.SQLException("Failed to insert row into " + uri); | |
} | |
break; | |
case MATCH_COMPANIES: | |
resultId = sqLiteDatabase.insert(DatabaseContract.CompaniesEntry.TABLE_NAME, null, values); | |
if (resultId > 0) { | |
returnUri = ContentUris.withAppendedId(DatabaseContract.CompaniesEntry.CONTENT_URI, resultId); | |
} else { | |
throw new android.database.SQLException("Failed to insert row into " + uri); | |
} | |
break; | |
default: | |
throw new UnsupportedOperationException("Unknown uri: " + uri); | |
} | |
getContext().getContentResolver().notifyChange(uri, null); | |
return returnUri; | |
} | |
@Override | |
public int update(Uri uri, ContentValues values, String selection, | |
String[] selectionArgs) { | |
// TODO: Implement this to handle requests to update one or more rows. | |
throw new UnsupportedOperationException("Not yet implemented"); | |
} | |
@Override | |
public int delete(Uri uri, String selection, String[] selectionArgs) { | |
// Implement this to handle requests to delete one or more rows. | |
throw new UnsupportedOperationException("Not yet implemented"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment