Skip to content

Instantly share code, notes, and snippets.

@snadjafi
Created November 7, 2013 23:29
Show Gist options
  • Select an option

  • Save snadjafi/7363682 to your computer and use it in GitHub Desktop.

Select an option

Save snadjafi/7363682 to your computer and use it in GitHub Desktop.
package com.kuapay.library.activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.webkit.CookieManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.kuapay.library.KuapayLibrary;
import com.kuapay.library.R;
import com.kuapay.library.util.KuaUtils;
import com.kuapay.library.util.Kualog;
import java.util.HashMap;
public class FacebookLinkBrowserActivity extends KuaActivity
{
//region Constant
private static final String DEBUG_TAG = "[FacebookLinkBrowserActivity]";
public static final String AUTH_URL_TAG = "auth_url_tag";
//endregion
//region Variables
private WebView facebookWebView;
private TextView headerTextView;
private ImageButton backImageButton;
private LinearLayout loadingLinearLayout;
//endregion
//region Listeners
private View.OnClickListener backArrowImageButtonClickListener = new View.OnClickListener()
{
@Override
public void onClick(View view)
{
onBackPressed();
}
};
//endregion
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_social_link);
bindUIElements();
setUpListeners();
setUpFacebookWebView();
setUpLoadingView();
}
@Override
public void onBackPressed()
{
facebookWebView.stopLoading();
KuapayLibrary.socialProvider.callbackFailureFacebookLinkedResponse("Facebook not linked");
finish();
}
@Override
public void onLoggedOut()
{
finish();
}
private void bindUIElements()
{
facebookWebView = (WebView) findViewById(R.id.social_link_webView);
backImageButton = (ImageButton) findViewById(R.id.social_link_back_arrow_imageButton);
headerTextView = (TextView) findViewById(R.id.view_header_textView);
loadingLinearLayout = (LinearLayout) findViewById(R.id.social_link_loading);
}
private void setUpListeners()
{
backImageButton.setOnClickListener(backArrowImageButtonClickListener);
}
private void setUpFacebookWebView()
{
headerTextView.setText(getString(R.string.txt_link_facebook));
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
facebookWebView.getSettings().setJavaScriptEnabled(true);
facebookWebView.setWebViewClient(new FacebookWebViewClient());
facebookWebView.addJavascriptInterface(new CustomJavaScriptInterface(), "HTMLOUT");
facebookWebView.loadUrl(getIntent().getStringExtra(AUTH_URL_TAG));
}
private void setUpLoadingView()
{
TextView titleTextView = (TextView) findViewById(R.id.loading_dialog_text);
titleTextView.setText(getString(R.string.txt_linking_facebook));
ImageView loadingMinutesImageView = (ImageView) findViewById(R.id.loading_dialog_minutes_imageView);
Animation rotation = AnimationUtils.loadAnimation(FacebookLinkBrowserActivity.this, R.anim.rotate_indefinitely);
rotation.setRepeatCount(Animation.INFINITE);
loadingMinutesImageView.startAnimation(rotation);
ImageView loadingHoursImageView = (ImageView) findViewById(R.id.loading_dialog_hours_imageView);
Animation slowRotation = AnimationUtils.loadAnimation(FacebookLinkBrowserActivity.this, R.anim.rotate_indefinitely);
slowRotation.setDuration(3000*12);
slowRotation.setRepeatCount(Animation.INFINITE);
loadingHoursImageView.startAnimation(slowRotation);
}
private class FacebookWebViewClient extends WebViewClient
{
@Override
public void onPageFinished(WebView view, String url)
{
Kualog.print(DEBUG_TAG, "FacebookWebViewClient onPageFinished() url = " + url);
if (url.startsWith("https://www.kuapay.com") || url.startsWith("http://www.kuapay.com"))
{
try
{
HashMap<String, String> urlParams = KuaUtils.getUrlParams(url);
if (urlParams.containsKey("code"))
{
facebookWebView.loadUrl(KuapayLibrary.urlProvider.getFacebookAccessTokenUrl(urlParams.get("code")));
loadingLinearLayout.setVisibility(View.VISIBLE);
}
else if(urlParams.containsKey("error"))
{
onBackPressed();
}
}
catch (Exception e)
{
Kualog.printException(DEBUG_TAG, "FacebookWebViewClient.onPageFinished()",e);
onBackPressed();
}
}
else if(url.startsWith("https://graph.facebook.com/"))
{
view.loadUrl("javascript:window.HTMLOUT.showHTML('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
loadingLinearLayout.setVisibility(View.VISIBLE);
}
// else if(url.contains("error=access_denied"))
// {
// onBackPressed();
// }
else
{
loadingLinearLayout.setVisibility(View.GONE);
}
}
}
private class CustomJavaScriptInterface
{
@SuppressWarnings("unused")
public void showHTML(String html)
{
//todo : refactor!!
if(html.contains("access_token=") && html.contains("expires=")) //you still receive access_token & expires if permissions are denied
{
String body = html.substring(html.indexOf("access_token="), html.length()-1);
body = body.substring(13, body.indexOf("<"));
String accessToken = body.substring(0, body.indexOf("&"));
String expires = body.substring(accessToken.length() + "access_token=".length());
KuapayLibrary.socialProvider.setFacebookAuthentication(accessToken, expires);
KuapayLibrary.socialProvider.callbackSuccessFacebookLinkedResponse("Facebook linked");
finish();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment