Fixing bug where folders didn't show up on the bookmarks page
This commit is contained in:
parent
73136e4b56
commit
12a93d208d
@ -8,6 +8,7 @@ import android.app.Application;
|
|||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
|
import android.text.TextUtils;
|
||||||
|
|
||||||
import com.anthonycr.bonsai.Single;
|
import com.anthonycr.bonsai.Single;
|
||||||
import com.anthonycr.bonsai.SingleAction;
|
import com.anthonycr.bonsai.SingleAction;
|
||||||
@ -75,8 +76,16 @@ public final class BookmarkPage {
|
|||||||
|
|
||||||
private static final String FOLDER_ICON = "folder.png";
|
private static final String FOLDER_ICON = "folder.png";
|
||||||
|
|
||||||
private File mFilesDir;
|
@NonNull
|
||||||
private File mCacheDir;
|
private static File getBookmarkPage(@NonNull Application application, @Nullable String folder) {
|
||||||
|
String prefix = !TextUtils.isEmpty(folder) ? folder + '-' : "";
|
||||||
|
return new File(application.getFilesDir(), prefix + FILENAME);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private static File getFaviconFile(@NonNull Application application) {
|
||||||
|
return new File(application.getCacheDir(), FOLDER_ICON);
|
||||||
|
}
|
||||||
|
|
||||||
@Inject Application mApp;
|
@Inject Application mApp;
|
||||||
@Inject BookmarkModel mManager;
|
@Inject BookmarkModel mManager;
|
||||||
@ -95,12 +104,10 @@ public final class BookmarkPage {
|
|||||||
return Single.create(new SingleAction<String>() {
|
return Single.create(new SingleAction<String>() {
|
||||||
@Override
|
@Override
|
||||||
public void onSubscribe(@NonNull SingleSubscriber<String> subscriber) {
|
public void onSubscribe(@NonNull SingleSubscriber<String> subscriber) {
|
||||||
mCacheDir = mApp.getCacheDir();
|
|
||||||
mFilesDir = mApp.getFilesDir();
|
|
||||||
cacheDefaultFolderIcon();
|
cacheDefaultFolderIcon();
|
||||||
buildBookmarkPage(null);
|
buildBookmarkPage(null);
|
||||||
|
|
||||||
File bookmarkWebPage = new File(mFilesDir, FILENAME);
|
File bookmarkWebPage = getBookmarkPage(mApp, null);
|
||||||
|
|
||||||
subscriber.onItem(Constants.FILE + bookmarkWebPage);
|
subscriber.onItem(Constants.FILE + bookmarkWebPage);
|
||||||
subscriber.onComplete();
|
subscriber.onComplete();
|
||||||
@ -110,9 +117,8 @@ public final class BookmarkPage {
|
|||||||
|
|
||||||
private void cacheDefaultFolderIcon() {
|
private void cacheDefaultFolderIcon() {
|
||||||
FileOutputStream outputStream = null;
|
FileOutputStream outputStream = null;
|
||||||
File image = new File(mCacheDir, FOLDER_ICON);
|
|
||||||
try {
|
try {
|
||||||
outputStream = new FileOutputStream(image);
|
outputStream = new FileOutputStream(getFaviconFile(mApp));
|
||||||
mFolderIcon.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
|
mFolderIcon.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
|
||||||
mFolderIcon.recycle();
|
mFolderIcon.recycle();
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
@ -126,49 +132,64 @@ public final class BookmarkPage {
|
|||||||
mManager.getBookmarksFromFolderSorted(folder)
|
mManager.getBookmarksFromFolderSorted(folder)
|
||||||
.subscribe(new SingleOnSubscribe<List<HistoryItem>>() {
|
.subscribe(new SingleOnSubscribe<List<HistoryItem>>() {
|
||||||
@Override
|
@Override
|
||||||
public void onItem(@Nullable List<HistoryItem> list) {
|
public void onItem(@Nullable final List<HistoryItem> list) {
|
||||||
Preconditions.checkNonNull(list);
|
Preconditions.checkNonNull(list);
|
||||||
|
|
||||||
final File bookmarkWebPage;
|
if (folder == null) {
|
||||||
if (folder == null || folder.isEmpty()) {
|
mManager.getFoldersSorted()
|
||||||
bookmarkWebPage = new File(mFilesDir, FILENAME);
|
.subscribe(new SingleOnSubscribe<List<HistoryItem>>() {
|
||||||
} else {
|
@Override
|
||||||
bookmarkWebPage = new File(mFilesDir, folder + '-' + FILENAME);
|
public void onItem(@Nullable List<HistoryItem> item) {
|
||||||
}
|
Preconditions.checkNonNull(item);
|
||||||
final StringBuilder bookmarkBuilder = new StringBuilder(HEADING_1 + mTitle + HEADING_2);
|
|
||||||
|
|
||||||
final String folderIconPath = Constants.FILE + mCacheDir + '/' + FOLDER_ICON;
|
list.addAll(item);
|
||||||
for (int n = 0, size = list.size(); n < size; n++) {
|
|
||||||
final HistoryItem item = list.get(n);
|
buildPageHtml(list, null);
|
||||||
bookmarkBuilder.append(PART1);
|
}
|
||||||
if (item.isFolder()) {
|
});
|
||||||
final File folderPage = new File(mFilesDir, item.getTitle() + '-' + FILENAME);
|
} else {
|
||||||
bookmarkBuilder.append(Constants.FILE).append(folderPage);
|
buildPageHtml(list, folder);
|
||||||
bookmarkBuilder.append(PART2);
|
|
||||||
bookmarkBuilder.append(folderIconPath);
|
|
||||||
buildBookmarkPage(item.getTitle());
|
|
||||||
} else {
|
|
||||||
bookmarkBuilder.append(item.getUrl());
|
|
||||||
bookmarkBuilder.append(PART2).append(PART3);
|
|
||||||
bookmarkBuilder.append(item.getUrl());
|
|
||||||
}
|
|
||||||
bookmarkBuilder.append(PART4);
|
|
||||||
bookmarkBuilder.append(item.getTitle());
|
|
||||||
bookmarkBuilder.append(PART5);
|
|
||||||
}
|
|
||||||
bookmarkBuilder.append(END);
|
|
||||||
FileWriter bookWriter = null;
|
|
||||||
try {
|
|
||||||
//noinspection IOResourceOpenedButNotSafelyClosed
|
|
||||||
bookWriter = new FileWriter(bookmarkWebPage, false);
|
|
||||||
bookWriter.write(bookmarkBuilder.toString());
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} finally {
|
|
||||||
Utils.close(bookWriter);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void buildPageHtml(@NonNull List<HistoryItem> bookmarksAndFolders, @Nullable String folder) {
|
||||||
|
final File bookmarkWebPage = getBookmarkPage(mApp, folder);
|
||||||
|
|
||||||
|
final StringBuilder bookmarkBuilder = new StringBuilder(HEADING_1 + mTitle + HEADING_2);
|
||||||
|
|
||||||
|
final String folderIconPath = getFaviconFile(mApp).toString();
|
||||||
|
|
||||||
|
for (int n = 0, size = bookmarksAndFolders.size(); n < size; n++) {
|
||||||
|
final HistoryItem item = bookmarksAndFolders.get(n);
|
||||||
|
bookmarkBuilder.append(PART1);
|
||||||
|
if (item.isFolder()) {
|
||||||
|
final File folderPage = getBookmarkPage(mApp, item.getTitle());
|
||||||
|
bookmarkBuilder.append(Constants.FILE).append(folderPage);
|
||||||
|
bookmarkBuilder.append(PART2);
|
||||||
|
bookmarkBuilder.append(folderIconPath);
|
||||||
|
buildBookmarkPage(item.getTitle());
|
||||||
|
} else {
|
||||||
|
bookmarkBuilder.append(item.getUrl());
|
||||||
|
bookmarkBuilder.append(PART2).append(PART3);
|
||||||
|
bookmarkBuilder.append(item.getUrl());
|
||||||
|
}
|
||||||
|
bookmarkBuilder.append(PART4);
|
||||||
|
bookmarkBuilder.append(item.getTitle());
|
||||||
|
bookmarkBuilder.append(PART5);
|
||||||
|
}
|
||||||
|
bookmarkBuilder.append(END);
|
||||||
|
FileWriter bookWriter = null;
|
||||||
|
try {
|
||||||
|
//noinspection IOResourceOpenedButNotSafelyClosed
|
||||||
|
bookWriter = new FileWriter(bookmarkWebPage, false);
|
||||||
|
bookWriter.write(bookmarkBuilder.toString());
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
Utils.close(bookWriter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -79,7 +79,7 @@ public final class DownloadsPage {
|
|||||||
public void onSubscribe(@NonNull SingleSubscriber<String> subscriber) {
|
public void onSubscribe(@NonNull SingleSubscriber<String> subscriber) {
|
||||||
buildDownloadsPage();
|
buildDownloadsPage();
|
||||||
|
|
||||||
File downloadsWebPage = new File(getDownloadsPageFile(mApp), FILENAME);
|
File downloadsWebPage = getDownloadsPageFile(mApp);
|
||||||
|
|
||||||
subscriber.onItem(Constants.FILE + downloadsWebPage);
|
subscriber.onItem(Constants.FILE + downloadsWebPage);
|
||||||
subscriber.onComplete();
|
subscriber.onComplete();
|
||||||
|
@ -49,6 +49,7 @@ import acr.browser.lightning.utils.AdBlock;
|
|||||||
import acr.browser.lightning.utils.IntentUtils;
|
import acr.browser.lightning.utils.IntentUtils;
|
||||||
import acr.browser.lightning.utils.Preconditions;
|
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.Utils;
|
import acr.browser.lightning.utils.Utils;
|
||||||
|
|
||||||
public class LightningWebClient extends WebViewClient {
|
public class LightningWebClient extends WebViewClient {
|
||||||
@ -359,12 +360,12 @@ public class LightningWebClient extends WebViewClient {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else if (url.startsWith(Constants.FILE)) {
|
} else if (url.startsWith(Constants.FILE) && !UrlUtils.isSpecialUrl(url)) {
|
||||||
File file = new File(url.replace(Constants.FILE, ""));
|
File file = new File(url.replace(Constants.FILE, ""));
|
||||||
|
|
||||||
if (file.exists()) {
|
if (file.exists()) {
|
||||||
String newMimeType = MimeTypeMap.getSingleton()
|
String newMimeType = MimeTypeMap.getSingleton()
|
||||||
.getMimeTypeFromExtension(Utils.guessFileExtension(file.toString()));
|
.getMimeTypeFromExtension(Utils.guessFileExtension(file.toString()));
|
||||||
|
|
||||||
Intent intent = new Intent(Intent.ACTION_VIEW);
|
Intent intent = new Intent(Intent.ACTION_VIEW);
|
||||||
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||||
|
Loading…
Reference in New Issue
Block a user