Android Open Source - HapiPodcastJ Podcast Provider






From Project

Back to project page HapiPodcastJ.

License

The source code is released under:

GNU General Public License

If you think the Android project HapiPodcastJ 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 info.xuluan.podcast.provider;
/*  ww  w. j ava  2 s. c  o  m*/
import info.xuluan.podcast.utils.Log;

import java.util.HashMap;
import java.util.Locale;

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;

public class PodcastProvider extends ContentProvider {

  public static final String AUTHORITY = PodcastProvider.class.getName()
      .toLowerCase(Locale.getDefault());

  private static final int TYPE_ALL_SUBSCRIPTIONS = 0;
  private static final int TYPE_SINGLE_SUBSCRIPTION = 1;
  private static final int TYPE_ALL_ITEMS = 2;
  private static final int TYPE_SINGLE_ITEM = 3;

  private final Log log = Log.getLog(getClass());

  private PodcastOpenHelper mHelper = null;

  private static HashMap<String, String> sItemProjectionMap = null;
  private static HashMap<String, String> sSubProjectionMap = null;

  private static final UriMatcher matcher = new UriMatcher(
      UriMatcher.NO_MATCH);

  static {
    matcher.addURI(AUTHORITY, "subscriptions", TYPE_ALL_SUBSCRIPTIONS);
    matcher.addURI(AUTHORITY, "subscriptions/#", TYPE_SINGLE_SUBSCRIPTION);
    matcher.addURI(AUTHORITY, "items", TYPE_ALL_ITEMS);
    matcher.addURI(AUTHORITY, "items/#", TYPE_SINGLE_ITEM);
  }

  @Override
  public boolean onCreate() {
    mHelper = new PodcastOpenHelper(getContext());
    if (sItemProjectionMap == null) {
      sItemProjectionMap = new HashMap<String, String>();
      for (int i = 0; i < ItemColumns.ALL_COLUMNS.length; i++) {
        sItemProjectionMap.put(ItemColumns.ALL_COLUMNS[i],
            ItemColumns.ALL_COLUMNS[i]);
      }

    }
    if (sSubProjectionMap == null) {
      sSubProjectionMap = new HashMap<String, String>();
      for (int i = 0; i < SubscriptionColumns.ALL_COLUMNS.length; i++) {
        sSubProjectionMap.put(SubscriptionColumns.ALL_COLUMNS[i],
            SubscriptionColumns.ALL_COLUMNS[i]);
      }
    }
    return true;
  }

  @Override
  public Cursor query(Uri uri, String[] projection, String selection,
      String[] selectionArgs, String sort) {
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    String orderBy = null;

    switch (matcher.match(uri)) {
    case TYPE_ALL_SUBSCRIPTIONS:
      qb.setTables(SubscriptionColumns.TABLE_NAME);
      qb.setProjectionMap(sSubProjectionMap);

      if (TextUtils.isEmpty(sort)) {
        orderBy = SubscriptionColumns.DEFAULT_SORT_ORDER;
      } else {
        orderBy = sort;
      }
      // c = db.query(SubscriptionColumns.TABLE_NAME, projection,
      // selection, selectionArgs, null, null, orderBy);
      break;
    case TYPE_SINGLE_SUBSCRIPTION:
      qb.setTables(SubscriptionColumns.TABLE_NAME);
      qb.setProjectionMap(sSubProjectionMap);
      qb.appendWhere(SubscriptionColumns._ID + "="
          + uri.getPathSegments().get(1));

      // String s_id = uri.getPathSegments().get(1);
      // c = db.query(SubscriptionColumns.TABLE_NAME, projection,
      // SubscriptionColumns._ID + "=" + s_id, null, null, null, null);
      break;
    case TYPE_ALL_ITEMS:
      qb.setTables(ItemColumns.TABLE_NAME);
      qb.setProjectionMap(sItemProjectionMap);

      if (TextUtils.isEmpty(sort)) {
        orderBy = ItemColumns.DEFAULT_SORT_ORDER;
      } else {
        orderBy = sort;
      }
      // c = db.query(ItemColumns.TABLE_NAME, projection, selection,
      // selectionArgs, null, null, orderBy);
      break;
    case TYPE_SINGLE_ITEM:
      qb.setTables(ItemColumns.TABLE_NAME);
      qb.setProjectionMap(sItemProjectionMap);
      qb
          .appendWhere(ItemColumns._ID + "="
              + uri.getPathSegments().get(1));
      // String i_id = uri.getPathSegments().get(1);
      // c = db.query(ItemColumns.TABLE_NAME, projection, ItemColumns._ID
      // + "=" + i_id, null, null, null, null);
      break;
    default:
      throw new IllegalArgumentException("Unknown URI " + uri);
    }
    SQLiteDatabase db = mHelper.getReadableDatabase();
    Cursor c = qb.query(db, projection, selection, selectionArgs, null,
        null, orderBy);

    c.setNotificationUri(getContext().getContentResolver(), uri);
    return c;
  }

