Android Open Source - MobileConnectTestApp Display Identity Website Activity






From Project

Back to project page MobileConnectTestApp.

License

The source code is released under:

MIT License

If you think the Android project MobileConnectTestApp 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 com.gsma.android.xoperatorapidemo.activity.identity;
//from w ww .j a v  a2s  .  c  o m
import java.util.HashMap;
import java.util.UUID;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.gsma.android.mobileconnecttest.R;
import com.gsma.android.xoperatorapidemo.utils.HttpUtils;
import com.gsma.android.xoperatorapidemo.utils.ParameterList;

@SuppressLint("SetJavaScriptEnabled")
public class DisplayIdentityWebsiteActivity extends Activity {
  private static final String TAG = "DisplayIdentityWebsiteActivity";

  String authUri; //
  String tokenUri; //
  String userinfoUri; //
  String clientId; //
  String clientSecret; //
  String scopes; // 
  String returnUri; // 
  
  String secret;

  Activity initiator;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_identity_webview);
    
    initiator=this;
  }
  
  public void onStart() {
    super.onStart();//
    
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
      /*
       * Extract the parameters from the bundle provided
       */
      authUri = extras.getString("authUri");
      tokenUri = extras.getString("tokenUri");
      userinfoUri = extras.getString("userinfoUri");
      clientId = extras.getString("clientId");
      clientSecret = extras.getString("clientSecret");
      scopes = extras.getString("scopes");
      returnUri = extras.getString("returnUri");

      secret=UUID.randomUUID().toString();      

      Log.d(TAG, "authUri = "+authUri);
      Log.d(TAG, "tokenUri = "+tokenUri);
      Log.d(TAG, "userinfoUri = "+userinfoUri);
      Log.d(TAG, "clientId = "+clientId);
      Log.d(TAG, "scopes = "+scopes);
      Log.d(TAG, "returnUri = "+returnUri);
      
      String requestUri=authUri;
      if (authUri.indexOf("?") == -1) {
        requestUri+="?";
      } else if (authUri.indexOf("&") == -1) {
        requestUri+="&";
      }
      
      requestUri+="client_id="+HttpUtils.encodeUriParameter(clientId);
      requestUri+="&scope="+HttpUtils.encodeUriParameter(scopes);
      requestUri+="&redirect_uri="+HttpUtils.encodeUriParameter(returnUri);
      requestUri+="&response_type=code";
      requestUri+="&state="+HttpUtils.encodeUriParameter(secret);

      WebView view = (WebView) findViewById(R.id.identityWebView);
      
      view.setWebViewClient(new WebViewClient() {

        /*
         * This is a stub - could be extended to handle error situations
         * by returning to a relevant application screen
         * 
         * @see
         * android.webkit.WebViewClient#onReceivedError(android.webkit
         * .WebView, int, java.lang.String, java.lang.String)
         */
        @Override
        public void onReceivedError(WebView view, int errorCode,
            String description, String failingUrl) {
          Log.d(TAG, "onReceivedError errorCode=" + errorCode
              + " description=" + description + " failingUrl="
              + failingUrl);
        }

        /*
         * The onPageStarted method is called whenever the WebView
         * starts to load a new page - by examining the url for a
         * discovery token we can extract this and move to the next
         * stage of the process
         * 
         * @see
         * android.webkit.WebViewClient#onPageStarted(android.webkit
         * .WebView, java.lang.String, android.graphics.Bitmap)
         */
        @Override
        public void onPageStarted(WebView view, String url,
            Bitmap favicon) {
          Log.d(TAG, "onPageStarted url=" + url);
          /*
           * Check to see if the url contains the discovery token
           * identifier - it could be a url parameter or a page
           * fragment. The following checks and string manipulations
           * retrieve the actual discovery token
           */
          
          if (url != null && url.startsWith(returnUri)) {
            Log.d(TAG, "intercepted return");
            
            ParameterList parameters=ParameterList.getKeyValuesFromUrl(url, 0);
            
            String state=parameters.getValue("state");
            String code=parameters.getValue("code");
            String error=parameters.getValue("error");

            Log.d(TAG, "state = "+state);
            Log.d(TAG, "code = "+code);
            Log.d(TAG, "error = "+error);
            
            if (secret.equalsIgnoreCase(state)) {

              view.stopLoading();

              Intent intent = new Intent(
                  initiator,
                  AuthorizationCompleteActivity.class);
              intent.putExtra("state", state);
              intent.putExtra("code", code);
              intent.putExtra("error", error);
              intent.putExtra("authUri", authUri);
              intent.putExtra("tokenUri", tokenUri);
              intent.putExtra("userinfoUri", userinfoUri);
              intent.putExtra("clientId", clientId);
              intent.putExtra("clientSecret", clientSecret);
              intent.putExtra("scopes", scopes);
              intent.putExtra("returnUri", returnUri);
              
              startActivity(intent);
            }

          }
        }

      });
      
      /*
       * enable JavaScript - the discovery web pages are enhanced with
       * JavaScript
       */
      WebSettings settings = view.getSettings();
      settings.setJavaScriptEnabled(true);

      /*
       * load the specified URI along with the authorization header
       */
      HashMap<String, String> extraheaders = new HashMap<String, String>();
      view.loadUrl(requestUri, extraheaders);
    }
  }

}




