Android Open Source - MobileConnectTestApp Phone Utils






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.utils;
//from ww w  .  j  a  v a 2s.co  m
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

/**
 * extract useful phone state information and return in the form of a PhoneState
 * object
 */
public class PhoneUtils {
  private static final String TAG = "PhoneUtils";

  /**
   * convert information which can be obtained from the Android OS into
   * PhoneState information necessary for discovery
   * 
   * @param telephonyMgr
   * @param connectivityMgr
   * @return
   */
  public static PhoneState getPhoneState(TelephonyManager telephonyMgr,
      ConnectivityManager connectivityMgr) {

    /*
     * the users' phone number is obtained - this is not always available/
     * valid
     */
    String msisdn = telephonyMgr.getLine1Number();

    /*
     * get the active network
     */
    NetworkInfo activeNetwork = connectivityMgr.getActiveNetworkInfo();

//    if (activeNetwork != null) {
//      Log.d(TAG, "activeNetwork = " + activeNetwork.toString());
//    }

    /*
     * subscriberId is normally the IMSI - not directly useful to discovery
     */
    String subscriberId = telephonyMgr.getSubscriberId();

    /*
     * check if the device is currently connected
     */
    boolean connected = activeNetwork != null
        ? activeNetwork.isConnected()
        : false;

    /*
     * check if the device is currently roaming
     */
    boolean roaming = activeNetwork != null
        ? activeNetwork.isRoaming()
        : false;

    /*
     * check if the device is using mobile/cellular data
     */
    boolean usingMobileData = activeNetwork != null ? activeNetwork
        .getType() == ConnectivityManager.TYPE_MOBILE : false;
    
    /*
     * get the SIM serial number
     */
    String simSerialNumber = telephonyMgr.getSimSerialNumber();

    /*
     * the simOperator indicates the registered network MCC/MNC the
     * networkOperator indicates the current network MCC/MNC
     */
    String simOperator = telephonyMgr.getSimOperator();
    String networkOperator = telephonyMgr.getNetworkOperator();

//    Log.d(TAG, "Connected to Internet? " + connected);
//    Log.d(TAG, "Connected to mobile data? " + usingMobileData);
//    Log.d(TAG, "Roaming? " + roaming);
//    Log.d(TAG, "Detected MSISDN? " + msisdn);
//    Log.d(TAG, "subscriberId? " + subscriberId);
//    Log.d(TAG, "deviceId? " + telephonyMgr.getDeviceId());
//    Log.d(TAG, "simOperator? " + simOperator);
//    Log.d(TAG, "simOperatorName? " + telephonyMgr.getSimOperatorName());
//    Log.d(TAG, "simCountryIso? " + telephonyMgr.getSimCountryIso());
//    Log.d(TAG, "simSerialNumber = " + simSerialNumber);
//    Log.d(TAG, "networkOperator = " + networkOperator);

    /*
     * Mobile Country Code is obtained from the first three digits of
     * simOperator, Mobile Network Code is any remaining digits
     */
    String mcc = null;
    String mnc = null;
    if (simOperator != null && simOperator.length() > 3) {
      mcc = simOperator.substring(0, 3);
      mnc = simOperator.substring(3);
    }

    /*
     * return a new PhoneState object from the parameters used in discovery
     */
    return new PhoneState(msisdn, simOperator, mcc, mnc, connected,
        usingMobileData, roaming, simSerialNumber);

  }
}




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