001package com.gigya.android.sdk.ui.plugin; 002 003import android.graphics.Bitmap; 004import android.net.Uri; 005import android.os.Build; 006import android.support.annotation.NonNull; 007import android.support.annotation.RequiresApi; 008import android.webkit.WebResourceError; 009import android.webkit.WebResourceRequest; 010import android.webkit.WebView; 011import android.webkit.WebViewClient; 012 013import com.gigya.android.sdk.ui.Presenter; 014import com.gigya.android.sdk.utils.ObjectUtils; 015import com.gigya.android.sdk.utils.UrlUtils; 016 017import java.util.HashMap; 018import java.util.Map; 019 020public class GigyaPluginWebViewClient extends WebViewClient { 021 022 final private IGigyaPluginWebViewClientInteractions _interactions; 023 024 public GigyaPluginWebViewClient(@NonNull IGigyaPluginWebViewClientInteractions interactions) { 025 _interactions = interactions; 026 } 027 028 @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) 029 @Override 030 public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { 031 Uri uri = request.getUrl(); 032 overrideUrlLoad(uri); 033 return true; 034 } 035 036 @Override 037 public boolean shouldOverrideUrlLoading(WebView view, String urlString) { 038 Uri uri = Uri.parse(urlString); 039 overrideUrlLoad(uri); 040 return true; 041 } 042 043 @Override 044 public void onPageStarted(WebView view, String url, Bitmap favicon) { 045 _interactions.onPageStarted(); 046 } 047 048 @RequiresApi(api = Build.VERSION_CODES.M) 049 @Override 050 public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { 051 Map<String, Object> eventMap = new HashMap<>(); 052 eventMap.put("eventName", "error"); 053 eventMap.put("errorCode", error.getErrorCode()); 054 eventMap.put("description", error.getDescription()); 055 eventMap.put("dismiss", true); 056 _interactions.onPageError(new GigyaPluginEvent(eventMap)); 057 } 058 059 private void overrideUrlLoad(final Uri uri) { 060 final Map<String, Object> eventMap = new HashMap<>(); 061 final String uriString = uri.toString(); 062 if (isJSLoadError(uri)) { 063 eventMap.put("eventName", "error"); 064 eventMap.put("description", "Failed loading socialize.js"); 065 eventMap.put("errorCode", 500032); 066 eventMap.put("dismiss", true); 067 _interactions.onPageError(new GigyaPluginEvent(eventMap)); 068 } else if (isJSException(uri)) { 069 eventMap.put("eventName", "error"); 070 eventMap.put("errorCode", 405001); 071 eventMap.put("description", "Javascript error while loading plugin. Please make sure the plugin name is correct."); 072 eventMap.put("dismiss", true); 073 _interactions.onPageError(new GigyaPluginEvent(eventMap)); 074 } else if (!_interactions.onUrlInvoke(uriString)) { 075 _interactions.onBrowserIntent(uri); 076 } 077 } 078 079 private boolean isJSLoadError(final Uri uri) { 080 return UrlUtils.isGigyaScheme(uri.getScheme()) && ObjectUtils.safeEquals(uri.getHost(), Presenter.Consts.ON_JS_LOAD_ERROR); 081 } 082 083 private boolean isJSException(final Uri uri) { 084 return UrlUtils.isGigyaScheme(uri.getScheme()) && ObjectUtils.safeEquals(uri.getHost(), Presenter.Consts.ON_JS_EXCEPTION); 085 } 086} 087 088interface IGigyaPluginWebViewClientInteractions { 089 void onPageStarted(); 090 091 void onPageError(GigyaPluginEvent errorEvent); 092 093 boolean onUrlInvoke(final String url); 094 095 void onBrowserIntent(final Uri uri); 096} 097 098