From 438662bffd7d85b1baf0691cfe79078b6256016e Mon Sep 17 00:00:00 2001 From: Anthony Restaino Date: Mon, 12 Sep 2016 20:31:10 -0400 Subject: [PATCH] Removing some unused code, breaking out strings into constants, improving network receiver --- .../lightning/activity/BrowserActivity.java | 27 +--------- .../lightning/async/ImageDownloadTask.java | 13 ++--- .../browser/lightning/constant/Constants.java | 50 ++++++++++++------- .../lightning/constant/HistoryPage.java | 5 +- .../lightning/fragment/BookmarksFragment.java | 2 +- .../fragment/GeneralSettingsFragment.java | 20 ++++---- .../preference/PreferenceManager.java | 7 +-- .../lightning/receiver/NetworkReceiver.java | 11 ++-- .../lightning/search/BaseSuggestionsTask.java | 1 + .../acr/browser/lightning/utils/Utils.java | 4 -- .../browser/lightning/view/LightningView.java | 4 +- .../lightning/view/LightningWebClient.java | 4 +- 12 files changed, 68 insertions(+), 80 deletions(-) diff --git a/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java b/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java index 84c57ad..c3b2eeb 100644 --- a/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java +++ b/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java @@ -2073,27 +2073,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements return true; } - // TODO Check if all the calls are relative to TabsFragement - - /** - * A utility method that creates a FrameLayout button with the given ID and - * sets the image of the button to the given image ID. The OnClick and OnLongClick - * listeners are set to this class, so BrowserActivity should handle those events - * there. Additionally, it tints the images according to the current theme. - * This method only is a convenience so that this code does not have to be repeated - * for the several "Buttons" that use this. - * - * @param buttonId the view id of the button - * @param imageId the image to set as the button image - */ - private void setupFrameLayoutButton(@IdRes int buttonId, @IdRes int imageId) { - final View frameButton = findViewById(buttonId); - final ImageView buttonImage = (ImageView) findViewById(imageId); - frameButton.setOnClickListener(this); - frameButton.setOnLongClickListener(this); - buttonImage.setColorFilter(mIconColor, PorterDuff.Mode.SRC_IN); - } - /** * This NetworkReceiver notifies each of the WebViews in the browser whether * the network is currently connected or not. This is important because some @@ -2102,10 +2081,8 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements */ private final NetworkReceiver mNetworkReceiver = new NetworkReceiver() { @Override - public void onReceive(Context context, Intent intent) { - super.onReceive(context, intent); - boolean isConnected = isConnected(context); - Log.d(TAG, "Network Connected: " + String.valueOf(isConnected)); + public void onConnectivityChange(boolean isConnected) { + Log.d(TAG, "Network Connected: " + isConnected); mTabsManager.notifyConnectionStatus(isConnected); } }; diff --git a/app/src/main/java/acr/browser/lightning/async/ImageDownloadTask.java b/app/src/main/java/acr/browser/lightning/async/ImageDownloadTask.java index 52c02c5..8ff8f2f 100644 --- a/app/src/main/java/acr/browser/lightning/async/ImageDownloadTask.java +++ b/app/src/main/java/acr/browser/lightning/async/ImageDownloadTask.java @@ -1,5 +1,6 @@ package acr.browser.lightning.async; +import android.app.Application; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; @@ -26,7 +27,7 @@ public class ImageDownloadTask extends AsyncTask { private static final String TAG = ImageDownloadTask.class.getSimpleName(); @NonNull private final WeakReference mFaviconImage; - @NonNull private final WeakReference mContextReference; + @NonNull private final Application mContext; @NonNull private final HistoryItem mWeb; private final String mUrl; @NonNull private final Bitmap mDefaultBitmap; @@ -34,7 +35,7 @@ public class ImageDownloadTask extends AsyncTask { public ImageDownloadTask(@NonNull ImageView bmImage, @NonNull HistoryItem web, @NonNull Bitmap defaultBitmap, - @NonNull Context context) { + @NonNull Application context) { // Set a tag on the ImageView so we know if the view // has gone out of scope and should not be used bmImage.setTag(web.getUrl().hashCode()); @@ -42,7 +43,7 @@ public class ImageDownloadTask extends AsyncTask { this.mWeb = web; this.mUrl = web.getUrl(); this.mDefaultBitmap = defaultBitmap; - this.mContextReference = new WeakReference<>(context.getApplicationContext()); + this.mContext = context; } @NonNull @@ -53,11 +54,7 @@ public class ImageDownloadTask extends AsyncTask { if (mUrl == null) { return mDefaultBitmap; } - Context context = mContextReference.get(); - if (context == null) { - return mDefaultBitmap; - } - File cache = context.getCacheDir(); + File cache = mContext.getCacheDir(); final Uri uri = Uri.parse(mUrl); if (uri.getHost() == null || uri.getScheme() == null) { return mDefaultBitmap; diff --git a/app/src/main/java/acr/browser/lightning/constant/Constants.java b/app/src/main/java/acr/browser/lightning/constant/Constants.java index 3861a28..76fa1cf 100644 --- a/app/src/main/java/acr/browser/lightning/constant/Constants.java +++ b/app/src/main/java/acr/browser/lightning/constant/Constants.java @@ -12,8 +12,11 @@ public final class Constants { public static final boolean FULL_VERSION = BuildConfig.FULL_VERSION; + // Hardcoded user agents public static final String DESKTOP_USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36"; public static final String MOBILE_USER_AGENT = "Mozilla/5.0 (Linux; U; Android 4.4; en-us; Nexus 4 Build/JOP24G) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30"; + + // Search query URLs public static final String YAHOO_SEARCH = "https://search.yahoo.com/search?p="; public static final String GOOGLE_SEARCH = "https://www.google.com/search?client=lightning&ie=UTF-8&oe=UTF-8&q="; public static final String BING_SEARCH = "https://www.bing.com/search?q="; @@ -21,36 +24,45 @@ public final class Constants { public static final String DUCK_LITE_SEARCH = "https://duckduckgo.com/lite/?t=lightning&q="; public static final String STARTPAGE_MOBILE_SEARCH = "https://startpage.com/do/m/mobilesearch?language=english&query="; public static final String STARTPAGE_SEARCH = "https://startpage.com/do/search?language=english&query="; - public static final String ASK_SEARCH = "http://www.ask.com/web?qsrc=0&o=0&l=dir&qo=lightningBrowser&q="; - public static final String HOMEPAGE = "about:home"; + public static final String ASK_SEARCH = "http://www.ask.com/web?qsrc=0&o=0&l=dir&qo=LightningBrowser&q="; public static final String BAIDU_SEARCH = "https://www.baidu.com/s?wd="; public static final String YANDEX_SEARCH = "https://yandex.ru/yandsearch?lr=21411&text="; + + // Custom local page schemes + public static final String SCHEME_HOMEPAGE = "about:home"; + public static final String SCHEME_BLANK = "about:blank"; + public static final String SCHEME_BOOKMARKS = "about:bookmarks"; + + // Miscellaneous JavaScript public static final String JAVASCRIPT_INVERT_PAGE = "javascript:(function(){var e='img {-webkit-filter: invert(100%);'+'-moz-filter: invert(100%);'+'-o-filter: invert(100%);'+'-ms-filter: invert(100%); }',t=document.getElementsByTagName('head')[0],n=document.createElement('style');if(!window.counter){window.counter=1}else{window.counter++;if(window.counter%2==0){var e='html {-webkit-filter: invert(0%); -moz-filter: invert(0%); -o-filter: invert(0%); -ms-filter: invert(0%); }'}}n.type='text/css';if(n.styleSheet){n.styleSheet.cssText=e}else{n.appendChild(document.createTextNode(e))}t.appendChild(n)})();"; public static final String JAVASCRIPT_TEXT_REFLOW = "javascript:document.getElementsByTagName('body')[0].style.width=window.innerWidth+'px';"; public static final String JAVASCRIPT_THEME_COLOR = "(function () {\n" + - " \"use strict\";\n" + - " var metas, i, tag;\n" + - " metas = document.getElementsByTagName('meta');\n" + - " if (metas !== null) {\n" + - " for (i = 0; i < metas.length; i++) {\n" + - " tag = metas[i].getAttribute('name');\n" + - " if (tag !== null && tag.toLowerCase() === 'theme-color') {\n" + - " return metas[i].getAttribute('content');\n" + - " }\n" + - " console.log(tag);\n" + - " }\n" + - " }\n" + - '\n' + - " return '';\n" + - "}());"; + " \"use strict\";\n" + + " var metas, i, tag;\n" + + " metas = document.getElementsByTagName('meta');\n" + + " if (metas !== null) {\n" + + " for (i = 0; i < metas.length; i++) {\n" + + " tag = metas[i].getAttribute('name');\n" + + " if (tag !== null && tag.toLowerCase() === 'theme-color') {\n" + + " return metas[i].getAttribute('content');\n" + + " }\n" + + " console.log(tag);\n" + + " }\n" + + " }\n" + + '\n' + + " return '';\n" + + "}());"; public static final String LOAD_READING_URL = "ReadingUrl"; - public static final String SEPARATOR = "\\|\\$\\|SEPARATOR\\|\\$\\|"; + // URL Schemes public static final String HTTP = "http://"; public static final String HTTPS = "https://"; public static final String FILE = "file://"; + public static final String ABOUT = "about:"; public static final String FOLDER = "folder://"; + + // Application log tag public static final String TAG = "Lightning"; // These should match the order of @array/proxy_choices_array @@ -59,8 +71,10 @@ public final class Constants { public static final int PROXY_I2P = 2; public static final int PROXY_MANUAL = 3; + // Default text encoding we will use public static final String DEFAULT_ENCODING = "UTF-8"; + // Allowable text encodings for the WebView public static final String[] TEXT_ENCODINGS = {"ISO-8859-1", "UTF-8", "GBK", "Big5", "ISO-2022-JP", "SHIFT_JS", "EUC-JP", "EUC-KR"}; public static final String INTENT_ORIGIN = "URL_INTENT_ORIGIN"; diff --git a/app/src/main/java/acr/browser/lightning/constant/HistoryPage.java b/app/src/main/java/acr/browser/lightning/constant/HistoryPage.java index c1a581f..44c3ca8 100644 --- a/app/src/main/java/acr/browser/lightning/constant/HistoryPage.java +++ b/app/src/main/java/acr/browser/lightning/constant/HistoryPage.java @@ -7,6 +7,7 @@ import android.app.Application; import android.os.AsyncTask; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.util.Log; import java.io.File; import java.io.FileWriter; @@ -24,6 +25,8 @@ import acr.browser.lightning.view.LightningView; public class HistoryPage extends AsyncTask { + private static final String TAG = HistoryPage.class.getSimpleName(); + public static final String FILENAME = "history.html"; private static final String HEADING_1 = ""; @@ -95,7 +98,7 @@ public class HistoryPage extends AsyncTask<Void, Void, Void> { historyWriter = new FileWriter(historyWebPage, false); historyWriter.write(historyBuilder.toString()); } catch (IOException e) { - e.printStackTrace(); + Log.e(TAG, "Unable to write history page to disk", e); } finally { Utils.close(historyWriter); } diff --git a/app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java b/app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java index bdb009b..b1f2d73 100644 --- a/app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java +++ b/app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java @@ -399,7 +399,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, holder.favicon.setImageBitmap(mFolderBitmap); } else if (web.getBitmap() == null) { holder.favicon.setImageBitmap(mWebpageBitmap); - new ImageDownloadTask(holder.favicon, web, mWebpageBitmap, context) + new ImageDownloadTask(holder.favicon, web, mWebpageBitmap, BrowserApp.get(context)) .executeOnExecutor(AsyncExecutor.getInstance()); } else { holder.favicon.setImageBitmap(web.getBitmap()); diff --git a/app/src/main/java/acr/browser/lightning/fragment/GeneralSettingsFragment.java b/app/src/main/java/acr/browser/lightning/fragment/GeneralSettingsFragment.java index 94b0e7d..6731042 100644 --- a/app/src/main/java/acr/browser/lightning/fragment/GeneralSettingsFragment.java +++ b/app/src/main/java/acr/browser/lightning/fragment/GeneralSettingsFragment.java @@ -124,11 +124,11 @@ public class GeneralSettingsFragment extends LightningPreferenceFragment impleme } - if (mHomepage.contains("about:home")) { + if (mHomepage.contains(Constants.SCHEME_HOMEPAGE)) { home.setSummary(getResources().getString(R.string.action_homepage)); - } else if (mHomepage.contains("about:blank")) { + } else if (mHomepage.contains(Constants.SCHEME_BLANK)) { home.setSummary(getResources().getString(R.string.action_blank)); - } else if (mHomepage.contains("about:bookmarks")) { + } else if (mHomepage.contains(Constants.SCHEME_BOOKMARKS)) { home.setSummary(getResources().getString(R.string.action_bookmarks)); } else { home.setSummary(mHomepage); @@ -316,11 +316,11 @@ public class GeneralSettingsFragment extends LightningPreferenceFragment impleme picker.setTitle(getResources().getString(R.string.home)); mHomepage = mPreferenceManager.getHomepage(); int n; - if (mHomepage.contains("about:home")) { + if (mHomepage.startsWith(Constants.SCHEME_HOMEPAGE)) { n = 1; - } else if (mHomepage.contains("about:blank")) { + } else if (mHomepage.startsWith(Constants.SCHEME_BLANK)) { n = 2; - } else if (mHomepage.contains("about:bookmarks")) { + } else if (mHomepage.startsWith(Constants.SCHEME_BOOKMARKS)) { n = 3; } else { n = 4; @@ -332,15 +332,15 @@ public class GeneralSettingsFragment extends LightningPreferenceFragment impleme public void onClick(DialogInterface dialog, int which) { switch (which + 1) { case 1: - mPreferenceManager.setHomepage("about:home"); + mPreferenceManager.setHomepage(Constants.SCHEME_HOMEPAGE); home.setSummary(getResources().getString(R.string.action_homepage)); break; case 2: - mPreferenceManager.setHomepage("about:blank"); + mPreferenceManager.setHomepage(Constants.SCHEME_BLANK); home.setSummary(getResources().getString(R.string.action_blank)); break; case 3: - mPreferenceManager.setHomepage("about:bookmarks"); + mPreferenceManager.setHomepage(Constants.SCHEME_BOOKMARKS); home.setSummary(getResources().getString(R.string.action_bookmarks)); break; case 4: @@ -402,7 +402,7 @@ public class GeneralSettingsFragment extends LightningPreferenceFragment impleme homePicker.setTitle(getResources().getString(R.string.title_custom_homepage)); final EditText getHome = new EditText(mActivity); mHomepage = mPreferenceManager.getHomepage(); - if (!mHomepage.startsWith("about:")) { + if (!mHomepage.startsWith(Constants.ABOUT)) { getHome.setText(mHomepage); } else { String defaultUrl = "https://www.google.com"; diff --git a/app/src/main/java/acr/browser/lightning/preference/PreferenceManager.java b/app/src/main/java/acr/browser/lightning/preference/PreferenceManager.java index dbe6082..905de88 100644 --- a/app/src/main/java/acr/browser/lightning/preference/PreferenceManager.java +++ b/app/src/main/java/acr/browser/lightning/preference/PreferenceManager.java @@ -2,14 +2,9 @@ package acr.browser.lightning.preference; import android.content.Context; import android.content.SharedPreferences; -import android.support.annotation.IntDef; import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.util.concurrent.Future; - import javax.inject.Inject; import javax.inject.Singleton; @@ -170,7 +165,7 @@ public class PreferenceManager { @NonNull public String getHomepage() { - return mPrefs.getString(Name.HOMEPAGE, Constants.HOMEPAGE); + return mPrefs.getString(Name.HOMEPAGE, Constants.SCHEME_HOMEPAGE); } public boolean getIncognitoCookiesEnabled() { diff --git a/app/src/main/java/acr/browser/lightning/receiver/NetworkReceiver.java b/app/src/main/java/acr/browser/lightning/receiver/NetworkReceiver.java index 97ffe38..5c394a9 100644 --- a/app/src/main/java/acr/browser/lightning/receiver/NetworkReceiver.java +++ b/app/src/main/java/acr/browser/lightning/receiver/NetworkReceiver.java @@ -6,13 +6,18 @@ import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.support.annotation.NonNull; +import android.util.Log; -public class NetworkReceiver extends BroadcastReceiver { +public abstract class NetworkReceiver extends BroadcastReceiver { + + public abstract void onConnectivityChange(boolean isConnected); @Override - public void onReceive(Context context, Intent intent) {} + public void onReceive(Context context, Intent intent) { + onConnectivityChange(isConnected(context)); + } - public static boolean isConnected(@NonNull Context context) { + private static boolean isConnected(@NonNull Context context) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (cm == null) return false; diff --git a/app/src/main/java/acr/browser/lightning/search/BaseSuggestionsTask.java b/app/src/main/java/acr/browser/lightning/search/BaseSuggestionsTask.java index 0bf2912..61494e6 100644 --- a/app/src/main/java/acr/browser/lightning/search/BaseSuggestionsTask.java +++ b/app/src/main/java/acr/browser/lightning/search/BaseSuggestionsTask.java @@ -118,6 +118,7 @@ abstract class BaseSuggestionsTask { HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); connection.setDoInput(true); connection.setRequestProperty("Accept-Encoding", "gzip"); + connection.setRequestProperty("Accept-Charset", getEncoding()); connection.connect(); if (connection.getResponseCode() >= HttpURLConnection.HTTP_MULT_CHOICE || connection.getResponseCode() < HttpURLConnection.HTTP_OK) { 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 f76356d..8418965 100644 --- a/app/src/main/java/acr/browser/lightning/utils/Utils.java +++ b/app/src/main/java/acr/browser/lightning/utils/Utils.java @@ -216,10 +216,6 @@ public final class Utils { return domain.startsWith("www.") ? domain.substring(4) : domain; } - public static String[] getArray(@NonNull String input) { - return input.split(Constants.SEPARATOR); - } - public static void trimCache(@NonNull Context context) { try { File dir = context.getCacheDir(); 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 777c570..e81396e 100644 --- a/app/src/main/java/acr/browser/lightning/view/LightningView.java +++ b/app/src/main/java/acr/browser/lightning/view/LightningView.java @@ -193,10 +193,10 @@ public class LightningView { return; } switch (sHomepage) { - case "about:home": + case Constants.SCHEME_HOMEPAGE: loadStartpage(); break; - case "about:bookmarks": + case Constants.SCHEME_BOOKMARKS: loadBookmarkpage(); break; default: diff --git a/app/src/main/java/acr/browser/lightning/view/LightningWebClient.java b/app/src/main/java/acr/browser/lightning/view/LightningWebClient.java index 7f0a0de..e59d119 100644 --- a/app/src/main/java/acr/browser/lightning/view/LightningWebClient.java +++ b/app/src/main/java/acr/browser/lightning/view/LightningWebClient.java @@ -295,7 +295,7 @@ public class LightningWebClient extends WebViewClient { // If we are in incognito, immediately load, we don't want the url to leave the app return super.shouldOverrideUrlLoading(view, url); } - if (url.startsWith("about:")) { + if (url.startsWith(Constants.ABOUT)) { // If this is an about page, immediately load, we don't need to leave the app return super.shouldOverrideUrlLoading(view, url); } @@ -310,7 +310,7 @@ public class LightningWebClient extends WebViewClient { view.loadUrl(url, headers); return true; } - if (url.startsWith("about:") && Utils.doesSupportHeaders()) { + if (url.startsWith(Constants.ABOUT) && Utils.doesSupportHeaders()) { // If this is an about page, immediately load, we don't need to leave the app view.loadUrl(url, headers); return true;