Android Open Source - HapiPodcastJ Podcast Open Helper






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;
//from   w  w  w .  jav a  2  s  .co  m
import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.TextUtils;
import info.xuluan.podcast.utils.Log;

public class PodcastOpenHelper extends SQLiteOpenHelper {

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

  //Version 12: inherited from original HapiPodcast
  //Version 13: add KEEP column to items table
  //Version 14: add SUSPENDED column to subscriptions table
  private final static int DBVERSION = 14;
  private boolean downgradeEnabled = false; //for debugging
  
  public PodcastOpenHelper(Context context) {
    super(context, "podcast.db", null, DBVERSION);
  }

  //For debugging, allow opening the database using an older version number
  public PodcastOpenHelper(Context context, int version) {
    super(context, "podcast.db", null, version);
    downgradeEnabled = true;
  }
  
  @Override
  public void onCreate(SQLiteDatabase db) {

    db.execSQL(SubscriptionColumns.sql_create_table(DBVERSION));
    db.execSQL(SubscriptionColumns.sql_index_subs_url);
    db.execSQL(SubscriptionColumns.sql_index_last_update);

    db.execSQL(ItemColumns.sql_create_table);
    db.execSQL(ItemColumns.sql_index_item_res);
    db.execSQL(ItemColumns.sql_index_item_created);

    db.execSQL(SubscriptionColumns.sql_insert_default);
    db.execSQL(SubscriptionColumns.sql_insert_default1);
    db.execSQL(SubscriptionColumns.sql_insert_default2);
    //db.execSQL(SubscriptionColumns.sql_insert_default3);
    //db.execSQL(SubscriptionColumns.sql_insert_default4);

  }

  @SuppressLint("NewApi")
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (oldVersion>=newVersion) {
      if ((newVersion < oldVersion) && downgradeEnabled) {
        onDowngrade(db, oldVersion, newVersion);
      }
      return;
    }
    
    if (oldVersion < 12) {
      log.debug("Database is version "+oldVersion+", drop and recreate");
      db.execSQL("DROP TABLE " + ItemColumns.TABLE_NAME);
      db.execSQL("DROP TABLE " + SubscriptionColumns.TABLE_NAME);
      onCreate(db);
      return;
    }
    
    log.debug("Upgrading database from version "+oldVersion+" to "+newVersion);      
    if (oldVersion<=12) {
      //Add the KEEP column to the items table,
      //use that rather than the old KEEP status
      log.debug("executing sql: "+ItemColumns.sql_upgrade_table_add_keep_column);
      db.execSQL(ItemColumns.sql_upgrade_table_add_keep_column);
      log.debug("executing sql: "+ItemColumns.sql_populate_keep_from_status);
      db.execSQL(ItemColumns.sql_populate_keep_from_status);
      log.debug("executing sql: "+ItemColumns.sql_change_keep_status_to_played);
      db.execSQL(ItemColumns.sql_change_keep_status_to_played);
      log.debug("Done upgrading database");
    }
    if (oldVersion<=13) {
      //Add the SUSPENDED column to the subscriptions table
      log.debug("executing sql: "+SubscriptionColumns.sql_upgrade_subscriptions_add_suspended_column);
      db.execSQL(SubscriptionColumns.sql_upgrade_subscriptions_add_suspended_column);
    }
  }

  //For debugging, allow downgrading the database.
  //Before API 11, this method did not exist in the API, and onUpgrade would be called whenever
  //oldVersion!=newVersion, even when oldVersion>newVersion.
  //Starting with API 11, the default behavior is to throw an exception if
  //oldVersion>newVersion and there is no onDowngrade. By defining our own onDowngrade
  //and putting code in onUpgrade to call us when oldVersion>newVersion, we end up
  //executing this method on a downgrade for any API version.
  public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (oldVersion<=newVersion)
      return;
    
    if (newVersion<14 && oldVersion>=14) {
      //Remove the stuff added in version 14
      dropTableColumn(db, SubscriptionColumns.TABLE_NAME, SubscriptionColumns.SUSPENDED,
          SubscriptionColumns.sql_create_table(13));
    }
    
    if (newVersion<13 && oldVersion>=13) {
      //Remove stuff added in version 13
      //TODO - for each item marked in the KEEP columns of the items table,
      // if the status is played, change it to keep.
      //TODO - remove the KEEP column from the items table
    }
  }
  
  private void dropTableColumn(SQLiteDatabase db, String table, String dropColumn,
      String createTableSql) {
    List<String> columnList = tableColumns(db, table, dropColumn);
    String columnNames = TextUtils.join(",", columnList);
    String oldTable = table + "_old";
    db.execSQL("ALTER TABLE "+table+" RENAME TO "+oldTable+";");
    db.execSQL(createTableSql);
    db.execSQL("INSERT INTO "+table+" SELECT "+columnNames+" FROM "+oldTable+";");
    db.execSQL("DROP TABLE "+oldTable+";");
  }
  
  private List<String> tableColumns(SQLiteDatabase db, String tableName, String excludingColumn) { 
    ArrayList<String> columns = new ArrayList<String>();
    String columnsCommand = "PRAGMA TABLE_INFO(" + tableName + ");";
    Cursor columnsCursor = db.rawQuery(columnsCommand, null);
    int nameColIndex = columnsCursor.getColumnIndex("name");
    while (columnsCursor.moveToNext()) {
      String column = columnsCursor.getString(nameColIndex);
      if (!column.equals(excludingColumn))
        columns.add(column);
    }
    columnsCursor.close();
    return columns;
  }

}




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