Advertisement
FamiHug

Provider1

Jul 8th, 2012
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.57 KB | None | 0 0
  1. /*
  2.  * Copyright (C) 2007 The Android Open Source Project
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16.  
  17. package com.example.android.notepad;
  18.  
  19. import com.example.android.notepad.NotePad.Notes;
  20.  
  21. import android.content.ContentProvider;
  22. import android.content.ContentUris;
  23. import android.content.ContentValues;
  24. import android.content.Context;
  25. import android.content.UriMatcher;
  26. import android.content.res.Resources;
  27. import android.database.Cursor;
  28. import android.database.SQLException;
  29. import android.database.sqlite.SQLiteDatabase;
  30. import android.database.sqlite.SQLiteOpenHelper;
  31. import android.database.sqlite.SQLiteQueryBuilder;
  32. import android.net.Uri;
  33. import android.text.TextUtils;
  34. import android.util.Log;
  35.  
  36. import java.util.HashMap;
  37.  
  38. /**
  39.  * Provides access to a database of notes. Each note has a title, the note
  40.  * itself, a creation date and a modified data.
  41.  */
  42. public class NotePadProvider extends ContentProvider {
  43.  
  44.     private static final String TAG = "NotePadProvider";
  45.  
  46.     private static final String DATABASE_NAME = "note_pad.db";
  47.     private static final int DATABASE_VERSION = 2;
  48.     private static final String NOTES_TABLE_NAME = "notes";
  49.  
  50.     private static HashMap<String, String> sNotesProjectionMap;
  51.  
  52.     private static final int NOTES = 1;
  53.     private static final int NOTE_ID = 2;
  54.  
  55.     private static final UriMatcher sUriMatcher;
  56.  
  57.     /**
  58.      * This class helps open, create, and upgrade the database file.
  59.      */
  60.     private static class DatabaseHelper extends SQLiteOpenHelper {
  61.  
  62.         DatabaseHelper(Context context) {
  63.             super(context, DATABASE_NAME, null, DATABASE_VERSION);
  64.         }
  65.  
  66.         @Override
  67.         public void onCreate(SQLiteDatabase db) {
  68.             db.execSQL("CREATE TABLE " + NOTES_TABLE_NAME + " (" + Notes._ID
  69.                     + " INTEGER PRIMARY KEY," + Notes.TITLE + " TEXT,"
  70.                     + Notes.NOTE + " TEXT," + Notes.CREATED_DATE + " INTEGER,"
  71.                     + Notes.MODIFIED_DATE + " INTEGER" + ");");
  72.         }
  73.  
  74.         @Override
  75.         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  76.             Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
  77.                     + newVersion + ", which will destroy all old data");
  78.             db.execSQL("DROP TABLE IF EXISTS notes");
  79.             onCreate(db);
  80.         }
  81.     }
  82.  
  83.     private DatabaseHelper mOpenHelper;
  84.  
  85.     @Override
  86.     public boolean onCreate() {
  87.         mOpenHelper = new DatabaseHelper(getContext());
  88.         return true;
  89.     }
  90.  
  91.     @Override
  92.     public Cursor query(Uri uri, String[] projection, String selection,
  93.             String[] selectionArgs, String sortOrder) {
  94.         SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
  95.  
  96.         switch (sUriMatcher.match(uri)) {
  97.         case NOTES:
  98.             qb.setTables(NOTES_TABLE_NAME);
  99.             qb.setProjectionMap(sNotesProjectionMap);
  100.             break;
  101.  
  102.         case NOTE_ID:
  103.             qb.setTables(NOTES_TABLE_NAME);
  104.             qb.setProjectionMap(sNotesProjectionMap);
  105.             qb.appendWhere(Notes._ID + "=" + uri.getPathSegments().get(1));
  106.             break;
  107.  
  108.         default:
  109.             throw new IllegalArgumentException("Unknown URI " + uri);
  110.         }
  111.  
  112.         // If no sort order is specified use the default
  113.         String orderBy;
  114.         if (TextUtils.isEmpty(sortOrder)) {
  115.             orderBy = NotePad.Notes.DEFAULT_SORT_ORDER;
  116.         } else {
  117.             orderBy = sortOrder;
  118.         }
  119.  
  120.         // Get the database and run the query
  121.         SQLiteDatabase db = mOpenHelper.getReadableDatabase();
  122.         Cursor c = qb.query(db, projection, selection, selectionArgs, null,
  123.                 null, orderBy);
  124.  
  125.         // Tell the cursor what uri to watch, so it knows when its source data
  126.         // changes
  127.         c.setNotificationUri(getContext().getContentResolver(), uri);
  128.         return c;
  129.     }
  130.  
  131.     @Override
  132.     public String getType(Uri uri) {
  133.         switch (sUriMatcher.match(uri)) {
  134.         case NOTES:
  135.             return Notes.CONTENT_TYPE;
  136.  
  137.         case NOTE_ID:
  138.             return Notes.CONTENT_ITEM_TYPE;
  139.  
  140.         default:
  141.             throw new IllegalArgumentException("Unknown URI " + uri);
  142.         }
  143.     }
  144.  
  145.     @Override
  146.     public Uri insert(Uri uri, ContentValues initialValues) {
  147.         // Validate the requested uri
  148.         if (sUriMatcher.match(uri) != NOTES) {
  149.             throw new IllegalArgumentException("Unknown URI " + uri);
  150.         }
  151.  
  152.         ContentValues values;
  153.         if (initialValues != null) {
  154.             values = new ContentValues(initialValues);
  155.         } else {
  156.             values = new ContentValues();
  157.         }
  158.  
  159.         Long now = Long.valueOf(System.currentTimeMillis());
  160.  
  161.         // Make sure that the fields are all set
  162.         if (values.containsKey(NotePad.Notes.CREATED_DATE) == false) {
  163.             values.put(NotePad.Notes.CREATED_DATE, now);
  164.         }
  165.  
  166.         if (values.containsKey(NotePad.Notes.MODIFIED_DATE) == false) {
  167.             values.put(NotePad.Notes.MODIFIED_DATE, now);
  168.         }
  169.  
  170.         if (values.containsKey(NotePad.Notes.TITLE) == false) {
  171.             Resources r = Resources.getSystem();
  172.             values.put(NotePad.Notes.TITLE,
  173.                     r.getString(android.R.string.untitled));
  174.         }
  175.  
  176.         if (values.containsKey(NotePad.Notes.NOTE) == false) {
  177.             values.put(NotePad.Notes.NOTE, "");
  178.         }
  179.  
  180.         SQLiteDatabase db = mOpenHelper.getWritableDatabase();
  181.         long rowId = db.insert(NOTES_TABLE_NAME, Notes.NOTE, values);
  182.         if (rowId > 0) {
  183.             Uri noteUri = ContentUris.withAppendedId(NotePad.Notes.CONTENT_URI,
  184.                     rowId);
  185.             getContext().getContentResolver().notifyChange(noteUri, null);
  186.             return noteUri;
  187.         }
  188.         //Cho nay nay
  189.         throw new SQLException("Failed to insert row into " + uri);
  190.     }
  191.  
  192.     @Override
  193.     public int delete(Uri uri, String where, String[] whereArgs) {
  194.         SQLiteDatabase db = mOpenHelper.getWritableDatabase();
  195.         int count;
  196.         switch (sUriMatcher.match(uri)) {
  197.         case NOTES:
  198.             count = db.delete(NOTES_TABLE_NAME, where, whereArgs);
  199.             break;
  200.  
  201.         case NOTE_ID:
  202.             String noteId = uri.getPathSegments().get(1);
  203.             count = db.delete(NOTES_TABLE_NAME,
  204.                     Notes._ID
  205.                             + "="
  206.                             + noteId
  207.                             + (!TextUtils.isEmpty(where) ? " AND (" + where
  208.                                     + ')' : ""), whereArgs);
  209.             break;
  210.  
  211.         default:
  212.             throw new IllegalArgumentException("Unknown URI " + uri);
  213.         }
  214.  
  215.         getContext().getContentResolver().notifyChange(uri, null);
  216.         return count;
  217.     }
  218.  
  219.     @Override
  220.     public int update(Uri uri, ContentValues values, String where,
  221.             String[] whereArgs) {
  222.         SQLiteDatabase db = mOpenHelper.getWritableDatabase();
  223.         int count;
  224.         switch (sUriMatcher.match(uri)) {
  225.         case NOTES:
  226.             count = db.update(NOTES_TABLE_NAME, values, where, whereArgs);
  227.             break;
  228.  
  229.         case NOTE_ID:
  230.             String noteId = uri.getPathSegments().get(1);
  231.             count = db.update(NOTES_TABLE_NAME, values,
  232.                     Notes._ID
  233.                             + "="
  234.                             + noteId
  235.                             + (!TextUtils.isEmpty(where) ? " AND (" + where
  236.                                     + ')' : ""), whereArgs);
  237.             break;
  238.  
  239.         default:
  240.             throw new IllegalArgumentException("Unknown URI " + uri);
  241.         }
  242.  
  243.         getContext().getContentResolver().notifyChange(uri, null);
  244.         return count;
  245.     }
  246.  
  247.     static {
  248.         sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
  249.         sUriMatcher.addURI(NotePad.AUTHORITY, "notes", NOTES);
  250.         sUriMatcher.addURI(NotePad.AUTHORITY, "notes/#", NOTE_ID);
  251.  
  252.         sNotesProjectionMap = new HashMap<String, String>();
  253.         sNotesProjectionMap.put(Notes._ID, Notes._ID);
  254.         sNotesProjectionMap.put(Notes.TITLE, Notes.TITLE);
  255.         sNotesProjectionMap.put(Notes.NOTE, Notes.NOTE);
  256.         sNotesProjectionMap.put(Notes.CREATED_DATE, Notes.CREATED_DATE);
  257.         sNotesProjectionMap.put(Notes.MODIFIED_DATE, Notes.MODIFIED_DATE);
  258.     }
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement