Created
April 27, 2016 15:11
-
-
Save nguyenkims/28f3dc9d2414c3184263fab52b93351e to your computer and use it in GitHub Desktop.
webview example that handles facebook comments
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
<?xml version="1.0" encoding="utf-8"?> | |
<FrameLayout | |
android:id="@+id/container" | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
</FrameLayout> |
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
package ovh.meo.mypackage; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.net.Uri; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.os.Message; | |
import android.util.Log; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.webkit.CookieManager; | |
import android.webkit.CookieSyncManager; | |
import android.webkit.WebChromeClient; | |
import android.webkit.WebSettings; | |
import android.webkit.WebView; | |
import android.webkit.WebViewClient; | |
import android.widget.FrameLayout; | |
public class WebViewActivity extends Activity { | |
private static final String TAG = WebViewActivity.class.getSimpleName(); | |
protected WebView mainWebView; | |
private Context mContext; | |
private WebView mWebviewPop; | |
private FrameLayout mContainer; | |
// the url that points to the page where the facebook comments plugin is on | |
private String url = "http://7d2fd1cd.ngrok.io/fb.html";// "http://stage.example.com"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_web_view); | |
mContainer = (FrameLayout) findViewById(R.id.container); | |
mContext = this.getApplicationContext(); | |
// if you want to make sure that we need to log in facebook again ... | |
// clearCookies(this); | |
CookieManager cookieManager = CookieManager.getInstance(); | |
cookieManager.setAcceptCookie(true); | |
mainWebView = getWebView(mContext); | |
// handle the opening of facebook | |
mainWebView.setWebChromeClient(new MyCustomChromeClient()); | |
mainWebView.loadUrl(url); | |
mContainer.addView(mainWebView); | |
} | |
/** | |
* get webview with all necessary properties to support facebook login | |
*/ | |
WebView getWebView(Context context) { | |
WebView wv = new WebView(context); | |
wv.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, | |
ViewGroup.LayoutParams.MATCH_PARENT)); | |
wv.clearCache(true); | |
// Settings for the webview | |
WebSettings webSettings = wv.getSettings(); | |
webSettings.setJavaScriptEnabled(true); | |
webSettings.setAppCacheEnabled(true); | |
webSettings.setDomStorageEnabled(true); | |
webSettings.setJavaScriptCanOpenWindowsAutomatically(true); | |
webSettings.setSupportMultipleWindows(true); | |
if (Build.VERSION.SDK_INT >= 21) { | |
webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); | |
CookieManager.getInstance().setAcceptThirdPartyCookies(wv, true); | |
} | |
return wv; | |
} | |
private class MyCustomChromeClient extends WebChromeClient { | |
@Override | |
public boolean onCreateWindow(WebView view, boolean isDialog, | |
boolean isUserGesture, Message resultMsg) { | |
mWebviewPop = getWebView(mContext); | |
mWebviewPop.setWebViewClient(new WebViewClient() { | |
@Override | |
public boolean shouldOverrideUrlLoading(WebView view, String url) { | |
String host = Uri.parse(url).getHost(); | |
Log.d(TAG, "host=" + host); | |
if (host.contains("facebook.com")) { | |
view.loadUrl(url); | |
return true; | |
} | |
// Otherwise, the link is not for a page on my site, so launch | |
// another Activity that handles URLs | |
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); | |
startActivity(intent); | |
return false; | |
} | |
}); | |
mWebviewPop.setWebChromeClient(new MyCustomChromeClient()); | |
mContainer.addView(mWebviewPop); | |
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj; | |
transport.setWebView(mWebviewPop); | |
resultMsg.sendToTarget(); | |
return true; | |
} | |
@Override | |
public void onCloseWindow(WebView window) { | |
// facebook login finishes, close the facebook window and return to the main one | |
Log.d("onCloseWindow", "called"); | |
mWebviewPop.setVisibility(View.GONE); | |
mContainer.removeView(mWebviewPop); | |
mWebviewPop = null; | |
} | |
} | |
@SuppressWarnings("deprecation") | |
public static void clearCookies(Context context) { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) { | |
Log.d(TAG, "Using clearCookies code for API >=" + String.valueOf(Build.VERSION_CODES.LOLLIPOP_MR1)); | |
CookieManager.getInstance().removeAllCookies(null); | |
CookieManager.getInstance().flush(); | |
} else { | |
Log.d(TAG, "Using clearCookies code for API <" + String.valueOf(Build.VERSION_CODES.LOLLIPOP_MR1)); | |
CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(context); | |
cookieSyncMngr.startSync(); | |
CookieManager cookieManager = CookieManager.getInstance(); | |
cookieManager.removeAllCookie(); | |
cookieManager.removeSessionCookie(); | |
cookieSyncMngr.stopSync(); | |
cookieSyncMngr.sync(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment