package jp.inaka.mironal.android.twironal.content;
import java.util.HashMap;
import jp.inaka.mironal.android.twironal.content.tweet.TweetContents;
import jp.inaka.mironal.android.twironal.content.tweet.TwitterOpenHelper;
import jp.inaka.mironal.android.twironal.content.user.UserContents;
import android.content.ContentProvider;
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;
public class TimelineProvider extends ContentProvider{
private static final int TIMELINE = 1;
private static UriMatcher URI_MATCHER;
private static HashMap<String,String > sTimelineProjectionMap;
static {
URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
URI_MATCHER.addURI( TimelineContents.AUTHORITY
, TimelineContents.PATH
, TIMELINE);
sTimelineProjectionMap = new HashMap<String, String>();
sTimelineProjectionMap.put(TwitterOpenHelper.TWEET_TABLE
+"."
+TimelineContents._ID,
TwitterOpenHelper.TWEET_TABLE
+"."
+TimelineContents._ID );
sTimelineProjectionMap.put(TimelineContents.NAME, TimelineContents.NAME);
sTimelineProjectionMap.put(TimelineContents.CREATE_AT, TimelineContents.CREATE_AT);
sTimelineProjectionMap.put(TimelineContents.TWEET, TimelineContents.TWEET);
sTimelineProjectionMap.put(TimelineContents.IMAGE, TimelineContents.IMAGE);
}
private TwitterOpenHelper mOpenHelper;
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
throw new UnsupportedOperationException("read onry.");
}
@Override
public String getType(Uri uri) {
// TODO \bhEX^u
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
throw new UnsupportedOperationException("read onry.");
}
@Override
public boolean onCreate() {
mOpenHelper = new TwitterOpenHelper(getContext());
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
String sortOrder) {
if( URI_MATCHER.match(uri) != TIMELINE ){
throw new IllegalArgumentException();
}
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables( TwitterOpenHelper.TWEET_TABLE
+ " INNER JOIN "
+ TwitterOpenHelper.USER_TABLE
+ " ON ( "
+ TwitterOpenHelper.TWEET_TABLE
+ "."
+ TweetContents.USER_ID
+ " = "
+ TwitterOpenHelper.USER_TABLE
+ "."
+ UserContents._ID
+ ")" );
qb.setProjectionMap(sTimelineProjectionMap);
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
throw new UnsupportedOperationException("read onry.");
}
}
|