From 73136e4b563a0ad39a36f5d2077784165a00254c Mon Sep 17 00:00:00 2001 From: anthony restaino Date: Tue, 13 Jun 2017 21:01:26 -0400 Subject: [PATCH] Reducing usage of file names --- .../lightning/activity/BrowserActivity.java | 7 ++----- .../lightning/constant/DownloadsPage.java | 11 ++++++----- .../browser/lightning/constant/HistoryPage.java | 16 ++++++++++++++-- .../browser/lightning/constant/StartPage.java | 7 ++++++- .../lightning/dialog/LightningDialogBuilder.java | 3 ++- .../browser/lightning/view/LightningView.java | 7 +++---- 6 files changed, 33 insertions(+), 18 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 ff28dff..b83caa9 100644 --- a/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java +++ b/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java @@ -95,7 +95,6 @@ import acr.browser.lightning.browser.BookmarksView; import acr.browser.lightning.browser.BrowserPresenter; import acr.browser.lightning.browser.BrowserView; import acr.browser.lightning.browser.TabsView; -import acr.browser.lightning.constant.BookmarkPage; import acr.browser.lightning.constant.Constants; import acr.browser.lightning.constant.DownloadsPage; import acr.browser.lightning.constant.HistoryPage; @@ -2178,8 +2177,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @Override public void handleBookmarksChange() { final LightningView currentTab = mTabsManager.getCurrentTab(); - if (currentTab != null && currentTab.getUrl().startsWith(Constants.FILE) - && currentTab.getUrl().endsWith(BookmarkPage.FILENAME)) { + if (currentTab != null && UrlUtils.isBookmarkUrl(currentTab.getUrl())) { currentTab.loadBookmarkpage(); } if (currentTab != null) { @@ -2190,8 +2188,7 @@ 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)) { + if (currentTab != null && UrlUtils.isDownloadsUrl(currentTab.getUrl())) { currentTab.loadDownloadspage(); } if (currentTab != null) { diff --git a/app/src/main/java/acr/browser/lightning/constant/DownloadsPage.java b/app/src/main/java/acr/browser/lightning/constant/DownloadsPage.java index 5e5eb8a..0d5e7da 100644 --- a/app/src/main/java/acr/browser/lightning/constant/DownloadsPage.java +++ b/app/src/main/java/acr/browser/lightning/constant/DownloadsPage.java @@ -56,7 +56,10 @@ public final class DownloadsPage { private static final String END = ""; - private File mFilesDir; + @NonNull + private static File getDownloadsPageFile(@NonNull Application application) { + return new File(application.getFilesDir(), FILENAME); + } @Inject Application mApp; @Inject PreferenceManager mPreferenceManager; @@ -74,11 +77,9 @@ public final class DownloadsPage { return Single.create(new SingleAction() { @Override public void onSubscribe(@NonNull SingleSubscriber subscriber) { - mFilesDir = mApp.getFilesDir(); - buildDownloadsPage(); - File downloadsWebPage = new File(mFilesDir, FILENAME); + File downloadsWebPage = new File(getDownloadsPageFile(mApp), FILENAME); subscriber.onItem(Constants.FILE + downloadsWebPage); subscriber.onComplete(); @@ -120,7 +121,7 @@ public final class DownloadsPage { FileWriter bookWriter = null; try { //noinspection IOResourceOpenedButNotSafelyClosed - bookWriter = new FileWriter(new File(mFilesDir, FILENAME), false); + bookWriter = new FileWriter(getDownloadsPageFile(mApp), false); bookWriter.write(downloadsBuilder.toString()); } catch (IOException e) { e.printStackTrace(); diff --git a/app/src/main/java/acr/browser/lightning/constant/HistoryPage.java b/app/src/main/java/acr/browser/lightning/constant/HistoryPage.java index 0e102ca..e42d83a 100644 --- a/app/src/main/java/acr/browser/lightning/constant/HistoryPage.java +++ b/app/src/main/java/acr/browser/lightning/constant/HistoryPage.java @@ -61,6 +61,18 @@ public class HistoryPage { private static final String END = ""; + /** + * Get the file that the history page is stored in + * or should be stored in. + * + * @param application the application used to access the file. + * @return a valid file object, note that the file might not exist. + */ + @NonNull + private static File getHistoryPageFile(@NonNull Application application) { + return new File(application.getFilesDir(), FILENAME); + } + @NonNull private final String mTitle; @Inject Application mApp; @@ -98,7 +110,7 @@ public class HistoryPage { } historyBuilder.append(END); - File historyWebPage = new File(mApp.getFilesDir(), FILENAME); + File historyWebPage = getHistoryPageFile(mApp); FileWriter historyWriter = null; try { //noinspection IOResourceOpenedButNotSafelyClosed @@ -131,7 +143,7 @@ public class HistoryPage { return Completable.create(new CompletableAction() { @Override public void onSubscribe(@NonNull CompletableSubscriber subscriber) { - File historyWebPage = new File(application.getFilesDir(), FILENAME); + File historyWebPage = getHistoryPageFile(application); if (historyWebPage.exists()) { historyWebPage.delete(); } diff --git a/app/src/main/java/acr/browser/lightning/constant/StartPage.java b/app/src/main/java/acr/browser/lightning/constant/StartPage.java index b422ab1..d35cd2a 100644 --- a/app/src/main/java/acr/browser/lightning/constant/StartPage.java +++ b/app/src/main/java/acr/browser/lightning/constant/StartPage.java @@ -54,6 +54,11 @@ public class StartPage { private static final String END = "\" + document.getElementById(\"search_input\").value;document.getElementById(\"search_input\").value = \"\";}return false;}"; + @NonNull + private static File getStartPageFile(@NonNull Application application) { + return new File(application.getFilesDir(), FILENAME); + } + @NonNull private final String mTitle; @Inject Application mApp; @@ -151,7 +156,7 @@ public class StartPage { homepageBuilder.append(searchUrl); homepageBuilder.append(END); - File homepage = new File(mApp.getFilesDir(), FILENAME); + File homepage = getStartPageFile(mApp); FileWriter hWriter = null; try { //noinspection IOResourceOpenedButNotSafelyClosed 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 7d7d8eb..c9d70a2 100644 --- a/app/src/main/java/acr/browser/lightning/dialog/LightningDialogBuilder.java +++ b/app/src/main/java/acr/browser/lightning/dialog/LightningDialogBuilder.java @@ -35,6 +35,7 @@ import acr.browser.lightning.download.DownloadHandler; import acr.browser.lightning.preference.PreferenceManager; import acr.browser.lightning.utils.IntentUtils; import acr.browser.lightning.utils.Preconditions; +import acr.browser.lightning.utils.UrlUtils; /** * TODO Rename this class it doesn't build dialogs only for bookmarks @@ -72,7 +73,7 @@ public class LightningDialogBuilder { @NonNull final UIController uiController, @NonNull final String url) { final HistoryItem item; - if (url.startsWith(Constants.FILE) && url.endsWith(BookmarkPage.FILENAME)) { + if (UrlUtils.isBookmarkUrl(url)) { // TODO hacky, make a better bookmark mechanism in the future final Uri uri = Uri.parse(url); final String filename = uri.getLastPathSegment(); 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 8532dea..cf852e9 100644 --- a/app/src/main/java/acr/browser/lightning/view/LightningView.java +++ b/app/src/main/java/acr/browser/lightning/view/LightningView.java @@ -48,7 +48,6 @@ 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; import acr.browser.lightning.dialog.LightningDialogBuilder; @@ -989,21 +988,21 @@ public class LightningView { final WebView.HitTestResult result = mWebView.getHitTestResult(); String currentUrl = mWebView.getUrl(); if (currentUrl != null && UrlUtils.isSpecialUrl(currentUrl)) { - if (currentUrl.endsWith(HistoryPage.FILENAME)) { + if (UrlUtils.isHistoryUrl(currentUrl)) { if (url != null) { mBookmarksDialogBuilder.showLongPressedHistoryLinkDialog(mActivity, mUIController, url); } else if (result != null && result.getExtra() != null) { final String newUrl = result.getExtra(); mBookmarksDialogBuilder.showLongPressedHistoryLinkDialog(mActivity, mUIController, newUrl); } - } else if (currentUrl.endsWith(BookmarkPage.FILENAME)) { + } else if (UrlUtils.isBookmarkUrl(currentUrl)) { if (url != null) { mBookmarksDialogBuilder.showLongPressedDialogForBookmarkUrl(mActivity, mUIController, url); } else if (result != null && result.getExtra() != null) { final String newUrl = result.getExtra(); mBookmarksDialogBuilder.showLongPressedDialogForBookmarkUrl(mActivity, mUIController, newUrl); } - } else if (currentUrl.endsWith(DownloadsPage.FILENAME)) { + } else if (UrlUtils.isDownloadsUrl(currentUrl)) { if (url != null) { mBookmarksDialogBuilder.showLongPressedDialogForDownloadUrl(mActivity, mUIController, url); } else if (result != null && result.getExtra() != null) {