Java Source Code List

com.gsma.android.xoperatorapidemo.activity.MainActivity.java
com.gsma.android.xoperatorapidemo.activity.SettingsActivity.java
com.gsma.android.xoperatorapidemo.activity.discovery.ActiveDiscoveryTask.java
com.gsma.android.xoperatorapidemo.activity.discovery.DiscoveryProcessEndpoints.java
com.gsma.android.xoperatorapidemo.activity.discovery.DisplayDiscoveryWebsiteActivity.java
com.gsma.android.xoperatorapidemo.activity.discovery.PassiveDiscoveryTask.java
com.gsma.android.xoperatorapidemo.activity.discovery.ProcessDiscoveryToken.java
com.gsma.android.xoperatorapidemo.activity.identity.AuthorizationCompleteActivity.java
com.gsma.android.xoperatorapidemo.activity.identity.DisplayIdentityWebsiteActivity.java
com.gsma.android.xoperatorapidemo.activity.identity.OpenIDConnectAbstractActivity.java
com.gsma.android.xoperatorapidemo.activity.identity.RetrieveTokenTask.java
com.gsma.android.xoperatorapidemo.activity.identity.RetrieveUserinfoTask.java
com.gsma.android.xoperatorapidemo.discovery.Api.java
com.gsma.android.xoperatorapidemo.discovery.DeveloperOperatorSetting.java
com.gsma.android.xoperatorapidemo.discovery.DiscoveryData.java
com.gsma.android.xoperatorapidemo.discovery.DiscoveryDeveloperOperatorSettings.java
com.gsma.android.xoperatorapidemo.discovery.DiscoveryServingOperatorSettings.java
com.gsma.android.xoperatorapidemo.discovery.DiscoveryStartupSettings.java
com.gsma.android.xoperatorapidemo.discovery.LinkConstants.java
com.gsma.android.xoperatorapidemo.discovery.Link.java
com.gsma.android.xoperatorapidemo.discovery.Response.java
com.gsma.android.xoperatorapidemo.discovery.ServingOperatorSetting.java
com.gsma.android.xoperatorapidemo.identity.UserinfoAddress.java
com.gsma.android.xoperatorapidemo.identity.Userinfo.java
com.gsma.android.xoperatorapidemo.logo.LogoCacheItem.java
com.gsma.android.xoperatorapidemo.logo.LogoCache.java
com.gsma.android.xoperatorapidemo.logo.LogoLoaderTask.java
com.gsma.android.xoperatorapidemo.logo.LogoResponseArray.java
com.gsma.android.xoperatorapidemo.logo.LogoResponse.java
com.gsma.android.xoperatorapidemo.utils.HttpUtils.java
com.gsma.android.xoperatorapidemo.utils.JsonUtils.java
com.gsma.android.xoperatorapidemo.utils.KeyValuePair.java
com.gsma.android.xoperatorapidemo.utils.ParameterList.java
com.gsma.android.xoperatorapidemo.utils.PhoneState.java
com.gsma.android.xoperatorapidemo.utils.PhoneUtils.java
com.gsma.android.xoperatorapidemo.utils.PreferencesUtils.java