From c3df77677196710d5f6a3a6643e4401ee47f0e37 Mon Sep 17 00:00:00 2001 From: DF1E Date: Sun, 28 May 2017 14:28:48 +0200 Subject: [PATCH] add delete downloads dialog --- .../lightning/activity/BrowserActivity.java | 12 ++++++ .../lightning/controller/UIController.java | 2 + .../database/downloads/DownloadsDatabase.java | 4 +- .../database/downloads/DownloadsModel.java | 4 +- .../dialog/LightningDialogBuilder.java | 41 +++++++++++++++++++ .../browser/lightning/view/LightningView.java | 26 ++++++++++++ app/src/main/res/values/strings.xml | 2 + 7 files changed, 87 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java b/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java index 8e1a416..16eed9e 100644 --- a/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java +++ b/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java @@ -2130,6 +2130,18 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements } } + @Override + public void handleDownloadDeleted() { + final LightningView currentTab = mTabsManager.getCurrentTab(); + if (currentTab != null && currentTab.getUrl().startsWith(Constants.FILE) + && currentTab.getUrl().endsWith(DownloadsPage.FILENAME)) { + currentTab.loadDownloadspage(); + } + if (currentTab != null) { + mBookmarksView.handleUpdatedUrl(currentTab.getUrl()); + } + } + @Override public void handleBookmarkDeleted(@NonNull HistoryItem item) { mBookmarksView.handleBookmarkDeleted(item); diff --git a/app/src/main/java/acr/browser/lightning/controller/UIController.java b/app/src/main/java/acr/browser/lightning/controller/UIController.java index a39a1bd..a9021a7 100644 --- a/app/src/main/java/acr/browser/lightning/controller/UIController.java +++ b/app/src/main/java/acr/browser/lightning/controller/UIController.java @@ -86,6 +86,8 @@ public interface UIController { void handleBookmarksChange(); + void handleDownloadDeleted(); + void handleBookmarkDeleted(@NonNull HistoryItem item); void handleNewTab(@NonNull LightningDialogBuilder.NewTab newTabType, @NonNull String url); diff --git a/app/src/main/java/acr/browser/lightning/database/downloads/DownloadsDatabase.java b/app/src/main/java/acr/browser/lightning/database/downloads/DownloadsDatabase.java index db2f83c..a14faff 100644 --- a/app/src/main/java/acr/browser/lightning/database/downloads/DownloadsDatabase.java +++ b/app/src/main/java/acr/browser/lightning/database/downloads/DownloadsDatabase.java @@ -214,11 +214,11 @@ public class DownloadsDatabase extends SQLiteOpenHelper implements DownloadsMode @NonNull @Override - public Single deleteDownload(@NonNull final DownloadItem bookmark) { + public Single deleteDownload(@NonNull final String url) { return Single.create(new SingleAction() { @Override public void onSubscribe(@NonNull SingleSubscriber subscriber) { - int rows = lazyDatabase().delete(TABLE_DOWNLOADS, KEY_URL + "=?", new String[]{bookmark.getUrl()}); + int rows = lazyDatabase().delete(TABLE_DOWNLOADS, KEY_URL + "=?", new String[]{url}); subscriber.onItem(rows > 0); subscriber.onComplete(); diff --git a/app/src/main/java/acr/browser/lightning/database/downloads/DownloadsModel.java b/app/src/main/java/acr/browser/lightning/database/downloads/DownloadsModel.java index 7ce5f3e..d8dc67e 100644 --- a/app/src/main/java/acr/browser/lightning/database/downloads/DownloadsModel.java +++ b/app/src/main/java/acr/browser/lightning/database/downloads/DownloadsModel.java @@ -60,12 +60,12 @@ public interface DownloadsModel { /** * Deletes a download from the database. * - * @param download the download to delete. + * @param url the download url to delete. * @return an observable that emits true when * the download is deleted, false otherwise. */ @NonNull - Single deleteDownload(@NonNull DownloadItem download); + Single deleteDownload(@NonNull String url); /** * Deletes all downloads in the database. diff --git a/app/src/main/java/acr/browser/lightning/dialog/LightningDialogBuilder.java b/app/src/main/java/acr/browser/lightning/dialog/LightningDialogBuilder.java index 490a9f8..46f23d8 100644 --- a/app/src/main/java/acr/browser/lightning/dialog/LightningDialogBuilder.java +++ b/app/src/main/java/acr/browser/lightning/dialog/LightningDialogBuilder.java @@ -8,6 +8,7 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v7.app.AlertDialog; import android.text.TextUtils; +import android.util.Log; import android.view.View; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; @@ -26,15 +27,20 @@ import acr.browser.lightning.activity.MainActivity; import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.constant.BookmarkPage; import acr.browser.lightning.constant.Constants; +import acr.browser.lightning.constant.DownloadsPage; import acr.browser.lightning.controller.UIController; import acr.browser.lightning.database.HistoryItem; import acr.browser.lightning.database.bookmark.BookmarkModel; +import acr.browser.lightning.database.downloads.DownloadItem; +import acr.browser.lightning.database.downloads.DownloadsModel; import acr.browser.lightning.database.history.HistoryModel; import acr.browser.lightning.preference.PreferenceManager; import acr.browser.lightning.utils.IntentUtils; import acr.browser.lightning.utils.Preconditions; import acr.browser.lightning.utils.Utils; +import static android.content.ContentValues.TAG; + /** * TODO Rename this class it doesn't build dialogs only for bookmarks *

@@ -49,6 +55,7 @@ public class LightningDialogBuilder { } @Inject BookmarkModel mBookmarkManager; + @Inject DownloadsModel mDownloadsModel; @Inject PreferenceManager mPreferenceManager; @Inject @@ -152,6 +159,40 @@ public class LightningDialogBuilder { }); } + /** + * Show the appropriated dialog for the long pressed link. + * + * @param activity used to show the dialog + * @param url the long pressed url + */ + public void showLongPressedDialogForDownloadUrl(@NonNull final Activity activity, + @NonNull final UIController uiController, + @NonNull final String url) { + + BrowserDialog.show(activity, R.string.action_bookmarks, + new BrowserDialog.Item(R.string.dialog_delete_download) { + @Override + public void onClick() { + mDownloadsModel.deleteDownload(url).subscribe(new SingleOnSubscribe() { + @Override + public void onItem(@Nullable Boolean item) { + if (item != null && !item) + Log.i(TAG, "error deleting download from database"); + else + uiController.handleDownloadDeleted(); + } + }); + } + }, + new BrowserDialog.Item(R.string.dialog_delete_all_downloads) { + @Override + public void onClick() { + // TODO: fix refreshing downloads page + mDownloadsModel.deleteAllDownloads().subscribeOn(Schedulers.io()).subscribe(); + } + }); + } + private void showEditBookmarkDialog(@NonNull final Activity activity, @NonNull final UIController uiController, @NonNull final HistoryItem item) { diff --git a/app/src/main/java/acr/browser/lightning/view/LightningView.java b/app/src/main/java/acr/browser/lightning/view/LightningView.java index 1f137f0..8532dea 100644 --- a/app/src/main/java/acr/browser/lightning/view/LightningView.java +++ b/app/src/main/java/acr/browser/lightning/view/LightningView.java @@ -47,6 +47,7 @@ import javax.inject.Inject; import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.constant.BookmarkPage; import acr.browser.lightning.constant.Constants; +import acr.browser.lightning.constant.DownloadsPage; import acr.browser.lightning.constant.HistoryPage; import acr.browser.lightning.constant.StartPage; import acr.browser.lightning.controller.UIController; @@ -242,6 +243,24 @@ public class LightningView { }); } + /** + * This method gets the bookmark page URL from the {@link BookmarkPage} + * class asynchronously and loads the URL in the WebView on the + * UI thread. It also caches the default folder icon locally. + */ + public void loadDownloadspage() { + new DownloadsPage().getDownloadsPage() + .subscribeOn(Schedulers.io()) + .observeOn(Schedulers.main()) + .subscribe(new SingleOnSubscribe() { + @Override + public void onItem(@Nullable String item) { + Preconditions.checkNonNull(item); + loadUrl(item); + } + }); + } + /** * Initialize the preference driven settings of the WebView. This method * must be called whenever the preferences are changed within SharedPreferences. @@ -984,6 +1003,13 @@ public class LightningView { final String newUrl = result.getExtra(); mBookmarksDialogBuilder.showLongPressedDialogForBookmarkUrl(mActivity, mUIController, newUrl); } + } else if (currentUrl.endsWith(DownloadsPage.FILENAME)) { + if (url != null) { + mBookmarksDialogBuilder.showLongPressedDialogForDownloadUrl(mActivity, mUIController, url); + } else if (result != null && result.getExtra() != null) { + final String newUrl = result.getExtra(); + mBookmarksDialogBuilder.showLongPressedDialogForDownloadUrl(mActivity, mUIController, newUrl); + } } } else { if (url != null) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7479d79..71a594c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -246,4 +246,6 @@ Rename folder Remove folder Close browser + Delete download + Delete all downloads