Browse Source

Making html page generation reactive

master
anthony restaino 8 years ago
parent
commit
0819d35711
  1. 4
      app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java
  2. 24
      app/src/main/java/acr/browser/lightning/activity/TabsManager.java
  3. 6
      app/src/main/java/acr/browser/lightning/app/AppComponent.java
  4. 55
      app/src/main/java/acr/browser/lightning/constant/BookmarkPage.java
  5. 42
      app/src/main/java/acr/browser/lightning/constant/HistoryPage.java
  6. 54
      app/src/main/java/acr/browser/lightning/constant/StartPage.java
  7. 25
      app/src/main/java/acr/browser/lightning/view/LightningView.java

4
app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java

@ -423,7 +423,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
mTabsManager.newTab(this, "", false); mTabsManager.newTab(this, "", false);
mTabsManager.switchToTab(0); mTabsManager.switchToTab(0);
mTabsManager.clearSavedState(); mTabsManager.clearSavedState();
HistoryPage.deleteHistoryPage(getApplication()); new HistoryPage().deleteHistoryPage().subscribe();
closeBrowser(); closeBrowser();
// System exit needed in the case of receiving // System exit needed in the case of receiving
// the panic intent since finish() isn't completely // the panic intent since finish() isn't completely
@ -1533,7 +1533,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
* function that opens the HTML history page in the browser * function that opens the HTML history page in the browser
*/ */
private void openHistory() { private void openHistory() {
HistoryPage.getHistoryPage() new HistoryPage().getHistoryPage()
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(Schedulers.main()) .observeOn(Schedulers.main())
.subscribe(new SingleOnSubscribe<String>() { .subscribe(new SingleOnSubscribe<String>() {

24
app/src/main/java/acr/browser/lightning/activity/TabsManager.java

@ -164,11 +164,29 @@ public class TabsManager {
String url = item.getString(URL_KEY); String url = item.getString(URL_KEY);
if (url != null && tab.getWebView() != null) { if (url != null && tab.getWebView() != null) {
if (UrlUtils.isBookmarkUrl(url)) { if (UrlUtils.isBookmarkUrl(url)) {
new BookmarkPage(tab, activity, mBookmarkManager).load(); new BookmarkPage(activity).getBookmarkPage()
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.main())
.subscribe(new SingleOnSubscribe<String>() {
@Override
public void onItem(@Nullable String item) {
Preconditions.checkNonNull(item);
tab.loadUrl(item);
}
});
} else if (UrlUtils.isStartPageUrl(url)) { } else if (UrlUtils.isStartPageUrl(url)) {
new StartPage(tab, mApp).load(); new StartPage().getHomepage()
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.main())
.subscribe(new SingleOnSubscribe<String>() {
@Override
public void onItem(@Nullable String item) {
Preconditions.checkNonNull(item);
tab.loadUrl(item);
}
});
} else if (UrlUtils.isHistoryUrl(url)) { } else if (UrlUtils.isHistoryUrl(url)) {
HistoryPage.getHistoryPage() new HistoryPage().getHistoryPage()
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(Schedulers.main()) .observeOn(Schedulers.main())
.subscribe(new SingleOnSubscribe<String>() { .subscribe(new SingleOnSubscribe<String>() {

6
app/src/main/java/acr/browser/lightning/app/AppComponent.java

@ -8,6 +8,8 @@ import acr.browser.lightning.activity.TabsManager;
import acr.browser.lightning.activity.ThemableBrowserActivity; import acr.browser.lightning.activity.ThemableBrowserActivity;
import acr.browser.lightning.activity.ThemableSettingsActivity; import acr.browser.lightning.activity.ThemableSettingsActivity;
import acr.browser.lightning.browser.BrowserPresenter; import acr.browser.lightning.browser.BrowserPresenter;
import acr.browser.lightning.constant.BookmarkPage;
import acr.browser.lightning.constant.HistoryPage;
import acr.browser.lightning.constant.StartPage; import acr.browser.lightning.constant.StartPage;
import acr.browser.lightning.dialog.LightningDialogBuilder; import acr.browser.lightning.dialog.LightningDialogBuilder;
import acr.browser.lightning.download.LightningDownloadListener; import acr.browser.lightning.download.LightningDownloadListener;
@ -62,6 +64,10 @@ public interface AppComponent {
void inject(StartPage startPage); void inject(StartPage startPage);
void inject(HistoryPage historyPage);
void inject(BookmarkPage bookmarkPage);
void inject(BrowserPresenter presenter); void inject(BrowserPresenter presenter);
void inject(TabsManager manager); void inject(TabsManager manager);

55
app/src/main/java/acr/browser/lightning/constant/BookmarkPage.java

@ -6,27 +6,30 @@ package acr.browser.lightning.constant;
import android.app.Activity; import android.app.Activity;
import android.app.Application; import android.app.Application;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import com.anthonycr.bonsai.Single;
import com.anthonycr.bonsai.SingleAction;
import com.anthonycr.bonsai.SingleSubscriber;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.List; import java.util.List;
import javax.inject.Inject;
import acr.browser.lightning.R; import acr.browser.lightning.R;
import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.database.BookmarkManager; import acr.browser.lightning.database.BookmarkManager;
import acr.browser.lightning.database.HistoryItem; import acr.browser.lightning.database.HistoryItem;
import acr.browser.lightning.utils.ThemeUtils; import acr.browser.lightning.utils.ThemeUtils;
import acr.browser.lightning.utils.Utils; import acr.browser.lightning.utils.Utils;
import acr.browser.lightning.view.LightningView;
public final class BookmarkPage extends AsyncTask<Void, Void, Void> { public final class BookmarkPage {
/** /**
* The bookmark page standard suffix * The bookmark page standard suffix
@ -67,38 +70,34 @@ public final class BookmarkPage extends AsyncTask<Void, Void, Void> {
private File mFilesDir; private File mFilesDir;
private File mCacheDir; private File mCacheDir;
private final Application mApp; @Inject Application mApp;
private final BookmarkManager mManager; @Inject BookmarkManager mManager;
@NonNull private final WeakReference<LightningView> mTabReference;
private final Bitmap mFolderIcon; private final Bitmap mFolderIcon;
@NonNull private final String mTitle; @NonNull private final String mTitle;
public BookmarkPage(LightningView tab, @NonNull Activity activity, BookmarkManager manager) { public BookmarkPage(@NonNull Activity activity) {
mApp = BrowserApp.get(activity); BrowserApp.getAppComponent().inject(this);
final Bitmap folderIcon = ThemeUtils.getThemedBitmap(activity, R.drawable.ic_folder, false); mFolderIcon = ThemeUtils.getThemedBitmap(activity, R.drawable.ic_folder, false);
mTitle = mApp.getString(R.string.action_bookmarks); mTitle = mApp.getString(R.string.action_bookmarks);
mManager = manager;
mTabReference = new WeakReference<>(tab);
mFolderIcon = folderIcon;
} }
@NonNull
public Single<String> getBookmarkPage() {
return Single.create(new SingleAction<String>() {
@Override @Override
protected Void doInBackground(Void... params) { public void onSubscribe(@NonNull SingleSubscriber<String> subscriber) {
mCacheDir = mApp.getCacheDir(); mCacheDir = mApp.getCacheDir();
mFilesDir = mApp.getFilesDir(); mFilesDir = mApp.getFilesDir();
cacheDefaultFolderIcon(); cacheDefaultFolderIcon();
buildBookmarkPage(null, mManager); buildBookmarkPage(null);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
LightningView tab = mTabReference.get();
if (tab != null) {
File bookmarkWebPage = new File(mFilesDir, FILENAME); File bookmarkWebPage = new File(mFilesDir, FILENAME);
tab.loadUrl(Constants.FILE + bookmarkWebPage);
subscriber.onItem(Constants.FILE + bookmarkWebPage);
subscriber.onComplete();
} }
});
} }
private void cacheDefaultFolderIcon() { private void cacheDefaultFolderIcon() {
@ -115,8 +114,8 @@ public final class BookmarkPage extends AsyncTask<Void, Void, Void> {
} }
} }
private void buildBookmarkPage(@Nullable final String folder, @NonNull final BookmarkManager manager) { private void buildBookmarkPage(@Nullable final String folder) {
final List<HistoryItem> list = manager.getBookmarksCopyFromFolder(folder, true); final List<HistoryItem> list = mManager.getBookmarksCopyFromFolder(folder, true);
final File bookmarkWebPage; final File bookmarkWebPage;
if (folder == null || folder.isEmpty()) { if (folder == null || folder.isEmpty()) {
bookmarkWebPage = new File(mFilesDir, FILENAME); bookmarkWebPage = new File(mFilesDir, FILENAME);
@ -134,7 +133,7 @@ public final class BookmarkPage extends AsyncTask<Void, Void, Void> {
bookmarkBuilder.append(Constants.FILE).append(folderPage); bookmarkBuilder.append(Constants.FILE).append(folderPage);
bookmarkBuilder.append(PART2); bookmarkBuilder.append(PART2);
bookmarkBuilder.append(folderIconPath); bookmarkBuilder.append(folderIconPath);
buildBookmarkPage(item.getTitle(), manager); buildBookmarkPage(item.getTitle());
} else { } else {
bookmarkBuilder.append(item.getUrl()); bookmarkBuilder.append(item.getUrl());
bookmarkBuilder.append(PART2).append(PART3); bookmarkBuilder.append(PART2).append(PART3);
@ -157,8 +156,4 @@ public final class BookmarkPage extends AsyncTask<Void, Void, Void> {
} }
} }
public void load() {
executeOnExecutor(BrowserApp.getIOThread());
}
} }

42
app/src/main/java/acr/browser/lightning/constant/HistoryPage.java

@ -8,6 +8,9 @@ import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.util.Log; import android.util.Log;
import com.anthonycr.bonsai.Completable;
import com.anthonycr.bonsai.CompletableAction;
import com.anthonycr.bonsai.CompletableSubscriber;
import com.anthonycr.bonsai.Single; import com.anthonycr.bonsai.Single;
import com.anthonycr.bonsai.SingleAction; import com.anthonycr.bonsai.SingleAction;
import com.anthonycr.bonsai.SingleOnSubscribe; import com.anthonycr.bonsai.SingleOnSubscribe;
@ -19,6 +22,8 @@ import java.io.IOException;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import javax.inject.Inject;
import acr.browser.lightning.R; import acr.browser.lightning.R;
import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.database.HistoryItem; import acr.browser.lightning.database.HistoryItem;
@ -46,15 +51,21 @@ public class HistoryPage {
private static final String END = "</div></body></html>"; private static final String END = "</div></body></html>";
private HistoryPage() {} @NonNull private final String mTitle;
@Inject Application mApp;
public HistoryPage() {
BrowserApp.getAppComponent().inject(this);
mTitle = mApp.getString(R.string.action_history);
}
@NonNull @NonNull
public static Single<String> getHistoryPage() { public Single<String> getHistoryPage() {
return Single.create(new SingleAction<String>() { return Single.create(new SingleAction<String>() {
@Override @Override
public void onSubscribe(@NonNull final SingleSubscriber<String> subscriber) { public void onSubscribe(@NonNull final SingleSubscriber<String> subscriber) {
final String title = BrowserApp.getApplication().getString(R.string.action_history); final StringBuilder historyBuilder = new StringBuilder(HEADING_1 + mTitle + HEADING_2);
final StringBuilder historyBuilder = new StringBuilder(HEADING_1 + title + HEADING_2);
HistoryModel.lastHundredVisitedHistoryItems() HistoryModel.lastHundredVisitedHistoryItems()
.subscribe(new SingleOnSubscribe<List<HistoryItem>>() { .subscribe(new SingleOnSubscribe<List<HistoryItem>>() {
@ -76,7 +87,7 @@ public class HistoryPage {
} }
historyBuilder.append(END); historyBuilder.append(END);
File historyWebPage = new File(BrowserApp.getApplication().getFilesDir(), FILENAME); File historyWebPage = new File(mApp.getFilesDir(), FILENAME);
FileWriter historyWriter = null; FileWriter historyWriter = null;
try { try {
//noinspection IOResourceOpenedButNotSafelyClosed //noinspection IOResourceOpenedButNotSafelyClosed
@ -97,17 +108,26 @@ public class HistoryPage {
} }
/** /**
* Use this method to immediately delete the history * Use this observable to immediately delete the history
* page on the current thread. This will clear the * page. This will clear the cached history page that was
* cached history page that was stored on file. * stored on file.
* *
* @param application the application object needed to get the file. * @return a completable that deletes the history page
* when subscribed.
*/ */
public static void deleteHistoryPage(@NonNull Application application) { @NonNull
File historyWebPage = new File(application.getFilesDir(), FILENAME); public Completable deleteHistoryPage() {
return Completable.create(new CompletableAction() {
@Override
public void onSubscribe(@NonNull CompletableSubscriber subscriber) {
File historyWebPage = new File(mApp.getFilesDir(), FILENAME);
if (historyWebPage.exists()) { if (historyWebPage.exists()) {
historyWebPage.delete(); historyWebPage.delete();
} }
subscriber.onComplete();
}
});
} }
} }

54
app/src/main/java/acr/browser/lightning/constant/StartPage.java

@ -4,14 +4,15 @@
package acr.browser.lightning.constant; package acr.browser.lightning.constant;
import android.app.Application; import android.app.Application;
import android.os.AsyncTask;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.anthonycr.bonsai.Single;
import com.anthonycr.bonsai.SingleAction;
import com.anthonycr.bonsai.SingleSubscriber;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.lang.ref.WeakReference;
import javax.inject.Inject; import javax.inject.Inject;
@ -19,9 +20,8 @@ import acr.browser.lightning.R;
import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.preference.PreferenceManager; import acr.browser.lightning.preference.PreferenceManager;
import acr.browser.lightning.utils.Utils; import acr.browser.lightning.utils.Utils;
import acr.browser.lightning.view.LightningView;
public class StartPage extends AsyncTask<Void, Void, Void> { public class StartPage {
public static final String FILENAME = "homepage.html"; public static final String FILENAME = "homepage.html";
@ -55,44 +55,21 @@ public class StartPage extends AsyncTask<Void, Void, Void> {
private static final String END = "\" + document.getElementById(\"search_input\").value;document.getElementById(\"search_input\").value = \"\";}return false;}</script></body></html>"; private static final String END = "\" + document.getElementById(\"search_input\").value;document.getElementById(\"search_input\").value = \"\";}return false;}</script></body></html>";
@NonNull private final String mTitle; @NonNull private final String mTitle;
@NonNull private final Application mApp;
@NonNull private final WeakReference<LightningView> mTabReference;
@Inject Application mApp;
@Inject PreferenceManager mPreferenceManager; @Inject PreferenceManager mPreferenceManager;
private String mStartpageUrl; public StartPage() {
public StartPage(LightningView tab, @NonNull Application app) {
BrowserApp.getAppComponent().inject(this); BrowserApp.getAppComponent().inject(this);
mTitle = app.getString(R.string.home); mTitle = mApp.getString(R.string.home);
mApp = app;
mTabReference = new WeakReference<>(tab);
}
@Nullable
@Override
protected Void doInBackground(Void... params) {
mStartpageUrl = getHomepage();
return null;
} }
@NonNull
public Single<String> getHomepage() {
return Single.create(new SingleAction<String>() {
@Override @Override
protected void onPostExecute(Void aVoid) { public void onSubscribe(@NonNull SingleSubscriber<String> subscriber) {
super.onPostExecute(aVoid);
LightningView tab = mTabReference.get();
if (tab != null) {
tab.loadUrl(mStartpageUrl);
}
}
/**
* This method builds the homepage and returns the local URL to be loaded
* when it finishes building.
*
* @return the URL to load
*/
@NonNull
private String getHomepage() {
StringBuilder homepageBuilder = new StringBuilder(HEAD_1 + mTitle + HEAD_2); StringBuilder homepageBuilder = new StringBuilder(HEAD_1 + mTitle + HEAD_2);
String icon; String icon;
String searchUrl; String searchUrl;
@ -186,11 +163,10 @@ public class StartPage extends AsyncTask<Void, Void, Void> {
Utils.close(hWriter); Utils.close(hWriter);
} }
return Constants.FILE + homepage; subscriber.onItem(Constants.FILE + homepage);
}
public void load() { }
executeOnExecutor(BrowserApp.getIOThread()); });
} }
} }

25
app/src/main/java/acr/browser/lightning/view/LightningView.java

@ -57,6 +57,7 @@ import acr.browser.lightning.preference.PreferenceManager;
import com.anthonycr.bonsai.Schedulers; import com.anthonycr.bonsai.Schedulers;
import acr.browser.lightning.utils.Preconditions;
import acr.browser.lightning.utils.ProxyUtils; import acr.browser.lightning.utils.ProxyUtils;
import acr.browser.lightning.utils.UrlUtils; import acr.browser.lightning.utils.UrlUtils;
import acr.browser.lightning.utils.Utils; import acr.browser.lightning.utils.Utils;
@ -213,7 +214,16 @@ public class LightningView {
* UI thread. * UI thread.
*/ */
private void loadStartpage() { private void loadStartpage() {
new StartPage(this, BrowserApp.get(mActivity)).load(); new StartPage().getHomepage()
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.main())
.subscribe(new SingleOnSubscribe<String>() {
@Override
public void onItem(@Nullable String item) {
Preconditions.checkNonNull(item);
loadUrl(item);
}
});
} }
/** /**
@ -222,9 +232,16 @@ public class LightningView {
* UI thread. It also caches the default folder icon locally. * UI thread. It also caches the default folder icon locally.
*/ */
public void loadBookmarkpage() { public void loadBookmarkpage() {
if (mWebView == null) new BookmarkPage(mActivity).getBookmarkPage()
return; .subscribeOn(Schedulers.io())
new BookmarkPage(this, mActivity, mBookmarkManager).load(); .observeOn(Schedulers.main())
.subscribe(new SingleOnSubscribe<String>() {
@Override
public void onItem(@Nullable String item) {
Preconditions.checkNonNull(item);
loadUrl(item);
}
});
} }
/** /**

Loading…
Cancel
Save