Advertisement
Guest User

Untitled

a guest
Jul 21st, 2012
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.27 KB | None | 0 0
  1. public class HotelsProvider extends ContentProvider {
  2.  
  3. private static final String TAG = "HotelsProvider";
  4. private static final String AUTHORITY ="com.example.app.provider";
  5.  
  6. private SQLiteOpenHelper mOpenHelper;
  7.  
  8. private static final int HOTELS = 1;
  9. private static final int HOTELS_ID = 2;
  10. private static final UriMatcher sUriMatcher;
  11. private static HashMap<String, String> sHotelsProjectionMap;
  12.  
  13. private static class DatabaseHelper extends SQLiteOpenHelper {
  14.  
  15. private static final String DATABASE_NAME = "hotels.db";
  16. private static final int DATABASE_VERSION = 1;
  17. private static final String SP_KEY_DB_VER = "db_ver";
  18. private final Context mContext;
  19.  
  20. public DatabaseHelper(Context context) {
  21. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  22. mContext = context;
  23. initialize();
  24. }
  25.  
  26. /**
  27. * Initializes database. Creates database if doesn't exist.
  28. */
  29. private void initialize() {
  30. if (databaseExists()) {
  31. SharedPreferences prefs = PreferenceManager
  32. .getDefaultSharedPreferences(mContext);
  33. int dbVersion = prefs.getInt(SP_KEY_DB_VER, 1);
  34. if (DATABASE_VERSION != dbVersion) {
  35. File dbFile = mContext.getDatabasePath(DATABASE_NAME);
  36. if (!dbFile.delete()) {
  37. Log.w(TAG, "Unable to update database");
  38. }
  39. }
  40. }
  41. if (!databaseExists()) {
  42. createDatabase();
  43. }
  44. }
  45.  
  46. /**
  47. * Returns true if database file exists, false otherwise.
  48. * @return
  49. */
  50. private boolean databaseExists() {
  51. File dbFile = mContext.getDatabasePath(DATABASE_NAME);
  52. return dbFile.exists();
  53. }
  54.  
  55. /**
  56. * Creates database by copying it from assets directory.
  57. */
  58. private void createDatabase() {
  59. String parentPath = mContext.getDatabasePath(DATABASE_NAME).getParent();
  60. String path = mContext.getDatabasePath(DATABASE_NAME).getPath();
  61.  
  62. File file = new File(parentPath);
  63. if (!file.exists()) {
  64. if (!file.mkdir()) {
  65. Log.w(TAG, "Unable to create database directory");
  66. return;
  67. }
  68. }
  69.  
  70. InputStream is = null;
  71. OutputStream os = null;
  72. try {
  73. is = mContext.getAssets().open(DATABASE_NAME);
  74. os = new FileOutputStream(path);
  75.  
  76. byte[] buffer = new byte[1024];
  77. int length;
  78. while ((length = is.read(buffer)) > 0) {
  79. os.write(buffer, 0, length);
  80. }
  81. os.flush();
  82. SharedPreferences prefs = PreferenceManager
  83. .getDefaultSharedPreferences(mContext);
  84. SharedPreferences.Editor editor = prefs.edit();
  85. editor.putInt(SP_KEY_DB_VER, DATABASE_VERSION);
  86. editor.commit();
  87. } catch (IOException e) {
  88. e.printStackTrace();
  89. } finally {
  90. if (is != null) {
  91. try {
  92. is.close();
  93. } catch (IOException e) {
  94. e.printStackTrace();
  95. }
  96. }
  97. if (os != null) {
  98. try {
  99. os.close();
  100. } catch (IOException e) {
  101. e.printStackTrace();
  102. }
  103. }
  104. }
  105. }
  106.  
  107. @Override
  108. public void onCreate(SQLiteDatabase db) {
  109. }
  110.  
  111. @Override
  112. public void onUpgrade(SQLiteDatabase db, int oldVersion,
  113. int newVersion) {
  114. }
  115. }
  116.  
  117. @Override
  118. public boolean onCreate() {
  119. mOpenHelper = new DatabaseHelper(getContext());
  120. return true;
  121. }
  122.  
  123. @Override
  124. public Cursor query(Uri uri, String[] projection,
  125. String selection, String[] selectionArgs, String sortOrder) {
  126. SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
  127. switch (sUriMatcher.match(uri)) {
  128. case HOTELS:
  129. qb.setTables(Hotels.TABLE_NAME);
  130. qb.setProjectionMap(sHotelsProjectionMap);
  131. break;
  132. case HOTELS_ID:
  133. qb.setTables(Hotels.TABLE_NAME);
  134. qb.setProjectionMap(sHotelsProjectionMap);
  135. qb.appendWhere(Hotels._ID + "=" + uri.getPathSegments().get(1));
  136. break;
  137. default:
  138. throw new IllegalArgumentException("Unknown URI " + uri);
  139. }
  140.  
  141. SQLiteDatabase db = mOpenHelper.getReadableDatabase();
  142. Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
  143. c.setNotificationUri(getContext().getContentResolver(), uri);
  144. return c;
  145. }
  146.  
  147. @Override
  148. public String getType(Uri uri) {
  149. switch(sUriMatcher.match(uri)) {
  150. case HOTELS:
  151. return Hotels.CONTENT_TYPE;
  152. case HOTELS_ID:
  153. return Hotels.CONTENT_ITEM_TYPE;
  154. default:
  155. throw new IllegalArgumentException("Unknown URI " + uri);
  156. }
  157. }
  158.  
  159. @Override
  160. public Uri insert(Uri uri, ContentValues initialValues) {
  161. if (sUriMatcher.match(uri) != HOTELS) {
  162. throw new IllegalArgumentException("Unknown URI " + uri);
  163. }
  164.  
  165. ContentValues values;
  166. if (initialValues != null) {
  167. values = new ContentValues(initialValues);
  168. } else {
  169. values = new ContentValues();
  170. }
  171.  
  172. SQLiteDatabase db = mOpenHelper.getWritableDatabase();
  173.  
  174. long rowId = db.insert(Hotels.TABLE_NAME, null, values);
  175. if (rowId > 0) {
  176. Uri hotelUri = ContentUris.withAppendedId(Hotels.CONTENT_URI, rowId);
  177. getContext().getContentResolver().notifyChange(hotelUri, null);
  178. return hotelUri;
  179. }
  180.  
  181. throw new SQLException("Failed to insert row into " + uri);
  182. }
  183.  
  184. @Override
  185. public int delete(Uri uri, String selection, String[] selectionArgs) {
  186. SQLiteDatabase db = mOpenHelper.getWritableDatabase();
  187. int count;
  188. switch (sUriMatcher.match(uri)) {
  189. case HOTELS:
  190. count = db.delete(Hotels.TABLE_NAME, selection, selectionArgs);
  191. break;
  192. case HOTELS_ID:
  193. String rowId = uri.getPathSegments().get(1);
  194. String finalWhere = Hotels._ID + "=" + rowId;
  195. if (selection != null) {
  196. finalWhere += " AND " + selection;
  197. }
  198. count = db.delete(Hotels.TABLE_NAME, finalWhere, selectionArgs);
  199. break;
  200. default:
  201. throw new IllegalArgumentException("Unknown URI " + uri);
  202. }
  203.  
  204. getContext().getContentResolver().notifyChange(uri, null);
  205. return count;
  206. }
  207.  
  208. @Override
  209. public int update(Uri uri, ContentValues values, String selection,
  210. String[] selectionArgs) {
  211. SQLiteDatabase db = mOpenHelper.getWritableDatabase();
  212. int count;
  213. String finalWhere;
  214. switch (sUriMatcher.match(uri)) {
  215. case HOTELS:
  216. count = db.update(Hotels.TABLE_NAME, values, selection, selectionArgs);
  217. break;
  218. case HOTELS_ID:
  219. String rowId = uri.getPathSegments().get(1);
  220. finalWhere = Hotels._ID + "=" + rowId;
  221. if (selection != null) {
  222. finalWhere += " AND " + selection;
  223. }
  224. count = db.update(Hotels.TABLE_NAME, values, finalWhere, selectionArgs);
  225. break;
  226. default:
  227. throw new IllegalArgumentException("Unknown URI " + uri);
  228. }
  229. getContext().getContentResolver().notifyChange(uri, null);
  230. return count;
  231. }
  232.  
  233. static {
  234. sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
  235. sUriMatcher.addURI(AUTHORITY, Hotels.TABLE_NAME, HOTELS);
  236. sUriMatcher.addURI(AUTHORITY, Hotels.TABLE_NAME + "/#", HOTELS_ID);
  237.  
  238. sHotelsProjectionMap = new HashMap<String, String>();
  239. sHotelsProjectionMap.put(Hotels._ID, Hotels._ID);
  240. sHotelsProjectionMap.put(Hotels.NAME, Hotels.NAME);
  241. sHotelsProjectionMap.put(Hotels.ADDRESS, Hotels.ADDRESS);
  242. sHotelsProjectionMap.put(Hotels.LATITUDE, Hotels.LATITUDE);
  243. sHotelsProjectionMap.put(Hotels.LONGITUDE, Hotels.LONGITUDE);
  244. }
  245.  
  246. /**
  247. * Hotels table.
  248. */
  249. public static final class Hotels implements BaseColumns {
  250.  
  251. private static final String CONTENT_TYPE =
  252. ContentResolver.CURSOR_DIR_BASE_TYPE + "/vnd.getrich.hotels";
  253. private static final String CONTENT_ITEM_TYPE =
  254. ContentResolver.CURSOR_ITEM_BASE_TYPE + "/vnd.getrich.hotels";
  255.  
  256. /**
  257. * Name of the table.
  258. */
  259. public static final String TABLE_NAME = "hotels";
  260.  
  261. /**
  262. * The content URI base for a single hotel. Callers must
  263. * append a numeric hotel id to this Uri to retrieve a hotel.
  264. */
  265. public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + TABLE_NAME);
  266.  
  267. /**
  268. * Name of the hotel.
  269. * Type: TEXT
  270. */
  271. public static final String NAME = "name";
  272.  
  273. /**
  274. * The address of the hotel.
  275. * Type: TEXT
  276. */
  277. public static final String ADDRESS = "address";
  278.  
  279. /**
  280. * Latitude coord of the hotel.
  281. * Type: REAL
  282. */
  283. public static final String LATITUDE = "latitude";
  284.  
  285. /**
  286. * Longitude coord of the hotel.
  287. * Type: REAL
  288. */
  289. public static final String LONGITUDE = "longitude";
  290.  
  291. private Hotels() {
  292. }
  293. }
  294. }
  295.  
  296. To get all hotels:
  297. ContentResolver resolver = getContentResolver();
  298. final Cursor cursor = resolver.query(Stores.CONTENT_URI, null, null, null, null);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement