From dbc186db9bf94895355e44bdb94cbb97274dcf09 Mon Sep 17 00:00:00 2001 From: Anthony Restaino Date: Wed, 23 Mar 2016 20:10:42 -0400 Subject: [PATCH] Fixed bugs related restore tabs option - fixed tab being blank if option was off - fixed bug where bookmarks would show sub folder if browser was closed in that folder if option was on --- .../lightning/activity/TabsManager.java | 27 +++++++++++++++-- .../lightning/constant/BookmarkPage.java | 9 ++++-- .../acr/browser/lightning/utils/UrlUtils.java | 30 +++++++++++++++++++ .../browser/lightning/view/LightningView.java | 3 +- 4 files changed, 62 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/acr/browser/lightning/activity/TabsManager.java b/app/src/main/java/acr/browser/lightning/activity/TabsManager.java index 2e188dd..a73f8b1 100644 --- a/app/src/main/java/acr/browser/lightning/activity/TabsManager.java +++ b/app/src/main/java/acr/browser/lightning/activity/TabsManager.java @@ -21,7 +21,12 @@ import javax.inject.Inject; import acr.browser.lightning.R; import acr.browser.lightning.app.BrowserApp; +import acr.browser.lightning.constant.BookmarkPage; import acr.browser.lightning.constant.Constants; +import acr.browser.lightning.constant.HistoryPage; +import acr.browser.lightning.constant.StartPage; +import acr.browser.lightning.database.BookmarkManager; +import acr.browser.lightning.database.HistoryDatabase; import acr.browser.lightning.preference.PreferenceManager; import acr.browser.lightning.react.Action; import acr.browser.lightning.react.Observable; @@ -29,6 +34,7 @@ import acr.browser.lightning.react.OnSubscribe; import acr.browser.lightning.react.Schedulers; import acr.browser.lightning.react.Subscriber; import acr.browser.lightning.utils.FileUtils; +import acr.browser.lightning.utils.UrlUtils; import acr.browser.lightning.view.LightningView; /** @@ -40,6 +46,7 @@ public class TabsManager { private static final String TAG = TabsManager.class.getSimpleName(); private static final String BUNDLE_KEY = "WEBVIEW_"; + private static final String URL_KEY = "URL_KEY"; private static final String BUNDLE_STORAGE = "SAVED_TABS.parcel"; private final List mTabList = new ArrayList<>(1); @@ -50,6 +57,8 @@ public class TabsManager { private List mPostInitializationWorkList = new ArrayList<>(); @Inject PreferenceManager mPreferenceManager; + @Inject BookmarkManager mBookmarkManager; + @Inject HistoryDatabase mHistoryManager; @Inject Bus mEventBus; @Inject Application mApp; @@ -123,6 +132,7 @@ public class TabsManager { } else { newTab(activity, null, false); finishInitialization(); + subscriber.onComplete(); } } @@ -132,12 +142,22 @@ public class TabsManager { private void restoreLostTabs(@Nullable final String url, @NonNull final Activity activity, @NonNull final Subscriber subscriber) { + restoreState().subscribeOn(Schedulers.worker()) .observeOn(Schedulers.main()).subscribe(new OnSubscribe() { @Override public void onNext(Bundle item) { LightningView tab = newTab(activity, "", false); - if (tab.getWebView() != null) { + String url = item.getString(URL_KEY); + if (url != null && tab.getWebView() != null) { + if (UrlUtils.isBookmarkUrl(url)) { + new BookmarkPage(tab, activity, mBookmarkManager).load(); + } else if (UrlUtils.isStartPageUrl(url)) { + new StartPage(tab, mApp).load(); + } else if (UrlUtils.isHistoryUrl(url)) { + new HistoryPage(tab, mApp, mHistoryManager).load(); + } + } else if (tab.getWebView() != null) { tab.getWebView().restoreState(item); } } @@ -357,9 +377,12 @@ public class TabsManager { for (int n = 0; n < mTabList.size(); n++) { LightningView tab = mTabList.get(n); Bundle state = new Bundle(ClassLoader.getSystemClassLoader()); - if (tab.getWebView() != null) { + if (tab.getWebView() != null && !UrlUtils.isSpecialUrl(tab.getUrl())) { tab.getWebView().saveState(state); outState.putBundle(BUNDLE_KEY + n, state); + } else if (tab.getWebView() != null) { + state.putString(URL_KEY, tab.getUrl()); + outState.putBundle(BUNDLE_KEY + n, state); } } FileUtils.writeBundleToStorage(mApp, outState, BUNDLE_STORAGE); diff --git a/app/src/main/java/acr/browser/lightning/constant/BookmarkPage.java b/app/src/main/java/acr/browser/lightning/constant/BookmarkPage.java index da8ec96..91030da 100644 --- a/app/src/main/java/acr/browser/lightning/constant/BookmarkPage.java +++ b/app/src/main/java/acr/browser/lightning/constant/BookmarkPage.java @@ -3,6 +3,7 @@ */ package acr.browser.lightning.constant; +import android.app.Activity; import android.app.Application; import android.graphics.Bitmap; import android.os.AsyncTask; @@ -21,6 +22,7 @@ import acr.browser.lightning.R; import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.database.BookmarkManager; import acr.browser.lightning.database.HistoryItem; +import acr.browser.lightning.utils.ThemeUtils; import acr.browser.lightning.utils.Utils; import acr.browser.lightning.view.LightningView; @@ -69,9 +71,10 @@ public final class BookmarkPage extends AsyncTask { private final Bitmap mFolderIcon; @NonNull private final String mTitle; - public BookmarkPage(LightningView tab, @NonNull Application app, BookmarkManager manager, Bitmap folderIcon) { - mApp = app; - mTitle = app.getString(R.string.action_bookmarks); + public BookmarkPage(LightningView tab, @NonNull Activity activity, BookmarkManager manager) { + mApp = BrowserApp.get(activity); + final Bitmap folderIcon = ThemeUtils.getThemedBitmap(activity, R.drawable.ic_folder, false); + mTitle = mApp.getString(R.string.action_bookmarks); mManager = manager; mTabReference = new WeakReference<>(tab); mFolderIcon = folderIcon; diff --git a/app/src/main/java/acr/browser/lightning/utils/UrlUtils.java b/app/src/main/java/acr/browser/lightning/utils/UrlUtils.java index 3522499..2baa6bd 100644 --- a/app/src/main/java/acr/browser/lightning/utils/UrlUtils.java +++ b/app/src/main/java/acr/browser/lightning/utils/UrlUtils.java @@ -166,4 +166,34 @@ public class UrlUtils { url.endsWith(HistoryPage.FILENAME) || url.endsWith(StartPage.FILENAME)); } + + /** + * Determines if the url is a url for the bookmark page. + * + * @param url the url to check, may be null. + * @return true if the url is a bookmark url, false otherwise. + */ + public static boolean isBookmarkUrl(@Nullable String url) { + return url != null && url.startsWith(Constants.FILE) && url.endsWith(BookmarkPage.FILENAME); + } + + /** + * Determines if the url is a url for the history page. + * + * @param url the url to check, may be null. + * @return true if the url is a history url, false otherwise. + */ + public static boolean isHistoryUrl(@Nullable String url) { + return url != null && url.startsWith(Constants.FILE) && url.endsWith(HistoryPage.FILENAME); + } + + /** + * Determines if the url is a url for the start page. + * + * @param url the url to check, may be null. + * @return true if the url is a start page url, false otherwise. + */ + public static boolean isStartPageUrl(@Nullable String url) { + return url != null && url.startsWith(Constants.FILE) && url.endsWith(StartPage.FILENAME); + } } \ No newline at end of file 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 d4dfe65..23b8063 100644 --- a/app/src/main/java/acr/browser/lightning/view/LightningView.java +++ b/app/src/main/java/acr/browser/lightning/view/LightningView.java @@ -196,8 +196,7 @@ public class LightningView { public void loadBookmarkpage() { if (mWebView == null) return; - Bitmap folderIcon = ThemeUtils.getThemedBitmap(mActivity, R.drawable.ic_folder, false); - new BookmarkPage(this, BrowserApp.get(mActivity), mBookmarkManager, folderIcon).load(); + new BookmarkPage(this, mActivity, mBookmarkManager).load(); } /**