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. */ /** Remove a feed with all its items and media files and its image. */
public boolean deleteFeed(Context context, Feed feed) { public boolean deleteFeed(Context context, Feed feed) {
PodDBAdapter adapter = new PodDBAdapter(context); PodDBAdapter adapter = new PodDBAdapter(context);
adapter.open();
// delete image file // delete image file
if (feed.getImage() != null) { if (feed.getImage() != null) {
if (feed.getImage().isDownloaded() if (feed.getImage().isDownloaded()
@ -123,14 +123,16 @@ public class FeedManager {
unreadItems.remove(item); unreadItems.remove(item);
} }
if (queue.contains(item)) { if (queue.contains(item)) {
removeQueueItem(context, item); removeQueueItem(item, adapter);
} }
if (item.getMedia() != null && item.getMedia().isDownloaded()) { if (item.getMedia() != null && item.getMedia().isDownloaded()) {
File mediaFile = new File(item.getMedia().getFile_url()); File mediaFile = new File(item.getMedia().getFile_url());
mediaFile.delete(); mediaFile.delete();
} }
} }
adapter.removeFeed(feed); adapter.removeFeed(feed);
adapter.close();
return feeds.remove(feed); return feeds.remove(feed);
} }
@ -176,15 +178,29 @@ public class FeedManager {
if (downloadLog.size() > DOWNLOAD_LOG_SIZE) { if (downloadLog.size() > DOWNLOAD_LOG_SIZE) {
adapter.removeDownloadStatus(downloadLog.remove(0)); 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) { public void addQueueItem(Context context, FeedItem item) {
PodDBAdapter adapter = new PodDBAdapter(context); PodDBAdapter adapter = new PodDBAdapter(context);
queue.add(item); queue.add(item);
adapter.open();
adapter.setQueue(queue); 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) { public void removeQueueItem(Context context, FeedItem item) {
boolean removed = queue.remove(item); boolean removed = queue.remove(item);
if (removed) { if (removed) {
@ -203,10 +219,13 @@ public class FeedManager {
private void addNewFeed(Context context, Feed feed) { private void addNewFeed(Context context, Feed feed) {
feeds.add(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()) { for (FeedItem item : feed.getItems()) {
setFeedItem(context, item); setFeedItem(item, adapter);
} }
adapter.close();
} }
/* /*
@ -269,27 +288,79 @@ public class FeedManager {
return null; 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) { public long setFeed(Context context, Feed feed) {
PodDBAdapter adapter = new PodDBAdapter(context); 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) { public long setFeedItem(Context context, FeedItem item) {
PodDBAdapter adapter = new PodDBAdapter(context); 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) { public long setFeedImage(Context context, FeedImage image) {
PodDBAdapter adapter = new PodDBAdapter(context); 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) { public long setFeedMedia(Context context, FeedMedia media) {
PodDBAdapter adapter = new PodDBAdapter(context); PodDBAdapter adapter = new PodDBAdapter(context);
return adapter.setMedia(media); adapter.open();
long result = adapter.setMedia(media);
return result;
} }
/** Get a Feed by its id */ /** Get a Feed by its id */

View File

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