Android Open Source - scoreflex-android-sdk Connectivity Receiver






From Project

Back to project page scoreflex-android-sdk.

License

The source code is released under:

Apache License

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

/*
 * Licensed to Scoreflex (www.scoreflex.com) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Scoreflex licenses this
 * file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 */*from   w  w w.ja  v  a  2  s  .  c  o m*/
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.scoreflex;

import com.scoreflex.Scoreflex.Response;
import com.scoreflex.Scoreflex.ResponseHandler;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;


/**
 * A class that monitors the connectivity state of the device
 *
 */
public class ConnectivityReceiver extends BroadcastReceiver {

  private boolean checkCaptivePortalSafe(NetworkInfo.DetailedState state) {
    try {
      return state == NetworkInfo.DetailedState.valueOf("CAPTIVE_PORTAL_CHECK");
    } catch (Exception e) {
      return false;
    }
  }

  /**
   * Method called by the {@link #android.net.ConnectivityManager} to notify connectivity changes.
   */
  @Override
  public void onReceive(Context context, Intent intent) {
    try {
      ConnectivityManager cm = (ConnectivityManager) context
          .getSystemService(Context.CONNECTIVITY_SERVICE);
      if (intent.getExtras() != null) {
        NetworkInfo ni = (NetworkInfo) cm.getActiveNetworkInfo();
        if (ni != null
            && ni.isConnected()
            && !checkCaptivePortalSafe(ni.getDetailedState())) {
          if (Scoreflex.isInitialized()) {
            Scoreflex.get("/network/ping", null, new ResponseHandler() {

              @Override
              public void onFailure(Throwable e, Response errorResponse) {

              }

              @Override
              public void onSuccess(Response response) {

              }
            });
          }
        } else {
          Scoreflex.setNetworkAvailable(false);
        }
      }
      if (intent.getExtras().getBoolean(
          ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) {
        Scoreflex.setNetworkAvailable(false);
      }
    } catch (Exception e) {
      Log.d(
          "Scoreflex",
          "You might want to add : android.permission.ACCESS_NETWORK_STATE to your permissions");
      return;
    }
  }

}




Java Source Code List

com.scoreflex.ConnectivityReceiver.java
com.scoreflex.QueryStringParser.java
com.scoreflex.ScoreflexActivity.java
com.scoreflex.ScoreflexBroadcastReceiver.java
com.scoreflex.ScoreflexGcmClient.java
com.scoreflex.ScoreflexJobQueue.java
com.scoreflex.ScoreflexRequestParamsDecorator.java
com.scoreflex.ScoreflexRequestVault.java
com.scoreflex.ScoreflexRestClient.java
com.scoreflex.ScoreflexUriHelper.java
com.scoreflex.ScoreflexView.java
com.scoreflex.Scoreflex.java
com.scoreflex.SocialCallback.java
com.scoreflex.SocialShareCallback.java
com.scoreflex.facebook.ScoreflexFacebookWrapper.java
com.scoreflex.google.ScoreflexGcmWrapper.java
com.scoreflex.google.ScoreflexGoogleWrapper.java
com.scoreflex.model.JSONParcelable.java