Advertisement
Guest User

Android Webview and Facebook Login Not working

a guest
May 21st, 2014
13,179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.app.example;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.net.Uri;
  7. import android.net.http.SslError;
  8. import android.os.Bundle;
  9. import android.app.Activity;
  10. import android.os.Handler;
  11. import android.os.Message;
  12. import android.util.Log;
  13. import android.view.KeyEvent;
  14. import android.view.Menu;
  15. import android.view.View;
  16. import android.view.ViewGroup;
  17. import android.webkit.*;
  18. import android.widget.FrameLayout;
  19. import android.widget.ProgressBar;
  20. import android.app.ProgressDialog;
  21.  
  22. public class ExampleMainActivity extends Activity {
  23.  
  24.     protected WebView mainWebView;
  25.     private ProgressBar mProgress;
  26.     private Context mContext;
  27.     private WebView mWebview;
  28.     private WebView mWebviewPop;
  29.     private FrameLayout mContainer;
  30.  
  31.     private String url = "http://stage.example.com";
  32.     private String target_url_prefix = "stage.example.com";
  33.  
  34.  
  35.     public void onBackPressed(){
  36.  
  37.         if (mainWebView.isFocused() && mainWebView.canGoBack()) {
  38.             mainWebView.goBack();
  39.         }
  40.         else {
  41.             super.onBackPressed();
  42.             finish();
  43.         }
  44.     }
  45.  
  46.     @Override
  47.     protected void onCreate(Bundle savedInstanceState) {
  48.         super.onCreate(savedInstanceState);
  49.         setContentView(R.layout.activity_main);
  50.  
  51.         //Get main webview
  52.         mainWebView = (WebView) findViewById(R.id.mainWebView);
  53.  
  54.         //Progress Bar
  55.         mProgress = (ProgressBar) findViewById(R.id.progressBar);
  56.  
  57.         //Cookie manager for the webview
  58.         CookieManager cookieManager = CookieManager.getInstance();
  59.         cookieManager.setAcceptCookie(true);
  60.  
  61.         //Get outer container
  62.         mContainer = (FrameLayout) findViewById(R.id.webview_frame);
  63.  
  64.         //Settings
  65.         WebSettings webSettings = mainWebView.getSettings();
  66.         webSettings.setJavaScriptEnabled(true);
  67.         webSettings.setAppCacheEnabled(true);
  68.         webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
  69.         webSettings.setSupportMultipleWindows(true);
  70.  
  71.         mainWebView.setWebViewClient(new MyCustomWebViewClient());
  72.         mainWebView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
  73.  
  74.         mainWebView.setWebChromeClient(new MyCustomChromeClient());
  75.         mainWebView.loadUrl("http://stage.example.com/home");
  76.  
  77.         mContext=this.getApplicationContext();
  78.  
  79.     }
  80.  
  81.  
  82.     @Override
  83.     public boolean onCreateOptionsMenu(Menu menu) {
  84.         // Inflate the menu; this adds items to the action bar if it is present.
  85.         getMenuInflater().inflate(R.menu.example_main, menu);
  86.         return true;
  87.     }
  88.  
  89.     private class MyCustomWebViewClient extends WebViewClient {
  90.         @Override
  91.         public boolean shouldOverrideUrlLoading(WebView view, String url) {
  92.             String host = Uri.parse(url).getHost();
  93.             //Log.d("shouldOverrideUrlLoading", url);
  94.             if (host.equals(target_url_prefix))
  95.             {
  96.                 // This is my web site, so do not override; let my WebView load
  97.                 // the page
  98.                 if(mWebviewPop!=null)
  99.                 {
  100.                     mWebviewPop.setVisibility(View.GONE);
  101.                     mContainer.removeView(mWebviewPop);
  102.                     mWebviewPop=null;
  103.                 }
  104.                 return false;
  105.             }
  106.  
  107.             if(host.equals("m.facebook.com") || host.equals("www.facebook.com"))
  108.             {
  109.                 return false;
  110.             }
  111.             // Otherwise, the link is not for a page on my site, so launch
  112.             // another Activity that handles URLs
  113.             Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
  114.             startActivity(intent);
  115.             return true;
  116.         }
  117.  
  118.         @Override
  119.         public void onReceivedSslError(WebView view, SslErrorHandler handler,
  120.                                        SslError error) {
  121.             Log.d("onReceivedSslError", "onReceivedSslError");
  122.             //super.onReceivedSslError(view, handler, error);
  123.         }
  124.  
  125.     }
  126.  
  127.     private class MyCustomChromeClient extends WebChromeClient
  128.     {
  129.  
  130.         @Override
  131.         public boolean onCreateWindow(WebView view, boolean isDialog,
  132.         boolean isUserGesture, Message resultMsg) {
  133.             mWebviewPop = new WebView(mContext);
  134.             mWebviewPop.setVerticalScrollBarEnabled(false);
  135.             mWebviewPop.setHorizontalScrollBarEnabled(false);
  136.             mWebviewPop.setWebViewClient(new MyCustomWebViewClient());
  137.             mWebviewPop.getSettings().setJavaScriptEnabled(true);
  138.             mWebviewPop.getSettings().setSavePassword(false);
  139.             mWebviewPop.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
  140.                     ViewGroup.LayoutParams.MATCH_PARENT));
  141.             mContainer.addView(mWebviewPop);
  142.             WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
  143.             transport.setWebView(mWebviewPop);
  144.             resultMsg.sendToTarget();
  145.  
  146.             return true;
  147.         }
  148.  
  149.         @Override
  150.         public void onCloseWindow(WebView window) {
  151.             Log.d("onCloseWindow", "called");
  152.         }
  153.  
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement