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

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

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

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

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

Loading…
Cancel
Save