From a60ae614d9d27f1f91d53863087b6c345370f3d4 Mon Sep 17 00:00:00 2001 From: Anthony Restaino Date: Wed, 20 Jan 2016 22:02:15 -0500 Subject: [PATCH] Protect incognito activity from intents, clean up some code analysis warnings, simplify LightningView settings methods --- .../lightning/activity/IncognitoActivity.java | 5 --- .../lightning/activity/TabsManager.java | 42 ++++++++++--------- .../browser/lightning/app/AppComponent.java | 13 +++--- .../acr/browser/lightning/utils/Utils.java | 5 --- .../browser/lightning/view/LightningView.java | 22 +++++----- app/src/main/res/values/styles.xml | 4 -- 6 files changed, 38 insertions(+), 53 deletions(-) diff --git a/app/src/main/java/acr/browser/lightning/activity/IncognitoActivity.java b/app/src/main/java/acr/browser/lightning/activity/IncognitoActivity.java index 2ea3968..a2c100b 100644 --- a/app/src/main/java/acr/browser/lightning/activity/IncognitoActivity.java +++ b/app/src/main/java/acr/browser/lightning/activity/IncognitoActivity.java @@ -22,11 +22,6 @@ public class IncognitoActivity extends BrowserActivity { cookieManager.setAcceptCookie(mPreferences.getIncognitoCookiesEnabled()); } -// @Override -// public synchronized void initializeTabs() { -// newTab(null, true); -// } - @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.incognito, menu); diff --git a/app/src/main/java/acr/browser/lightning/activity/TabsManager.java b/app/src/main/java/acr/browser/lightning/activity/TabsManager.java index 6e866eb..fc34eed 100644 --- a/app/src/main/java/acr/browser/lightning/activity/TabsManager.java +++ b/app/src/main/java/acr/browser/lightning/activity/TabsManager.java @@ -43,22 +43,37 @@ public class TabsManager { @Inject public TabsManager() {} + /** + * Restores old tabs that were open before the browser + * was closed. Handles the intent used to open the browser. + * + * @param activity the activity needed to create tabs. + * @param intent the intent that started the browser activity. + * @param incognito whether or not we are in incognito mode. + */ public synchronized void restoreTabsAndHandleIntent(final Activity activity, final Intent intent, final boolean incognito) { + // If incognito, only create one tab, do not handle intent + // in order to protect user privacy + if (incognito && mWebViewList.isEmpty()) { + newTab(activity, null, true); + return; + } + String url = null; if (intent != null) { url = intent.getDataString(); } mWebViewList.clear(); mCurrentTab = null; - if (!incognito && mPreferenceManager.getRestoreLostTabsEnabled()) { + if (mPreferenceManager.getRestoreLostTabsEnabled()) { final String mem = mPreferenceManager.getMemoryUrl(); mPreferenceManager.setMemoryUrl(""); String[] array = Utils.getArray(mem); for (String urlString : array) { if (!urlString.isEmpty()) { - newTab(activity, urlString, incognito); + newTab(activity, urlString, false); } } } @@ -73,29 +88,19 @@ public class TabsManager { .setPositiveButton(R.string.action_open, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - newTab(activity, urlToLoad, incognito); + newTab(activity, urlToLoad, false); } }) .show(); } else { - newTab(activity, url, incognito); + newTab(activity, url, false); } } if (mWebViewList.size() == 0) { - newTab(activity, null, incognito); + newTab(activity, null, false); } } - /** - * Return a clone of the current tabs list. The list will not be updated, the user has to fetch - * a new copy when notified. - * - * @return a copy of the current tabs list - */ - public List getTabsList() { - return new ArrayList<>(mWebViewList); - } - /** * Return the tab at the given position in tabs list, or null if position is not in tabs list * range. @@ -139,7 +144,7 @@ public class TabsManager { */ public synchronized void resume(final Context context) { for (LightningView tab : mWebViewList) { - tab.initializePreferences(null, context); + tab.initializePreferences(context); } } @@ -185,7 +190,6 @@ public class TabsManager { * * @param position The position of the tab to remove */ - @Nullable private synchronized void removeTab(final int position) { if (position >= mWebViewList.size()) { return; @@ -205,7 +209,7 @@ public class TabsManager { if (current == position) { if (size() == 1) { mCurrentTab = null; - } else if (current < size() - 1 ) { + } else if (current < size() - 1) { // There is another tab after this one mCurrentTab = getTabAtPosition(current + 1); } else { @@ -231,7 +235,7 @@ public class TabsManager { * @return A string representation of the currently opened tabs */ public String tabsString() { - final StringBuilder builder = new StringBuilder(); + final StringBuilder builder = new StringBuilder(mWebViewList.size()); for (LightningView tab : mWebViewList) { final String url = tab.getUrl(); if (!url.isEmpty()) { diff --git a/app/src/main/java/acr/browser/lightning/app/AppComponent.java b/app/src/main/java/acr/browser/lightning/app/AppComponent.java index 4ccd906..3d6aca9 100644 --- a/app/src/main/java/acr/browser/lightning/app/AppComponent.java +++ b/app/src/main/java/acr/browser/lightning/app/AppComponent.java @@ -1,7 +1,5 @@ package acr.browser.lightning.app; -import android.content.Context; - import com.squareup.otto.Bus; import javax.inject.Singleton; @@ -38,19 +36,18 @@ public interface AppComponent { void inject(TabsFragment fragment); - PreferenceManager getPreferenceManager(); + void inject(LightningView lightningView); + + void inject(ThemableBrowserActivity activity); void inject(LightningPreferenceFragment fragment); + PreferenceManager getPreferenceManager(); + BookmarkPage getBookmarkPage(); Bus getBus(); HistoryDatabase getHistoryDatabase(); - Context getApplicationContext(); - - void inject(LightningView lightningView); - - void inject(ThemableBrowserActivity activity); } diff --git a/app/src/main/java/acr/browser/lightning/utils/Utils.java b/app/src/main/java/acr/browser/lightning/utils/Utils.java index 86e3d9e..7b6531b 100644 --- a/app/src/main/java/acr/browser/lightning/utils/Utils.java +++ b/app/src/main/java/acr/browser/lightning/utils/Utils.java @@ -194,11 +194,6 @@ public final class Utils { return domain.startsWith("www.") ? domain.substring(4) : domain; } - public static String getProtocol(String url) { - int index = url.indexOf('/'); - return url.substring(0, index + 2); - } - public static String[] getArray(String input) { return input.split(Constants.SEPARATOR); } diff --git a/app/src/main/java/acr/browser/lightning/view/LightningView.java b/app/src/main/java/acr/browser/lightning/view/LightningView.java index 388908b..4ae7d99 100644 --- a/app/src/main/java/acr/browser/lightning/view/LightningView.java +++ b/app/src/main/java/acr/browser/lightning/view/LightningView.java @@ -136,8 +136,8 @@ public class LightningView { mGestureDetector = new GestureDetector(activity, new CustomGestureListener()); mWebView.setOnTouchListener(new TouchListener()); mDefaultUserAgent = mWebView.getSettings().getUserAgentString(); - initializeSettings(mWebView.getSettings()); - initializePreferences(mWebView.getSettings(), activity); + initializeSettings(); + initializePreferences(activity); if (url != null) { if (!url.trim().isEmpty()) { @@ -233,19 +233,17 @@ public class LightningView { * Initialize the preference driven settings of the WebView. This method * must be called whenever the preferences are changed within SharedPreferences. * - * @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, it is used - * to get the default UserAgent for the WebView. + * @param context the context in which the WebView was created, it is used + * to get the default UserAgent for the WebView. */ @SuppressLint({"NewApi", "SetJavaScriptEnabled"}) - public synchronized void initializePreferences(@Nullable WebSettings settings, Context context) { - if (settings == null && mWebView == null) { + public synchronized void initializePreferences(Context context) { + if (mWebView == null) { return; - } else if (settings == null) { - settings = mWebView.getSettings(); } + WebSettings settings = mWebView.getSettings(); + if (mPreferences.getDoNotTrackEnabled()) { mRequestHeaders.put(HEADER_DNT, "1"); } else { @@ -365,10 +363,10 @@ 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. */ @SuppressLint("NewApi") - private void initializeSettings(WebSettings settings) { + private void initializeSettings() { + WebSettings settings = mWebView.getSettings(); if (API < Build.VERSION_CODES.JELLY_BEAN_MR2) { //noinspection deprecation settings.setAppCacheMaxSize(Long.MAX_VALUE); diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index a4f614a..8a4537c 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -125,8 +125,4 @@ normal - - \ No newline at end of file