Browse Source

Make incognito mode safer, fix crash in search adapter,

master
Anthony Restaino 9 years ago
parent
commit
c4e244a82b
  1. 9
      app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java
  2. 2
      app/src/main/java/acr/browser/lightning/controller/BrowserController.java
  3. 31
      app/src/main/java/acr/browser/lightning/object/SearchAdapter.java
  4. 121
      app/src/main/java/acr/browser/lightning/view/LightningView.java

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

@ -194,7 +194,7 @@ public abstract class BrowserActivity extends ThemableActivity implements Browse
private static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS = new FrameLayout.LayoutParams( private static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS = new FrameLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
public abstract boolean isIncognito(); abstract boolean isIncognito();
abstract void initializeTabs(); abstract void initializeTabs();
@ -356,12 +356,13 @@ public abstract class BrowserActivity extends ThemableActivity implements Browse
mDrawerLayout.setDrawerShadow(R.drawable.drawer_right_shadow, GravityCompat.END); mDrawerLayout.setDrawerShadow(R.drawable.drawer_right_shadow, GravityCompat.END);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_left_shadow, GravityCompat.START); mDrawerLayout.setDrawerShadow(R.drawable.drawer_left_shadow, GravityCompat.START);
initializeTabs();
if (API <= Build.VERSION_CODES.JELLY_BEAN_MR2) { if (API <= Build.VERSION_CODES.JELLY_BEAN_MR2) {
WebIconDatabase.getInstance().open(getDir("icons", MODE_PRIVATE).getPath()); WebIconDatabase.getInstance().open(getDir("icons", MODE_PRIVATE).getPath());
} }
initializeTabs();
mProxyUtils.checkForProxy(this); mProxyUtils.checkForProxy(this);
} }
@ -1227,7 +1228,7 @@ public abstract class BrowserActivity extends ThemableActivity implements Browse
return false; return false;
} }
mIsNewIntent = false; mIsNewIntent = false;
LightningView startingTab = new LightningView(mActivity, url, mDarkTheme); LightningView startingTab = new LightningView(mActivity, url, mDarkTheme, isIncognito());
if (mIdGenerator == 0) { if (mIdGenerator == 0) {
startingTab.resumeTimers(); startingTab.resumeTimers();
} }
@ -1452,7 +1453,7 @@ public abstract class BrowserActivity extends ThemableActivity implements Browse
initializePreferences(); initializePreferences();
for (int n = 0; n < mWebViewList.size(); n++) { for (int n = 0; n < mWebViewList.size(); n++) {
if (mWebViewList.get(n) != null) { if (mWebViewList.get(n) != null) {
mWebViewList.get(n).initializePreferences(this); mWebViewList.get(n).initializePreferences(null, this);
} else { } else {
mWebViewList.remove(n); mWebViewList.remove(n);
} }

2
app/src/main/java/acr/browser/lightning/controller/BrowserController.java

@ -50,8 +50,6 @@ public interface BrowserController {
void closeEmptyTab(); void closeEmptyTab();
boolean isIncognito();
boolean proxyIsNotReady(); boolean proxyIsNotReady();
void updateBookmarkIndicator(String url); void updateBookmarkIndicator(String url);

31
app/src/main/java/acr/browser/lightning/object/SearchAdapter.java

@ -51,6 +51,7 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
private final List<HistoryItem> mSuggestions = new ArrayList<>(); private final List<HistoryItem> mSuggestions = new ArrayList<>();
private final List<HistoryItem> mFilteredList = new ArrayList<>(); private final List<HistoryItem> mFilteredList = new ArrayList<>();
private final List<HistoryItem> mAllBookmarks = new ArrayList<>(); private final List<HistoryItem> mAllBookmarks = new ArrayList<>();
private final Object mLock = new Object();
private HistoryDatabase mDatabaseHandler; private HistoryDatabase mDatabaseHandler;
private final Context mContext; private final Context mContext;
private boolean mUseGoogle = true; private boolean mUseGoogle = true;
@ -58,6 +59,7 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
private final boolean mDarkTheme; private final boolean mDarkTheme;
private final boolean mIncognito; private final boolean mIncognito;
private final BookmarkManager mBookmarkManager; private final BookmarkManager mBookmarkManager;
private static final String CACHE_FILE_TYPE = ".sgg";
private static final String ENCODING = "ISO-8859-1"; private static final String ENCODING = "ISO-8859-1";
private static final long INTERVAL_DAY = 86400000; private static final long INTERVAL_DAY = 86400000;
private static final int MAX_SUGGESTIONS = 5; private static final int MAX_SUGGESTIONS = 5;
@ -105,11 +107,9 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
private class NameFilter implements FilenameFilter { private class NameFilter implements FilenameFilter {
private static final String ext = ".sgg";
@Override @Override
public boolean accept(File dir, String filename) { public boolean accept(File dir, String filename) {
return filename.endsWith(ext); return filename.endsWith(CACHE_FILE_TYPE);
} }
} }
@ -117,15 +117,19 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
public void refreshPreferences() { public void refreshPreferences() {
mUseGoogle = PreferenceManager.getInstance().getGoogleSearchSuggestionsEnabled(); mUseGoogle = PreferenceManager.getInstance().getGoogleSearchSuggestionsEnabled();
if (!mUseGoogle) { if (!mUseGoogle) {
synchronized (mSuggestions) {
mSuggestions.clear(); mSuggestions.clear();
} }
}
mDatabaseHandler = HistoryDatabase.getInstance(mContext.getApplicationContext()); mDatabaseHandler = HistoryDatabase.getInstance(mContext.getApplicationContext());
} }
public void refreshBookmarks() { public void refreshBookmarks() {
synchronized (mLock) {
mAllBookmarks.clear(); mAllBookmarks.clear();
mAllBookmarks.addAll(mBookmarkManager.getAllBookmarks(true)); mAllBookmarks.addAll(mBookmarkManager.getAllBookmarks(true));
} }
}
@Override @Override
public int getCount() { public int getCount() {
@ -217,7 +221,9 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
} }
int counter = 0; int counter = 0;
synchronized (mBookmarks) {
mBookmarks.clear(); mBookmarks.clear();
synchronized (mLock) {
for (int n = 0; n < mAllBookmarks.size(); n++) { for (int n = 0; n < mAllBookmarks.size(); n++) {
if (counter >= 5) { if (counter >= 5) {
break; break;
@ -232,12 +238,16 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
} }
} }
}
}
if (mDatabaseHandler == null || mDatabaseHandler.isClosed()) { if (mDatabaseHandler == null || mDatabaseHandler.isClosed()) {
mDatabaseHandler = HistoryDatabase.getInstance(mContext.getApplicationContext()); mDatabaseHandler = HistoryDatabase.getInstance(mContext.getApplicationContext());
} }
List<HistoryItem> historyList = mDatabaseHandler.findItemsContaining(constraint.toString()); List<HistoryItem> historyList = mDatabaseHandler.findItemsContaining(constraint.toString());
synchronized (mHistory) {
mHistory.clear(); mHistory.clear();
mHistory.addAll(historyList); mHistory.addAll(historyList);
}
results.count = 1; results.count = 1;
return results; return results;
} }
@ -249,10 +259,12 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
@Override @Override
protected void publishResults(CharSequence constraint, FilterResults results) { protected void publishResults(CharSequence constraint, FilterResults results) {
synchronized (mFilteredList) {
mFilteredList.clear(); mFilteredList.clear();
List<HistoryItem> filtered = getFilteredList(); List<HistoryItem> filtered = getFilteredList();
Collections.sort(filtered, mComparator); Collections.sort(filtered, mComparator);
mFilteredList.addAll(filtered); mFilteredList.addAll(filtered);
}
notifyDataSetChanged(); notifyDataSetChanged();
} }
@ -320,20 +332,24 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
@Override @Override
protected void onPostExecute(List<HistoryItem> result) { protected void onPostExecute(List<HistoryItem> result) {
synchronized (mSuggestions) {
mSuggestions.clear(); mSuggestions.clear();
mSuggestions.addAll(result); mSuggestions.addAll(result);
}
synchronized (mFilteredList) {
mFilteredList.clear(); mFilteredList.clear();
List<HistoryItem> filtered = getFilteredList(); List<HistoryItem> filtered = getFilteredList();
Collections.sort(filtered, mComparator); Collections.sort(filtered, mComparator);
mFilteredList.addAll(filtered); mFilteredList.addAll(filtered);
notifyDataSetChanged(); notifyDataSetChanged();
}
mIsExecuting = false; mIsExecuting = false;
} }
} }
private File downloadSuggestionsForQuery(String query) { private File downloadSuggestionsForQuery(String query) {
File cacheFile = new File(mContext.getCacheDir(), query.hashCode() + ".sgg"); File cacheFile = new File(mContext.getCacheDir(), query.hashCode() + CACHE_FILE_TYPE);
if (System.currentTimeMillis() - INTERVAL_DAY < cacheFile.lastModified()) { if (System.currentTimeMillis() - INTERVAL_DAY < cacheFile.lastModified()) {
return cacheFile; return cacheFile;
} }
@ -410,6 +426,9 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
private List<HistoryItem> getFilteredList() { private List<HistoryItem> getFilteredList() {
List<HistoryItem> list = new ArrayList<>(); List<HistoryItem> list = new ArrayList<>();
synchronized (mBookmarks) {
synchronized (mHistory) {
synchronized (mSuggestions) {
Iterator<HistoryItem> bookmark = mBookmarks.iterator(); Iterator<HistoryItem> bookmark = mBookmarks.iterator();
Iterator<HistoryItem> history = mHistory.iterator(); Iterator<HistoryItem> history = mHistory.iterator();
Iterator<HistoryItem> suggestion = mSuggestions.listIterator(); Iterator<HistoryItem> suggestion = mSuggestions.listIterator();
@ -426,7 +445,9 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
if (history.hasNext() && list.size() < MAX_SUGGESTIONS) { if (history.hasNext() && list.size() < MAX_SUGGESTIONS) {
list.add(history.next()); list.add(history.next());
} }
}
}
}
} }
return list; return list;
} }

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

@ -69,10 +69,10 @@ public class LightningView {
private final Title mTitle; private final Title mTitle;
private WebView mWebView; private WebView mWebView;
private boolean mIsIncognitoTab;
private BrowserController mBrowserController; private BrowserController mBrowserController;
private GestureDetector mGestureDetector; private GestureDetector mGestureDetector;
private final Activity mActivity; private final Activity mActivity;
private WebSettings mSettings;
private static String mHomepage; private static String mHomepage;
private static String mDefaultUserAgent; private static String mDefaultUserAgent;
private static Bitmap mWebpageBitmap; private static Bitmap mWebpageBitmap;
@ -93,10 +93,11 @@ public class LightningView {
0, 0, 0, 1.0f, 0 // alpha 0, 0, 0, 1.0f, 0 // alpha
}; };
public LightningView(Activity activity, String url, boolean darkTheme) { public LightningView(Activity activity, String url, boolean darkTheme, boolean isIncognito) {
mActivity = activity; mActivity = activity;
mWebView = new WebView(activity); mWebView = new WebView(activity);
mIsIncognitoTab = isIncognito;
mTitle = new Title(activity, darkTheme); mTitle = new Title(activity, darkTheme);
mAdBlock = AdBlock.getInstance(activity.getApplicationContext()); mAdBlock = AdBlock.getInstance(activity.getApplicationContext());
@ -134,9 +135,8 @@ public class LightningView {
mGestureDetector = new GestureDetector(activity, new CustomGestureListener()); mGestureDetector = new GestureDetector(activity, new CustomGestureListener());
mWebView.setOnTouchListener(new TouchListener()); mWebView.setOnTouchListener(new TouchListener());
mDefaultUserAgent = mWebView.getSettings().getUserAgentString(); mDefaultUserAgent = mWebView.getSettings().getUserAgentString();
mSettings = mWebView.getSettings();
initializeSettings(mWebView.getSettings(), activity); initializeSettings(mWebView.getSettings(), activity);
initializePreferences(activity); initializePreferences(mWebView.getSettings(), activity);
if (url != null) { if (url != null) {
if (!url.trim().isEmpty()) { if (!url.trim().isEmpty()) {
@ -155,6 +155,12 @@ public class LightningView {
} }
} }
/**
* This method builds the homepage and returns the local URL to be loaded
* when it finishes building.
*
* @return the URL to load
*/
private String getHomepage() { private String getHomepage() {
StringBuilder homepageBuilder = new StringBuilder(); StringBuilder homepageBuilder = new StringBuilder();
homepageBuilder.append(StartPage.HEAD); homepageBuilder.append(StartPage.HEAD);
@ -252,35 +258,42 @@ public class LightningView {
return Constants.FILE + homepage; return Constants.FILE + homepage;
} }
public synchronized void initializePreferences(Context context) { /**
* Initialize the preference driven settings of the WebView
*
* @param settings the WebSettings object to use, you can pass in null
* if you don't have a reference to them
* @param context the context in which the WebView was created
*/
public synchronized void initializePreferences(@Nullable WebSettings settings, Context context) {
if (settings == null && mWebView == null) {
return;
} else if (settings == null) {
settings = mWebView.getSettings();
}
mPreferences = PreferenceManager.getInstance(); mPreferences = PreferenceManager.getInstance();
mSettings.setDefaultTextEncodingName(mPreferences.getTextEncoding()); settings.setDefaultTextEncodingName(mPreferences.getTextEncoding());
mHomepage = mPreferences.getHomepage(); mHomepage = mPreferences.getHomepage();
mAdBlock.updatePreference(); mAdBlock.updatePreference();
if (mSettings == null && mWebView != null) {
mSettings = mWebView.getSettings();
} else if (mSettings == null) {
return;
}
setColorMode(mPreferences.getRenderingMode()); setColorMode(mPreferences.getRenderingMode());
if (!mBrowserController.isIncognito()) { if (!mIsIncognitoTab) {
mSettings.setGeolocationEnabled(mPreferences.getLocationEnabled()); settings.setGeolocationEnabled(mPreferences.getLocationEnabled());
} else { } else {
mSettings.setGeolocationEnabled(false); settings.setGeolocationEnabled(false);
} }
if (API < 19) { if (API < 19) {
switch (mPreferences.getFlashSupport()) { switch (mPreferences.getFlashSupport()) {
case 0: case 0:
mSettings.setPluginState(PluginState.OFF); settings.setPluginState(PluginState.OFF);
break; break;
case 1: case 1:
mSettings.setPluginState(PluginState.ON_DEMAND); settings.setPluginState(PluginState.ON_DEMAND);
break; break;
case 2: case 2:
mSettings.setPluginState(PluginState.ON); settings.setPluginState(PluginState.ON);
break; break;
default: default:
break; break;
@ -289,29 +302,29 @@ public class LightningView {
setUserAgent(context, mPreferences.getUserAgentChoice()); setUserAgent(context, mPreferences.getUserAgentChoice());
if (mPreferences.getSavePasswordsEnabled() && !mBrowserController.isIncognito()) { if (mPreferences.getSavePasswordsEnabled() && !mIsIncognitoTab) {
if (API < 18) { if (API < 18) {
mSettings.setSavePassword(true); settings.setSavePassword(true);
} }
mSettings.setSaveFormData(true); settings.setSaveFormData(true);
} else { } else {
if (API < 18) { if (API < 18) {
mSettings.setSavePassword(false); settings.setSavePassword(false);
} }
mSettings.setSaveFormData(false); settings.setSaveFormData(false);
} }
if (mPreferences.getJavaScriptEnabled()) { if (mPreferences.getJavaScriptEnabled()) {
mSettings.setJavaScriptEnabled(true); settings.setJavaScriptEnabled(true);
mSettings.setJavaScriptCanOpenWindowsAutomatically(true); settings.setJavaScriptCanOpenWindowsAutomatically(true);
} }
if (mPreferences.getTextReflowEnabled()) { if (mPreferences.getTextReflowEnabled()) {
mTextReflow = true; mTextReflow = true;
mSettings.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS); settings.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS);
if (API >= android.os.Build.VERSION_CODES.KITKAT) { if (API >= android.os.Build.VERSION_CODES.KITKAT) {
try { try {
mSettings.setLayoutAlgorithm(LayoutAlgorithm.TEXT_AUTOSIZING); settings.setLayoutAlgorithm(LayoutAlgorithm.TEXT_AUTOSIZING);
} catch (Exception e) { } catch (Exception e) {
// This shouldn't be necessary, but there are a number // This shouldn't be necessary, but there are a number
// of KitKat devices that crash trying to set this // of KitKat devices that crash trying to set this
@ -320,31 +333,35 @@ public class LightningView {
} }
} else { } else {
mTextReflow = false; mTextReflow = false;
mSettings.setLayoutAlgorithm(LayoutAlgorithm.NORMAL); settings.setLayoutAlgorithm(LayoutAlgorithm.NORMAL);
} }
mSettings.setBlockNetworkImage(mPreferences.getBlockImagesEnabled()); settings.setBlockNetworkImage(mPreferences.getBlockImagesEnabled());
mSettings.setSupportMultipleWindows(mPreferences.getPopupsEnabled()); if (!mIsIncognitoTab) {
mSettings.setUseWideViewPort(mPreferences.getUseWideViewportEnabled()); settings.setSupportMultipleWindows(mPreferences.getPopupsEnabled());
mSettings.setLoadWithOverviewMode(mPreferences.getOverviewModeEnabled()); } else {
settings.setSupportMultipleWindows(false);
}
settings.setUseWideViewPort(mPreferences.getUseWideViewportEnabled());
settings.setLoadWithOverviewMode(mPreferences.getOverviewModeEnabled());
switch (mPreferences.getTextSize()) { switch (mPreferences.getTextSize()) {
case 0: case 0:
mSettings.setTextZoom(200); settings.setTextZoom(200);
break; break;
case 1: case 1:
mSettings.setTextZoom(150); settings.setTextZoom(150);
break; break;
case 2: case 2:
mSettings.setTextZoom(125); settings.setTextZoom(125);
break; break;
case 3: case 3:
mSettings.setTextZoom(100); settings.setTextZoom(100);
break; break;
case 4: case 4:
mSettings.setTextZoom(75); settings.setTextZoom(75);
break; break;
case 5: case 5:
mSettings.setTextZoom(50); settings.setTextZoom(50);
break; break;
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
@ -353,6 +370,13 @@ public class LightningView {
} }
} }
/**
* Initialize the settings of the WebView that are intrinsic to Lightning and cannot
* be altered by the user. Distinguish between Incognito and Regular tabs here.
*
* @param settings the WebSettings object to use.
* @param context the Context which was used to construct the WebView.
*/
private void initializeSettings(WebSettings settings, Context context) { private void initializeSettings(WebSettings settings, Context context) {
if (API < Build.VERSION_CODES.JELLY_BEAN_MR2) { if (API < Build.VERSION_CODES.JELLY_BEAN_MR2) {
settings.setAppCacheMaxSize(Long.MAX_VALUE); settings.setAppCacheMaxSize(Long.MAX_VALUE);
@ -363,16 +387,23 @@ public class LightningView {
if (API > Build.VERSION_CODES.JELLY_BEAN) { if (API > Build.VERSION_CODES.JELLY_BEAN) {
settings.setMediaPlaybackRequiresUserGesture(true); settings.setMediaPlaybackRequiresUserGesture(true);
} }
if (API >= Build.VERSION_CODES.LOLLIPOP && !mBrowserController.isIncognito()) { if (API >= Build.VERSION_CODES.LOLLIPOP && !mIsIncognitoTab) {
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE); settings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
} else if (API >= Build.VERSION_CODES.LOLLIPOP) { } else if (API >= Build.VERSION_CODES.LOLLIPOP) {
// We're in Incognito mode, reject // We're in Incognito mode, reject
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_NEVER_ALLOW); settings.setMixedContentMode(WebSettings.MIXED_CONTENT_NEVER_ALLOW);
} }
if (!mIsIncognitoTab) {
settings.setDomStorageEnabled(true); settings.setDomStorageEnabled(true);
settings.setAppCacheEnabled(true); settings.setAppCacheEnabled(true);
settings.setCacheMode(WebSettings.LOAD_DEFAULT); settings.setCacheMode(WebSettings.LOAD_DEFAULT);
settings.setDatabaseEnabled(true); settings.setDatabaseEnabled(true);
} else {
settings.setDomStorageEnabled(false);
settings.setAppCacheEnabled(false);
settings.setDatabaseEnabled(false);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
}
settings.setSupportZoom(true); settings.setSupportZoom(true);
settings.setBuiltInZoomControls(true); settings.setBuiltInZoomControls(true);
settings.setDisplayZoomControls(false); settings.setDisplayZoomControls(false);
@ -401,22 +432,24 @@ public class LightningView {
} }
public void setUserAgent(Context context, int choice) { public void setUserAgent(Context context, int choice) {
if (mWebView == null) return;
WebSettings settings = mWebView.getSettings();
switch (choice) { switch (choice) {
case 1: case 1:
if (API > 16) { if (API > 16) {
mSettings.setUserAgentString(WebSettings.getDefaultUserAgent(context)); settings.setUserAgentString(WebSettings.getDefaultUserAgent(context));
} else { } else {
mSettings.setUserAgentString(mDefaultUserAgent); settings.setUserAgentString(mDefaultUserAgent);
} }
break; break;
case 2: case 2:
mSettings.setUserAgentString(Constants.DESKTOP_USER_AGENT); settings.setUserAgentString(Constants.DESKTOP_USER_AGENT);
break; break;
case 3: case 3:
mSettings.setUserAgentString(Constants.MOBILE_USER_AGENT); settings.setUserAgentString(Constants.MOBILE_USER_AGENT);
break; break;
case 4: case 4:
mSettings.setUserAgentString(mPreferences.getUserAgentString(mDefaultUserAgent)); settings.setUserAgentString(mPreferences.getUserAgentString(mDefaultUserAgent));
break; break;
} }
} }
@ -854,7 +887,7 @@ public class LightningView {
return true; return true;
} }
if (mBrowserController.isIncognito()) { if (mIsIncognitoTab) {
return super.shouldOverrideUrlLoading(view, url); return super.shouldOverrideUrlLoading(view, url);
} }
if (url.startsWith("about:")) { if (url.startsWith("about:")) {

Loading…
Cancel
Save