Advertisement
Ankhwatcher

FixProvider

Apr 4th, 2012
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.65 KB | None | 0 0
  1. import android.content.ContentProvider;
  2. import android.content.ContentUris;
  3. import android.content.ContentValues;
  4. import android.content.UriMatcher;
  5. import android.database.Cursor;
  6. import android.database.SQLException;
  7. import android.database.sqlite.SQLiteDatabase;
  8. import android.database.sqlite.SQLiteQueryBuilder;
  9. import android.net.Uri;
  10. import android.text.TextUtils;
  11.  
  12. public class FixProvider extends ContentProvider {
  13.     public static final String PROVIDER_NAME = "ie.appz.shortestwalkingroute.sqlite.FixProvider";
  14.     public static final Uri CONTENT_URI = Uri.parse("content://" + PROVIDER_NAME + "/" + FixOpenHelper.TABLE_NAME);
  15.     public static final String ROUTE = "ROUTE";
  16.     SQLiteDatabase fixDB;
  17.     FixOpenHelper fixOpenHelper ;
  18.     private static final int ALL = 1;
  19.     private static final int ONEROUTE = 2;
  20.     private static final UriMatcher uriMatcher;
  21.     static {
  22.         uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
  23.         uriMatcher.addURI(PROVIDER_NAME, ROUTE, ALL);
  24.         uriMatcher.addURI(PROVIDER_NAME, ROUTE+"/#", ONEROUTE);
  25.     }
  26.  
  27.     @Override
  28.     public int delete(Uri uri, String selection, String[] selectionArgs) {
  29.         int uriType = uriMatcher.match(uri);
  30.         fixDB = fixOpenHelper.getWritableDatabase();
  31.         int rowsAffected = 0;
  32.         switch (uriType) {
  33.         case ALL:
  34.             rowsAffected = fixDB.delete(FixOpenHelper.TABLE_NAME, selection, selectionArgs);
  35.             break;
  36.         case ONEROUTE:
  37.             String routeNo = uri.getLastPathSegment();
  38.             if (TextUtils.isEmpty(selection)) {
  39.                 rowsAffected = fixDB.delete(FixOpenHelper.TABLE_NAME, FixOpenHelper.ROUTE_NUMBER + "=" + routeNo, null);
  40.             } else {
  41.                 rowsAffected = fixDB.delete(FixOpenHelper.TABLE_NAME, selection + " and " + FixOpenHelper.ROUTE_NUMBER
  42.                         + "=" + routeNo, selectionArgs);
  43.             }
  44.             break;
  45.         default:
  46.             throw new IllegalArgumentException("Unknown or Invalid URI " + uri);
  47.         }
  48.         getContext().getContentResolver().notifyChange(uri, null);
  49.         return rowsAffected;
  50.     }
  51.  
  52.     @Override
  53.     public String getType(Uri uri) {
  54.         switch (uriMatcher.match(uri)) {
  55.         // ---get all books---
  56.         case ALL:
  57.             return "vnd.android.cursor.dir/vnd.appz.shortestwalkingroute.route ";
  58.             // ---get a particular book---
  59.         case ONEROUTE:
  60.             return "vnd.android.cursor.item/vnd.appz.shortestwalkingroute.route ";
  61.         default:
  62.             throw new IllegalArgumentException("Unsupported URI: " + uri);
  63.         }
  64.     }
  65.  
  66.     @Override
  67.     public Uri insert(Uri uri, ContentValues values) {
  68.         // ---add a new fix---
  69.         long rowID = fixDB.insert(FixOpenHelper.TABLE_NAME, "", values);
  70.  
  71.         // ---if added successfully---
  72.         if (rowID > 0) {
  73.             Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
  74.             getContext().getContentResolver().notifyChange(_uri, null);
  75.             return _uri;
  76.         }
  77.         throw new SQLException("Failed to insert row into " + uri);
  78.     }
  79.  
  80.     @Override
  81.     public boolean onCreate() {
  82.  
  83.         fixOpenHelper= new FixOpenHelper(getContext());
  84.         fixDB = fixOpenHelper.getWritableDatabase();
  85.         return (fixDB == null) ? false : true;
  86.     }
  87.  
  88.     @Override
  89.     public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
  90.         SQLiteQueryBuilder sqlBuilder = new SQLiteQueryBuilder();
  91.         sqlBuilder.setTables(FixOpenHelper.TABLE_NAME);
  92.         if (uriMatcher.match(uri) == ONEROUTE)
  93.         {
  94.             sqlBuilder.appendWhere(FixOpenHelper.ROUTE_NUMBER + " = " + uri.getPathSegments().get(1));
  95.         }
  96.         Cursor c = sqlBuilder.query(fixDB, projection, selection, selectionArgs, null, null, null);
  97.         // ---register to watch a content URI for changes---
  98.         c.setNotificationUri(getContext().getContentResolver(), uri);
  99.         return c;
  100.     }
  101.  
  102.     @Override
  103.     public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
  104.  
  105.         return 0;
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement