Stefano Pacifici
9 years ago
15 changed files with 833 additions and 767 deletions
@ -0,0 +1,234 @@
@@ -0,0 +1,234 @@
|
||||
package acr.browser.lightning.view; |
||||
|
||||
import android.Manifest; |
||||
import android.app.Activity; |
||||
import android.content.DialogInterface; |
||||
import android.graphics.Bitmap; |
||||
import android.net.Uri; |
||||
import android.os.Message; |
||||
import android.support.v7.app.AlertDialog; |
||||
import android.util.Log; |
||||
import android.view.View; |
||||
import android.webkit.GeolocationPermissions; |
||||
import android.webkit.ValueCallback; |
||||
import android.webkit.WebChromeClient; |
||||
import android.webkit.WebView; |
||||
|
||||
import com.squareup.otto.Bus; |
||||
|
||||
import java.io.File; |
||||
import java.io.FileOutputStream; |
||||
import java.io.IOException; |
||||
|
||||
import acr.browser.lightning.R; |
||||
import acr.browser.lightning.activity.BrowserActivity; |
||||
import acr.browser.lightning.app.BrowserApp; |
||||
import acr.browser.lightning.bus.BrowserEvents; |
||||
import acr.browser.lightning.constant.Constants; |
||||
import acr.browser.lightning.utils.PermissionsManager; |
||||
import acr.browser.lightning.utils.Utils; |
||||
|
||||
/** |
||||
* @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 BrowserActivity mActivity; |
||||
private final LightningView mLightningView; |
||||
private final Bus eventBus; |
||||
|
||||
LightningChromeClient(BrowserActivity activity, LightningView lightningView) { |
||||
mActivity = activity; |
||||
mLightningView = lightningView; |
||||
eventBus = BrowserApp.getAppComponent().getBus(); |
||||
} |
||||
|
||||
@Override |
||||
public void onProgressChanged(WebView view, int newProgress) { |
||||
if (mLightningView.isShown()) { |
||||
mActivity.updateProgress(newProgress); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void onReceivedIcon(WebView view, Bitmap icon) { |
||||
if (icon == null) |
||||
return; |
||||
mLightningView.mTitle.setFavicon(icon); |
||||
eventBus.post(new BrowserEvents.TabsChanged()); ; |
||||
cacheFavicon(view.getUrl(), icon); |
||||
} |
||||
|
||||
/** |
||||
* Naive caching of the favicon according to the domain name of the URL |
||||
* @param icon the icon to cache |
||||
*/ |
||||
private void cacheFavicon(final String url, final Bitmap icon) { |
||||
if (icon == null) return; |
||||
final Uri uri = Uri.parse(url); |
||||
if (uri.getHost() == null) { |
||||
return; |
||||
} |
||||
new Thread(new Runnable() { |
||||
@Override |
||||
public void run() { |
||||
String hash = String.valueOf(uri.getHost().hashCode()); |
||||
Log.d(Constants.TAG, "Caching icon for " + uri.getHost()); |
||||
FileOutputStream fos = null; |
||||
try { |
||||
File image = new File(BrowserApp.getAppContext().getCacheDir(), hash + ".png"); |
||||
fos = new FileOutputStream(image); |
||||
icon.compress(Bitmap.CompressFormat.PNG, 100, fos); |
||||
fos.flush(); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} finally { |
||||
Utils.close(fos); |
||||
} |
||||
} |
||||
}).start(); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void onReceivedTitle(WebView view, String title) { |
||||
if (title != null && !title.isEmpty()) { |
||||
mLightningView.mTitle.setTitle(title); |
||||
} else { |
||||
mLightningView.mTitle.setTitle(mActivity.getString(R.string.untitled)); |
||||
} |
||||
eventBus.post(new BrowserEvents.TabsChanged()); |
||||
if (view != null) { |
||||
mActivity.updateHistory(title, view.getUrl()); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void onGeolocationPermissionsShowPrompt(final String origin, |
||||
final GeolocationPermissions.Callback callback) { |
||||
PermissionsManager.getInstance().requestPermissionsIfNecessary(mActivity, PERMISSIONS); |
||||
final boolean remember = true; |
||||
AlertDialog.Builder builder = new AlertDialog.Builder(mActivity); |
||||
builder.setTitle(mActivity.getString(R.string.location)); |
||||
String org; |
||||
if (origin.length() > 50) { |
||||
org = origin.subSequence(0, 50) + "..."; |
||||
} else { |
||||
org = origin; |
||||
} |
||||
builder.setMessage(org + mActivity.getString(R.string.message_location)) |
||||
.setCancelable(true) |
||||
.setPositiveButton(mActivity.getString(R.string.action_allow), |
||||
new DialogInterface.OnClickListener() { |
||||
@Override |
||||
public void onClick(DialogInterface dialog, int id) { |
||||
callback.invoke(origin, true, remember); |
||||
} |
||||
}) |
||||
.setNegativeButton(mActivity.getString(R.string.action_dont_allow), |
||||
new DialogInterface.OnClickListener() { |
||||
@Override |
||||
public void onClick(DialogInterface dialog, int id) { |
||||
callback.invoke(origin, false, remember); |
||||
} |
||||
}); |
||||
AlertDialog alert = builder.create(); |
||||
alert.show(); |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, |
||||
Message resultMsg) { |
||||
mActivity.onCreateWindow(resultMsg); |
||||
return true; |
||||
} |
||||
|
||||
@Override |
||||
public void onCloseWindow(WebView window) { |
||||
mActivity.onCloseWindow(mLightningView); |
||||
} |
||||
|
||||
public void openFileChooser(ValueCallback<Uri> uploadMsg) { |
||||
mActivity.openFileChooser(uploadMsg); |
||||
} |
||||
|
||||
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) { |
||||
mActivity.openFileChooser(uploadMsg); |
||||
} |
||||
|
||||
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { |
||||
mActivity.openFileChooser(uploadMsg); |
||||
} |
||||
|
||||
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, |
||||
WebChromeClient.FileChooserParams fileChooserParams) { |
||||
mActivity.showFileChooser(filePathCallback); |
||||
return true; |
||||
} |
||||
|
||||
@Override |
||||
public Bitmap getDefaultVideoPoster() { |
||||
// TODO Simplify the method can be moved here
|
||||
return mActivity.getDefaultVideoPoster(); |
||||
} |
||||
|
||||
@Override |
||||
public View getVideoLoadingProgressView() { |
||||
// TODO Simplify the method can be moved here
|
||||
return mActivity.getVideoLoadingProgressView(); |
||||
} |
||||
|
||||
@Override |
||||
public void onHideCustomView() { |
||||
mActivity.onHideCustomView(); |
||||
super.onHideCustomView(); |
||||
} |
||||
|
||||
@Override |
||||
public void onShowCustomView(View view, CustomViewCallback callback) { |
||||
// 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;
|
||||
// if (frame.getFocusedChild() instanceof VideoView) {
|
||||
// VideoView video = (VideoView) frame.getFocusedChild();
|
||||
// video.stopPlayback();
|
||||
// frame.removeView(video);
|
||||
// video.setVisibility(View.GONE);
|
||||
// }
|
||||
// } else {
|
||||
mActivity.onShowCustomView(view, callback); |
||||
|
||||
// }
|
||||
|
||||
super.onShowCustomView(view, callback); |
||||
} |
||||
|
||||
@Override |
||||
@Deprecated |
||||
public void onShowCustomView(View view, int requestedOrientation, |
||||
CustomViewCallback callback) { |
||||
// 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;
|
||||
// if (frame.getFocusedChild() instanceof VideoView) {
|
||||
// VideoView video = (VideoView) frame.getFocusedChild();
|
||||
// video.stopPlayback();
|
||||
// frame.removeView(video);
|
||||
// video.setVisibility(View.GONE);
|
||||
// }
|
||||
// } else {
|
||||
mActivity.onShowCustomView(view, callback); |
||||
|
||||
// }
|
||||
|
||||
super.onShowCustomView(view, requestedOrientation, callback); |
||||
} |
||||
} |
@ -0,0 +1,66 @@
@@ -0,0 +1,66 @@
|
||||
package acr.browser.lightning.view; |
||||
|
||||
import android.content.Context; |
||||
import android.graphics.Bitmap; |
||||
|
||||
import acr.browser.lightning.R; |
||||
import acr.browser.lightning.utils.ThemeUtils; |
||||
import acr.browser.lightning.utils.Utils; |
||||
|
||||
/** |
||||
* @author Stefano Pacifici base on Anthony C. Restaino's code |
||||
* @date 2015/09/21 |
||||
*/ |
||||
class LightningViewTitle { |
||||
|
||||
private static Bitmap DEFAULT_ICON = null; |
||||
|
||||
private Bitmap mFavicon; |
||||
private String mTitle; |
||||
|
||||
public LightningViewTitle(Context context, boolean darkTheme) { |
||||
if (DEFAULT_ICON == null) { |
||||
DEFAULT_ICON = ThemeUtils.getThemedBitmap(context, R.drawable.ic_webpage, darkTheme); |
||||
} |
||||
mFavicon = DEFAULT_ICON; |
||||
mTitle = context.getString(R.string.action_new_tab); |
||||
} |
||||
|
||||
public void setFavicon(Bitmap favicon) { |
||||
if (favicon == null) { |
||||
mFavicon = DEFAULT_ICON; |
||||
} else { |
||||
mFavicon = Utils.padFavicon(favicon); |
||||
} |
||||
} |
||||
|
||||
public void setTitle(String title) { |
||||
if (title == null) { |
||||
mTitle = ""; |
||||
} else { |
||||
mTitle = title; |
||||
} |
||||
} |
||||
|
||||
public void setTitleAndFavicon(String title, Bitmap favicon) { |
||||
mTitle = title; |
||||
|
||||
if (favicon == null) { |
||||
mFavicon = DEFAULT_ICON; |
||||
} else { |
||||
mFavicon = Utils.padFavicon(favicon); |
||||
} |
||||
} |
||||
|
||||
public String getTitle() { |
||||
return mTitle; |
||||
} |
||||
|
||||
public Bitmap getFavicon() { |
||||
return mFavicon; |
||||
} |
||||
|
||||
public Bitmap getDefaultIcon() { |
||||
return DEFAULT_ICON; |
||||
} |
||||
} |
@ -0,0 +1,276 @@
@@ -0,0 +1,276 @@
|
||||
package acr.browser.lightning.view; |
||||
|
||||
import android.annotation.SuppressLint; |
||||
import android.content.ActivityNotFoundException; |
||||
import android.content.DialogInterface; |
||||
import android.content.Intent; |
||||
import android.graphics.Bitmap; |
||||
import android.net.MailTo; |
||||
import android.net.http.SslError; |
||||
import android.os.Build; |
||||
import android.os.Message; |
||||
import android.support.annotation.NonNull; |
||||
import android.support.v7.app.AlertDialog; |
||||
import android.text.InputType; |
||||
import android.text.method.PasswordTransformationMethod; |
||||
import android.util.Log; |
||||
import android.webkit.HttpAuthHandler; |
||||
import android.webkit.SslErrorHandler; |
||||
import android.webkit.WebResourceRequest; |
||||
import android.webkit.WebResourceResponse; |
||||
import android.webkit.WebView; |
||||
import android.webkit.WebViewClient; |
||||
import android.widget.EditText; |
||||
import android.widget.LinearLayout; |
||||
|
||||
import com.squareup.otto.Bus; |
||||
|
||||
import java.io.ByteArrayInputStream; |
||||
import java.net.URISyntaxException; |
||||
|
||||
import acr.browser.lightning.R; |
||||
import acr.browser.lightning.activity.BrowserActivity; |
||||
import acr.browser.lightning.app.BrowserApp; |
||||
import acr.browser.lightning.bus.BrowserEvents; |
||||
import acr.browser.lightning.constant.Constants; |
||||
import acr.browser.lightning.utils.AdBlock; |
||||
import acr.browser.lightning.utils.IntentUtils; |
||||
import acr.browser.lightning.utils.ProxyUtils; |
||||
import acr.browser.lightning.utils.Utils; |
||||
|
||||
/** |
||||
* @author Stefano Pacifici based on Anthony C. Restaino's code |
||||
* @date 2015/09/22 |
||||
*/ |
||||
public class LightningWebClient extends WebViewClient { |
||||
|
||||
private final BrowserActivity mActivity; |
||||
private final LightningView mLightningView; |
||||
private final AdBlock mAdBlock; |
||||
private final Bus mEventBus; |
||||
private final IntentUtils mIntentUtils; |
||||
|
||||
LightningWebClient(BrowserActivity activity, LightningView lightningView) { |
||||
mActivity = activity; |
||||
mLightningView = lightningView; |
||||
mAdBlock = AdBlock.getInstance(activity); |
||||
mAdBlock.updatePreference(); |
||||
mEventBus = BrowserApp.getAppComponent().getBus(); |
||||
mIntentUtils = new IntentUtils(activity); |
||||
} |
||||
|
||||
@Override |
||||
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) { |
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
||||
if (mAdBlock.isAd(request.getUrl().toString())) { |
||||
ByteArrayInputStream EMPTY = new ByteArrayInputStream("".getBytes()); |
||||
return new WebResourceResponse("text/plain", "utf-8", EMPTY); |
||||
} |
||||
} |
||||
return super.shouldInterceptRequest(view, request); |
||||
} |
||||
|
||||
@Override |
||||
public WebResourceResponse shouldInterceptRequest(WebView view, String url) { |
||||
if (mAdBlock.isAd(url)) { |
||||
ByteArrayInputStream EMPTY = new ByteArrayInputStream("".getBytes()); |
||||
return new WebResourceResponse("text/plain", "utf-8", EMPTY); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
@SuppressLint("NewApi") |
||||
@Override |
||||
public void onPageFinished(WebView view, String url) { |
||||
if (view.isShown()) { |
||||
mActivity.updateUrl(url, true); |
||||
view.postInvalidate(); |
||||
} |
||||
if (view.getTitle() == null || view.getTitle().isEmpty()) { |
||||
mLightningView.mTitle.setTitle(mActivity.getString(R.string.untitled)); |
||||
} else { |
||||
mLightningView.mTitle.setTitle(view.getTitle()); |
||||
} |
||||
if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT && |
||||
mLightningView.getInvertePage()) { |
||||
view.evaluateJavascript(Constants.JAVASCRIPT_INVERT_PAGE, null); |
||||
} |
||||
mEventBus.post(new BrowserEvents.TabsChanged()); |
||||
} |
||||
|
||||
@Override |
||||
public void onPageStarted(WebView view, String url, Bitmap favicon) { |
||||
if (mLightningView.isShown()) { |
||||
mActivity.updateUrl(url, false); |
||||
mActivity.showActionBar(); |
||||
} |
||||
mEventBus.post(new BrowserEvents.TabsChanged()); |
||||
} |
||||
|
||||
@Override |
||||
public void onReceivedHttpAuthRequest(final WebView view, @NonNull final HttpAuthHandler handler, |
||||
final String host, final String realm) { |
||||
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(mActivity); |
||||
final EditText name = new EditText(mActivity); |
||||
final EditText password = new EditText(mActivity); |
||||
LinearLayout passLayout = new LinearLayout(mActivity); |
||||
passLayout.setOrientation(LinearLayout.VERTICAL); |
||||
|
||||
passLayout.addView(name); |
||||
passLayout.addView(password); |
||||
|
||||
name.setHint(mActivity.getString(R.string.hint_username)); |
||||
name.setSingleLine(); |
||||
password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD); |
||||
password.setSingleLine(); |
||||
password.setTransformationMethod(new PasswordTransformationMethod()); |
||||
password.setHint(mActivity.getString(R.string.hint_password)); |
||||
builder.setTitle(mActivity.getString(R.string.title_sign_in)); |
||||
builder.setView(passLayout); |
||||
builder.setCancelable(true) |
||||
.setPositiveButton(mActivity.getString(R.string.title_sign_in), |
||||
new DialogInterface.OnClickListener() { |
||||
@Override |
||||
public void onClick(DialogInterface dialog, int id) { |
||||
String user = name.getText().toString(); |
||||
String pass = password.getText().toString(); |
||||
handler.proceed(user.trim(), pass.trim()); |
||||
Log.d(Constants.TAG, "Request Login"); |
||||
|
||||
} |
||||
}) |
||||
.setNegativeButton(mActivity.getString(R.string.action_cancel), |
||||
new DialogInterface.OnClickListener() { |
||||
@Override |
||||
public void onClick(DialogInterface dialog, int id) { |
||||
handler.cancel(); |
||||
|
||||
} |
||||
}); |
||||
AlertDialog alert = builder.create(); |
||||
alert.show(); |
||||
|
||||
} |
||||
|
||||
private boolean mIsRunning = false; |
||||
private float mZoomScale = 0.0f; |
||||
|
||||
@SuppressLint("NewApi") |
||||
@Override |
||||
public void onScaleChanged(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) |
||||
return; |
||||
if (Math.abs(mZoomScale - newScale) > 0.01f) { |
||||
mIsRunning = view.postDelayed(new Runnable() { |
||||
|
||||
@Override |
||||
public void run() { |
||||
mZoomScale = newScale; |
||||
view.evaluateJavascript(Constants.JAVASCRIPT_TEXT_REFLOW, null); |
||||
mIsRunning = false; |
||||
} |
||||
|
||||
}, 100); |
||||
} |
||||
|
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void onReceivedSslError(WebView view, @NonNull final SslErrorHandler handler, SslError error) { |
||||
AlertDialog.Builder builder = new AlertDialog.Builder(mActivity); |
||||
builder.setTitle(mActivity.getString(R.string.title_warning)); |
||||
builder.setMessage(mActivity.getString(R.string.message_untrusted_certificate)) |
||||
.setCancelable(true) |
||||
.setPositiveButton(mActivity.getString(R.string.action_yes), |
||||
new DialogInterface.OnClickListener() { |
||||
@Override |
||||
public void onClick(DialogInterface dialog, int id) { |
||||
handler.proceed(); |
||||
} |
||||
}) |
||||
.setNegativeButton(mActivity.getString(R.string.action_no), |
||||
new DialogInterface.OnClickListener() { |
||||
@Override |
||||
public void onClick(DialogInterface dialog, int id) { |
||||
handler.cancel(); |
||||
} |
||||
}); |
||||
AlertDialog alert = builder.create(); |
||||
if (error.getPrimaryError() == SslError.SSL_UNTRUSTED) { |
||||
alert.show(); |
||||
} else { |
||||
handler.proceed(); |
||||
} |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public void onFormResubmission(WebView view, @NonNull final Message dontResend, 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)) |
||||
.setCancelable(true) |
||||
.setPositiveButton(mActivity.getString(R.string.action_yes), |
||||
new DialogInterface.OnClickListener() { |
||||
@Override |
||||
public void onClick(DialogInterface dialog, int id) { |
||||
|
||||
resend.sendToTarget(); |
||||
} |
||||
}) |
||||
.setNegativeButton(mActivity.getString(R.string.action_no), |
||||
new DialogInterface.OnClickListener() { |
||||
@Override |
||||
public void onClick(DialogInterface dialog, int id) { |
||||
|
||||
dontResend.sendToTarget(); |
||||
} |
||||
}); |
||||
AlertDialog alert = builder.create(); |
||||
alert.show(); |
||||
} |
||||
|
||||
@Override |
||||
public boolean shouldOverrideUrlLoading(WebView view, String url) { |
||||
// Check if configured proxy is available
|
||||
if (!ProxyUtils.getInstance().isProxyReady()) { |
||||
// User has been notified
|
||||
return true; |
||||
} |
||||
|
||||
if (mLightningView.mIsIncognitoTab) { |
||||
return super.shouldOverrideUrlLoading(view, url); |
||||
} |
||||
if (url.startsWith("about:")) { |
||||
return super.shouldOverrideUrlLoading(view, url); |
||||
} |
||||
if (url.contains("mailto:")) { |
||||
MailTo mailTo = MailTo.parse(url); |
||||
Intent i = Utils.newEmailIntent(mailTo.getTo(), mailTo.getSubject(), |
||||
mailTo.getBody(), mailTo.getCc()); |
||||
mActivity.startActivity(i); |
||||
view.reload(); |
||||
return true; |
||||
} else if (url.startsWith("intent://")) { |
||||
Intent intent; |
||||
try { |
||||
intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); |
||||
} catch (URISyntaxException ex) { |
||||
return false; |
||||
} |
||||
if (intent != null) { |
||||
try { |
||||
mActivity.startActivity(intent); |
||||
} catch (ActivityNotFoundException e) { |
||||
Log.e(Constants.TAG, "ActivityNotFoundException"); |
||||
} |
||||
return true; |
||||
} |
||||
} |
||||
return mIntentUtils.startActivityForUrl(view, url); |
||||
} |
||||
} |
Loading…
Reference in new issue