Android Open Source - PatternAndroid Rest Provider






From Project

Back to project page PatternAndroid.

License

The source code is released under:

MIT License

If you think the Android project PatternAndroid 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.twitterclone.model.provider;
//ww w  . ja v  a 2  s  . co m
import com.twitterclone.model.provider.Contract.Tweets;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;

public class RestProvider extends ContentProvider {
  final String TAG = getClass().getSimpleName();
  
  private static final String TABLE_TWEETS = "tweets";
  
  private static final String DB_NAME = TABLE_TWEETS + ".db";
  private static final int DB_VERSION = 1;
  
  private static final UriMatcher sUriMatcher;
  
  private static final int PATH_ROOT = 0;
  private static final int PATH_TWEETS = 1;
  
  static {
    sUriMatcher = new UriMatcher(PATH_ROOT);
    sUriMatcher.addURI(Contract.AUTHORITY, Contract.Tweets.CONTENT_PATH, PATH_TWEETS); 
  }
  
  private DatabaseHeloper mDatabaseHelper;
  
  class DatabaseHeloper extends SQLiteOpenHelper {

    public DatabaseHeloper(Context context, String name, CursorFactory factory, int version) {
      super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
      String sql = 
        "create table " + TABLE_TWEETS + " (" + 
          Tweets._ID + " integer primary key autoincrement, " +
          Tweets.USER_NAME + " text, " +
          Tweets.BODY + " text, " +
          Tweets.DATE + " text" +
        ")";
      db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
    
  }

  @Override
  public boolean onCreate() {
    mDatabaseHelper = new DatabaseHeloper(getContext(), DB_NAME, null, DB_VERSION);
    return true;
  }
  

  @Override
  public Cursor query(Uri uri, String[] projection, String selection,
      String[] selectionArgs, String sortOrder) {
    switch (sUriMatcher.match(uri)) {
    case PATH_TWEETS: {
      Cursor cursor = mDatabaseHelper.getReadableDatabase().query(TABLE_TWEETS, projection, selection, selectionArgs, null, null, sortOrder);
      cursor.setNotificationUri(getContext().getContentResolver(), Contract.Tweets.CONTENT_URI);
      return cursor;
    }
    default:
      return null;
    }
  }

  @Override
  public String getType(Uri uri) {
    switch (sUriMatcher.match(uri)) {
    case PATH_TWEETS:
      return Contract.Tweets.CONTENT_TYPE;
    default:
      return null;
    }
  }

  @Override
  public Uri insert(Uri uri, ContentValues values) {
    switch (sUriMatcher.match(uri)) {
    case PATH_TWEETS: {
      mDatabaseHelper.getWritableDatabase().insert(TABLE_TWEETS, null, values);
      getContext().getContentResolver().notifyChange(Contract.Tweets.CONTENT_URI, null);
    }
    default:
      return null;
    }
  }

  @Override
  public int delete(Uri uri, String selection, String[] selectionArgs) {
    switch (sUriMatcher.match(uri)) {
    case PATH_TWEETS:
      return mDatabaseHelper.getWritableDatabase().delete(TABLE_TWEETS, selection, selectionArgs);
    default:
      return 0;
    }
  }

  @Override
  public int update(Uri uri, ContentValues values, String selection,
      String[] selectionArgs) {
    switch (sUriMatcher.match(uri)) {
    case PATH_TWEETS:
      return mDatabaseHelper.getWritableDatabase().update(TABLE_TWEETS, values, selection, selectionArgs);
    default:
      return 0;
    }
  }

}




Java Source Code List

android.support.util.Base64.java
android.support.util.LruCache.java
com.foxykeep.datadroid.exception.ConnectionException.java
com.foxykeep.datadroid.exception.CustomRequestException.java
com.foxykeep.datadroid.exception.DataException.java
com.foxykeep.datadroid.internal.network.NetworkConnectionImpl.java
com.foxykeep.datadroid.network.NetworkConnection.java
com.foxykeep.datadroid.network.UserAgentUtils.java
com.foxykeep.datadroid.requestmanager.RequestManager.java
com.foxykeep.datadroid.requestmanager.Request.java
com.foxykeep.datadroid.service.MultiThreadedIntentService.java
com.foxykeep.datadroid.service.RequestService.java
com.foxykeep.datadroid.util.DataDroidLog.java
com.foxykeep.datadroid.util.ObjectUtils.java
com.handmark.pulltorefresh.library.ILoadingLayout.java
com.handmark.pulltorefresh.library.IPullToRefresh.java
com.handmark.pulltorefresh.library.LoadingLayoutProxy.java
com.handmark.pulltorefresh.library.OverscrollHelper.java
com.handmark.pulltorefresh.library.PullToRefreshAdapterViewBase.java
com.handmark.pulltorefresh.library.PullToRefreshBase.java
com.handmark.pulltorefresh.library.PullToRefreshExpandableListView.java
com.handmark.pulltorefresh.library.PullToRefreshGridView.java
com.handmark.pulltorefresh.library.PullToRefreshHorizontalScrollView.java
com.handmark.pulltorefresh.library.PullToRefreshListView.java
com.handmark.pulltorefresh.library.PullToRefreshScrollView.java
com.handmark.pulltorefresh.library.PullToRefreshWebView.java
com.handmark.pulltorefresh.library.extras.PullToRefreshWebView2.java
com.handmark.pulltorefresh.library.extras.SoundPullEventListener.java
com.handmark.pulltorefresh.library.internal.EmptyViewMethodAccessor.java
com.handmark.pulltorefresh.library.internal.FlipLoadingLayout.java
com.handmark.pulltorefresh.library.internal.IndicatorLayout.java
com.handmark.pulltorefresh.library.internal.LoadingLayout.java
com.handmark.pulltorefresh.library.internal.RotateLoadingLayout.java
com.handmark.pulltorefresh.library.internal.Utils.java
com.handmark.pulltorefresh.library.internal.ViewCompat.java
com.twitterclone.model.RequestFactory.java
com.twitterclone.model.RestRequestManager.java
com.twitterclone.model.operations.RestTwitter.java
com.twitterclone.model.operations.TweetsOperation.java
com.twitterclone.model.provider.Contract.java
com.twitterclone.model.provider.RestProvider.java
com.twitterclone.model.service.RestService.java
com.twitterclone.ui.MainActivity.java