Make BookmarkPage an AsyncTask to simplify its use, change recursion to iteration in DownloadHandler
This commit is contained in:
parent
930880b339
commit
22960c9bd6
@ -3,34 +3,39 @@
|
||||
*/
|
||||
package acr.browser.lightning.constant;
|
||||
|
||||
import android.app.Application;
|
||||
import android.graphics.Bitmap;
|
||||
import android.os.AsyncTask;
|
||||
|
||||
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.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
|
||||
*/
|
||||
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" +
|
||||
"<meta content=en-us http-equiv=Content-Language />\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" +
|
||||
"<title>" +
|
||||
BrowserApp.getContext().getString(R.string.action_bookmarks) +
|
||||
"</title>\n" +
|
||||
"<title>";
|
||||
|
||||
private static final String HEADING_2 = "</title>\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{" +
|
||||
"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 File FILES_DIR = BrowserApp.getContext().getFilesDir();
|
||||
private static final File CACHE_DIR = BrowserApp.getContext().getCacheDir();
|
||||
private final File mFilesDir;
|
||||
private final File mCacheDir;
|
||||
|
||||
private BookmarkPage() {}
|
||||
private final BookmarkManager mManager;
|
||||
private final WeakReference<LightningView> mTabReference;
|
||||
private final Bitmap mFolderIcon;
|
||||
private final String mTitle;
|
||||
|
||||
public static void buildBookmarkPage(final String folder, final BookmarkManager manager) {
|
||||
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 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);
|
||||
}
|
||||
}
|
||||
|
||||
private void buildBookmarkPage(final String folder, final BookmarkManager manager) {
|
||||
final List<HistoryItem> list = manager.getBookmarksFromFolder(folder, true);
|
||||
final File bookmarkWebPage;
|
||||
if (folder == null || folder.isEmpty()) {
|
||||
bookmarkWebPage = new File(FILES_DIR, FILENAME);
|
||||
bookmarkWebPage = new File(mFilesDir, FILENAME);
|
||||
} 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++) {
|
||||
final HistoryItem item = list.get(n);
|
||||
bookmarkBuilder.append(BookmarkPage.PART1);
|
||||
bookmarkBuilder.append(PART1);
|
||||
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(BookmarkPage.PART2);
|
||||
bookmarkBuilder.append(PART2);
|
||||
bookmarkBuilder.append(folderIconPath);
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
buildBookmarkPage(item.getTitle(), manager);
|
||||
}
|
||||
}).run();
|
||||
buildBookmarkPage(item.getTitle(), manager);
|
||||
} else {
|
||||
bookmarkBuilder.append(item.getUrl());
|
||||
bookmarkBuilder.append(BookmarkPage.PART2).append(BookmarkPage.PART3);
|
||||
bookmarkBuilder.append(PART2).append(PART3);
|
||||
bookmarkBuilder.append(item.getUrl());
|
||||
}
|
||||
bookmarkBuilder.append(BookmarkPage.PART4);
|
||||
bookmarkBuilder.append(PART4);
|
||||
bookmarkBuilder.append(item.getTitle());
|
||||
bookmarkBuilder.append(BookmarkPage.PART5);
|
||||
bookmarkBuilder.append(PART5);
|
||||
}
|
||||
bookmarkBuilder.append(BookmarkPage.END);
|
||||
bookmarkBuilder.append(END);
|
||||
FileWriter bookWriter = null;
|
||||
try {
|
||||
//noinspection IOResourceOpenedButNotSafelyClosed
|
||||
|
@ -294,26 +294,28 @@ public class DownloadHandler {
|
||||
* @return the first existent parent
|
||||
*/
|
||||
private static String getFirstRealParentDirectory(String directory) {
|
||||
if (directory == null || directory.isEmpty()) {
|
||||
return "/";
|
||||
}
|
||||
directory = addNecessarySlashes(directory);
|
||||
File file = new File(directory);
|
||||
if (!file.isDirectory()) {
|
||||
int indexSlash = directory.lastIndexOf('/');
|
||||
if (indexSlash > 0) {
|
||||
String parent = directory.substring(0, indexSlash);
|
||||
int previousIndex = parent.lastIndexOf('/');
|
||||
if (previousIndex > 0) {
|
||||
return getFirstRealParentDirectory(parent.substring(0, previousIndex));
|
||||
while (true) {
|
||||
if (directory == null || directory.isEmpty()) {
|
||||
return "/";
|
||||
}
|
||||
directory = addNecessarySlashes(directory);
|
||||
File file = new File(directory);
|
||||
if (!file.isDirectory()) {
|
||||
int indexSlash = directory.lastIndexOf('/');
|
||||
if (indexSlash > 0) {
|
||||
String parent = directory.substring(0, indexSlash);
|
||||
int previousIndex = parent.lastIndexOf('/');
|
||||
if (previousIndex > 0) {
|
||||
directory = parent.substring(0, previousIndex);
|
||||
} else {
|
||||
return "/";
|
||||
}
|
||||
} else {
|
||||
return "/";
|
||||
}
|
||||
} else {
|
||||
return "/";
|
||||
return directory;
|
||||
}
|
||||
} else {
|
||||
return directory;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -199,32 +199,8 @@ public class LightningView {
|
||||
public void loadBookmarkpage() {
|
||||
if (mWebView == null)
|
||||
return;
|
||||
BrowserApp.getIOThread().execute(new Runnable() {
|
||||
@Override
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
Bitmap folderIcon = ThemeUtils.getThemedBitmap(mActivity, R.drawable.ic_folder, false);
|
||||
new BookmarkPage(mBookmarkManager, this, BrowserApp.get(mActivity), folderIcon).executeOnExecutor(BrowserApp.getIOThread());
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user