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.Intent;
import android.content.SharedPreferences;
import android.os.Debug;
import android.preference.PreferenceManager;
import android.util.Log;
import de.danoeh.antennapod.asynctask.FeedImageLoader;

View File

@ -569,8 +569,7 @@ public class FeedManager {
.getColumnIndex(PodDBAdapter.KEY_LASTUPDATE)));
Feed feed = new Feed(lastUpdate);
feed.id = feedlistCursor.getLong(feedlistCursor
.getColumnIndex(PodDBAdapter.KEY_ID));
feed.id = feedlistCursor.getLong(PodDBAdapter.KEY_INDEX);
feed.setTitle(feedlistCursor.getString(feedlistCursor
.getColumnIndex(PodDBAdapter.KEY_TITLE)));
feed.setLink(feedlistCursor.getString(feedlistCursor
@ -609,12 +608,13 @@ public class FeedManager {
Feed feed, Cursor itemlistCursor, PodDBAdapter adapter) {
Log.d(TAG, "Extracting Feeditems of feed " + feed.getTitle());
ArrayList<FeedItem> items = new ArrayList<FeedItem>();
ArrayList<String> mediaIds = new ArrayList<String>();
if (itemlistCursor.moveToFirst()) {
do {
FeedItem item = new FeedItem();
item.id = itemlistCursor.getLong(itemlistCursor
.getColumnIndex(PodDBAdapter.KEY_ID));
item.id = itemlistCursor.getLong(PodDBAdapter.KEY_INDEX);
item.setFeed(feed);
item.setTitle(itemlistCursor.getString(itemlistCursor
.getColumnIndex(PodDBAdapter.KEY_TITLE)));
@ -631,7 +631,8 @@ public class FeedManager {
long mediaId = itemlistCursor.getLong(itemlistCursor
.getColumnIndex(PodDBAdapter.KEY_MEDIA));
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
.getColumnIndex(PodDBAdapter.KEY_READ)) > 0) ? true
@ -641,37 +642,87 @@ public class FeedManager {
}
// extract chapters
Cursor chapterCursor = adapter
.getSimpleChaptersOfFeedItemCursor(item);
if (chapterCursor.moveToFirst()) {
item.setSimpleChapters(new ArrayList<SimpleChapter>());
do {
SimpleChapter chapter = new SimpleChapter(
chapterCursor
.getLong(chapterCursor
.getColumnIndex(PodDBAdapter.KEY_START)),
chapterCursor.getString(chapterCursor
.getColumnIndex(PodDBAdapter.KEY_TITLE)));
item.getSimpleChapters().add(chapter);
} while (chapterCursor.moveToNext());
boolean hasSimpleChapters = itemlistCursor
.getInt(itemlistCursor
.getColumnIndex(PodDBAdapter.KEY_HAS_SIMPLECHAPTERS)) > 0;
if (hasSimpleChapters) {
Cursor chapterCursor = adapter
.getSimpleChaptersOfFeedItemCursor(item);
if (chapterCursor.moveToFirst()) {
item.setSimpleChapters(new ArrayList<SimpleChapter>());
do {
SimpleChapter chapter = new SimpleChapter(
chapterCursor
.getLong(chapterCursor
.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);
} while (itemlistCursor.moveToNext());
}
extractMediafromFeedItemlist(adapter, items, mediaIds);
Collections.sort(items, new FeedItemPubdateComparator());
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,
PodDBAdapter adapter) {
Log.d(TAG, "Extracting DownloadLog");
Cursor logCursor = adapter.getDownloadLogCursor();
if (logCursor.moveToFirst()) {
do {
long id = logCursor.getLong(logCursor
.getColumnIndex(PodDBAdapter.KEY_ID));
long id = logCursor.getLong(PodDBAdapter.KEY_INDEX);
long feedfileId = logCursor.getLong(logCursor
.getColumnIndex(PodDBAdapter.KEY_FEEDFILE));
int feedfileType = logCursor.getInt(logCursor
@ -708,8 +759,7 @@ public class FeedManager {
Cursor cursor = adapter.getQueueCursor();
if (cursor.moveToFirst()) {
do {
int index = cursor.getInt(cursor
.getColumnIndex(PodDBAdapter.KEY_ID));
int index = cursor.getInt(PodDBAdapter.KEY_INDEX);
Feed feed = getFeed(cursor.getLong(cursor
.getColumnIndex(PodDBAdapter.KEY_FEED)));
if (feed != null) {

View File

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

View File

@ -26,6 +26,8 @@ public class PodDBAdapter {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "Antennapod.db";
public static final int KEY_INDEX = 0;
// Key-constants
public static final String KEY_ID = "id";
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_LANGUAGE = "language";
public static final String KEY_AUTHOR = "author";
public static final String KEY_HAS_SIMPLECHAPTERS = "has_simple_chapters";
// Table names
public static final String TABLE_NAME_FEEDS = "Feeds";
@ -85,7 +88,7 @@ public class PodDBAdapter {
+ " TEXT," + KEY_LINK + " TEXT," + KEY_DESCRIPTION + " TEXT,"
+ KEY_CONTENT_ENCODED + " TEXT," + KEY_PUBDATE + " INTEGER,"
+ 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 "
+ TABLE_NAME_FEED_CATEGORIES + " (" + TABLE_PRIMARY_KEY + KEY_NAME
@ -253,7 +256,10 @@ public class PodDBAdapter {
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) {
db.beginTransaction();
setFeed(feed);
@ -296,12 +302,10 @@ public class PodDBAdapter {
}
values.put(KEY_FEED, item.getFeed().getId());
values.put(KEY_READ, item.isRead());
values.put(KEY_HAS_SIMPLECHAPTERS, item.getSimpleChapters() != null);
if (item.getId() == 0) {
Log.d(TAG, "inserting new feeditem into db");
item.setId(db.insert(TABLE_NAME_FEED_ITEMS, null, values));
} else {
Log.d(TAG, "updating existing feeditem in db");
db.update(TABLE_NAME_FEED_ITEMS, values, KEY_ID + "=?",
new String[] { String.valueOf(item.getId()) });
}
@ -511,7 +515,7 @@ public class PodDBAdapter {
final FeedItem owner) throws SQLException {
Cursor cursor = db.query(TABLE_NAME_FEED_MEDIA, null, KEY_ID + "=?",
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);
}
FeedMedia media = new FeedMedia(rowIndex, owner, cursor.getInt(cursor
@ -526,6 +530,21 @@ public class PodDBAdapter {
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.
*