Bookmark page generation moved to LightningView to avoid call loop between BrowserActivity and LightningView through BrowserController

This commit is contained in:
Stefano Pacifici 2015-09-16 17:42:20 +02:00
parent 5c2cf07e20
commit 2563e81f7a
6 changed files with 46 additions and 44 deletions

View File

@ -1471,31 +1471,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
return super.onCreateOptionsMenu(menu);
}
/**
* open the HTML bookmarks page, parameter view is the WebView that should show the page
*/
@Override
public void openBookmarkPage(WebView view) {
if (view == null)
return;
Bitmap folderIcon = ThemeUtils.getThemedBitmap(this, 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);
}
File bookmarkWebPage = new File(mActivity.getFilesDir(), Constants.BOOKMARKS_FILENAME);
bookmarkPage.buildBookmarkPage(null, bookmarkManager.getBookmarksFromFolder(null, true));
view.loadUrl(Constants.FILE + bookmarkWebPage);
}
@Override
public void updateTabs() {
eventBus.post(new BrowserEvents.TabsChanged());
@ -2232,7 +2207,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
final WebView currentWebView = currentTab.getWebView();
if (currentTab != null && currentTab.getUrl().startsWith(Constants.FILE)
&& currentTab.getUrl().endsWith(Constants.BOOKMARKS_FILENAME)) {
openBookmarkPage(currentWebView);
currentTab.loadBookmarkpage();
}
if (currentTab != null) {
eventBus.post(new BrowserEvents.CurrentPageUrl(currentTab.getUrl()));
@ -2250,7 +2225,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
final WebView currentWebView = currentTab.getWebView();
if (currentTab != null && currentTab.getUrl().startsWith(Constants.FILE)
&& currentTab.getUrl().endsWith(Constants.BOOKMARKS_FILENAME)) {
openBookmarkPage(currentWebView);
currentTab.loadBookmarkpage();
}
if (currentTab != null) {
eventBus.post(new BrowserEvents.CurrentPageUrl(currentTab.getUrl()));

View File

@ -37,4 +37,6 @@ public interface AppComponent {
PreferenceManager getPreferenceManager();
void inject(LightningPreferenceFragment fragment);
BookmarkPage getBookmarkPage();
}

View File

@ -62,7 +62,8 @@ public final class BookmarkPage {
CACHE_DIR = context.getCacheDir();
}
public void buildBookmarkPage(final String folder, final List<HistoryItem> list) {
public void buildBookmarkPage(final String folder) {
final List<HistoryItem> list = manager.getBookmarksFromFolder(folder, true);
final File bookmarkWebPage;
if (folder == null || folder.isEmpty()) {
bookmarkWebPage = new File(FILES_DIR, Constants.BOOKMARKS_FILENAME);
@ -83,7 +84,7 @@ public final class BookmarkPage {
new Thread(new Runnable() {
@Override
public void run() {
buildBookmarkPage(item.getTitle(), manager.getBookmarksFromFolder(item.getTitle(), true));
buildBookmarkPage(item.getTitle());
}
}).run();
} else {

View File

@ -48,8 +48,6 @@ public interface BrowserController {
void longClickPage(String url);
void openBookmarkPage(WebView view);
void showFileChooser(ValueCallback<Uri[]> filePathCallback);
void closeEmptyTab();

View File

@ -2,6 +2,7 @@ package acr.browser.lightning.utils;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
@ -19,7 +20,7 @@ import acr.browser.lightning.controller.BrowserController;
public class IntentUtils {
private final Activity mActivity;
private final Context mActivity;
private static final Pattern ACCEPTED_URI_SCHEMA = Pattern.compile("(?i)"
+ // switch on case insensitive matching
@ -28,8 +29,8 @@ public class IntentUtils {
"(?:http|https|file):\\/\\/" + "|(?:inline|data|about|javascript):" + "|(?:.*:.*@)"
+ ')' + "(.*)");
public IntentUtils(BrowserController controller) {
mActivity = controller.getActivity();
public IntentUtils(Context context) {
mActivity = context.getApplicationContext();
}
public boolean startActivityForUrl(WebView tab, String url) {
@ -64,9 +65,11 @@ public class IntentUtils {
return false;
}
try {
if (mActivity.startActivityIfNeeded(intent, -1)) {
return true;
}
// TODO Restore this
// if (mActivity.startActivityIfNeeded(intent, -1)) {
// return true;
// }
mActivity.startActivity(intent);
} catch (ActivityNotFoundException ex) {
ex.printStackTrace();
}

View File

@ -51,6 +51,7 @@ import android.widget.LinearLayout;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
@ -75,14 +76,13 @@ public class LightningView {
private final Title mTitle;
private WebView mWebView;
private final boolean mIsIncognitoTab;
private final BrowserController mBrowserController;
private final BrowserController mBrowserController = null; // TODO REMOVE
private final GestureDetector mGestureDetector;
private final Activity mActivity;
private static String mHomepage;
private static String mDefaultUserAgent;
// TODO fix so that mWebpageBitmap can be static - static changes the icon when switching from light to dark and then back to light
private final Bitmap mWebpageBitmap;
@Inject
PreferenceManager mPreferences;
private final AdBlock mAdBlock;
private final IntentUtils mIntentUtils;
@ -104,7 +104,7 @@ public class LightningView {
private static final String[] PERMISSIONS = new String[]{Manifest.permission.ACCESS_FINE_LOCATION};
@SuppressLint("NewApi")
public LightningView(Activity activity, String url, boolean darkTheme, boolean isIncognito, BrowserController controller) {
public LightningView(Activity activity, String url, boolean darkTheme, boolean isIncognito) {
mPreferences = BrowserApp.getAppComponent().getPreferenceManager();
mActivity = activity;
mWebView = new WebView(activity);
@ -117,9 +117,7 @@ public class LightningView {
mMaxFling = ViewConfiguration.get(activity).getScaledMaximumFlingVelocity();
mBrowserController = controller;
mIntentUtils = new IntentUtils(mBrowserController);
mIntentUtils = new IntentUtils(activity);
mWebView.setDrawingCacheBackgroundColor(Color.WHITE);
mWebView.setFocusableInTouchMode(true);
mWebView.setFocusable(true);
@ -161,12 +159,37 @@ public class LightningView {
if (mHomepage.startsWith("about:home")) {
mWebView.loadUrl(StartPage.getHomepage(mActivity));
} else if (mHomepage.startsWith("about:bookmarks")) {
mBrowserController.openBookmarkPage(mWebView);
loadBookmarkpage();
} else {
mWebView.loadUrl(mHomepage);
}
}
/**
* Load the HTML bookmarks page in this view
*/
public void loadBookmarkpage() {
if (mWebView == null)
return;
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);
}
File bookmarkWebPage = new File(mActivity.getFilesDir(), Constants.BOOKMARKS_FILENAME);
BrowserApp.getAppComponent().getBookmarkPage().buildBookmarkPage(null);
mWebView.loadUrl(Constants.FILE + bookmarkWebPage);
}
/**
* Initialize the preference driven settings of the WebView
*