Browse Source

Non null annotations in LightningWebClient/ChromeClient

master
Anthony Restaino 9 years ago
parent
commit
8f38b91dc1
  1. 16
      app/src/main/java/acr/browser/lightning/utils/Preconditions.java
  2. 38
      app/src/main/java/acr/browser/lightning/view/LightningChromeClient.java
  3. 32
      app/src/main/java/acr/browser/lightning/view/LightningWebClient.java

16
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");
}
}
}

38
app/src/main/java/acr/browser/lightning/view/LightningChromeClient.java

@ -9,6 +9,8 @@ import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.net.Uri; import android.net.Uri;
import android.os.Message; import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -17,30 +19,28 @@ import android.webkit.ValueCallback;
import android.webkit.WebChromeClient; import android.webkit.WebChromeClient;
import android.webkit.WebView; import android.webkit.WebView;
import com.anthonycr.grant.PermissionsManager;
import com.anthonycr.grant.PermissionsResultAction;
import com.squareup.otto.Bus; import com.squareup.otto.Bus;
import acr.browser.lightning.R; import acr.browser.lightning.R;
import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.bus.BrowserEvents; import acr.browser.lightning.bus.BrowserEvents;
import acr.browser.lightning.controller.UIController; 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 { class LightningChromeClient extends WebChromeClient {
private static final String[] PERMISSIONS = new String[]{Manifest.permission.ACCESS_FINE_LOCATION}; private static final String[] PERMISSIONS = new String[]{Manifest.permission.ACCESS_FINE_LOCATION};
private final Activity mActivity; @NonNull private final Activity mActivity;
private final LightningView mLightningView; @NonNull private final LightningView mLightningView;
private final UIController mUIController; @NonNull private final UIController mUIController;
private final Bus eventBus; @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; mActivity = activity;
mUIController = (UIController) activity; mUIController = (UIController) activity;
mLightningView = lightningView; mLightningView = lightningView;
@ -55,7 +55,7 @@ class LightningChromeClient extends WebChromeClient {
} }
@Override @Override
public void onReceivedIcon(WebView view, Bitmap icon) { public void onReceivedIcon(@NonNull WebView view, Bitmap icon) {
mLightningView.getTitleInfo().setFavicon(icon); mLightningView.getTitleInfo().setFavicon(icon);
eventBus.post(new BrowserEvents.TabsChanged()); eventBus.post(new BrowserEvents.TabsChanged());
cacheFavicon(view.getUrl(), icon, mActivity); cacheFavicon(view.getUrl(), icon, mActivity);
@ -66,7 +66,7 @@ class LightningChromeClient extends WebChromeClient {
* *
* @param icon the icon to cache * @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; if (icon == null) return;
final Uri uri = Uri.parse(url); final Uri uri = Uri.parse(url);
if (uri.getHost() == null) { if (uri.getHost() == null) {
@ -77,7 +77,7 @@ class LightningChromeClient extends WebChromeClient {
@Override @Override
public void onReceivedTitle(WebView view, String title) { public void onReceivedTitle(@Nullable WebView view, @Nullable String title) {
if (title != null && !title.isEmpty()) { if (title != null && !title.isEmpty()) {
mLightningView.getTitleInfo().setTitle(title); mLightningView.getTitleInfo().setTitle(title);
} else { } else {
@ -90,8 +90,8 @@ class LightningChromeClient extends WebChromeClient {
} }
@Override @Override
public void onGeolocationPermissionsShowPrompt(final String origin, public void onGeolocationPermissionsShowPrompt(@NonNull final String origin,
final GeolocationPermissions.Callback callback) { @NonNull final GeolocationPermissions.Callback callback) {
PermissionsManager.getInstance().requestPermissionsIfNecessaryForResult(mActivity, PERMISSIONS, new PermissionsResultAction() { PermissionsManager.getInstance().requestPermissionsIfNecessaryForResult(mActivity, PERMISSIONS, new PermissionsResultAction() {
@Override @Override
public void onGranted() { public void onGranted() {
@ -170,11 +170,9 @@ class LightningChromeClient extends WebChromeClient {
* *
* @return a Bitmap that can be used as a place holder for videos. * @return a Bitmap that can be used as a place holder for videos.
*/ */
@Nullable
@Override @Override
public Bitmap getDefaultVideoPoster() { public Bitmap getDefaultVideoPoster() {
if (mActivity == null) {
return null;
}
final Resources resources = mActivity.getResources(); final Resources resources = mActivity.getResources();
return BitmapFactory.decodeResource(resources, android.R.drawable.spinner_background); return BitmapFactory.decodeResource(resources, android.R.drawable.spinner_background);
} }

32
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.Build;
import android.os.Message; import android.os.Message;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.text.InputType; import android.text.InputType;
import android.text.method.PasswordTransformationMethod; 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.controller.UIController;
import acr.browser.lightning.utils.AdBlock; import acr.browser.lightning.utils.AdBlock;
import acr.browser.lightning.utils.IntentUtils; import acr.browser.lightning.utils.IntentUtils;
import acr.browser.lightning.utils.Preconditions;
import acr.browser.lightning.utils.ProxyUtils; import acr.browser.lightning.utils.ProxyUtils;
import acr.browser.lightning.utils.Utils; import acr.browser.lightning.utils.Utils;
public class LightningWebClient extends WebViewClient { public class LightningWebClient extends WebViewClient {
private final Activity mActivity; @NonNull private final Activity mActivity;
private final LightningView mLightningView; @NonNull private final LightningView mLightningView;
private final UIController mUIController; @NonNull private final UIController mUIController;
private final Bus mEventBus; @NonNull private final Bus mEventBus;
private final IntentUtils mIntentUtils; @NonNull private final IntentUtils mIntentUtils;
@Inject ProxyUtils mProxyUtils; @Inject ProxyUtils mProxyUtils;
@Inject AdBlock mAdBlock; @Inject AdBlock mAdBlock;
LightningWebClient(Activity activity, LightningView lightningView) { LightningWebClient(@NonNull Activity activity, @NonNull LightningView lightningView) {
BrowserApp.getAppComponent().inject(this); BrowserApp.getAppComponent().inject(this);
Preconditions.checkNonNull(activity);
Preconditions.checkNonNull(lightningView);
mActivity = activity; mActivity = activity;
mUIController = (UIController) activity; mUIController = (UIController) activity;
mLightningView = lightningView; mLightningView = lightningView;
@ -68,7 +72,7 @@ public class LightningWebClient extends WebViewClient {
@TargetApi(Build.VERSION_CODES.LOLLIPOP) @TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override @Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) { public WebResourceResponse shouldInterceptRequest(WebView view, @NonNull WebResourceRequest request) {
if (mAdBlock.isAd(request.getUrl().toString())) { if (mAdBlock.isAd(request.getUrl().toString())) {
ByteArrayInputStream EMPTY = new ByteArrayInputStream("".getBytes()); ByteArrayInputStream EMPTY = new ByteArrayInputStream("".getBytes());
return new WebResourceResponse("text/plain", "utf-8", EMPTY); return new WebResourceResponse("text/plain", "utf-8", EMPTY);
@ -76,6 +80,7 @@ public class LightningWebClient extends WebViewClient {
return super.shouldInterceptRequest(view, request); return super.shouldInterceptRequest(view, request);
} }
@Nullable
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.KITKAT_WATCH) @TargetApi(Build.VERSION_CODES.KITKAT_WATCH)
@Override @Override
@ -89,7 +94,7 @@ public class LightningWebClient extends WebViewClient {
@TargetApi(Build.VERSION_CODES.KITKAT) @TargetApi(Build.VERSION_CODES.KITKAT)
@Override @Override
public void onPageFinished(WebView view, String url) { public void onPageFinished(@NonNull WebView view, String url) {
if (view.isShown()) { if (view.isShown()) {
mUIController.updateUrl(url, true); mUIController.updateUrl(url, true);
view.postInvalidate(); view.postInvalidate();
@ -166,7 +171,7 @@ public class LightningWebClient extends WebViewClient {
@TargetApi(Build.VERSION_CODES.KITKAT) @TargetApi(Build.VERSION_CODES.KITKAT)
@Override @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() && if (view.isShown() && mLightningView.mPreferences.getTextReflowEnabled() &&
Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) { Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
if (mIsRunning) if (mIsRunning)
@ -187,7 +192,8 @@ public class LightningWebClient extends WebViewClient {
} }
} }
private static List<Integer> getAllSslErrorMessageCodes(SslError error) { @NonNull
private static List<Integer> getAllSslErrorMessageCodes(@NonNull SslError error) {
List<Integer> errorCodeMessageCodes = new ArrayList<>(); List<Integer> errorCodeMessageCodes = new ArrayList<>();
if (error.hasError(SslError.SSL_DATE_INVALID)) { if (error.hasError(SslError.SSL_DATE_INVALID)) {
@ -213,7 +219,7 @@ public class LightningWebClient extends WebViewClient {
} }
@Override @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<Integer> errorCodeMessageCodes = getAllSslErrorMessageCodes(error); List<Integer> errorCodeMessageCodes = getAllSslErrorMessageCodes(error);
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
@ -245,7 +251,7 @@ public class LightningWebClient extends WebViewClient {
} }
@Override @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); AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
builder.setTitle(mActivity.getString(R.string.title_form_resubmission)); builder.setTitle(mActivity.getString(R.string.title_form_resubmission));
builder.setMessage(mActivity.getString(R.string.message_form_resubmission)) builder.setMessage(mActivity.getString(R.string.message_form_resubmission))
@ -269,7 +275,7 @@ public class LightningWebClient extends WebViewClient {
} }
@Override @Override
public boolean shouldOverrideUrlLoading(WebView view, String url) { public boolean shouldOverrideUrlLoading(@NonNull WebView view, @NonNull String url) {
// Check if configured proxy is available // Check if configured proxy is available
if (!mProxyUtils.isProxyReady()) { if (!mProxyUtils.isProxyReady()) {
// User has been notified // User has been notified

Loading…
Cancel
Save