Browse Source

Make BookmarkPage an AsyncTask to simplify its use, change recursion to iteration in DownloadHandler

master
Anthony Restaino 9 years ago
parent
commit
22960c9bd6
  1. 101
      app/src/main/java/acr/browser/lightning/constant/BookmarkPage.java
  2. 32
      app/src/main/java/acr/browser/lightning/download/DownloadHandler.java
  3. 28
      app/src/main/java/acr/browser/lightning/view/LightningView.java

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

@ -3,34 +3,39 @@
*/ */
package acr.browser.lightning.constant; package acr.browser.lightning.constant;
import android.app.Application;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
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.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;
import acr.browser.lightning.view.LightningView;
public final class BookmarkPage { public final class BookmarkPage extends AsyncTask<Void, Void, Void> {
/** /**
* The bookmark page standard suffix * The bookmark page standard suffix
*/ */
public static final String FILENAME = "bookmarks.html"; public static final String FILENAME = "bookmarks.html";
private static final String HEADING = "<!DOCTYPE html><html xmlns=http://www.w3.org/1999/xhtml>\n" + private static final String HEADING_1 = "<!DOCTYPE html><html xmlns=http://www.w3.org/1999/xhtml>\n" +
"<head>\n" + "<head>\n" +
"<meta content=en-us http-equiv=Content-Language />\n" + "<meta content=en-us http-equiv=Content-Language />\n" +
"<meta content='text/html; charset=utf-8' http-equiv=Content-Type />\n" + "<meta content='text/html; charset=utf-8' http-equiv=Content-Type />\n" +
"<meta name=viewport content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no'>\n" + "<meta name=viewport content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no'>\n" +
"<title>" + "<title>";
BrowserApp.getContext().getString(R.string.action_bookmarks) +
"</title>\n" + private static final String HEADING_2 = "</title>\n" +
"</head>\n" + "</head>\n" +
"<style>body{background:#e1e1e1;max-width:100%;min-height:100%}#content{width:100%;max-width:800px;margin:0 auto;text-align:center}.box{vertical-align:middle;text-align:center;position:relative;display:inline-block;height:45px;width:150px;margin:10px;background-color:#fff;box-shadow:0 3px 6px 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-content{height:25px;width:100%;vertical-align:middle;text-align:center;display:table-cell}p.ellipses{" + "<style>body{background:#e1e1e1;max-width:100%;min-height:100%}#content{width:100%;max-width:800px;margin:0 auto;text-align:center}.box{vertical-align:middle;text-align:center;position:relative;display:inline-block;height:45px;width:150px;margin:10px;background-color:#fff;box-shadow:0 3px 6px 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-content{height:25px;width:100%;vertical-align:middle;text-align:center;display:table-cell}p.ellipses{" +
"width:130px;font-size: small;font-family: Arial, Helvetica, 'sans-serif';white-space:nowrap;overflow:hidden;text-align:left;vertical-align:middle;margin:auto;text-overflow:ellipsis;-o-text-overflow:ellipsis;-ms-text-overflow:ellipsis}.box a{width:100%;height:100%;position:absolute;left:0;top:0}img{vertical-align:middle;margin-right:10px;width:20px;height:20px;}.margin{margin:10px}</style>\n" + "width:130px;font-size: small;font-family: Arial, Helvetica, 'sans-serif';white-space:nowrap;overflow:hidden;text-align:left;vertical-align:middle;margin:auto;text-overflow:ellipsis;-o-text-overflow:ellipsis;-ms-text-overflow:ellipsis}.box a{width:100%;height:100%;position:absolute;left:0;top:0}img{vertical-align:middle;margin-right:10px;width:20px;height:20px;}.margin{margin:10px}</style>\n" +
@ -52,46 +57,84 @@ public final class BookmarkPage {
private static final String END = "</div></body></html>"; private static final String END = "</div></body></html>";
private static final File FILES_DIR = BrowserApp.getContext().getFilesDir(); private final File mFilesDir;
private static final File CACHE_DIR = BrowserApp.getContext().getCacheDir(); private final File mCacheDir;
private final BookmarkManager mManager;
private final WeakReference<LightningView> mTabReference;
private final Bitmap mFolderIcon;
private final String mTitle;
public BookmarkPage(BookmarkManager manager, LightningView tab, Application app, Bitmap folderIcon) {
mFilesDir = app.getFilesDir();
mCacheDir = app.getCacheDir();
mTitle = app.getString(R.string.action_bookmarks);
mManager = manager;
mTabReference = new WeakReference<>(tab);
mFolderIcon = folderIcon;
}
@Override
protected Void doInBackground(Void... params) {
cacheDefaultFolderIcon();
buildBookmarkPage(null, mManager);
return 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);
}
}
private BookmarkPage() {} private void cacheDefaultFolderIcon() {
FileOutputStream outputStream = null;
File image = new File(mCacheDir, "folder.png");
try {
outputStream = new FileOutputStream(image);
mFolderIcon.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
mFolderIcon.recycle();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
Utils.close(outputStream);
}
}
public static void buildBookmarkPage(final String folder, final BookmarkManager manager) { private void buildBookmarkPage(final String folder, final BookmarkManager manager) {
final List<HistoryItem> list = manager.getBookmarksFromFolder(folder, true); final List<HistoryItem> list = manager.getBookmarksFromFolder(folder, true);
final File bookmarkWebPage; final File bookmarkWebPage;
if (folder == null || folder.isEmpty()) { if (folder == null || folder.isEmpty()) {
bookmarkWebPage = new File(FILES_DIR, FILENAME); bookmarkWebPage = new File(mFilesDir, FILENAME);
} else { } else {
bookmarkWebPage = new File(FILES_DIR, folder + '-' + FILENAME); bookmarkWebPage = new File(mFilesDir, folder + '-' + FILENAME);
} }
final StringBuilder bookmarkBuilder = new StringBuilder(BookmarkPage.HEADING); final StringBuilder bookmarkBuilder = new StringBuilder(HEADING_1 + mTitle + HEADING_2);
final String folderIconPath = Constants.FILE + CACHE_DIR + "/folder.png"; final String folderIconPath = Constants.FILE + mCacheDir + "/folder.png";
for (int n = 0, size = list.size(); n < size; n++) { for (int n = 0, size = list.size(); n < size; n++) {
final HistoryItem item = list.get(n); final HistoryItem item = list.get(n);
bookmarkBuilder.append(BookmarkPage.PART1); bookmarkBuilder.append(PART1);
if (item.isFolder()) { if (item.isFolder()) {
final File folderPage = new File(FILES_DIR, item.getTitle() + '-' + FILENAME); final File folderPage = new File(mFilesDir, item.getTitle() + '-' + FILENAME);
bookmarkBuilder.append(Constants.FILE).append(folderPage); bookmarkBuilder.append(Constants.FILE).append(folderPage);
bookmarkBuilder.append(BookmarkPage.PART2); bookmarkBuilder.append(PART2);
bookmarkBuilder.append(folderIconPath); bookmarkBuilder.append(folderIconPath);
new Thread(new Runnable() { buildBookmarkPage(item.getTitle(), manager);
@Override
public void run() {
buildBookmarkPage(item.getTitle(), manager);
}
}).run();
} else { } else {
bookmarkBuilder.append(item.getUrl()); bookmarkBuilder.append(item.getUrl());
bookmarkBuilder.append(BookmarkPage.PART2).append(BookmarkPage.PART3); bookmarkBuilder.append(PART2).append(PART3);
bookmarkBuilder.append(item.getUrl()); bookmarkBuilder.append(item.getUrl());
} }
bookmarkBuilder.append(BookmarkPage.PART4); bookmarkBuilder.append(PART4);
bookmarkBuilder.append(item.getTitle()); bookmarkBuilder.append(item.getTitle());
bookmarkBuilder.append(BookmarkPage.PART5); bookmarkBuilder.append(PART5);
} }
bookmarkBuilder.append(BookmarkPage.END); bookmarkBuilder.append(END);
FileWriter bookWriter = null; FileWriter bookWriter = null;
try { try {
//noinspection IOResourceOpenedButNotSafelyClosed //noinspection IOResourceOpenedButNotSafelyClosed

32
app/src/main/java/acr/browser/lightning/download/DownloadHandler.java

@ -294,26 +294,28 @@ public class DownloadHandler {
* @return the first existent parent * @return the first existent parent
*/ */
private static String getFirstRealParentDirectory(String directory) { private static String getFirstRealParentDirectory(String directory) {
if (directory == null || directory.isEmpty()) { while (true) {
return "/"; if (directory == null || directory.isEmpty()) {
} return "/";
directory = addNecessarySlashes(directory); }
File file = new File(directory); directory = addNecessarySlashes(directory);
if (!file.isDirectory()) { File file = new File(directory);
int indexSlash = directory.lastIndexOf('/'); if (!file.isDirectory()) {
if (indexSlash > 0) { int indexSlash = directory.lastIndexOf('/');
String parent = directory.substring(0, indexSlash); if (indexSlash > 0) {
int previousIndex = parent.lastIndexOf('/'); String parent = directory.substring(0, indexSlash);
if (previousIndex > 0) { int previousIndex = parent.lastIndexOf('/');
return getFirstRealParentDirectory(parent.substring(0, previousIndex)); if (previousIndex > 0) {
directory = parent.substring(0, previousIndex);
} else {
return "/";
}
} else { } else {
return "/"; return "/";
} }
} else { } else {
return "/"; return directory;
} }
} else {
return directory;
} }
} }

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

@ -199,32 +199,8 @@ public class LightningView {
public void loadBookmarkpage() { public void loadBookmarkpage() {
if (mWebView == null) if (mWebView == null)
return; return;
BrowserApp.getIOThread().execute(new Runnable() { Bitmap folderIcon = ThemeUtils.getThemedBitmap(mActivity, R.drawable.ic_folder, false);
@Override new BookmarkPage(mBookmarkManager, this, BrowserApp.get(mActivity), folderIcon).executeOnExecutor(BrowserApp.getIOThread());
public void run() {
Bitmap folderIcon = ThemeUtils.getThemedBitmap(mActivity, R.drawable.ic_folder, false);
FileOutputStream outputStream = null;
File image = new File(mActivity.getCacheDir(), "folder.png");
try {
outputStream = new FileOutputStream(image);
folderIcon.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
folderIcon.recycle();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
Utils.close(outputStream);
}
final File bookmarkWebPage = new File(mActivity.getFilesDir(), BookmarkPage.FILENAME);
BookmarkPage.buildBookmarkPage(null, mBookmarkManager);
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
mWebView.loadUrl(Constants.FILE + bookmarkWebPage, mRequestHeaders);
}
});
}
});
} }
/** /**

Loading…
Cancel
Save