Android Open Source - userapp-android O Auth Fragment






From Project

Back to project page userapp-android.

License

The source code is released under:

MIT License

If you think the Android project userapp-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package io.userapp.client.android;
/*  www .j  a  v a 2  s .  c  o m*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class OAuthFragment extends Fragment {

  private WebView webView;
  private UserApp.Session session = null;
  private UserApp.Session.StatusCallback callback = null;
  private String url = null;
  private FragmentActivity activity = null;
  private OAuthFragment self = this;
  
  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    WebViewRelativeLayout popWindow = new WebViewRelativeLayout(this.getActivity());
    
      webView = popWindow.getTheWebView();
      
      webView.setWebViewClient(new WebViewClient() {
          public void onPageStarted(WebView view, String url, Bitmap favicon) {
            if (url.startsWith(UserApp.OAUTH_REDIRECT_URI)) {
              // Hide the webview
              FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
                  transaction.remove(self);
                  transaction.commit();
              
              // Parse the token
              Pattern regex = Pattern.compile("ua\\_token=([a-z0-9\\-\\_]+)$", Pattern.CASE_INSENSITIVE);
              Matcher matcher = regex.matcher(url);
              
              if (matcher.find()) {
                    String token = matcher.group(1);
                    
                    // Login using the token
                    session.loginWithToken(token, callback);
                } else {
                  callback.call(false, new Exception("UserApp token is missing."));
                }
            }
          }
      });
      
      if (this.url != null) {
        webView.loadUrl(url);
      }
      
        return popWindow;
    }
  
  public void authorize(String url, UserApp.Session.StatusCallback callback) {
      this.url = url;
      this.callback = callback;
      
      // Show the webview
      FragmentTransaction transaction = this.activity.getSupportFragmentManager().beginTransaction();
        transaction.add(android.R.id.content, this, "oauth_fragment");
        transaction.addToBackStack(null);
        transaction.commit();
    }
  
  public void setSession(UserApp.Session session) {
      this.session = session;
    }
  
  public UserApp.Session getSession() {
      return this.session;
    }
  
  public void setActivity(FragmentActivity activity) {
      this.activity = activity;
    }
  
}




Java Source Code List

com.example.demo.LoginFragment.java
com.example.demo.MainActivity.java
com.example.demo.MainFragment.java
com.example.demo.SignupFragment.java
io.userapp.client.JsonHelper.java
io.userapp.client.UserAppClient.java
io.userapp.client.android.AuthFragment.java
io.userapp.client.android.Feature.java
io.userapp.client.android.Lock.java
io.userapp.client.android.OAuthFragment.java
io.userapp.client.android.Permission.java
io.userapp.client.android.Property.java
io.userapp.client.android.Subscription.java
io.userapp.client.android.UserApp.java
io.userapp.client.android.User.java
io.userapp.client.android.WebViewRelativeLayout.java
io.userapp.client.exceptions.InvalidMethodException.java
io.userapp.client.exceptions.InvalidServiceException.java
io.userapp.client.exceptions.ServiceException.java
io.userapp.client.exceptions.TransportException.java
io.userapp.client.exceptions.UserAppException.java
io.userapp.client.rest.RestfulContext.java
io.userapp.client.rest.Restful.java
io.userapp.client.rest.UserCredentials.java
io.userapp.client.rest.core.HttpHeaderCollection.java
io.userapp.client.rest.core.HttpMethodType.java
io.userapp.client.rest.core.HttpProtocolVersionType.java
io.userapp.client.rest.core.HttpResponseHead.java
io.userapp.client.rest.core.HttpResponseStatusHead.java
io.userapp.client.rest.core.HttpResponse.java
io.userapp.client.rest.core.HttpStatusCode.java