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

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

@ -164,11 +164,29 @@ public class TabsManager { @@ -164,11 +164,29 @@ public class TabsManager {
String url = item.getString(URL_KEY);
if (url != null && tab.getWebView() != null) {
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)) {
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)) {
HistoryPage.getHistoryPage()
new HistoryPage().getHistoryPage()
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.main())
.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; @@ -8,6 +8,8 @@ import acr.browser.lightning.activity.TabsManager;
import acr.browser.lightning.activity.ThemableBrowserActivity;
import acr.browser.lightning.activity.ThemableSettingsActivity;
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.dialog.LightningDialogBuilder;
import acr.browser.lightning.download.LightningDownloadListener;
@ -62,6 +64,10 @@ public interface AppComponent { @@ -62,6 +64,10 @@ public interface AppComponent {
void inject(StartPage startPage);
void inject(HistoryPage historyPage);
void inject(BookmarkPage bookmarkPage);
void inject(BrowserPresenter presenter);
void inject(TabsManager manager);

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

@ -6,27 +6,30 @@ package acr.browser.lightning.constant; @@ -6,27 +6,30 @@ package acr.browser.lightning.constant;
import android.app.Activity;
import android.app.Application;
import android.graphics.Bitmap;
import android.os.AsyncTask;
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.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.List;
import javax.inject.Inject;
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;
public final class BookmarkPage extends AsyncTask<Void, Void, Void> {
public final class BookmarkPage {
/**
* The bookmark page standard suffix
@ -67,38 +70,34 @@ public final class BookmarkPage extends AsyncTask<Void, Void, Void> { @@ -67,38 +70,34 @@ public final class BookmarkPage extends AsyncTask<Void, Void, Void> {
private File mFilesDir;
private File mCacheDir;
private final Application mApp;
private final BookmarkManager mManager;
@NonNull private final WeakReference<LightningView> mTabReference;
@Inject Application mApp;
@Inject BookmarkManager mManager;
private final Bitmap mFolderIcon;
@NonNull private final String mTitle;
public BookmarkPage(LightningView tab, @NonNull Activity activity, BookmarkManager manager) {
mApp = BrowserApp.get(activity);
final Bitmap folderIcon = ThemeUtils.getThemedBitmap(activity, R.drawable.ic_folder, false);
public BookmarkPage(@NonNull Activity activity) {
BrowserApp.getAppComponent().inject(this);
mFolderIcon = ThemeUtils.getThemedBitmap(activity, R.drawable.ic_folder, false);
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
protected Void doInBackground(Void... params) {
public void onSubscribe(@NonNull SingleSubscriber<String> subscriber) {
mCacheDir = mApp.getCacheDir();
mFilesDir = mApp.getFilesDir();
cacheDefaultFolderIcon();
buildBookmarkPage(null, mManager);
return null;
}
buildBookmarkPage(null);
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
LightningView tab = mTabReference.get();
if (tab != null) {
File bookmarkWebPage = new File(mFilesDir, FILENAME);
tab.loadUrl(Constants.FILE + bookmarkWebPage);
subscriber.onItem(Constants.FILE + bookmarkWebPage);
subscriber.onComplete();
}
});
}
private void cacheDefaultFolderIcon() {
@ -115,8 +114,8 @@ public final class BookmarkPage extends AsyncTask<Void, Void, Void> { @@ -115,8 +114,8 @@ public final class BookmarkPage extends AsyncTask<Void, Void, Void> {
}
}
private void buildBookmarkPage(@Nullable final String folder, @NonNull final BookmarkManager manager) {
final List<HistoryItem> list = manager.getBookmarksCopyFromFolder(folder, true);
private void buildBookmarkPage(@Nullable final String folder) {
final List<HistoryItem> list = mManager.getBookmarksCopyFromFolder(folder, true);
final File bookmarkWebPage;
if (folder == null || folder.isEmpty()) {
bookmarkWebPage = new File(mFilesDir, FILENAME);
@ -134,7 +133,7 @@ public final class BookmarkPage extends AsyncTask<Void, Void, Void> { @@ -134,7 +133,7 @@ public final class BookmarkPage extends AsyncTask<Void, Void, Void> {
bookmarkBuilder.append(Constants.FILE).append(folderPage);
bookmarkBuilder.append(PART2);
bookmarkBuilder.append(folderIconPath);
buildBookmarkPage(item.getTitle(), manager);
buildBookmarkPage(item.getTitle());
} else {
bookmarkBuilder.append(item.getUrl());
bookmarkBuilder.append(PART2).append(PART3);
@ -157,8 +156,4 @@ public final class BookmarkPage extends AsyncTask<Void, Void, Void> { @@ -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; @@ -8,6 +8,9 @@ import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
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.SingleAction;
import com.anthonycr.bonsai.SingleOnSubscribe;
@ -19,6 +22,8 @@ import java.io.IOException; @@ -19,6 +22,8 @@ import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.inject.Inject;
import acr.browser.lightning.R;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.database.HistoryItem;
@ -46,15 +51,21 @@ public class HistoryPage { @@ -46,15 +51,21 @@ public class HistoryPage {
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
public static Single<String> getHistoryPage() {
public Single<String> getHistoryPage() {
return Single.create(new SingleAction<String>() {
@Override
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 + title + HEADING_2);
final StringBuilder historyBuilder = new StringBuilder(HEADING_1 + mTitle + HEADING_2);
HistoryModel.lastHundredVisitedHistoryItems()
.subscribe(new SingleOnSubscribe<List<HistoryItem>>() {
@ -76,7 +87,7 @@ public class HistoryPage { @@ -76,7 +87,7 @@ public class HistoryPage {
}
historyBuilder.append(END);
File historyWebPage = new File(BrowserApp.getApplication().getFilesDir(), FILENAME);
File historyWebPage = new File(mApp.getFilesDir(), FILENAME);
FileWriter historyWriter = null;
try {
//noinspection IOResourceOpenedButNotSafelyClosed
@ -97,17 +108,26 @@ public class HistoryPage { @@ -97,17 +108,26 @@ public class HistoryPage {
}
/**
* Use this method to immediately delete the history
* page on the current thread. This will clear the
* cached history page that was stored on file.
* Use this observable to immediately delete the history
* page. This will clear the cached history page that was
* 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) {
File historyWebPage = new File(application.getFilesDir(), FILENAME);
@NonNull
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()) {
historyWebPage.delete();
}
subscriber.onComplete();
}
});
}
}

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

@ -4,14 +4,15 @@ @@ -4,14 +4,15 @@
package acr.browser.lightning.constant;
import android.app.Application;
import android.os.AsyncTask;
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.FileWriter;
import java.io.IOException;
import java.lang.ref.WeakReference;
import javax.inject.Inject;
@ -19,9 +20,8 @@ import acr.browser.lightning.R; @@ -19,9 +20,8 @@ import acr.browser.lightning.R;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.preference.PreferenceManager;
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";
@ -55,44 +55,21 @@ public class StartPage extends AsyncTask<Void, Void, Void> { @@ -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>";
@NonNull private final String mTitle;
@NonNull private final Application mApp;
@NonNull private final WeakReference<LightningView> mTabReference;
@Inject Application mApp;
@Inject PreferenceManager mPreferenceManager;
private String mStartpageUrl;
public StartPage(LightningView tab, @NonNull Application app) {
public StartPage() {
BrowserApp.getAppComponent().inject(this);
mTitle = app.getString(R.string.home);
mApp = app;
mTabReference = new WeakReference<>(tab);
}
@Nullable
@Override
protected Void doInBackground(Void... params) {
mStartpageUrl = getHomepage();
return null;
mTitle = mApp.getString(R.string.home);
}
@NonNull
public Single<String> getHomepage() {
return Single.create(new SingleAction<String>() {
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
LightningView tab = mTabReference.get();
if (tab != null) {
tab.loadUrl(mStartpageUrl);
}
}
public void onSubscribe(@NonNull SingleSubscriber<String> subscriber) {
/**
* 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);
String icon;
String searchUrl;
@ -186,11 +163,10 @@ public class StartPage extends AsyncTask<Void, Void, Void> { @@ -186,11 +163,10 @@ public class StartPage extends AsyncTask<Void, Void, Void> {
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; @@ -57,6 +57,7 @@ import acr.browser.lightning.preference.PreferenceManager;
import com.anthonycr.bonsai.Schedulers;
import acr.browser.lightning.utils.Preconditions;
import acr.browser.lightning.utils.ProxyUtils;
import acr.browser.lightning.utils.UrlUtils;
import acr.browser.lightning.utils.Utils;
@ -213,7 +214,16 @@ public class LightningView { @@ -213,7 +214,16 @@ public class LightningView {
* UI thread.
*/
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 { @@ -222,9 +232,16 @@ public class LightningView {
* UI thread. It also caches the default folder icon locally.
*/
public void loadBookmarkpage() {
if (mWebView == null)
return;
new BookmarkPage(this, mActivity, mBookmarkManager).load();
new BookmarkPage(mActivity).getBookmarkPage()
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.main())
.subscribe(new SingleOnSubscribe<String>() {
@Override
public void onItem(@Nullable String item) {
Preconditions.checkNonNull(item);
loadUrl(item);
}
});
}
/**

Loading…
Cancel
Save