Android Open Source - Curio_android_SDK Network Util






From Project

Back to project page Curio_android_SDK.

License

The source code is released under:

Apache License

If you think the Android project Curio_android_SDK 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

/*
 * Copyright (C) 2014 Turkcell/*from  w  w w . ja va  2  s. c o m*/
 * 
 * Created by Can Ciloglu on 16 Haz 2014
 *
 */
package com.turkcell.curio.utils;

import com.turkcell.curio.INetworkConnectivityChangeListener;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * Utility class for data network connection changes and connection types.
 *  
 * @author Can Ciloglu
 *
 */
public class NetworkUtil {

  private static NetworkUtil instance;
  private boolean isConnected;
  private boolean previosConnectionState;
  private String connectionType;
  private INetworkConnectivityChangeListener listener;

  /**
   * Should be called first to create instance.
   */
  public static synchronized NetworkUtil createInstance(Context context, INetworkConnectivityChangeListener listener){
    if(instance == null){
      instance = new NetworkUtil(context, listener);
    }
    return instance;
  }

  /**
   * Be sure that createInstance is called first.
   * @return
   */
  public static synchronized NetworkUtil getInstance(){
    if(instance == null){
      throw new IllegalStateException("NetworkUtil is not created. You should call createInstance method first.");
    }
    return instance;
  }
  
  /**
   * Private constructor.
   * 
   * @param context
   * @param listener 
   */
  private NetworkUtil(Context context, INetworkConnectivityChangeListener listener) {
    this.listener = listener;
    
    setConnectivityState(context);
    
    context.getApplicationContext().registerReceiver(new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
        setConnectivityState(context);
        
        if(previosConnectionState != isConnected()){
          previosConnectionState = isConnected();
          NetworkUtil.this.listener.networkConnectivityChanged(isConnected());
        }
      }
    }, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
  }

  /**
   * Sets connectivity state.
   * 
   * @param context
   */
  protected void setConnectivityState(Context context) {
    // Check if there is an active Internet connection.
    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = connMgr.getActiveNetworkInfo();
    setConnected(activeNetwork != null && activeNetwork.isConnectedOrConnecting());

    // Get connection type
    if (isConnected) {
      if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
        connectionType = Constants.CONNECTION_TYPE_STR_WIFI;
      } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
        connectionType = Constants.CONNECTION_TYPE_STR_MOBILE;
      } else {
        connectionType = Constants.CONNECTION_TYPE_STR_OTHER;
      }
    }else{
      connectionType = "";
    }
  }

  private void setConnected(boolean isConnected) {
    this.isConnected = isConnected;
  }
  
  public boolean isConnected() {
    return isConnected;
  }
  
  public String getConnectionType() {
    return connectionType;
  }
}




Java Source Code List

com.turkcell.curio.CurioClient.java
com.turkcell.curio.CurioRequestProcessor.java
com.turkcell.curio.DBRequestProcessor.java
com.turkcell.curio.ICurioResultListener.java
com.turkcell.curio.INetworkConnectivityChangeListener.java
com.turkcell.curio.model.OfflineRequest.java
com.turkcell.curio.model.OnlineRequest.java
com.turkcell.curio.model.Screen.java
com.turkcell.curio.utils.Constants.java
com.turkcell.curio.utils.CurioClientSettings.java
com.turkcell.curio.utils.CurioDBContract.java
com.turkcell.curio.utils.CurioDBHelper.java
com.turkcell.curio.utils.CurioLogger.java
com.turkcell.curio.utils.CurioUtil.java
com.turkcell.curio.utils.NetworkUtil.java
com.turkcell.curio.utils.ParameterLoader.java
com.turkcell.curio.utils.PushUtil.java
com.turkcell.curio.utils.UUIDGenerator.java
com.turkcell.curio.utils.VisitorCodeManager.java
com.turkcell.curiosample.BlankActivity.java
com.turkcell.curiosample.MainActivity.java
com.turkcell.curiosample.PushNotificationBroadcastReceiver.java
com.turkcell.curiosample.PushNotificationIntentService.java