Android Open Source - androidwisprclient Network Connectivity Receiver






From Project

Back to project page androidwisprclient.

License

The source code is released under:

GNU General Public License

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

/*
 * This file is part of FONAccess.// w  ww  .j a  v a 2s.  c  o  m
 * 
 * FONAccess is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * FONAccess is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with FONAccess. If not, see <http://www.gnu.org/licenses/>.
 */
package com.oakley.fon;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.BadParcelableException;
import android.util.Log;

import com.oakley.fon.util.FONUtils;
import com.oakley.fon.util.Utils;

public class NetworkConnectivityReceiver extends BroadcastReceiver {
  private static String TAG = NetworkConnectivityReceiver.class.getName();

  @Override
  public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    Log.d(TAG, "Action Received: " + action + " From intent: " + intent);

    // We look if we are connected
    if (isConnectedIntent(intent)) {
      WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
      WifiInfo connectionInfo = wm.getConnectionInfo();
      String ssid = connectionInfo.getSSID();
      String bssid = connectionInfo.getBSSID();
      Log.v(TAG,
          "Connected. SSID:" + ssid + ", bssid:" + bssid + ", supplicantState:"
              + connectionInfo.getSupplicantState());

      // We look if it's a FON Access Point
      if (FONUtils.isSupportedNetwork(ssid, bssid)) {

        boolean active = Utils.getBooleanPreference(context, R.string.pref_active, true);

        if (active) {
          String username = Utils.getStringPreference(context, R.string.pref_username, "");
          String password = Utils.getStringPreference(context, R.string.pref_password, "");
          if (username.trim().length() > 0 && password.trim().length() > 0) {
            // If the application is active and we have username and password we launch
            // the login intent
            Intent logIntent = new Intent(context, WISPrLoggerService.class);
            logIntent.setAction("LOG");
            logIntent.putExtra(context.getString(R.string.pref_username), username);
            logIntent.putExtra(context.getString(R.string.pref_password), password);
            logIntent.putExtra(context.getString(R.string.pref_ssid), ssid);
            logIntent.putExtra(context.getString(R.string.pref_bssid), bssid);
            context.startService(logIntent);
          } else {
            Log.v(TAG, "Username & Password not available");
            cleanNotification(context);
          }
        } else {
          Log.v(TAG, "Application inactive");
          cleanNotification(context);
        }
      } else {
        Log.v(TAG, "Not a FON Access Point");
        cleanNotification(context);
      }
    } else if (isDisconnectedIntent(intent)) {
      Log.v(TAG, "Disconnected");
      cleanNotification(context);
    }
  }

  private void cleanNotification(Context context) {
    Log.v(TAG, "Cleaning Notificacion Icon");
    Intent cleaningIntent = new Intent(context, NotificationCleaningService.class);
    cleaningIntent.setAction(NotificationCleaningService.ACTION_CLEAN);
    context.startService(cleaningIntent);
  }

  private boolean isConnectedIntent(Intent intent) {
    NetworkInfo networkInfo = null;
    try {
      networkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
    } catch (BadParcelableException ignored) {}

    return (networkInfo != null && networkInfo.isConnected() && networkInfo.getType() == ConnectivityManager.TYPE_WIFI);
  }

  private boolean isDisconnectedIntent(Intent intent) {
    boolean res = false;
    NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
    // Log.d(TAG, "NetworkInfo:" + networkInfo);

    if (networkInfo != null) {
      State state = networkInfo.getState();
      res = (state.equals(NetworkInfo.State.DISCONNECTING) || state.equals(NetworkInfo.State.DISCONNECTED))
          && (networkInfo.getType() == ConnectivityManager.TYPE_WIFI);
    } else {
      int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
      // Log.d(TAG, "wifiState:" + wifiState);
      if (wifiState == WifiManager.WIFI_STATE_DISABLED || wifiState == WifiManager.WIFI_STATE_DISABLING) {
        res = true;
      }
    }

    return res;
  }
}




Java Source Code List

com.oakley.fon.AndroidWISPr.java
com.oakley.fon.LogOffService.java
com.oakley.fon.NetworkConnectivityReceiver.java
com.oakley.fon.NetworkScanReceiver.java
com.oakley.fon.NotificationCleaningService.java
com.oakley.fon.PreferencesAdvanced.java
com.oakley.fon.WISPrLoggerService.java
com.oakley.fon.handler.WISPrInfoHandler.java
com.oakley.fon.handler.WISPrResponseHandler.java
com.oakley.fon.logger.BTFonLogger.java
com.oakley.fon.logger.HTTPLogger.java
com.oakley.fon.logger.LivedoorLogger.java
com.oakley.fon.logger.LoggerResult.java
com.oakley.fon.logger.NeufLogger.java
com.oakley.fon.logger.SimpleHTTPLogger.java
com.oakley.fon.logger.WISPrLogger.java
com.oakley.fon.logger.WebLogger.java
com.oakley.fon.util.BackupAgentHelperWrapper.java
com.oakley.fon.util.BackupAgent.java
com.oakley.fon.util.FONUtils.java
com.oakley.fon.util.HttpResult.java
com.oakley.fon.util.HttpUtils.java
com.oakley.fon.util.LogRedirectHandler.java
com.oakley.fon.util.RemoveHttpsRedirectHandler.java
com.oakley.fon.util.Utils.java
com.oakley.fon.util.WISPrConstants.java
com.oakley.fon.util.WISPrUtil.java