Created
July 13, 2017 08:53
-
-
Save showsky/e5260847ccf176b6200aa914ff0d0c16 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import android.content.Context; | |
| import android.content.Intent; | |
| import android.net.Uri; | |
| import android.util.AttributeSet; | |
| import android.util.TypedValue; | |
| import android.view.View; | |
| import android.view.ViewGroup; | |
| import android.webkit.WebResourceError; | |
| import android.webkit.WebResourceRequest; | |
| import android.webkit.WebSettings; | |
| import android.webkit.WebView; | |
| import android.webkit.WebViewClient; | |
| import java.util.regex.Matcher; | |
| import java.util.regex.Pattern; | |
| /** | |
| * Created by showsky on 12/07/2017. | |
| */ | |
| final public class SitemajiAdView extends WebView { | |
| private final static String TAG = SitemajiAdView.class.getSimpleName(); | |
| private WebViewClient mWebViewClient = new WebViewClient() { | |
| @Override | |
| public void onPageFinished(WebView view, String url) { | |
| super.onPageFinished(view, url); | |
| Pattern pattern = Pattern.compile(".*\\?model=(\\d+)x(\\d+)", Pattern.CASE_INSENSITIVE); | |
| Matcher matcher= pattern.matcher(url); | |
| int width = 0; | |
| int high = 0; | |
| if (matcher.find() && matcher.groupCount() == 2) { | |
| try { | |
| width = Integer.parseInt(matcher.group(1)); | |
| high = Integer.parseInt(matcher.group(2)); | |
| } catch (NumberFormatException e) {} | |
| } | |
| ViewGroup.LayoutParams params = view.getLayoutParams(); | |
| params.width = (int) TypedValue.applyDimension( | |
| TypedValue.COMPLEX_UNIT_DIP, | |
| width, | |
| getResources().getDisplayMetrics() | |
| ); | |
| params.height = (int) TypedValue.applyDimension( | |
| TypedValue.COMPLEX_UNIT_DIP, | |
| high, | |
| getResources().getDisplayMetrics() | |
| ); | |
| view.setLayoutParams(params); | |
| } | |
| @Override | |
| public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { | |
| super.onReceivedError(view, request, error); | |
| } | |
| @Override | |
| public boolean shouldOverrideUrlLoading(WebView view, String url) { | |
| Intent intent = new Intent(Intent.ACTION_VIEW); | |
| intent.setData(Uri.parse(url)); | |
| view.getContext().startActivity(intent); | |
| return true; | |
| } | |
| }; | |
| public SitemajiAdView(Context context) { | |
| super(context); | |
| initWebView(); | |
| } | |
| public SitemajiAdView(Context context, AttributeSet attrs) { | |
| super(context, attrs); | |
| initWebView(); | |
| } | |
| public SitemajiAdView(Context context, AttributeSet attrs, int defStyleAttr) { | |
| super(context, attrs, defStyleAttr); | |
| initWebView(); | |
| } | |
| private void initWebView() { | |
| WebSettings setting = getSettings(); | |
| setting.setJavaScriptEnabled(true); | |
| setting.setCacheMode(WebSettings.LOAD_NO_CACHE); | |
| setLayerType(View.LAYER_TYPE_SOFTWARE, null); | |
| setHorizontalScrollBarEnabled(false); | |
| setVerticalScrollBarEnabled(false); | |
| setWebViewClient(mWebViewClient); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment