1 /* 2 Copyright 2011 The Perkeep Authors 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.net.wifi.WifiInfo; 25 import android.net.wifi.WifiManager; 26 import android.util.Log; 27 28 public class WifiPowerReceiver extends BroadcastReceiver { 29 private static final String TAG = "WifiPowerReceiver"; 30 31 @Override onReceive(Context context, Intent intent)32 public void onReceive(Context context, Intent intent) { 33 String action = intent.getAction(); 34 Log.d(TAG, "intent: " + intent); 35 if (Intent.ACTION_POWER_CONNECTED.equals(action)) { 36 Intent cmd = new Intent(UploadService.INTENT_POWER_CONNECTED); 37 cmd.setClass(context, UploadService.class); 38 context.startService(cmd); 39 return; 40 } 41 42 if (Intent.ACTION_POWER_DISCONNECTED.equals(action)) { 43 Intent cmd = new Intent(UploadService.INTENT_POWER_DISCONNECTED); 44 cmd.setClass(context, UploadService.class); 45 context.startService(cmd); 46 } 47 48 if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) { 49 boolean wifi = onWifi(context); 50 Log.d(TAG, "onWifi = " + wifi); 51 Intent cmd = new Intent(wifi ? UploadService.INTENT_NETWORK_WIFI : UploadService.INTENT_NETWORK_NOT_WIFI); 52 String ssid = getSSID(context); 53 cmd.putExtra("SSID", ssid); 54 Log.d(TAG, "extra ssid (chk)= " + cmd.getStringExtra("SSID")); 55 cmd.setClass(context, UploadService.class); 56 context.startService(cmd); 57 } 58 } 59 onPower(Context context)60 public static boolean onPower(Context context) { 61 // TODO Auto-generated method stub 62 return false; 63 } 64 onWifi(Context context)65 public static boolean onWifi(Context context) { 66 NetworkInfo ni = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo(); 67 if (ni != null && ni.isConnected() 68 && (ni.getType() == ConnectivityManager.TYPE_WIFI || ni.getType() == ConnectivityManager.TYPE_ETHERNET)) { 69 return true; 70 } 71 return false; 72 } 73 getSSID(Context context)74 public static String getSSID(Context context) { 75 NetworkInfo ni = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo(); 76 if (ni != null && ni.isConnected() 77 && (ni.getType() == ConnectivityManager.TYPE_WIFI || ni.getType() == ConnectivityManager.TYPE_ETHERNET)) { 78 WifiManager wifiMgr = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); 79 if (wifiMgr != null) { 80 WifiInfo wifiInfo = wifiMgr.getConnectionInfo(); 81 String ssid = wifiInfo.getSSID(); 82 if (ssid.startsWith("\"") && ssid.endsWith("\"")){ 83 ssid = ssid.substring(1, ssid.length()-1); 84 } 85 return ssid; 86 } 87 } 88 return ""; 89 } 90 } 91