Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class HotelsProvider extends ContentProvider {
- private static final String TAG = "HotelsProvider";
- private static final String AUTHORITY ="com.example.app.provider";
- private SQLiteOpenHelper mOpenHelper;
- private static final int HOTELS = 1;
- private static final int HOTELS_ID = 2;
- private static final UriMatcher sUriMatcher;
- private static HashMap<String, String> sHotelsProjectionMap;
- private static class DatabaseHelper extends SQLiteOpenHelper {
- private static final String DATABASE_NAME = "hotels.db";
- private static final int DATABASE_VERSION = 1;
- private static final String SP_KEY_DB_VER = "db_ver";
- private final Context mContext;
- public DatabaseHelper(Context context) {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- mContext = context;
- initialize();
- }
- /**
- * Initializes database. Creates database if doesn't exist.
- */
- private void initialize() {
- if (databaseExists()) {
- SharedPreferences prefs = PreferenceManager
- .getDefaultSharedPreferences(mContext);
- int dbVersion = prefs.getInt(SP_KEY_DB_VER, 1);
- if (DATABASE_VERSION != dbVersion) {
- File dbFile = mContext.getDatabasePath(DATABASE_NAME);
- if (!dbFile.delete()) {
- Log.w(TAG, "Unable to update database");
- }
- }
- }
- if (!databaseExists()) {
- createDatabase();
- }
- }
- /**
- * Returns true if database file exists, false otherwise.
- * @return
- */
- private boolean databaseExists() {
- File dbFile = mContext.getDatabasePath(DATABASE_NAME);
- return dbFile.exists();
- }
- /**
- * Creates database by copying it from assets directory.
- */
- private void createDatabase() {
- String parentPath = mContext.getDatabasePath(DATABASE_NAME).getParent();
- String path = mContext.getDatabasePath(DATABASE_NAME).getPath();
- File file = new File(parentPath);
- if (!file.exists()) {
- if (!file.mkdir()) {
- Log.w(TAG, "Unable to create database directory");
- return;
- }
- }
- InputStream is = null;
- OutputStream os = null;
- try {
- is = mContext.getAssets().open(DATABASE_NAME);
- os = new FileOutputStream(path);
- byte[] buffer = new byte[1024];
- int length;
- while ((length = is.read(buffer)) > 0) {
- os.write(buffer, 0, length);
- }
- os.flush();
- SharedPreferences prefs = PreferenceManager
- .getDefaultSharedPreferences(mContext);
- SharedPreferences.Editor editor = prefs.edit();
- editor.putInt(SP_KEY_DB_VER, DATABASE_VERSION);
- editor.commit();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (is != null) {
- try {
- is.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (os != null) {
- try {
- os.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion,
- int newVersion) {
- }
- }
- @Override
- public boolean onCreate() {
- mOpenHelper = new DatabaseHelper(getContext());
- return true;
- }
- @Override
- public Cursor query(Uri uri, String[] projection,
- String selection, String[] selectionArgs, String sortOrder) {
- SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
- switch (sUriMatcher.match(uri)) {
- case HOTELS:
- qb.setTables(Hotels.TABLE_NAME);
- qb.setProjectionMap(sHotelsProjectionMap);
- break;
- case HOTELS_ID:
- qb.setTables(Hotels.TABLE_NAME);
- qb.setProjectionMap(sHotelsProjectionMap);
- qb.appendWhere(Hotels._ID + "=" + uri.getPathSegments().get(1));
- break;
- default:
- throw new IllegalArgumentException("Unknown URI " + uri);
- }
- SQLiteDatabase db = mOpenHelper.getReadableDatabase();
- Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
- c.setNotificationUri(getContext().getContentResolver(), uri);
- return c;
- }
- @Override
- public String getType(Uri uri) {
- switch(sUriMatcher.match(uri)) {
- case HOTELS:
- return Hotels.CONTENT_TYPE;
- case HOTELS_ID:
- return Hotels.CONTENT_ITEM_TYPE;
- default:
- throw new IllegalArgumentException("Unknown URI " + uri);
- }
- }
- @Override
- public Uri insert(Uri uri, ContentValues initialValues) {
- if (sUriMatcher.match(uri) != HOTELS) {
- throw new IllegalArgumentException("Unknown URI " + uri);
- }
- ContentValues values;
- if (initialValues != null) {
- values = new ContentValues(initialValues);
- } else {
- values = new ContentValues();
- }
- SQLiteDatabase db = mOpenHelper.getWritableDatabase();
- long rowId = db.insert(Hotels.TABLE_NAME, null, values);
- if (rowId > 0) {
- Uri hotelUri = ContentUris.withAppendedId(Hotels.CONTENT_URI, rowId);
- getContext().getContentResolver().notifyChange(hotelUri, null);
- return hotelUri;
- }
- throw new SQLException("Failed to insert row into " + uri);
- }
- @Override
- public int delete(Uri uri, String selection, String[] selectionArgs) {
- SQLiteDatabase db = mOpenHelper.getWritableDatabase();
- int count;
- switch (sUriMatcher.match(uri)) {
- case HOTELS:
- count = db.delete(Hotels.TABLE_NAME, selection, selectionArgs);
- break;
- case HOTELS_ID:
- String rowId = uri.getPathSegments().get(1);
- String finalWhere = Hotels._ID + "=" + rowId;
- if (selection != null) {
- finalWhere += " AND " + selection;
- }
- count = db.delete(Hotels.TABLE_NAME, finalWhere, selectionArgs);
- break;
- default:
- throw new IllegalArgumentException("Unknown URI " + uri);
- }
- getContext().getContentResolver().notifyChange(uri, null);
- return count;
- }
- @Override
- public int update(Uri uri, ContentValues values, String selection,
- String[] selectionArgs) {
- SQLiteDatabase db = mOpenHelper.getWritableDatabase();
- int count;
- String finalWhere;
- switch (sUriMatcher.match(uri)) {
- case HOTELS:
- count = db.update(Hotels.TABLE_NAME, values, selection, selectionArgs);
- break;
- case HOTELS_ID:
- String rowId = uri.getPathSegments().get(1);
- finalWhere = Hotels._ID + "=" + rowId;
- if (selection != null) {
- finalWhere += " AND " + selection;
- }
- count = db.update(Hotels.TABLE_NAME, values, finalWhere, selectionArgs);
- break;
- default:
- throw new IllegalArgumentException("Unknown URI " + uri);
- }
- getContext().getContentResolver().notifyChange(uri, null);
- return count;
- }
- static {
- sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
- sUriMatcher.addURI(AUTHORITY, Hotels.TABLE_NAME, HOTELS);
- sUriMatcher.addURI(AUTHORITY, Hotels.TABLE_NAME + "/#", HOTELS_ID);
- sHotelsProjectionMap = new HashMap<String, String>();
- sHotelsProjectionMap.put(Hotels._ID, Hotels._ID);
- sHotelsProjectionMap.put(Hotels.NAME, Hotels.NAME);
- sHotelsProjectionMap.put(Hotels.ADDRESS, Hotels.ADDRESS);
- sHotelsProjectionMap.put(Hotels.LATITUDE, Hotels.LATITUDE);
- sHotelsProjectionMap.put(Hotels.LONGITUDE, Hotels.LONGITUDE);
- }
- /**
- * Hotels table.
- */
- public static final class Hotels implements BaseColumns {
- private static final String CONTENT_TYPE =
- ContentResolver.CURSOR_DIR_BASE_TYPE + "/vnd.getrich.hotels";
- private static final String CONTENT_ITEM_TYPE =
- ContentResolver.CURSOR_ITEM_BASE_TYPE + "/vnd.getrich.hotels";
- /**
- * Name of the table.
- */
- public static final String TABLE_NAME = "hotels";
- /**
- * The content URI base for a single hotel. Callers must
- * append a numeric hotel id to this Uri to retrieve a hotel.
- */
- public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + TABLE_NAME);
- /**
- * Name of the hotel.
- * Type: TEXT
- */
- public static final String NAME = "name";
- /**
- * The address of the hotel.
- * Type: TEXT
- */
- public static final String ADDRESS = "address";
- /**
- * Latitude coord of the hotel.
- * Type: REAL
- */
- public static final String LATITUDE = "latitude";
- /**
- * Longitude coord of the hotel.
- * Type: REAL
- */
- public static final String LONGITUDE = "longitude";
- private Hotels() {
- }
- }
- }
- To get all hotels:
- ContentResolver resolver = getContentResolver();
- final Cursor cursor = resolver.query(Stores.CONTENT_URI, null, null, null, null);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement