Improved startup performance

This commit is contained in:
daniel oeh 2012-07-14 13:54:09 +02:00
parent bbcdeecc40
commit 96d9421970
4 changed files with 110 additions and 32 deletions

View File

@ -8,6 +8,7 @@ import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Debug;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import de.danoeh.antennapod.asynctask.FeedImageLoader; import de.danoeh.antennapod.asynctask.FeedImageLoader;

View File

@ -569,8 +569,7 @@ public class FeedManager {
.getColumnIndex(PodDBAdapter.KEY_LASTUPDATE))); .getColumnIndex(PodDBAdapter.KEY_LASTUPDATE)));
Feed feed = new Feed(lastUpdate); Feed feed = new Feed(lastUpdate);
feed.id = feedlistCursor.getLong(feedlistCursor feed.id = feedlistCursor.getLong(PodDBAdapter.KEY_INDEX);
.getColumnIndex(PodDBAdapter.KEY_ID));
feed.setTitle(feedlistCursor.getString(feedlistCursor feed.setTitle(feedlistCursor.getString(feedlistCursor
.getColumnIndex(PodDBAdapter.KEY_TITLE))); .getColumnIndex(PodDBAdapter.KEY_TITLE)));
feed.setLink(feedlistCursor.getString(feedlistCursor feed.setLink(feedlistCursor.getString(feedlistCursor
@ -609,12 +608,13 @@ public class FeedManager {
Feed feed, Cursor itemlistCursor, PodDBAdapter adapter) { Feed feed, Cursor itemlistCursor, PodDBAdapter adapter) {
Log.d(TAG, "Extracting Feeditems of feed " + feed.getTitle()); Log.d(TAG, "Extracting Feeditems of feed " + feed.getTitle());
ArrayList<FeedItem> items = new ArrayList<FeedItem>(); ArrayList<FeedItem> items = new ArrayList<FeedItem>();
ArrayList<String> mediaIds = new ArrayList<String>();
if (itemlistCursor.moveToFirst()) { if (itemlistCursor.moveToFirst()) {
do { do {
FeedItem item = new FeedItem(); FeedItem item = new FeedItem();
item.id = itemlistCursor.getLong(itemlistCursor item.id = itemlistCursor.getLong(PodDBAdapter.KEY_INDEX);
.getColumnIndex(PodDBAdapter.KEY_ID));
item.setFeed(feed); item.setFeed(feed);
item.setTitle(itemlistCursor.getString(itemlistCursor item.setTitle(itemlistCursor.getString(itemlistCursor
.getColumnIndex(PodDBAdapter.KEY_TITLE))); .getColumnIndex(PodDBAdapter.KEY_TITLE)));
@ -631,7 +631,8 @@ public class FeedManager {
long mediaId = itemlistCursor.getLong(itemlistCursor long mediaId = itemlistCursor.getLong(itemlistCursor
.getColumnIndex(PodDBAdapter.KEY_MEDIA)); .getColumnIndex(PodDBAdapter.KEY_MEDIA));
if (mediaId != 0) { if (mediaId != 0) {
item.setMedia(adapter.getFeedMedia(mediaId, item)); mediaIds.add(String.valueOf(mediaId));
item.setMedia(new FeedMedia(mediaId, item));
} }
item.read = (itemlistCursor.getInt(itemlistCursor item.read = (itemlistCursor.getInt(itemlistCursor
.getColumnIndex(PodDBAdapter.KEY_READ)) > 0) ? true .getColumnIndex(PodDBAdapter.KEY_READ)) > 0) ? true
@ -641,37 +642,87 @@ public class FeedManager {
} }
// extract chapters // extract chapters
Cursor chapterCursor = adapter boolean hasSimpleChapters = itemlistCursor
.getSimpleChaptersOfFeedItemCursor(item); .getInt(itemlistCursor
if (chapterCursor.moveToFirst()) { .getColumnIndex(PodDBAdapter.KEY_HAS_SIMPLECHAPTERS)) > 0;
item.setSimpleChapters(new ArrayList<SimpleChapter>()); if (hasSimpleChapters) {
do { Cursor chapterCursor = adapter
SimpleChapter chapter = new SimpleChapter( .getSimpleChaptersOfFeedItemCursor(item);
chapterCursor if (chapterCursor.moveToFirst()) {
.getLong(chapterCursor item.setSimpleChapters(new ArrayList<SimpleChapter>());
.getColumnIndex(PodDBAdapter.KEY_START)), do {
chapterCursor.getString(chapterCursor SimpleChapter chapter = new SimpleChapter(
.getColumnIndex(PodDBAdapter.KEY_TITLE))); chapterCursor
item.getSimpleChapters().add(chapter); .getLong(chapterCursor
} while (chapterCursor.moveToNext()); .getColumnIndex(PodDBAdapter.KEY_START)),
chapterCursor.getString(chapterCursor
.getColumnIndex(PodDBAdapter.KEY_TITLE)));
item.getSimpleChapters().add(chapter);
} while (chapterCursor.moveToNext());
}
chapterCursor.close();
} }
chapterCursor.close();
items.add(item); items.add(item);
} while (itemlistCursor.moveToNext()); } while (itemlistCursor.moveToNext());
} }
extractMediafromFeedItemlist(adapter, items, mediaIds);
Collections.sort(items, new FeedItemPubdateComparator()); Collections.sort(items, new FeedItemPubdateComparator());
return items; return items;
} }
private void extractMediafromFeedItemlist(PodDBAdapter adapter,
ArrayList<FeedItem> items, ArrayList<String> mediaIds) {
ArrayList<FeedItem> itemsCopy = new ArrayList<FeedItem>(items);
Cursor cursor = adapter.getFeedMediaCursor(mediaIds
.toArray(new String[mediaIds.size()]));
if (cursor.moveToFirst()) {
do {
long mediaId = cursor.getLong(PodDBAdapter.KEY_INDEX);
// find matching feed item
FeedItem item = getMatchingItemForMedia(mediaId, itemsCopy);
itemsCopy.remove(item);
if (item != null) {
item.setMedia(new FeedMedia(
mediaId,
item,
cursor.getInt(cursor
.getColumnIndex(PodDBAdapter.KEY_DURATION)),
cursor.getInt(cursor
.getColumnIndex(PodDBAdapter.KEY_POSITION)),
cursor.getLong(cursor
.getColumnIndex(PodDBAdapter.KEY_SIZE)),
cursor.getString(cursor
.getColumnIndex(PodDBAdapter.KEY_MIME_TYPE)),
cursor.getString(cursor
.getColumnIndex(PodDBAdapter.KEY_FILE_URL)),
cursor.getString(cursor
.getColumnIndex(PodDBAdapter.KEY_DOWNLOAD_URL)),
cursor.getInt(cursor
.getColumnIndex(PodDBAdapter.KEY_DOWNLOADED)) > 0));
}
} while (cursor.moveToNext());
cursor.close();
}
}
private FeedItem getMatchingItemForMedia(long mediaId,
ArrayList<FeedItem> items) {
for (FeedItem item : items) {
if (item.getMedia() != null && item.getMedia().getId() == mediaId) {
return item;
}
}
return null;
}
private void extractDownloadLogFromCursor(Context context, private void extractDownloadLogFromCursor(Context context,
PodDBAdapter adapter) { PodDBAdapter adapter) {
Log.d(TAG, "Extracting DownloadLog"); Log.d(TAG, "Extracting DownloadLog");
Cursor logCursor = adapter.getDownloadLogCursor(); Cursor logCursor = adapter.getDownloadLogCursor();
if (logCursor.moveToFirst()) { if (logCursor.moveToFirst()) {
do { do {
long id = logCursor.getLong(logCursor long id = logCursor.getLong(PodDBAdapter.KEY_INDEX);
.getColumnIndex(PodDBAdapter.KEY_ID));
long feedfileId = logCursor.getLong(logCursor long feedfileId = logCursor.getLong(logCursor
.getColumnIndex(PodDBAdapter.KEY_FEEDFILE)); .getColumnIndex(PodDBAdapter.KEY_FEEDFILE));
int feedfileType = logCursor.getInt(logCursor int feedfileType = logCursor.getInt(logCursor
@ -708,8 +759,7 @@ public class FeedManager {
Cursor cursor = adapter.getQueueCursor(); Cursor cursor = adapter.getQueueCursor();
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
do { do {
int index = cursor.getInt(cursor int index = cursor.getInt(PodDBAdapter.KEY_INDEX);
.getColumnIndex(PodDBAdapter.KEY_ID));
Feed feed = getFeed(cursor.getLong(cursor Feed feed = getFeed(cursor.getLong(cursor
.getColumnIndex(PodDBAdapter.KEY_FEED))); .getColumnIndex(PodDBAdapter.KEY_FEED)));
if (feed != null) { if (feed != null) {

View File

@ -24,6 +24,14 @@ public class FeedMedia extends FeedFile{
this.size = size; this.size = size;
this.mime_type = mime_type; this.mime_type = mime_type;
} }
public FeedMedia(long id,FeedItem item) {
super();
this.id = id;
this.item = item;
}
public int getDuration() { public int getDuration() {
return duration; return duration;

View File

@ -26,6 +26,8 @@ public class PodDBAdapter {
private static final int DATABASE_VERSION = 1; private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "Antennapod.db"; private static final String DATABASE_NAME = "Antennapod.db";
public static final int KEY_INDEX = 0;
// Key-constants // Key-constants
public static final String KEY_ID = "id"; public static final String KEY_ID = "id";
public static final String KEY_TITLE = "title"; public static final String KEY_TITLE = "title";
@ -57,6 +59,7 @@ public class PodDBAdapter {
public static final String KEY_START = "start"; public static final String KEY_START = "start";
public static final String KEY_LANGUAGE = "language"; public static final String KEY_LANGUAGE = "language";
public static final String KEY_AUTHOR = "author"; public static final String KEY_AUTHOR = "author";
public static final String KEY_HAS_SIMPLECHAPTERS = "has_simple_chapters";
// Table names // Table names
public static final String TABLE_NAME_FEEDS = "Feeds"; public static final String TABLE_NAME_FEEDS = "Feeds";
@ -85,7 +88,7 @@ public class PodDBAdapter {
+ " TEXT," + KEY_LINK + " TEXT," + KEY_DESCRIPTION + " TEXT," + " TEXT," + KEY_LINK + " TEXT," + KEY_DESCRIPTION + " TEXT,"
+ KEY_CONTENT_ENCODED + " TEXT," + KEY_PUBDATE + " INTEGER," + KEY_CONTENT_ENCODED + " TEXT," + KEY_PUBDATE + " INTEGER,"
+ KEY_MEDIA + " INTEGER," + KEY_FEED + " INTEGER," + KEY_READ + KEY_MEDIA + " INTEGER," + KEY_FEED + " INTEGER," + KEY_READ
+ " INTEGER," + KEY_PAYMENT_LINK + " TEXT)"; + " INTEGER," + KEY_PAYMENT_LINK + " TEXT," + KEY_HAS_SIMPLECHAPTERS + " INTEGER)";
private static final String CREATE_TABLE_FEED_CATEGORIES = "CREATE TABLE " private static final String CREATE_TABLE_FEED_CATEGORIES = "CREATE TABLE "
+ TABLE_NAME_FEED_CATEGORIES + " (" + TABLE_PRIMARY_KEY + KEY_NAME + TABLE_NAME_FEED_CATEGORIES + " (" + TABLE_PRIMARY_KEY + KEY_NAME
@ -252,8 +255,11 @@ public class PodDBAdapter {
} }
return media.getId(); return media.getId();
} }
/** Insert all FeedItems of a feed and the feed object itself in a single transaction */ /**
* Insert all FeedItems of a feed and the feed object itself in a single
* transaction
*/
public void setCompleteFeed(Feed feed) { public void setCompleteFeed(Feed feed) {
db.beginTransaction(); db.beginTransaction();
setFeed(feed); setFeed(feed);
@ -263,7 +269,7 @@ public class PodDBAdapter {
db.setTransactionSuccessful(); db.setTransactionSuccessful();
db.endTransaction(); db.endTransaction();
} }
public long setSingleFeedItem(FeedItem item) { public long setSingleFeedItem(FeedItem item) {
db.beginTransaction(); db.beginTransaction();
long result = setFeedItem(item); long result = setFeedItem(item);
@ -296,12 +302,10 @@ public class PodDBAdapter {
} }
values.put(KEY_FEED, item.getFeed().getId()); values.put(KEY_FEED, item.getFeed().getId());
values.put(KEY_READ, item.isRead()); values.put(KEY_READ, item.isRead());
values.put(KEY_HAS_SIMPLECHAPTERS, item.getSimpleChapters() != null);
if (item.getId() == 0) { if (item.getId() == 0) {
Log.d(TAG, "inserting new feeditem into db");
item.setId(db.insert(TABLE_NAME_FEED_ITEMS, null, values)); item.setId(db.insert(TABLE_NAME_FEED_ITEMS, null, values));
} else { } else {
Log.d(TAG, "updating existing feeditem in db");
db.update(TABLE_NAME_FEED_ITEMS, values, KEY_ID + "=?", db.update(TABLE_NAME_FEED_ITEMS, values, KEY_ID + "=?",
new String[] { String.valueOf(item.getId()) }); new String[] { String.valueOf(item.getId()) });
} }
@ -511,7 +515,7 @@ public class PodDBAdapter {
final FeedItem owner) throws SQLException { final FeedItem owner) throws SQLException {
Cursor cursor = db.query(TABLE_NAME_FEED_MEDIA, null, KEY_ID + "=?", Cursor cursor = db.query(TABLE_NAME_FEED_MEDIA, null, KEY_ID + "=?",
new String[] { String.valueOf(rowIndex) }, null, null, null); new String[] { String.valueOf(rowIndex) }, null, null, null);
if ((cursor.getCount() == 0) || !cursor.moveToFirst()) { if (!cursor.moveToFirst()) {
throw new SQLException("No FeedMedia found at index: " + rowIndex); throw new SQLException("No FeedMedia found at index: " + rowIndex);
} }
FeedMedia media = new FeedMedia(rowIndex, owner, cursor.getInt(cursor FeedMedia media = new FeedMedia(rowIndex, owner, cursor.getInt(cursor
@ -526,6 +530,21 @@ public class PodDBAdapter {
return media; return media;
} }
public final Cursor getFeedMediaCursor(String... mediaIds) {
return db.query(TABLE_NAME_FEED_MEDIA, null, KEY_ID + " IN "
+ buildInOperator(mediaIds.length), mediaIds, null, null, null);
}
/** Builds an IN-operator argument depending on the number of items. */
private String buildInOperator(int size) {
StringBuffer buffer = new StringBuffer("(");
for (int i = 0; i <= size; i++) {
buffer.append("?,");
}
buffer.append("?)");
return buffer.toString();
}
/** /**
* Searches the DB for a FeedImage of the given id. * Searches the DB for a FeedImage of the given id.
* *