Android Open Source - LearningAndroid2edYamba2 Status Provider






From Project

Back to project page LearningAndroid2edYamba2.

License

The source code is released under:

Apache License

If you think the Android project LearningAndroid2edYamba2 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.marakana.android.yamba;
//  w  w  w . j  av a 2 s.c  om
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

public class StatusProvider extends ContentProvider {
  private static final String TAG = StatusProvider.class.getSimpleName();
  private DbHelper dbHelper;

  private static final UriMatcher sURIMatcher = new UriMatcher(
      UriMatcher.NO_MATCH);
  static {
    sURIMatcher.addURI(StatusContract.AUTHORITY, StatusContract.TABLE,
        StatusContract.STATUS_DIR);
    sURIMatcher.addURI(StatusContract.AUTHORITY, StatusContract.TABLE
        + "/#", StatusContract.STATUS_ITEM);
  }

  @Override
  public boolean onCreate() {
    dbHelper = new DbHelper(getContext());
    Log.d(TAG, "onCreated");
    return true; // clk: fix to return true instead of false, following
    // http://developer.android.com/guide/topics/providers/content-provider-creating.html#OnCreate
  }

  @Override
  public String getType(Uri uri) {
    switch (sURIMatcher.match(uri)) {
    case StatusContract.STATUS_DIR:
      Log.d(TAG, "gotType: " + StatusContract.STATUS_TYPE_DIR);
      return StatusContract.STATUS_TYPE_DIR;
    case StatusContract.STATUS_ITEM:
      Log.d(TAG, "gotType: " + StatusContract.STATUS_TYPE_ITEM);
      return StatusContract.STATUS_TYPE_ITEM;
    default:
      throw new IllegalArgumentException("Illegal uri: " + uri);
    }
  }

  @Override
  public Uri insert(Uri uri, ContentValues values) {
    Uri ret = null;

    // Assert correct uri
    if (sURIMatcher.match(uri) != StatusContract.STATUS_DIR) {
      throw new IllegalArgumentException("Illegal uri: " + uri);
    }

    SQLiteDatabase db = dbHelper.getWritableDatabase();
    long rowId = db.insertWithOnConflict(StatusContract.TABLE, null,
        values, SQLiteDatabase.CONFLICT_IGNORE);

    // Was insert successful?
    if (rowId != -1) {
      long id = values.getAsLong(StatusContract.Column.ID);
      ret = ContentUris.withAppendedId(uri, id);
      Log.d(TAG, "inserted uri: " + ret);
      
      // Notify that data for this uri has changed
      // clk: Notify registered observers that a row was updated
      //   Note: Cursor returned by query() registers by calling
      //         setNotificationUri(ContentResolver, Uri)
      //   2nd argument for a ContentObserver is null here
      getContext().getContentResolver().notifyChange(uri, null);
    }

    return ret;
  }

  @Override
  public int update(Uri uri, ContentValues values, String selection,
      String[] selectionArgs) {
    String where;

    switch (sURIMatcher.match(uri)) {
    case StatusContract.STATUS_DIR:
      // so we count updated rows
      where = selection;
      break;
    case StatusContract.STATUS_ITEM:
      long id = ContentUris.parseId(uri);
      where = StatusContract.Column.ID
          + "="
          + id
          + (TextUtils.isEmpty(selection) ? "" : " and ( "
              + selection + " )");
      break;
    default:
      throw new IllegalArgumentException("Illegal uri: " + uri);
    }

    SQLiteDatabase db = dbHelper.getWritableDatabase();
    int ret = db.update(StatusContract.TABLE, values, where, selectionArgs);

    if(ret>0) {
      // Notify that data for this uri has changed
      getContext().getContentResolver().notifyChange(uri, null);
    }
    Log.d(TAG, "updated records: " + ret);
    return ret;
  }

  // Implement Purge feature
  // Use db.delete()
  // DELETE FROM status WHERE id=? AND user='?'
  // uri: content://com.marakana.android.yamba.StatusProvider/status/47
  @Override
  public int delete(Uri uri, String selection, String[] selectionArgs) {
    String where;

    switch (sURIMatcher.match(uri)) {
    case StatusContract.STATUS_DIR:
      // so we count deleted rows
      where = (selection == null) ? "1" : selection;
      break;
    case StatusContract.STATUS_ITEM:
      long id = ContentUris.parseId(uri);
      where = StatusContract.Column.ID
          + "="
          + id
          + (TextUtils.isEmpty(selection) ? "" : " and ( "
              + selection + " )");
      break;
    default:
      throw new IllegalArgumentException("Illegal uri: " + uri);
    }

    SQLiteDatabase db = dbHelper.getWritableDatabase();
    int ret = db.delete(StatusContract.TABLE, where, selectionArgs);

    if(ret>0) {
      // Notify that data for this uri has changed
      getContext().getContentResolver().notifyChange(uri, null);
    }
    Log.d(TAG, "deleted records: " + ret);
    return ret;
  }

  // SELECT username, message, created_at FROM status WHERE user='bob' ORDER
  // BY created_at DESC;
  @Override
  public Cursor query(Uri uri, String[] projection, String selection,
      String[] selectionArgs, String sortOrder) {

    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    qb.setTables( StatusContract.TABLE );

    switch (sURIMatcher.match(uri)) {
    case StatusContract.STATUS_DIR:
      break;
    case StatusContract.STATUS_ITEM:
      qb.appendWhere(StatusContract.Column.ID + "="
          + uri.getLastPathSegment());
      break;
    default:
      throw new IllegalArgumentException("Illegal uri: " + uri);
    }

    String orderBy = (TextUtils.isEmpty(sortOrder)) ? StatusContract.DEFAULT_SORT
        : sortOrder;
    
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    Cursor cursor = qb.query(db, projection, selection, selectionArgs, null, null, orderBy);
    
    // register for uri changes
    // clk: So the Cursor returned to CursorLoader after it does a query
    //   will know of changes and CursorLoader will know to re-query
    cursor.setNotificationUri(getContext().getContentResolver(), uri); 
    
    Log.d(TAG, "queried records: "+cursor.getCount());
    return cursor;
  }

}




Java Source Code List

com.marakana.android.yamba.BootReceiver.java
com.marakana.android.yamba.DbHelper.java
com.marakana.android.yamba.DetailsActivity.java
com.marakana.android.yamba.DetailsFragment.java
com.marakana.android.yamba.FreshnessView.java
com.marakana.android.yamba.MainActivity.java
com.marakana.android.yamba.NotificationReceiver.java
com.marakana.android.yamba.RefreshService.java
com.marakana.android.yamba.SettingsActivity.java
com.marakana.android.yamba.SettingsFragment.java
com.marakana.android.yamba.StatusActivity.java
com.marakana.android.yamba.StatusContract.java
com.marakana.android.yamba.StatusFragment.java
com.marakana.android.yamba.StatusProvider.java
com.marakana.android.yamba.SubActivity.java
com.marakana.android.yamba.TimelineFragment.java
com.marakana.android.yamba.YambaWidget.java