From c883c82d674b88ebffc72e9bb318b48f1da5ea69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Sieradzki?= Date: Fri, 25 Jul 2014 19:31:33 +0200 Subject: [PATCH 01/14] Add missing @Override annotation --- src/acr/browser/lightning/BrowserApp.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/acr/browser/lightning/BrowserApp.java b/src/acr/browser/lightning/BrowserApp.java index f59c4ab..5b49829 100644 --- a/src/acr/browser/lightning/BrowserApp.java +++ b/src/acr/browser/lightning/BrowserApp.java @@ -7,6 +7,7 @@ public class BrowserApp extends Application { private static Context context; + @Override public void onCreate() { super.onCreate(); context = getApplicationContext(); From 3d14b3aa11fe44ef1b5d0560bf1141711b45ebda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Sieradzki?= Date: Fri, 25 Jul 2014 19:31:59 +0200 Subject: [PATCH 02/14] Remove unnecessary code --- src/acr/browser/lightning/ClickHandler.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/acr/browser/lightning/ClickHandler.java b/src/acr/browser/lightning/ClickHandler.java index 7aa35b0..4a9f702 100644 --- a/src/acr/browser/lightning/ClickHandler.java +++ b/src/acr/browser/lightning/ClickHandler.java @@ -15,16 +15,14 @@ public class ClickHandler extends Handler { try { mBrowserController = (BrowserController) context; } catch (ClassCastException e) { - throw new ClassCastException(context.toString() - + " must implement BrowserController"); + throw new ClassCastException(context + " must implement BrowserController"); } } @Override public void handleMessage(Message msg) { super.handleMessage(msg); - String url = null; - url = msg.getData().getString("url"); + String url = msg.getData().getString("url"); mBrowserController.longClickPage(url); } } From f90f9fa877a08e2d52e8af4ff6396bc4f3a5403f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Sieradzki?= Date: Fri, 25 Jul 2014 19:33:29 +0200 Subject: [PATCH 03/14] Make util class non-instantiatable and non-extendable. --- src/acr/browser/lightning/Constants.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/acr/browser/lightning/Constants.java b/src/acr/browser/lightning/Constants.java index ba27e0c..539bb79 100644 --- a/src/acr/browser/lightning/Constants.java +++ b/src/acr/browser/lightning/Constants.java @@ -5,10 +5,9 @@ package acr.browser.lightning; import android.os.Environment; -public class Constants { +public final class Constants { - public Constants() { - // TODO Auto-generated constructor stub + private Constants() { } public static final String DESKTOP_USER_AGENT = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/20 Safari/537.17"; From 406af6e699af6d426b905312abb23c3c8dd15197 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Sieradzki?= Date: Fri, 25 Jul 2014 19:33:53 +0200 Subject: [PATCH 04/14] Add missing @Override annotation, remove unnecessary code --- src/acr/browser/lightning/DownloadHandler.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/acr/browser/lightning/DownloadHandler.java b/src/acr/browser/lightning/DownloadHandler.java index a6d7ed4..36d2909 100644 --- a/src/acr/browser/lightning/DownloadHandler.java +++ b/src/acr/browser/lightning/DownloadHandler.java @@ -93,7 +93,7 @@ public class DownloadHandler { break; } } - if (needed == false) { + if (!needed) { return path; } @@ -204,6 +204,7 @@ public class DownloadHandler { final DownloadManager manager = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE); new Thread("Browser download") { + @Override public void run() { manager.enqueue(request); } From b9320522c7eaa28b3537d37d079e085d080439be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Sieradzki?= Date: Fri, 25 Jul 2014 19:34:49 +0200 Subject: [PATCH 05/14] Add missing equals() and hashCode() methods for class that implements Comparable --- src/acr/browser/lightning/HistoryItem.java | 43 ++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/acr/browser/lightning/HistoryItem.java b/src/acr/browser/lightning/HistoryItem.java index 66aded0..90b30a2 100644 --- a/src/acr/browser/lightning/HistoryItem.java +++ b/src/acr/browser/lightning/HistoryItem.java @@ -101,4 +101,47 @@ public class HistoryItem implements Comparable { public int compareTo(HistoryItem another) { return mTitle.compareTo(another.mTitle); } + + @Override + public boolean equals(Object o) { + + if (this == o) { + return true; + } + if (o == null || ((Object) this).getClass() != o.getClass()) { + return false; + } + + HistoryItem that = (HistoryItem) o; + + if (mId != that.mId) { + return false; + } + if (mImageId != that.mImageId) { + return false; + } + if (mBitmap != null ? !mBitmap.equals(that.mBitmap) : that.mBitmap != null) { + return false; + } + if (!mTitle.equals(that.mTitle)) { + return false; + } + if (!mUrl.equals(that.mUrl)) { + return false; + } + + return true; + } + + @Override + public int hashCode() { + + int result = mId; + result = 31 * result + mUrl.hashCode(); + result = 31 * result + mTitle.hashCode(); + result = 31 * result + (mBitmap != null ? mBitmap.hashCode() : 0); + result = 31 * result + mImageId; + + return result; + } } From a33f3a340af59e6481b6c77b74b2b6a7554f1f86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Sieradzki?= Date: Fri, 25 Jul 2014 19:35:41 +0200 Subject: [PATCH 06/14] Fix "'handlers.size() == 0' can be replaced with 'handlers.isEmpty()'" --- src/acr/browser/lightning/IntentUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/acr/browser/lightning/IntentUtils.java b/src/acr/browser/lightning/IntentUtils.java index a08e29d..ddfaf5d 100644 --- a/src/acr/browser/lightning/IntentUtils.java +++ b/src/acr/browser/lightning/IntentUtils.java @@ -80,7 +80,7 @@ public class IntentUtils { PackageManager pm = mActivity.getPackageManager(); List handlers = pm.queryIntentActivities(intent, PackageManager.GET_RESOLVED_FILTER); - if (handlers == null || handlers.size() == 0) { + if (handlers == null || handlers.isEmpty()) { return false; } for (ResolveInfo resolveInfo : handlers) { From 82d8af3057986f220e39992122fc23d49d05ffa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Sieradzki?= Date: Fri, 25 Jul 2014 19:59:55 +0200 Subject: [PATCH 07/14] Fix method names, redundant initialization of variables, typos in comments and other improvements --- .../browser/lightning/BrowserActivity.java | 6 +- src/acr/browser/lightning/LightningView.java | 89 +++++++------------ 2 files changed, 35 insertions(+), 60 deletions(-) diff --git a/src/acr/browser/lightning/BrowserActivity.java b/src/acr/browser/lightning/BrowserActivity.java index e43572b..0a2c6c6 100644 --- a/src/acr/browser/lightning/BrowserActivity.java +++ b/src/acr/browser/lightning/BrowserActivity.java @@ -1014,11 +1014,11 @@ public class BrowserActivity extends Activity implements BrowserController { return; } if (mCurrentView != null) { - mCurrentView.setIsForgroundTab(false); + mCurrentView.setForegroundTab(false); mCurrentView.onPause(); } mCurrentView = view; - mCurrentView.setIsForgroundTab(true); + mCurrentView.setForegroundTab(true); if (view.getWebView() != null) { updateUrl(view.getUrl()); updateProgress(view.getProgress()); @@ -1587,7 +1587,7 @@ public class BrowserActivity extends Activity implements BrowserController { LightningView web = data.get(position); holder.txtTitle.setText(web.getTitle()); - if (web.getIsForgroundTab()) { + if (web.isForegroundTab()) { holder.txtTitle.setTextAppearance(context, R.style.boldText); } else { holder.txtTitle.setTextAppearance(context, R.style.normalText); diff --git a/src/acr/browser/lightning/LightningView.java b/src/acr/browser/lightning/LightningView.java index cfbafd7..37303aa 100644 --- a/src/acr/browser/lightning/LightningView.java +++ b/src/acr/browser/lightning/LightningView.java @@ -61,9 +61,9 @@ public class LightningView { private AdBlock mAdBlock; - private boolean isForgroundTab = false; + private boolean isForegroundTab; - private IntentUtils mIntentUtils = null; + private IntentUtils mIntentUtils; @SuppressWarnings("deprecation") @SuppressLint("NewApi") @@ -81,7 +81,7 @@ public class LightningView { try { mBrowserController = (BrowserController) activity; } catch (ClassCastException e) { - throw new ClassCastException(activity.toString() + throw new ClassCastException(activity + " must implement BrowserController"); } mIntentUtils = new IntentUtils(mBrowserController); @@ -109,11 +109,11 @@ public class LightningView { new CustomGestureListener()); mWebView.setOnTouchListener(new OnTouchListener() { - float mLocation = 0; + float mLocation; - float mY = 0; + float mY; - int mAction = 0; + int mAction; @Override public boolean onTouch(View view, MotionEvent arg1) { @@ -142,10 +142,8 @@ public class LightningView { initializeSettings(mWebView.getSettings(), activity); initializePreferences(activity); - if (url != null) { - if (!url.equals("")) { - mWebView.loadUrl(url); - } + if (url != null && !url.trim().isEmpty()) { + mWebView.loadUrl(url); } else { if (mHomepage.startsWith("about:home")) { mSettings.setUseWideViewPort(false); @@ -159,7 +157,7 @@ public class LightningView { } public String getHomepage() { - String home = ""; + String home; home = HomepageVariables.HEAD; switch (mPreferences.getInt(PreferenceConstants.SEARCH, 1)) { case 0: @@ -394,11 +392,7 @@ public class LightningView { } public boolean isShown() { - if (mWebView != null) { - return mWebView.isShown(); - } else { - return false; - } + return mWebView != null && mWebView.isShown(); } public synchronized void onPause() { @@ -413,13 +407,13 @@ public class LightningView { } } - public void setIsForgroundTab(boolean isForground) { - isForgroundTab = isForground; + public void setForegroundTab(boolean isForeground) { + isForegroundTab = isForeground; mBrowserController.update(); } - public boolean getIsForgroundTab() { - return isForgroundTab; + public boolean isForegroundTab() { + return isForegroundTab; } public int getProgress() { @@ -449,10 +443,8 @@ public class LightningView { } public void requestFocus() { - if (mWebView != null) { - if (!mWebView.hasFocus()) { - mWebView.requestFocus(); - } + if (mWebView != null && !mWebView.hasFocus()) { + mWebView.requestFocus(); } } @@ -524,19 +516,11 @@ public class LightningView { } public boolean canGoBack() { - if (mWebView != null) { - return mWebView.canGoBack(); - } else { - return false; - } + return mWebView != null && mWebView.canGoBack(); } public boolean canGoForward() { - if (mWebView != null) { - return mWebView.canGoForward(); - } else { - return false; - } + return mWebView != null && mWebView.canGoForward(); } public WebView getWebView() { @@ -583,11 +567,8 @@ public class LightningView { public WebResourceResponse shouldInterceptRequest(WebView view, String url) { if (mAdBlock.isAd(url)) { - ByteArrayInputStream EMPTY = new ByteArrayInputStream( - "".getBytes()); - WebResourceResponse response = new WebResourceResponse( - "text/plain", "utf-8", EMPTY); - return response; + ByteArrayInputStream EMPTY = new ByteArrayInputStream("".getBytes()); + return new WebResourceResponse("text/plain", "utf-8", EMPTY); } boolean useProxy = mPreferences.getBoolean( @@ -650,12 +631,12 @@ public class LightningView { } if (cType != null && cType.startsWith("text")) { - InputStream fStream = null; + InputStream fStream; BufferedInputStream bis = new BufferedInputStream( conn.getInputStream()); ByteArrayBuffer baf = new ByteArrayBuffer(connLen); - int read = 0; + int read; int bufSize = 2048; byte[] buffer = new byte[bufSize]; while (true) { @@ -681,10 +662,7 @@ public class LightningView { fStream = new ReplacingInputStream(fStream, "\"poster\"".getBytes(), "\"foo\"".getBytes()); - WebResourceResponse response = new WebResourceResponse( - cType, cEnc, fStream); - - return response; + return new WebResourceResponse(cType, cEnc, fStream); }/** * else if (mDoLeakHardening) { WebResourceResponse response = * new WebResourceResponse( cType, cEnc, conn.getInputStream()); @@ -700,11 +678,8 @@ public class LightningView { Log.e(Constants.TAG, "Error filtering stream", e); ByteArrayInputStream EMPTY = new ByteArrayInputStream( "".getBytes()); - WebResourceResponse response = new WebResourceResponse( - "text/plain", "utf-8", EMPTY); - return response; + return new WebResourceResponse("text/plain", "utf-8", EMPTY); } - } @Override @@ -714,7 +689,7 @@ public class LightningView { } if (view.getTitle() == null) { mTitle.setTitle(mActivity.getString(R.string.untitled)); - } else if (view.getTitle().length() > 0) { + } else if (!view.getTitle().isEmpty()) { mTitle.setTitle(view.getTitle()); } else { mTitle.setTitle(mActivity.getString(R.string.untitled)); @@ -912,7 +887,7 @@ public class LightningView { @Override public void onReceivedTitle(WebView view, String title) { - if (title.length() > 0) { + if (!title.isEmpty()) { mTitle.setTitle(title); } else { mTitle.setTitle(mActivity.getString(R.string.untitled)); @@ -929,7 +904,7 @@ public class LightningView { builder.setTitle(mActivity.getString(R.string.location)); String org = null; if (origin.length() > 50) { - org = (String) origin.subSequence(0, 50) + "..."; + org = origin.subSequence(0, 50) + "..."; } else { org = origin; } @@ -1004,7 +979,7 @@ public class LightningView { @Override public void onShowCustomView(View view, CustomViewCallback callback) { - // While these lines might look like they work, in practive, + // While these lines might look like they work, in practice, // Full-screen videos won't work correctly. I may test this out some more // if (view instanceof FrameLayout) { // FrameLayout frame = (FrameLayout) view; @@ -1028,7 +1003,7 @@ public class LightningView { @Deprecated public void onShowCustomView(View view, int requestedOrientation, CustomViewCallback callback) { - // While these lines might look like they work, in practive, + // While these lines might look like they work, in practice, // Full-screen videos won't work correctly. I may test this out some more // if (view instanceof FrameLayout) { // FrameLayout frame = (FrameLayout) view; @@ -1046,7 +1021,6 @@ public class LightningView { super.onShowCustomView(view, requestedOrientation, callback); } - } public class Title { @@ -1073,9 +1047,10 @@ public class LightningView { public void setTitle(String title) { if (title == null) { - title = ""; + mTitle = ""; + } else { + mTitle = title; } - mTitle = title; } public void setTitleAndFavicon(String title, Bitmap favicon) { From 4c409c209f9dee47da927e84e2f777891a98f946 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Sieradzki?= Date: Fri, 25 Jul 2014 20:00:58 +0200 Subject: [PATCH 08/14] Make util class non-instantiatable and non-extendable --- src/acr/browser/lightning/PreferenceConstants.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/acr/browser/lightning/PreferenceConstants.java b/src/acr/browser/lightning/PreferenceConstants.java index f375a9c..cfe32ba 100644 --- a/src/acr/browser/lightning/PreferenceConstants.java +++ b/src/acr/browser/lightning/PreferenceConstants.java @@ -3,7 +3,10 @@ */ package acr.browser.lightning; -public class PreferenceConstants { +public final class PreferenceConstants { + + private PreferenceConstants() { + } public static final String ADOBE_FLASH_SUPPORT = "enableflash"; From f36fd4bb8a462e9f9cbec8b706bcc5fd3b12a926 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Sieradzki?= Date: Fri, 25 Jul 2014 20:02:57 +0200 Subject: [PATCH 09/14] Use interface instead of concrete class, add missing @Override --- src/acr/browser/lightning/ReplacingInputStream.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/acr/browser/lightning/ReplacingInputStream.java b/src/acr/browser/lightning/ReplacingInputStream.java index dc7e8e5..4090e09 100644 --- a/src/acr/browser/lightning/ReplacingInputStream.java +++ b/src/acr/browser/lightning/ReplacingInputStream.java @@ -3,14 +3,15 @@ package acr.browser.lightning; import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; +import java.util.Deque; import java.util.Iterator; import java.util.LinkedList; public class ReplacingInputStream extends FilterInputStream { - LinkedList inQueue = new LinkedList(); + Deque inQueue = new LinkedList(); - LinkedList outQueue = new LinkedList(); + Deque outQueue = new LinkedList(); final byte[] search, replacement; @@ -70,6 +71,7 @@ public class ReplacingInputStream extends FilterInputStream { /** * Returns false. REFilterInputStream does not support mark() and reset() methods. */ + @Override public boolean markSupported() { return false; } @@ -77,6 +79,7 @@ public class ReplacingInputStream extends FilterInputStream { /** * Reads from the stream into the provided array. */ + @Override public int read(byte[] b, int off, int len) throws IOException { int i; int ok = 0; From 299c5ae6eb972d6c36285b2df5a9056689c8e2a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Sieradzki?= Date: Fri, 25 Jul 2014 20:14:35 +0200 Subject: [PATCH 10/14] Add logging exception, fix typo in comment --- src/acr/browser/lightning/DownloadHandler.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/acr/browser/lightning/DownloadHandler.java b/src/acr/browser/lightning/DownloadHandler.java index 36d2909..d02428d 100644 --- a/src/acr/browser/lightning/DownloadHandler.java +++ b/src/acr/browser/lightning/DownloadHandler.java @@ -160,9 +160,9 @@ public class DownloadHandler { webAddress = new WebAddress(url); webAddress.setPath(encodePath(webAddress.getPath())); } catch (Exception e) { - // This only happens for very bad urls, we want to chatch the + // This only happens for very bad urls, we want to catch the // exception here - Log.e(LOGTAG, "Exception trying to parse url:" + url); + Log.e(LOGTAG, "Exception while trying to parse url '" + url + '\'', e); return; } From e0ae78cfcd155ed58c2349f2e4e4cd4caa6585d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Sieradzki?= Date: Fri, 25 Jul 2014 20:15:20 +0200 Subject: [PATCH 11/14] Remove redundant variable initialization, flip equals() on strings --- src/acr/browser/lightning/SearchAdapter.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/acr/browser/lightning/SearchAdapter.java b/src/acr/browser/lightning/SearchAdapter.java index 0a5b562..1b8aa73 100644 --- a/src/acr/browser/lightning/SearchAdapter.java +++ b/src/acr/browser/lightning/SearchAdapter.java @@ -45,7 +45,7 @@ public class SearchAdapter extends BaseAdapter implements Filterable { private Context mContext; - private boolean mIncognito = false; + private boolean mIncognito; public SearchAdapter(Context context, boolean incognito) { mDatabaseHandler = new DatabaseHandler(context); @@ -278,13 +278,13 @@ public class SearchAdapter extends BaseAdapter implements Filterable { int counter = 0; while (eventType != XmlPullParser.END_DOCUMENT) { if (eventType == XmlPullParser.START_TAG) { - if (xpp.getName().equals("suggestion")) { + if ("suggestion".equals(xpp.getName())) { String suggestion = xpp.getAttributeValue(null, "data"); filter.add(new HistoryItem(mContext .getString(R.string.suggestion) + " \"" - + suggestion + "\"", suggestion, + + suggestion + '"', suggestion, R.drawable.ic_search)); counter++; if (counter >= 5) { From d47d03b7aff90e18f5a08719cbb95f96690759c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Sieradzki?= Date: Fri, 25 Jul 2014 20:16:09 +0200 Subject: [PATCH 12/14] Add missing 'private' and 'public' access' modifiers, fix warnings --- src/acr/browser/lightning/SettingsController.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/acr/browser/lightning/SettingsController.java b/src/acr/browser/lightning/SettingsController.java index 783aac3..00c5eee 100644 --- a/src/acr/browser/lightning/SettingsController.java +++ b/src/acr/browser/lightning/SettingsController.java @@ -5,24 +5,24 @@ package acr.browser.lightning; public class SettingsController { - static boolean clearHistory = false; + private static boolean clearHistory; /** * The purpose of this class is so that I can clear the dropdown history in the main activities if the user selects * to clear the history from the disk in advanced settings */ - static void setClearHistory(boolean choice) { + public static void setClearHistory(boolean choice) { clearHistory = choice; } /** * return the choice */ - static boolean getClearHistory() { + public static boolean getClearHistory() { if (clearHistory) { clearHistory = false; return true; } - return clearHistory; + return false; } } From c7b97621c776141c3efc50ce38074e1827a36f10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Sieradzki?= Date: Fri, 25 Jul 2014 20:17:03 +0200 Subject: [PATCH 13/14] Make util class non-instantiatable and non-extendable, flip equals() on strings, remove redundant code --- src/acr/browser/lightning/Utils.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/acr/browser/lightning/Utils.java b/src/acr/browser/lightning/Utils.java index 8a2b642..21493f6 100644 --- a/src/acr/browser/lightning/Utils.java +++ b/src/acr/browser/lightning/Utils.java @@ -18,7 +18,10 @@ import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; -public class Utils { +public final class Utils { + + private Utils() { + } public static void downloadFile(final Activity activity, final String url, final String userAgent, final String contentDisposition, final boolean privateBrowsing) { @@ -31,7 +34,7 @@ public class Utils { public static synchronized void addBookmark(Context context, String title, String url) { File book = new File(context.getFilesDir(), "bookmarks"); File bookUrl = new File(context.getFilesDir(), "bookurl"); - if ((title.equals("Bookmarks") || title.equals("History")) && url.startsWith("file://")) { + if (("Bookmarks".equals(title) || "History".equals(title)) && url.startsWith("file://")) { return; } try { @@ -102,8 +105,7 @@ public class Utils { */ public static int convertToDensityPixels(Context context, int densityPixels) { float scale = context.getResources().getDisplayMetrics().density; - int pixels = (int) (densityPixels * scale + 0.5f); - return pixels; + return (int) (densityPixels * scale + 0.5f); } public static String getDomainName(String url) { @@ -169,6 +171,6 @@ public class Utils { } } // The directory is now empty so delete it - return dir.delete(); + return dir != null && dir.delete(); } } From 3c8e46188ad5a7fe1ee7dab218ac630ec4764f9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Sieradzki?= Date: Fri, 25 Jul 2014 20:18:44 +0200 Subject: [PATCH 14/14] Fix that NullPointerException should NEVER be thrown by programmer, make exceptions more informative, simplify code logic, flip equals() on strings ...and other small fixes --- src/acr/browser/lightning/WebAddress.java | 99 +++++++++++------------ 1 file changed, 48 insertions(+), 51 deletions(-) diff --git a/src/acr/browser/lightning/WebAddress.java b/src/acr/browser/lightning/WebAddress.java index 2823d60..2e1e2c2 100644 --- a/src/acr/browser/lightning/WebAddress.java +++ b/src/acr/browser/lightning/WebAddress.java @@ -10,8 +10,6 @@ import java.util.regex.Pattern; import static android.util.Patterns.GOOD_IRI_CHAR; /** - * {@hide} - * * Web Address Parser * * This is called WebAddress, rather than URL or URI, because it attempts to @@ -54,15 +52,14 @@ public class WebAddress { /* anchor */".*", Pattern.CASE_INSENSITIVE); /** - * parses given uriString. + * Parses given URI-like string. */ - public WebAddress(String address) throws Exception { + public WebAddress(String address) { + if (address == null) { - throw new NullPointerException(); + throw new IllegalArgumentException("address can't be null"); } - // android.util.Log.d(LOGTAG, "WebAddress: " + address); - mScheme = ""; mHost = ""; mPort = -1; @@ -71,73 +68,73 @@ public class WebAddress { Matcher m = sAddressPattern.matcher(address); String t; - if (m.matches()) { - t = m.group(MATCH_GROUP_SCHEME); - if (t != null) { - mScheme = t.toLowerCase(Locale.ROOT); - } - t = m.group(MATCH_GROUP_AUTHORITY); - if (t != null) { - mAuthInfo = t; - } - t = m.group(MATCH_GROUP_HOST); - if (t != null) { - mHost = t; - } - t = m.group(MATCH_GROUP_PORT); - if (t != null && t.length() > 0) { - // The ':' character is not returned by the regex. - try { - mPort = Integer.parseInt(t); - } catch (NumberFormatException ex) { - throw new Exception(); - } + if (!m.matches()) { + throw new IllegalArgumentException("Parsing of address '" + + address + "' failed"); + } + + t = m.group(MATCH_GROUP_SCHEME); + if (t != null) { + mScheme = t.toLowerCase(Locale.ROOT); + } + t = m.group(MATCH_GROUP_AUTHORITY); + if (t != null) { + mAuthInfo = t; + } + t = m.group(MATCH_GROUP_HOST); + if (t != null) { + mHost = t; + } + t = m.group(MATCH_GROUP_PORT); + if (t != null && !t.isEmpty()) { + // The ':' character is not returned by the regex. + try { + mPort = Integer.parseInt(t); + } catch (NumberFormatException ex) { + throw new RuntimeException("Parsing of port number failed", ex); } - t = m.group(MATCH_GROUP_PATH); - if (t != null && t.length() > 0) { - /* - * handle busted myspace frontpage redirect with missing initial - * "/" - */ - if (t.charAt(0) == '/') { - mPath = t; - } else { - mPath = "/" + t; - } + } + t = m.group(MATCH_GROUP_PATH); + if (t != null && !t.isEmpty()) { + /* + * handle busted myspace frontpage redirect with missing initial + * "/" + */ + if (t.charAt(0) == '/') { + mPath = t; + } else { + mPath = '/' + t; } - - } else { - // nothing found... outa here - throw new Exception(); } /* * Get port from scheme or scheme from port, if necessary and possible */ - if (mPort == 443 && mScheme.equals("")) { + if (mPort == 443 && "".equals(mScheme)) { mScheme = "https"; } else if (mPort == -1) { - if (mScheme.equals("https")) { + if ("https".equals(mScheme)) { mPort = 443; } else { mPort = 80; // default } } - if (mScheme.equals("")) { + if ("".equals(mScheme)) { mScheme = "http"; } } @Override public String toString() { + String port = ""; - if ((mPort != 443 && mScheme.equals("https")) - || (mPort != 80 && mScheme.equals("http"))) { - port = ":" + Integer.toString(mPort); + if ((mPort != 443 && "https".equals(mScheme)) + || (mPort != 80 && "http".equals(mScheme))) { + port = ':' + Integer.toString(mPort); } String authInfo = ""; - if (mAuthInfo.length() > 0) { - authInfo = mAuthInfo + "@"; + if (!mAuthInfo.isEmpty()) { + authInfo = mAuthInfo + '@'; } return mScheme + "://" + authInfo + mHost + port + mPath;