Android Open Source - MobileConnectTestApp Display Discovery 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.discovery;
//ww  w . j  a va  2s  . c  om
import java.util.HashMap;

import com.gsma.android.mobileconnecttest.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/*
 * This class will handle the display of the web pages provided by the Discovery service, when the user
 * is asked to confirm their country and MSISDN.
 * 
 * It will scan for the provision of a discovery token in a resulting redirect and forward to the action
 * using a discovery token to obtain operator endpoints
 */
@SuppressLint("SetJavaScriptEnabled")
public class DisplayDiscoveryWebsiteActivity extends Activity {
  private static final String TAG = "DisplayDiscoveryWebsite";
  
  WebView discoveryWebsiteView;
  View originalView;
  
  boolean finished;

//  Activity invokingActivity;
  String consumerKey;
  String consumerSecret;
  String serviceUri;
  String uri;
  Boolean enableCookies;

  public void onStart() {
    super.onStart();
    
    Bundle extras = getIntent().getExtras();

    consumerKey = extras.getString("consumerKey");
    consumerSecret = extras.getString("consumerSecret");
    serviceUri = extras.getString("serviceUri");
    uri = extras.getString("uri");
    enableCookies = extras.getBoolean("enableCookies");
    
    discoveryWebsiteView=new WebView(this);
    
    final DisplayDiscoveryWebsiteActivity thisActivity=this;
    
    setContentView(discoveryWebsiteView);

    Log.d(TAG, "uri - " + uri);
    Log.d(TAG, "consumerKey = " + consumerKey);
    Log.d(TAG, "serviceUri = " + consumerSecret);
    
    finished=false;

    HashMap<String, String> extraheaders = new HashMap<String, String>();
    extraheaders.put("Accept", "text/html");

    Log.d(TAG, "uri = "+uri);

    CookieManager cookieManager=CookieManager.getInstance();
    cookieManager.setAcceptCookie(enableCookies!=null?enableCookies.booleanValue():false);
    Log.d(TAG, "Allowing cookies = "+cookieManager.acceptCookie());

//    invokingActivity.setContentView(discoveryWebsiteView);

    discoveryWebsiteView.getSettings().setJavaScriptEnabled(true); 
    discoveryWebsiteView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

    /*
     * Add a handler which will scan for URL changes
     */
    discoveryWebsiteView.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);
        finished=true;
        finish();
//        _invokingActivity.setContentView(originalView);
      }
      
      @Override
          public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
              super.onReceivedSslError(view, handler, error);
             //Error happens here and returns an empty page.
              Log.d(TAG, "onReceivedSslError");
              finished=true;
              finish();
//        _invokingActivity.setContentView(originalView);
          }

      @Override
      public void onPageFinished(WebView view, String url) {
        Log.d(TAG, "onPageFinished "+url);
          }
      
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Log.d(TAG, "shouldOverrideUrlLoading "+url);
              view.loadUrl(url);
              return true;
          }
      
      /*
       * 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.indexOf("mcc_mnc") > -1) {
          String[] parts = url.split("mcc_mnc", 2);
          if (parts.length == 2) {
            String mcc_mnc = parts[1].replaceFirst("=",
                "").trim();

            Log.d(TAG, "mcc_mnc = " + mcc_mnc);

            /*
             * There is no need to load the remainder of this url as
             * the discovery token was located
             */
            view.stopLoading();

            /*
             * With the discovery token this can now be used to
             * retrieve service endpoints (back to MainActivity)
             */
            ProcessDiscoveryToken.start(thisActivity, mcc_mnc, consumerKey, serviceUri);
            
            finished=true;
            
            thisActivity.finish();
            
          } // have a discovery token pair

        }
      }

    });

    /*
     * load the specified URI along with the authorization header
     */
    
    discoveryWebsiteView.loadUrl(uri, extraheaders);
  }
  
  public void discover(Activity invokingActivity, String uri, String consumerKey, String consumerSecret, String serviceUri, Boolean enableCookies) {

    try {
      Intent intent = new Intent(invokingActivity, this.getClass());
      intent.putExtra("consumerKey", consumerKey);
      intent.putExtra("consumerSecret", consumerSecret);
      intent.putExtra("serviceUri", serviceUri);
      intent.putExtra("uri", uri);
      intent.putExtra("enableCookies", enableCookies);
      
      invokingActivity.startActivity(intent);
    } catch ( ActivityNotFoundException e) {
        e.printStackTrace();
    }
  }

  public void stopLoading() {
    if (discoveryWebsiteView!=null) {
      discoveryWebsiteView.stopLoading();
    }
  }
  
  public boolean isFinished() {
    return finished;
  }
  
  
  
}




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