github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/clients/android/src/org/camlistore/WifiPowerReceiver.java (about)

     1  /*
     2  Copyright 2011 Google Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15   */
    16  
    17  package org.camlistore;
    18  
    19  import android.content.BroadcastReceiver;
    20  import android.content.Context;
    21  import android.content.Intent;
    22  import android.net.ConnectivityManager;
    23  import android.net.NetworkInfo;
    24  import android.util.Log;
    25  
    26  public class WifiPowerReceiver extends BroadcastReceiver {
    27      private static final String TAG = "WifiPowerReceiver";
    28  
    29      @Override
    30      public void onReceive(Context context, Intent intent) {
    31          String action = intent.getAction();
    32          Log.d(TAG, "intent: " + intent);
    33          if (Intent.ACTION_POWER_CONNECTED.equals(action)) {
    34              Intent cmd = new Intent(UploadService.INTENT_POWER_CONNECTED);
    35              cmd.setClass(context, UploadService.class);
    36              context.startService(cmd);
    37              return;
    38          }
    39  
    40          if (Intent.ACTION_POWER_DISCONNECTED.equals(action)) {
    41              Intent cmd = new Intent(UploadService.INTENT_POWER_DISCONNECTED);
    42              cmd.setClass(context, UploadService.class);
    43              context.startService(cmd);
    44          }
    45  
    46          if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) {
    47              boolean wifi = onWifi(context);
    48              Log.d(TAG, "onWifi = " + wifi);
    49              Intent cmd = new Intent(wifi ? UploadService.INTENT_NETWORK_WIFI : UploadService.INTENT_NETWORK_NOT_WIFI);
    50              cmd.setClass(context, UploadService.class);
    51              context.startService(cmd);
    52          }
    53      }
    54  
    55      public static boolean onPower(Context context) {
    56          // TODO Auto-generated method stub
    57          return false;
    58      }
    59  
    60      public static boolean onWifi(Context context) {
    61          NetworkInfo ni = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
    62          if (ni != null && ni.isConnected()
    63                  && (ni.getType() == ConnectivityManager.TYPE_WIFI || ni.getType() == ConnectivityManager.TYPE_ETHERNET)) {
    64              return true;
    65          }
    66          return false;
    67      }
    68  }