hpsaturn

SocialWebClient

Nov 15th, 2012
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.53 KB | None | 0 0
  1. /**
  2.     CONFIG VARS:
  3.     public static final String PatternFBLogin      = "login";
  4.     public static final String PatternFBSendLogin  = "skip_api_login";
  5.     public static final String PatternFBShareLogin = "FBSuccessLogin";
  6.     public static final String PatternTWShareLogin = "TWSuccessLogin";
  7.         public static final String PatternGetUserId    = "mt://fbconnect/close/?userId=";
  8.     public static final String PatternErrorCode    = "mt://fbconnect/close/?errorCode=";
  9. **/
  10.  
  11.  
  12. public class SocialWebClient extends WebViewClient{
  13.  
  14.     public static final String TAG = SocialWebClient.class.getSimpleName();
  15.     private static final boolean DEBUG = Config.DEBUG;
  16.    
  17.     private static final Pattern patternUserId    = Pattern.compile( ".*(userId)+=([0-9]+)$" );
  18.     private static final Pattern patternErrorCode = Pattern.compile( ".*(errorMessage)+=(\\w.*)$" );
  19.    
  20.     private boolean loadingFinished;
  21.     private boolean redirect;
  22.     public boolean userFbReady;
  23.     public boolean errorCodeReady;
  24.     public boolean userTwReady;
  25.     private boolean userIdReady;
  26.     private WebView webview;
  27.  
  28.     private ProgressDialog loader;
  29.     private OnSocialWebClient listener;
  30.    
  31.    
  32.     public SocialWebClient(WebView webview,OnSocialWebClient listener,ProgressDialog loader) {
  33.         this.webview=webview;
  34.         this.loader=loader;
  35.         this.listener=listener;
  36.     }
  37.    
  38.     public interface OnSocialWebClient {
  39.         void onFacebookShareReady();
  40.         void onTwitterShareReady();
  41.         void onFacebookLoginReady(String userId);
  42.         void onSeviceError(String msg);
  43.     }
  44.  
  45.     @Override
  46.     public boolean shouldOverrideUrlLoading(WebView view, String url) {
  47.        
  48.         if (!loadingFinished)redirect = true;
  49.  
  50.         view.loadUrl(url);            
  51.         if(DEBUG)Log.d(TAG,"[WEB VIEW] shouldOverrideUrlLoading: URL: "+url);
  52.        
  53.        
  54.         if(searchSubString(url, Config.PatternGetUserId)){
  55.             if(DEBUG)Log.d(TAG,"[WEB VIEW] FACEBOOK USER READY");
  56.             webview.stopLoading();
  57.             webview.clearCache(true);
  58.             String userId = getUserId(url);
  59.            
  60.             if(DEBUG)Log.d(TAG,"[WEB VIEW] USERID: "+userId);
  61.             listener.onFacebookLoginReady(userId);
  62.         }
  63.        
  64.         if(searchSubString(url, Config.PatternFBShareLogin)){
  65.             if(DEBUG)Log.d(TAG,"[WEB VIEW] FACEBOOK SHARE READY");
  66.             webview.stopLoading();
  67.             webview.clearCache(true);
  68.  
  69.             listener.onFacebookShareReady();      
  70.         }
  71.        
  72.         if(searchSubString(url, Config.PatternTWShareLogin)){
  73.             if(DEBUG)Log.d(TAG,"[WEB VIEW] TWITTER SHARE READY");
  74.             webview.stopLoading();
  75.             webview.clearCache(true);
  76.  
  77.             listener.onTwitterShareReady();
  78.         }
  79.        
  80.         if(searchSubString(url, Config.PatternErrorCode)){
  81.             webview.stopLoading();
  82.             errorCodeReady=false;
  83.             String msg = getMsg(url);
  84.            
  85.             if(DEBUG)Log.d(TAG,"[WEB VIEW] ERROR MSG: "+msg);
  86.             listener.onSeviceError(msg);
  87.         }
  88.  
  89.         return true;
  90.     }
  91.  
  92.     @Override
  93.     public void onPageStarted(WebView view, String url, Bitmap favicon) {
  94.         if (DEBUG)Log.d(TAG, "[WEB VIEW] onPageStarted: URL: " + url);
  95.         loadingFinished = false;
  96.         showLoader();
  97.         super.onPageStarted(view, url, favicon);
  98.     }
  99.  
  100.     @Override
  101.     public void onPageFinished(WebView view, String url) {
  102.         if (DEBUG)Log.d(TAG, "[WEB VIEW] onPageFinished: URL: " + url);
  103.  
  104.         if (!redirect) loadingFinished = true;
  105.  
  106.         if (loadingFinished && !redirect) {
  107.             dismissLoader();
  108.         } else {
  109.             redirect = false;
  110.         }
  111.         super.onPageFinished(view, url);
  112.     }
  113.    
  114.     @Override
  115.     public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
  116.         if(DEBUG)Log.d(TAG,"[WEB VIEW] shouldInterceptRequest: URL: "+url);
  117.         return super.shouldInterceptRequest(view, url);
  118.     }
  119.    
  120.     private boolean searchSubString (String full, String pattern){
  121.         return full.toUpperCase().indexOf(pattern.toUpperCase()) != -1;
  122.     }
  123.    
  124.     private String getUserId(String url) {
  125.         final Matcher m = patternUserId.matcher(url);
  126.         if ( m.matches())return m.group(2);
  127.         return null;
  128.     }
  129.    
  130.     private String getMsg(String url) {
  131.         final Matcher m = patternErrorCode.matcher(URLDecoder.decode(url));
  132.         if ( m.matches())return m.group(2);
  133.         return null;
  134.     }
  135.    
  136. }
Advertisement
Add Comment
Please, Sign In to add comment