Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- CONFIG VARS:
- public static final String PatternFBLogin = "login";
- public static final String PatternFBSendLogin = "skip_api_login";
- public static final String PatternFBShareLogin = "FBSuccessLogin";
- public static final String PatternTWShareLogin = "TWSuccessLogin";
- public static final String PatternGetUserId = "mt://fbconnect/close/?userId=";
- public static final String PatternErrorCode = "mt://fbconnect/close/?errorCode=";
- **/
- public class SocialWebClient extends WebViewClient{
- public static final String TAG = SocialWebClient.class.getSimpleName();
- private static final boolean DEBUG = Config.DEBUG;
- private static final Pattern patternUserId = Pattern.compile( ".*(userId)+=([0-9]+)$" );
- private static final Pattern patternErrorCode = Pattern.compile( ".*(errorMessage)+=(\\w.*)$" );
- private boolean loadingFinished;
- private boolean redirect;
- public boolean userFbReady;
- public boolean errorCodeReady;
- public boolean userTwReady;
- private boolean userIdReady;
- private WebView webview;
- private ProgressDialog loader;
- private OnSocialWebClient listener;
- public SocialWebClient(WebView webview,OnSocialWebClient listener,ProgressDialog loader) {
- this.webview=webview;
- this.loader=loader;
- this.listener=listener;
- }
- public interface OnSocialWebClient {
- void onFacebookShareReady();
- void onTwitterShareReady();
- void onFacebookLoginReady(String userId);
- void onSeviceError(String msg);
- }
- @Override
- public boolean shouldOverrideUrlLoading(WebView view, String url) {
- if (!loadingFinished)redirect = true;
- view.loadUrl(url);
- if(DEBUG)Log.d(TAG,"[WEB VIEW] shouldOverrideUrlLoading: URL: "+url);
- if(searchSubString(url, Config.PatternGetUserId)){
- if(DEBUG)Log.d(TAG,"[WEB VIEW] FACEBOOK USER READY");
- webview.stopLoading();
- webview.clearCache(true);
- String userId = getUserId(url);
- if(DEBUG)Log.d(TAG,"[WEB VIEW] USERID: "+userId);
- listener.onFacebookLoginReady(userId);
- }
- if(searchSubString(url, Config.PatternFBShareLogin)){
- if(DEBUG)Log.d(TAG,"[WEB VIEW] FACEBOOK SHARE READY");
- webview.stopLoading();
- webview.clearCache(true);
- listener.onFacebookShareReady();
- }
- if(searchSubString(url, Config.PatternTWShareLogin)){
- if(DEBUG)Log.d(TAG,"[WEB VIEW] TWITTER SHARE READY");
- webview.stopLoading();
- webview.clearCache(true);
- listener.onTwitterShareReady();
- }
- if(searchSubString(url, Config.PatternErrorCode)){
- webview.stopLoading();
- errorCodeReady=false;
- String msg = getMsg(url);
- if(DEBUG)Log.d(TAG,"[WEB VIEW] ERROR MSG: "+msg);
- listener.onSeviceError(msg);
- }
- return true;
- }
- @Override
- public void onPageStarted(WebView view, String url, Bitmap favicon) {
- if (DEBUG)Log.d(TAG, "[WEB VIEW] onPageStarted: URL: " + url);
- loadingFinished = false;
- showLoader();
- super.onPageStarted(view, url, favicon);
- }
- @Override
- public void onPageFinished(WebView view, String url) {
- if (DEBUG)Log.d(TAG, "[WEB VIEW] onPageFinished: URL: " + url);
- if (!redirect) loadingFinished = true;
- if (loadingFinished && !redirect) {
- dismissLoader();
- } else {
- redirect = false;
- }
- super.onPageFinished(view, url);
- }
- @Override
- public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
- if(DEBUG)Log.d(TAG,"[WEB VIEW] shouldInterceptRequest: URL: "+url);
- return super.shouldInterceptRequest(view, url);
- }
- private boolean searchSubString (String full, String pattern){
- return full.toUpperCase().indexOf(pattern.toUpperCase()) != -1;
- }
- private String getUserId(String url) {
- final Matcher m = patternUserId.matcher(url);
- if ( m.matches())return m.group(2);
- return null;
- }
- private String getMsg(String url) {
- final Matcher m = patternErrorCode.matcher(URLDecoder.decode(url));
- if ( m.matches())return m.group(2);
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment