diff --git a/app/src/main/java/acr/browser/lightning/utils/Preconditions.java b/app/src/main/java/acr/browser/lightning/utils/Preconditions.java new file mode 100644 index 0000000..e9ffd84 --- /dev/null +++ b/app/src/main/java/acr/browser/lightning/utils/Preconditions.java @@ -0,0 +1,16 @@ +package acr.browser.lightning.utils; + +public class Preconditions { + /** + * Ensure that an object is not null + * and throw a RuntimeException if it + * is null. + * + * @param object check nullness on this object. + */ + public static void checkNonNull(Object object) { + if (object == null) { + throw new RuntimeException("Object must not be null"); + } + } +} diff --git a/app/src/main/java/acr/browser/lightning/view/LightningChromeClient.java b/app/src/main/java/acr/browser/lightning/view/LightningChromeClient.java index f435213..5954a6d 100644 --- a/app/src/main/java/acr/browser/lightning/view/LightningChromeClient.java +++ b/app/src/main/java/acr/browser/lightning/view/LightningChromeClient.java @@ -9,6 +9,8 @@ import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Message; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v7.app.AlertDialog; import android.view.LayoutInflater; import android.view.View; @@ -17,30 +19,28 @@ import android.webkit.ValueCallback; import android.webkit.WebChromeClient; import android.webkit.WebView; +import com.anthonycr.grant.PermissionsManager; +import com.anthonycr.grant.PermissionsResultAction; import com.squareup.otto.Bus; import acr.browser.lightning.R; import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.bus.BrowserEvents; import acr.browser.lightning.controller.UIController; +import acr.browser.lightning.utils.Preconditions; -import com.anthonycr.grant.PermissionsManager; -import com.anthonycr.grant.PermissionsResultAction; - -/** - * @author Stefano Pacifici based on Anthony C. Restaino code - * @date 2015/09/21 - */ class LightningChromeClient extends WebChromeClient { private static final String[] PERMISSIONS = new String[]{Manifest.permission.ACCESS_FINE_LOCATION}; - private final Activity mActivity; - private final LightningView mLightningView; - private final UIController mUIController; - private final Bus eventBus; + @NonNull private final Activity mActivity; + @NonNull private final LightningView mLightningView; + @NonNull private final UIController mUIController; + @NonNull private final Bus eventBus; - LightningChromeClient(Activity activity, LightningView lightningView) { + LightningChromeClient(@NonNull Activity activity, @NonNull LightningView lightningView) { + Preconditions.checkNonNull(activity); + Preconditions.checkNonNull(lightningView); mActivity = activity; mUIController = (UIController) activity; mLightningView = lightningView; @@ -55,7 +55,7 @@ class LightningChromeClient extends WebChromeClient { } @Override - public void onReceivedIcon(WebView view, Bitmap icon) { + public void onReceivedIcon(@NonNull WebView view, Bitmap icon) { mLightningView.getTitleInfo().setFavicon(icon); eventBus.post(new BrowserEvents.TabsChanged()); cacheFavicon(view.getUrl(), icon, mActivity); @@ -66,7 +66,7 @@ class LightningChromeClient extends WebChromeClient { * * @param icon the icon to cache */ - private static void cacheFavicon(final String url, final Bitmap icon, final Context context) { + private static void cacheFavicon(final String url, @Nullable final Bitmap icon, @NonNull final Context context) { if (icon == null) return; final Uri uri = Uri.parse(url); if (uri.getHost() == null) { @@ -77,7 +77,7 @@ class LightningChromeClient extends WebChromeClient { @Override - public void onReceivedTitle(WebView view, String title) { + public void onReceivedTitle(@Nullable WebView view, @Nullable String title) { if (title != null && !title.isEmpty()) { mLightningView.getTitleInfo().setTitle(title); } else { @@ -90,8 +90,8 @@ class LightningChromeClient extends WebChromeClient { } @Override - public void onGeolocationPermissionsShowPrompt(final String origin, - final GeolocationPermissions.Callback callback) { + public void onGeolocationPermissionsShowPrompt(@NonNull final String origin, + @NonNull final GeolocationPermissions.Callback callback) { PermissionsManager.getInstance().requestPermissionsIfNecessaryForResult(mActivity, PERMISSIONS, new PermissionsResultAction() { @Override public void onGranted() { @@ -170,11 +170,9 @@ class LightningChromeClient extends WebChromeClient { * * @return a Bitmap that can be used as a place holder for videos. */ + @Nullable @Override public Bitmap getDefaultVideoPoster() { - if (mActivity == null) { - return null; - } final Resources resources = mActivity.getResources(); return BitmapFactory.decodeResource(resources, android.R.drawable.spinner_background); } 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 efab798..2781900 100644 --- a/app/src/main/java/acr/browser/lightning/view/LightningWebClient.java +++ b/app/src/main/java/acr/browser/lightning/view/LightningWebClient.java @@ -11,6 +11,7 @@ import android.net.http.SslError; import android.os.Build; import android.os.Message; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v7.app.AlertDialog; import android.text.InputType; import android.text.method.PasswordTransformationMethod; @@ -41,23 +42,26 @@ import acr.browser.lightning.constant.Constants; import acr.browser.lightning.controller.UIController; import acr.browser.lightning.utils.AdBlock; import acr.browser.lightning.utils.IntentUtils; +import acr.browser.lightning.utils.Preconditions; import acr.browser.lightning.utils.ProxyUtils; import acr.browser.lightning.utils.Utils; public class LightningWebClient extends WebViewClient { - private final Activity mActivity; - private final LightningView mLightningView; - private final UIController mUIController; - private final Bus mEventBus; - private final IntentUtils mIntentUtils; + @NonNull private final Activity mActivity; + @NonNull private final LightningView mLightningView; + @NonNull private final UIController mUIController; + @NonNull private final Bus mEventBus; + @NonNull private final IntentUtils mIntentUtils; @Inject ProxyUtils mProxyUtils; @Inject AdBlock mAdBlock; - LightningWebClient(Activity activity, LightningView lightningView) { + LightningWebClient(@NonNull Activity activity, @NonNull LightningView lightningView) { BrowserApp.getAppComponent().inject(this); + Preconditions.checkNonNull(activity); + Preconditions.checkNonNull(lightningView); mActivity = activity; mUIController = (UIController) activity; mLightningView = lightningView; @@ -68,7 +72,7 @@ public class LightningWebClient extends WebViewClient { @TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override - public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) { + public WebResourceResponse shouldInterceptRequest(WebView view, @NonNull WebResourceRequest request) { if (mAdBlock.isAd(request.getUrl().toString())) { ByteArrayInputStream EMPTY = new ByteArrayInputStream("".getBytes()); return new WebResourceResponse("text/plain", "utf-8", EMPTY); @@ -76,6 +80,7 @@ public class LightningWebClient extends WebViewClient { return super.shouldInterceptRequest(view, request); } + @Nullable @SuppressWarnings("deprecation") @TargetApi(Build.VERSION_CODES.KITKAT_WATCH) @Override @@ -89,7 +94,7 @@ public class LightningWebClient extends WebViewClient { @TargetApi(Build.VERSION_CODES.KITKAT) @Override - public void onPageFinished(WebView view, String url) { + public void onPageFinished(@NonNull WebView view, String url) { if (view.isShown()) { mUIController.updateUrl(url, true); view.postInvalidate(); @@ -166,7 +171,7 @@ public class LightningWebClient extends WebViewClient { @TargetApi(Build.VERSION_CODES.KITKAT) @Override - public void onScaleChanged(final WebView view, final float oldScale, final float newScale) { + public void onScaleChanged(@NonNull final WebView view, final float oldScale, final float newScale) { if (view.isShown() && mLightningView.mPreferences.getTextReflowEnabled() && Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) { if (mIsRunning) @@ -187,7 +192,8 @@ public class LightningWebClient extends WebViewClient { } } - private static List getAllSslErrorMessageCodes(SslError error) { + @NonNull + private static List getAllSslErrorMessageCodes(@NonNull SslError error) { List errorCodeMessageCodes = new ArrayList<>(); if (error.hasError(SslError.SSL_DATE_INVALID)) { @@ -213,7 +219,7 @@ public class LightningWebClient extends WebViewClient { } @Override - public void onReceivedSslError(WebView view, @NonNull final SslErrorHandler handler, SslError error) { + public void onReceivedSslError(WebView view, @NonNull final SslErrorHandler handler, @NonNull SslError error) { List errorCodeMessageCodes = getAllSslErrorMessageCodes(error); StringBuilder stringBuilder = new StringBuilder(); @@ -245,7 +251,7 @@ public class LightningWebClient extends WebViewClient { } @Override - public void onFormResubmission(WebView view, @NonNull final Message dontResend, final Message resend) { + public void onFormResubmission(WebView view, @NonNull final Message dontResend, @NonNull final Message resend) { AlertDialog.Builder builder = new AlertDialog.Builder(mActivity); builder.setTitle(mActivity.getString(R.string.title_form_resubmission)); builder.setMessage(mActivity.getString(R.string.message_form_resubmission)) @@ -269,7 +275,7 @@ public class LightningWebClient extends WebViewClient { } @Override - public boolean shouldOverrideUrlLoading(WebView view, String url) { + public boolean shouldOverrideUrlLoading(@NonNull WebView view, @NonNull String url) { // Check if configured proxy is available if (!mProxyUtils.isProxyReady()) { // User has been notified