Improved performance when removing something

This commit is contained in:
daniel oeh 2012-06-27 12:22:06 +02:00
parent 18992e4b37
commit 18e8212981
3 changed files with 87 additions and 40 deletions

View File

@ -108,7 +108,7 @@ public class FeedManager {
/** Remove a feed with all its items and media files and its image. */
public boolean deleteFeed(Context context, Feed feed) {
PodDBAdapter adapter = new PodDBAdapter(context);
adapter.open();
// delete image file
if (feed.getImage() != null) {
if (feed.getImage().isDownloaded()
@ -123,14 +123,16 @@ public class FeedManager {
unreadItems.remove(item);
}
if (queue.contains(item)) {
removeQueueItem(context, item);
removeQueueItem(item, adapter);
}
if (item.getMedia() != null && item.getMedia().isDownloaded()) {
File mediaFile = new File(item.getMedia().getFile_url());
mediaFile.delete();
}
}
adapter.removeFeed(feed);
adapter.close();
return feeds.remove(feed);
}
@ -176,15 +178,29 @@ public class FeedManager {
if (downloadLog.size() > DOWNLOAD_LOG_SIZE) {
adapter.removeDownloadStatus(downloadLog.remove(0));
}
return adapter.setDownloadStatus(status);
adapter.open();
long result = adapter.setDownloadStatus(status);
adapter.close();
return result;
}
public void addQueueItem(Context context, FeedItem item) {
PodDBAdapter adapter = new PodDBAdapter(context);
queue.add(item);
adapter.open();
adapter.setQueue(queue);
adapter.close();
}
/** Uses external adapter. */
public void removeQueueItem(FeedItem item, PodDBAdapter adapter) {
boolean removed = queue.remove(item);
if (removed) {
adapter.setQueue(queue);
}
}
/** Uses its own adapter. */
public void removeQueueItem(Context context, FeedItem item) {
boolean removed = queue.remove(item);
if (removed) {
@ -203,10 +219,13 @@ public class FeedManager {
private void addNewFeed(Context context, Feed feed) {
feeds.add(feed);
feed.setId(setFeed(context, feed));
PodDBAdapter adapter = new PodDBAdapter(context);
adapter.open();
feed.setId(setFeed(feed, adapter));
for (FeedItem item : feed.getItems()) {
setFeedItem(context, item);
setFeedItem(item, adapter);
}
adapter.close();
}
/*
@ -269,27 +288,79 @@ public class FeedManager {
return null;
}
/** Updates Information of an existing Feed */
/** Updates Information of an existing Feed. Uses external adapter. */
public long setFeed(Feed feed, PodDBAdapter adapter) {
if (adapter != null) {
return adapter.setFeed(feed);
} else {
Log.w(TAG, "Adapter in setFeed was null");
return 0;
}
}
/** Updates Information of an existing Feeditem. Uses external adapter. */
public long setFeedItem(FeedItem item, PodDBAdapter adapter) {
if (adapter != null) {
return adapter.setFeedItem(item);
} else {
Log.w(TAG, "Adapter in setFeedItem was null");
return 0;
}
}
/** Updates Information of an existing Feedimage. Uses external adapter. */
public long setFeedImage(FeedImage image, PodDBAdapter adapter) {
if (adapter != null) {
return adapter.setImage(image);
} else {
Log.w(TAG, "Adapter in setFeedImage was null");
return 0;
}
}
/** Updates Information of an existing Feedmedia object. Uses external adapter. */
public long setFeedImage(FeedMedia media, PodDBAdapter adapter) {
if (adapter != null) {
return adapter.setMedia(media);
} else {
Log.w(TAG, "Adapter in setFeedMedia was null");
return 0;
}
}
/** Updates Information of an existing Feed. Creates and opens its own adapter. */
public long setFeed(Context context, Feed feed) {
PodDBAdapter adapter = new PodDBAdapter(context);
return adapter.setFeed(feed);
adapter.open();
long result = adapter.setFeed(feed);
adapter.close();
return result;
}
/** Updates information of an existing FeedItem. Creates and opens its own adapter.*/
public long setFeedItem(Context context, FeedItem item) {
PodDBAdapter adapter = new PodDBAdapter(context);
return adapter.setFeedItem(item);
adapter.open();
long result = adapter.setFeedItem(item);
adapter.close();
return result;
}
/** Updates information of an existing FeedImage */
/** Updates information of an existing FeedImage. Creates and opens its own adapter. */
public long setFeedImage(Context context, FeedImage image) {
PodDBAdapter adapter = new PodDBAdapter(context);
return adapter.setImage(image);
adapter.open();
long result = adapter.setImage(image);
adapter.close();
return result;
}
/** Updates information of an existing FeedMedia object. */
/** Updates information of an existing FeedMedia object. Creates and opens its own adapter. */
public long setFeedMedia(Context context, FeedMedia media) {
PodDBAdapter adapter = new PodDBAdapter(context);
return adapter.setMedia(media);
adapter.open();
long result = adapter.setMedia(media);
return result;
}
/** Get a Feed by its id */

View File

@ -289,8 +289,8 @@ public class DownloadService extends Service {
sendDownloadHandledIntent(feed.getDownloadId(), statusId, hasImage, imageId);
feed.setDownloadId(0);
// Save information of feed in DB
manager.updateFeed(service, feed);
queryDownloads();
manager.updateFeed(service, feed);
queryDownloads();
}
/** Delete files that aren't needed anymore */

View File

@ -175,7 +175,6 @@ public class PodDBAdapter {
values.put(KEY_DOWNLOAD_URL, feed.getDownload_url());
values.put(KEY_DOWNLOADED, feed.isDownloaded());
values.put(KEY_LASTUPDATE, feed.getLastUpdate().getTime());
open();
if (feed.getId() == 0) {
// Create new entry
Log.d(this.toString(), "Inserting new Feed into db");
@ -185,7 +184,6 @@ public class PodDBAdapter {
db.update(TABLE_NAME_FEEDS, values, KEY_ID + "=?",
new String[] { Long.toString(feed.getId()) });
}
close();
return feed.getId();
}
@ -195,7 +193,6 @@ public class PodDBAdapter {
* @return the id of the entry
* */
public long setCategory(FeedCategory category) {
open();
ContentValues values = new ContentValues();
values.put(KEY_NAME, category.getName());
if (category.getId() == 0) {
@ -205,7 +202,6 @@ public class PodDBAdapter {
new String[] { String.valueOf(category.getId()) });
}
close();
return category.getId();
}
@ -215,7 +211,6 @@ public class PodDBAdapter {
* @return the id of the entry
* */
public long setImage(FeedImage image) {
open();
ContentValues values = new ContentValues();
values.put(KEY_TITLE, image.getTitle());
values.put(KEY_DOWNLOAD_URL, image.getDownload_url());
@ -227,7 +222,6 @@ public class PodDBAdapter {
db.update(TABLE_NAME_FEED_IMAGES, values, KEY_ID + "=?",
new String[] { String.valueOf(image.getId()) });
}
close();
return image.getId();
}
@ -237,7 +231,6 @@ public class PodDBAdapter {
* @return the id of the entry
*/
public long setMedia(FeedMedia media) {
open();
ContentValues values = new ContentValues();
values.put(KEY_DURATION, media.getDuration());
values.put(KEY_POSITION, media.getPosition());
@ -251,8 +244,7 @@ public class PodDBAdapter {
} else {
db.update(TABLE_NAME_FEED_MEDIA, values, KEY_ID + "=?",
new String[] { String.valueOf(media.getId()) });
}
close();
}
return media.getId();
}
@ -280,8 +272,7 @@ public class PodDBAdapter {
}
values.put(KEY_FEED, item.getFeed().getId());
values.put(KEY_READ, item.isRead());
open();
if (item.getId() == 0) {
item.setId(db.insert(TABLE_NAME_FEED_ITEMS, null, values));
} else {
@ -291,7 +282,6 @@ public class PodDBAdapter {
if (item.getSimpleChapters() != null) {
setSimpleChapters(item);
}
close();
return item.getId();
}
@ -328,20 +318,17 @@ public class PodDBAdapter {
values.put(KEY_REASON, status.getReason());
values.put(KEY_SUCCESSFUL, status.isSuccessful());
values.put(KEY_COMPLETION_DATE, status.getCompletionDate().getTime());
open();
if (status.getId() == 0) {
status.setId(db.insert(TABLE_NAME_DOWNLOAD_LOG, null, values));
} else {
db.update(TABLE_NAME_DOWNLOAD_LOG, values, KEY_ID + "=?",
new String[] { String.valueOf(status.getId()) });
}
close();
return status.getId();
}
public void setQueue(ArrayList<FeedItem> queue) {
ContentValues values = new ContentValues();
open();
db.delete(TABLE_NAME_QUEUE, null, null);
for (int i = 0; i < queue.size(); i++) {
FeedItem item = queue.get(i);
@ -351,21 +338,16 @@ public class PodDBAdapter {
db.insertWithOnConflict(TABLE_NAME_QUEUE, null, values,
SQLiteDatabase.CONFLICT_REPLACE);
}
close();
}
public void removeFeedMedia(FeedMedia media) {
open();
db.delete(TABLE_NAME_FEED_MEDIA, KEY_ID + "=?",
new String[] { String.valueOf(media.getId()) });
close();
}
public void removeFeedImage(FeedImage image) {
open();
db.delete(TABLE_NAME_FEED_IMAGES, KEY_ID + "=?",
new String[] { String.valueOf(image.getId()) });
close();
}
/** Remove a FeedItem and its FeedMedia entry. */
@ -373,10 +355,8 @@ public class PodDBAdapter {
if (item.getMedia() != null) {
removeFeedMedia(item.getMedia());
}
open();
db.delete(TABLE_NAME_FEED_ITEMS, KEY_ID + "=?",
new String[] { String.valueOf(item.getId()) });
close();
}
/** Remove a feed with all its FeedItems and Media entries. */
@ -387,17 +367,13 @@ public class PodDBAdapter {
for (FeedItem item : feed.getItems()) {
removeFeedItem(item);
}
open();
db.delete(TABLE_NAME_FEEDS, KEY_ID + "=?",
new String[] { String.valueOf(feed.getId()) });
close();
}
public void removeDownloadStatus(DownloadStatus remove) {
open();
db.delete(TABLE_NAME_DOWNLOAD_LOG, KEY_ID + "=?",
new String[] { String.valueOf(remove.getId()) });
close();
}
/**