Android Open Source - TodoList Todo Provider






From Project

Back to project page TodoList.

License

The source code is released under:

Apache License

If you think the Android project TodoList 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 lyc.app.provider;
//from   ww w  .  ja  v  a 2  s .  c om
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

import java.util.Arrays;

import lyc.app.App;

/**
 * Created by ivan on 14-10-10.
 */
public class TodoProvider extends ContentProvider {

  private static final String TAG = "TodoProvider";

  private static final String DATABASE_NAME = "app.db";
  private static final int DATABASE_VERSION = 1;

  private static final int ALL_ROWS = 1;
  private static final int SINGLE_ROW = 2;
  private static final UriMatcher uriMatcher;

  static {
    uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    uriMatcher.addURI(App.AUTHORITY, "todos", ALL_ROWS);
    uriMatcher.addURI(App.AUTHORITY, "todos/#", SINGLE_ROW);
  }

  private DatabaseHelper mOpenHelper;


  @Override
  public boolean onCreate() {
    mOpenHelper = new DatabaseHelper(getContext());
    return true;
  }

  @Override
  public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
                      String sortOrder) {
    Log.d(TAG, "uri = " + uri);
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    qb.setTables(App.Todo.TABLE_NAME);

    switch (uriMatcher.match(uri)) {
      case SINGLE_ROW:
        String todoID = uri.getPathSegments().get(1);
        qb.appendWhere(App.Todo._ID + "=" + todoID);
        break;
      case ALL_ROWS:
        break;
      default:
        throw new IllegalArgumentException("Unknown URI " + uri);
    }

    String orderBy;
    if (TextUtils.isEmpty(sortOrder)) {
      orderBy = App.Todo.DEFAULT_SORT_ORDER;
    } else {
      orderBy = sortOrder;
    }

    SQLiteDatabase db = mOpenHelper.getReadableDatabase();
    Log.d(TAG, selection + Arrays.toString(selectionArgs));
    Cursor cursor = qb.query(db, projection, selection, selectionArgs, null, null, orderBy);
    return cursor;
  }

  @Override
  public String getType(Uri uri) {
    switch (uriMatcher.match(uri)) {
      case ALL_ROWS:
        return App.Todo.CONTENT_TYPE;
      case SINGLE_ROW:
        return App.Todo.CONTENT_ITEM_TYPE;
      default:
        throw new IllegalArgumentException("Unknown URI " + uri);
    }
  }

  @Override
  public Uri insert(Uri uri, ContentValues initialValues) {
    if (uriMatcher.match(uri) != ALL_ROWS) {
      throw new IllegalArgumentException("Unknown URI " + uri);
    }

    ContentValues values;
    if (initialValues != null) {
      values = new ContentValues(initialValues);
    } else {
      values = new ContentValues();
    }

    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    long id = db.insert(App.Todo.TABLE_NAME, null, values);
    if (id > 0) {
      Uri insertedId = ContentUris.withAppendedId(App.Todo.CONTENT_ID_URI_BASE, id);
      Log.d(TAG, insertedId.toString());
      getContext().getContentResolver().notifyChange(insertedId, null);
      return insertedId;
    }
    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 = db.delete(App.Todo.TABLE_NAME, selection, selectionArgs);
    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;
    switch (uriMatcher.match(uri)) {
      case SINGLE_ROW:
        String rowID = uri.getPathSegments().get(1);
        String finalSelection = App.Todo._ID + "=" + rowID;
        if (!TextUtils.isEmpty(selection)) {
          finalSelection = finalSelection + " AND " + selection;
        }
        count = db.update(App.Todo.TABLE_NAME, values, finalSelection, selectionArgs);
        break;
      case ALL_ROWS:
        count = db.update(App.Todo.TABLE_NAME, values, selection, selectionArgs);
        break;
      default:
        throw new IllegalArgumentException("Unknown URI " + uri);
    }

    getContext().getContentResolver().notifyChange(uri, null);
    return count;
  }

  private static class DatabaseHelper extends SQLiteOpenHelper {

    DatabaseHelper(Context context) {
      super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
      db.execSQL("CREATE TABLE " + App.Todo.TABLE_NAME + " ("
                 + App.Todo._ID + " INTEGER PRIMARY KEY,"
                 + App.Todo.COLUMN_TITLE + " TEXT,"
                 + App.Todo.COLUMN_DESCRIPTION + " TEXT,"
                 + App.Todo.COLUMN_STATUS + " INTEGER,"
                 + App.Todo.COLUMN_PLAN_FINISHED_DATE + " INTEGER,"
                 + App.Todo.COLUMN_ACTUAL_FINISHED_DATE + " INTEGER,"
                 + App.Todo.COLUMN_CREATE_DATE + " INTEGER"
                 + ");");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                 + newVersion + ", which will destroy all old data");
      db.execSQL("DROP TABLE IF EXISTS notes");
      onCreate(db);
    }
  }
}




Java Source Code List

lyc.app.AddActivity.java
lyc.app.App.java
lyc.app.BootReceiver.java
lyc.app.DateUtils.java
lyc.app.MainActivity.java
lyc.app.TodoService.java
lyc.app.adapter.TodoCursorAdapter.java
lyc.app.fragment.FinishedTodoFragment.java
lyc.app.fragment.UnFinishedTodoFragment.java
lyc.app.provider.TodoProvider.java
lyc.simplesqlite.annotation.Column.java
lyc.simplesqlite.annotation.Table.java
lyc.simplesqlite.exception.AnnotationNotFoundException.java
lyc.simplesqlite.util.MetaData.java
lyc.simplesqlite.util.StringUtils.java