  @Override
  public Uri insert(Uri uri, ContentValues initialValues) {
    SQLiteDatabase db = mHelper.getWritableDatabase();
    Uri newUri = null;
    ContentValues values = null;
    long id = 0L;
    switch (matcher.match(uri)) {
    case TYPE_ALL_SUBSCRIPTIONS:
      try {
        values = SubscriptionColumns.checkValues(initialValues, uri);

        id = db.insertOrThrow(SubscriptionColumns.TABLE_NAME,
            SubscriptionColumns.URL, values);

        if (id > 0) {
          newUri = ContentUris.withAppendedId(
              SubscriptionColumns.URI, id);
          getContext().getContentResolver()
              .notifyChange(newUri, null);
          return newUri;
        }
      } catch (Exception e) {
        log.warn("Failed to insert sub into", e);
        return null;
      }

    case TYPE_ALL_ITEMS:

      try {
        values = ItemColumns.checkValues(initialValues, uri);
        id = db.insertOrThrow(ItemColumns.TABLE_NAME,
            ItemColumns.RESOURCE, values);
        if (id > 0) {
          newUri = ContentUris.withAppendedId(ItemColumns.URI, id);
          getContext().getContentResolver()
              .notifyChange(newUri, null);
          return newUri;
        }
      } catch (Exception e) {
        log.warn("Failed to insert item into", e);
        return null;
      }
    }
    throw new IllegalArgumentException("Illegal Uri: " + uri.toString());
  }

