Browse Source

Make HistoryPage an AsyncTask to be easier to use

master
Anthony Restaino 9 years ago
parent
commit
f6b60894f6
  1. 22
      app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java
  2. 7
      app/src/main/java/acr/browser/lightning/constant/BookmarkPage.java
  3. 71
      app/src/main/java/acr/browser/lightning/constant/HistoryPage.java
  4. 4
      app/src/main/java/acr/browser/lightning/constant/StartPage.java
  5. 7
      app/src/main/java/acr/browser/lightning/view/LightningView.java

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

@ -1400,22 +1400,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() {
// use a thread so that history retrieval doesn't block the UI new HistoryPage(mTabsManager.getCurrentTab(), getApplication()).load();
BrowserApp.getIOThread().execute(new Runnable() {
@Override
public void run() {
final String historyPage = HistoryPage.getHistoryPage(BrowserActivity.this);
BrowserActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
loadUrlInCurrentView(historyPage);
mSearch.setText("");
}
});
}
});
} }
/** /**
@ -2002,6 +1987,11 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
}, 150); }, 150);
} }
@Subscribe
public void loadHistory(final BrowserEvents.OpenHistoryInCurrentTab event) {
new HistoryPage(mTabsManager.getCurrentTab(), getApplication()).load();
}
/** /**
* Load the given url in a new tab, used by the the * Load the given url in a new tab, used by the the
* {@link acr.browser.lightning.fragment.BookmarksFragment} and by the * {@link acr.browser.lightning.fragment.BookmarksFragment} and by the

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

@ -16,6 +16,7 @@ import java.lang.ref.WeakReference;
import java.util.List; import java.util.List;
import acr.browser.lightning.R; import acr.browser.lightning.R;
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.Utils; import acr.browser.lightning.utils.Utils;
@ -65,7 +66,7 @@ public final class BookmarkPage extends AsyncTask<Void, Void, Void> {
private final Bitmap mFolderIcon; private final Bitmap mFolderIcon;
private final String mTitle; private final String mTitle;
public BookmarkPage(BookmarkManager manager, LightningView tab, Application app, Bitmap folderIcon) { public BookmarkPage(LightningView tab, Application app, BookmarkManager manager, Bitmap folderIcon) {
mFilesDir = app.getFilesDir(); mFilesDir = app.getFilesDir();
mCacheDir = app.getCacheDir(); mCacheDir = app.getCacheDir();
mTitle = app.getString(R.string.action_bookmarks); mTitle = app.getString(R.string.action_bookmarks);
@ -147,4 +148,8 @@ public final class BookmarkPage extends AsyncTask<Void, Void, Void> {
} }
} }
public void load() {
executeOnExecutor(BrowserApp.getIOThread());
}
} }

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

@ -3,27 +3,31 @@
*/ */
package acr.browser.lightning.constant; package acr.browser.lightning.constant;
import android.app.Application;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
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 java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import android.content.Context;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.database.HistoryItem;
import acr.browser.lightning.R; import acr.browser.lightning.R;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.database.HistoryDatabase; import acr.browser.lightning.database.HistoryDatabase;
import acr.browser.lightning.database.HistoryItem;
import acr.browser.lightning.utils.Utils; import acr.browser.lightning.utils.Utils;
import acr.browser.lightning.view.LightningView;
public class HistoryPage { public class HistoryPage extends AsyncTask<Void, Void, Void> {
public static final String FILENAME = "history.html"; public static final String FILENAME = "history.html";
private static final String HEADING = "<!DOCTYPE html><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta content=\"en-us\" http-equiv=\"Content-Language\" /><meta content=\"text/html; charset=utf-8\" http-equiv=\"Content-Type\" /><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\"><title>" private static final String HEADING_1 = "<!DOCTYPE html><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta content=\"en-us\" http-equiv=\"Content-Language\" /><meta content=\"text/html; charset=utf-8\" http-equiv=\"Content-Type\" /><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\"><title>";
+ BrowserApp.getContext().getString(R.string.action_history)
+ "</title></head><style>body { background: #e1e1e1;}.box { vertical-align:middle;position:relative; display: block; margin: 10px;padding-left:10px;padding-right:10px;padding-top:5px;padding-bottom:5px; background-color:#fff;box-shadow: 0px 2px 3px rgba( 0, 0, 0, 0.25 );font-family: Arial;color: #444;font-size: 12px;-moz-border-radius: 2px;-webkit-border-radius: 2px;border-radius: 2px;}.box a { width: 100%; height: 100%; position: absolute; left: 0; top: 0;}.black {color: black;font-size: 15px;font-family: Arial; white-space: nowrap; overflow: hidden;margin:auto; text-overflow: ellipsis; -o-text-overflow: ellipsis; -ms-text-overflow: ellipsis;}.font {color: gray;font-size: 10px;font-family: Arial; white-space: nowrap; overflow: hidden;margin:auto; text-overflow: ellipsis; -o-text-overflow: ellipsis; -ms-text-overflow: ellipsis;}</style><body><div id=\"content\">"; private static final String HEADING_2 = "</title></head><style>body { background: #e1e1e1;}.box { vertical-align:middle;position:relative; display: block; margin: 10px;padding-left:10px;padding-right:10px;padding-top:5px;padding-bottom:5px; background-color:#fff;box-shadow: 0px 2px 3px rgba( 0, 0, 0, 0.25 );font-family: Arial;color: #444;font-size: 12px;-moz-border-radius: 2px;-webkit-border-radius: 2px;border-radius: 2px;}.box a { width: 100%; height: 100%; position: absolute; left: 0; top: 0;}.black {color: black;font-size: 15px;font-family: Arial; white-space: nowrap; overflow: hidden;margin:auto; text-overflow: ellipsis; -o-text-overflow: ellipsis; -ms-text-overflow: ellipsis;}.font {color: gray;font-size: 10px;font-family: Arial; white-space: nowrap; overflow: hidden;margin:auto; text-overflow: ellipsis; -o-text-overflow: ellipsis; -ms-text-overflow: ellipsis;}</style><body><div id=\"content\">";
private static final String PART1 = "<div class=\"box\"><a href=\""; private static final String PART1 = "<div class=\"box\"><a href=\"";
@ -35,24 +39,52 @@ public class HistoryPage {
private static final String END = "</div></body></html>"; private static final String END = "</div></body></html>";
public static String getHistoryPage(Context context) { private final WeakReference<LightningView> mTabReference;
StringBuilder historyBuilder = new StringBuilder(HistoryPage.HEADING); private final File mFilesDir;
private final String mTitle;
private String mHistoryUrl = null;
public HistoryPage(LightningView tab, Application app) {
mTabReference = new WeakReference<>(tab);
mFilesDir = app.getFilesDir();
mTitle = app.getString(R.string.action_history);
}
@Override
protected Void doInBackground(Void... params) {
mHistoryUrl = getHistoryPage();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
LightningView tab = mTabReference.get();
if (tab != null && mHistoryUrl != null) {
tab.loadUrl(mHistoryUrl);
}
}
@NonNull
private String getHistoryPage() {
StringBuilder historyBuilder = new StringBuilder(HEADING_1 + mTitle + HEADING_2);
List<HistoryItem> historyList = getWebHistory(); List<HistoryItem> historyList = getWebHistory();
Iterator<HistoryItem> it = historyList.iterator(); Iterator<HistoryItem> it = historyList.iterator();
HistoryItem helper; HistoryItem helper;
while (it.hasNext()) { while (it.hasNext()) {
helper = it.next(); helper = it.next();
historyBuilder.append(HistoryPage.PART1); historyBuilder.append(PART1);
historyBuilder.append(helper.getUrl()); historyBuilder.append(helper.getUrl());
historyBuilder.append(HistoryPage.PART2); historyBuilder.append(PART2);
historyBuilder.append(helper.getTitle()); historyBuilder.append(helper.getTitle());
historyBuilder.append(HistoryPage.PART3); historyBuilder.append(PART3);
historyBuilder.append(helper.getUrl()); historyBuilder.append(helper.getUrl());
historyBuilder.append(HistoryPage.PART4); historyBuilder.append(PART4);
} }
historyBuilder.append(HistoryPage.END); historyBuilder.append(END);
File historyWebPage = new File(context.getFilesDir(), FILENAME); File historyWebPage = new File(mFilesDir, FILENAME);
FileWriter historyWriter = null; FileWriter historyWriter = null;
try { try {
//noinspection IOResourceOpenedButNotSafelyClosed //noinspection IOResourceOpenedButNotSafelyClosed
@ -70,4 +102,9 @@ public class HistoryPage {
HistoryDatabase databaseHandler = BrowserApp.getHistoryDatabase(); HistoryDatabase databaseHandler = BrowserApp.getHistoryDatabase();
return databaseHandler.getLastHundredItems(); return databaseHandler.getLastHundredItems();
} }
}
public void load() {
executeOnExecutor(BrowserApp.getIOThread());
}
}

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

@ -181,4 +181,8 @@ public class StartPage extends AsyncTask<Void, Void, Void> {
return Constants.FILE + homepage; return Constants.FILE + homepage;
} }
public void load() {
executeOnExecutor(BrowserApp.getIOThread());
}
} }

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

@ -34,9 +34,6 @@ import android.webkit.WebView;
import com.squareup.otto.Bus; import com.squareup.otto.Bus;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.Map; import java.util.Map;
@ -177,7 +174,7 @@ public class LightningView {
* UI thread. * UI thread.
*/ */
private void loadStartpage() { private void loadStartpage() {
new StartPage(this, BrowserApp.get(mActivity)).executeOnExecutor(BrowserApp.getIOThread()); new StartPage(this, BrowserApp.get(mActivity)).load();
} }
/** /**
@ -189,7 +186,7 @@ public class LightningView {
if (mWebView == null) if (mWebView == null)
return; return;
Bitmap folderIcon = ThemeUtils.getThemedBitmap(mActivity, R.drawable.ic_folder, false); Bitmap folderIcon = ThemeUtils.getThemedBitmap(mActivity, R.drawable.ic_folder, false);
new BookmarkPage(mBookmarkManager, this, BrowserApp.get(mActivity), folderIcon).executeOnExecutor(BrowserApp.getIOThread()); new BookmarkPage(this, BrowserApp.get(mActivity), mBookmarkManager, folderIcon).load();
} }
/** /**

Loading…
Cancel
Save