  @Override
  public int delete(Uri uri, String where, String[] whereArgs) {
    SQLiteDatabase db = mHelper.getWritableDatabase();
    int count = 0;
    switch (matcher.match(uri)) {
    case TYPE_ALL_SUBSCRIPTIONS:
      count = db.delete(SubscriptionColumns.TABLE_NAME, where, whereArgs);
      break;
    case TYPE_SINGLE_SUBSCRIPTION:
      String s_id = uri.getPathSegments().get(1);
      count = db.delete(SubscriptionColumns.TABLE_NAME,
          SubscriptionColumns._ID
              + "="
              + s_id
              + (!TextUtils.isEmpty(where) ? " AND (" + where
                  + ')' : ""), whereArgs);
      break;
    case TYPE_ALL_ITEMS:
      count = db.delete(ItemColumns.TABLE_NAME, where, whereArgs);
      break;
    case TYPE_SINGLE_ITEM:
      String i_id = uri.getPathSegments().get(1);
      count = db.delete(ItemColumns.TABLE_NAME,
          ItemColumns._ID
              + "="
              + i_id
              + (!TextUtils.isEmpty(where) ? " AND (" + where
                  + ')' : ""), whereArgs);
      break;

    default:
      throw new IllegalArgumentException("Unknown URI " + uri);
    }

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

  @Override
  public int update(Uri uri, ContentValues values, String where,
      String[] whereArgs) {
    SQLiteDatabase db = mHelper.getWritableDatabase();
    int count;
    switch (matcher.match(uri)) {
    case TYPE_ALL_SUBSCRIPTIONS:
      count = db.update(SubscriptionColumns.TABLE_NAME, values, where,
          whereArgs);
      break;

    case TYPE_ALL_ITEMS:
      count = db.update(ItemColumns.TABLE_NAME, values, where, whereArgs);
      break;

    default:
      throw new IllegalArgumentException("Unknown URI " + uri);
    }

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

  @Override
  public String getType(Uri uri) {
    switch (matcher.match(uri)) {
    case TYPE_ALL_SUBSCRIPTIONS:
      return "vnd.android.cursor.dir/vnd.xuluan.podcast.subscription";
    case TYPE_SINGLE_SUBSCRIPTION:
      return "vnd.android.cursor.item/vnd.xuluan.podcast.subscription";
    case TYPE_ALL_ITEMS:
      return "vnd.android.cursor.dir/vnd.xuluan.podcast.item";
    case TYPE_SINGLE_ITEM:
      return "vnd.android.cursor.item/vnd.xuluan.podcast.item";
    }
    throw new IllegalArgumentException("Unsupported URI: " + uri);
  }

  @Override
  public void onLowMemory() {
    if (mHelper != null) {
      SQLiteDatabase db = mHelper.getWritableDatabase();
      db.close();
      mHelper.close();

    }
  }

}




Java Source Code List

info.xuluan.podcast.AddChannelActivity.java
info.xuluan.podcast.BackupChannelsActivity.java
info.xuluan.podcast.ChannelActivity.java
info.xuluan.podcast.ChannelDetailsActivity.java
info.xuluan.podcast.ChannelsActivity.java
info.xuluan.podcast.DownloadActivity.java
info.xuluan.podcast.EpisodeDetailsActivity.java
info.xuluan.podcast.EpisodeIcons.java
info.xuluan.podcast.EpisodesActivity.java
info.xuluan.podcast.FlingGestureDetector.java
info.xuluan.podcast.Flingable.java
info.xuluan.podcast.HapiActivity.java
info.xuluan.podcast.HapiListActivity.java
info.xuluan.podcast.HapiPreferenceActivity.java
info.xuluan.podcast.HomeActivity.java
info.xuluan.podcast.MainActivity.java
info.xuluan.podcast.PlayerActivity.java
info.xuluan.podcast.PodcastBaseActivity.java
info.xuluan.podcast.PodcastTab.java
info.xuluan.podcast.Pref.java
info.xuluan.podcast.SearchActivity.java
info.xuluan.podcast.StartupActivity.java
info.xuluan.podcast.TabsHelper.java
info.xuluan.podcast.actionbar.ActionBarHelperBase.java
info.xuluan.podcast.actionbar.ActionBarHelperHoneycomb.java
info.xuluan.podcast.actionbar.ActionBarHelperICS.java
info.xuluan.podcast.actionbar.ActionBarHelper.java
info.xuluan.podcast.actionbar.SimpleMenuItem.java
info.xuluan.podcast.actionbar.SimpleMenu.java
info.xuluan.podcast.fetcher.FeedFetcher.java
info.xuluan.podcast.fetcher.Response.java
info.xuluan.podcast.parser.FeedHandler.java
info.xuluan.podcast.parser.FeedParserHandler.java
info.xuluan.podcast.parser.FeedParserListenerInterface.java
info.xuluan.podcast.parser.FeedParserListener.java
info.xuluan.podcast.parser.FeedParser.java
info.xuluan.podcast.parser.OPMLParserHandler.java
info.xuluan.podcast.parser.SearchItem.java
info.xuluan.podcast.provider.FeedItem.java
info.xuluan.podcast.provider.ItemColumns.java
info.xuluan.podcast.provider.PodcastOpenHelper.java
info.xuluan.podcast.provider.PodcastProvider.java
info.xuluan.podcast.provider.SubscriptionColumns.java
info.xuluan.podcast.provider.Subscription.java
info.xuluan.podcast.service.PlayerService.java
info.xuluan.podcast.service.PodcastService.java
info.xuluan.podcast.utils.DialogMenu.java
info.xuluan.podcast.utils.FileUtils.java
info.xuluan.podcast.utils.IconCursorAdapter.java
info.xuluan.podcast.utils.LabeledFrame.java
info.xuluan.podcast.utils.LockHandler.java
info.xuluan.podcast.utils.Log.java
info.xuluan.podcast.utils.SDCardMgr.java
info.xuluan.podcast.utils.StrUtils.java
info.xuluan.podcast.utils.ZipExporter.java
info.xuluan.podcast.utils.ZipImporter.java