1 /* 2 * Copyright (C) 2010 The Android Open Source Project 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 com.android.server.wifi; 18 19 import android.os.BatteryStats; 20 import android.os.RemoteException; 21 import android.util.Log; 22 23 import com.android.internal.app.IBatteryStats; 24 25 /** 26 * This class is used to track WifiState to update BatteryStats 27 */ 28 public class WifiStateTracker { 29 private static final String TAG = "WifiStateTracker"; 30 31 public static final int INVALID = 0; 32 public static final int SCAN_MODE = 1; 33 public static final int DISCONNECTED = 2; 34 public static final int CONNECTED = 3; 35 public static final int SOFT_AP = 4; 36 private int mWifiState; 37 private IBatteryStats mBatteryStats; 38 39 public WifiStateTracker(IBatteryStats stats) { 40 mWifiState = INVALID; 41 mBatteryStats = stats; 42 } 43 44 private void informWifiStateBatteryStats(int state) { 45 try { 46 mBatteryStats.noteWifiState(state, null); 47 } catch (RemoteException e) { 48 Log.e(TAG, "Battery stats unreachable " + e.getMessage()); 49 } 50 } 51 52 /** 53 * Inform the WifiState to this tracker to translate into the 54 * WifiState corresponding to BatteryStats. 55 * @param state state corresponding to the WifiStateMachine state 56 */ 57 public void updateState(int state) { 58 int reportState = BatteryStats.WIFI_STATE_OFF; 59 if (state != mWifiState) { 60 switch(state) { 61 case SCAN_MODE: 62 reportState = BatteryStats.WIFI_STATE_OFF_SCANNING; 63 break; 64 case DISCONNECTED: 65 reportState = BatteryStats.WIFI_STATE_ON_DISCONNECTED; 66 break; 67 case CONNECTED: 68 reportState = BatteryStats.WIFI_STATE_ON_CONNECTED_STA; 69 break; 70 case SOFT_AP: 71 reportState = BatteryStats.WIFI_STATE_SOFT_AP; 72 break; 73 case INVALID: 74 mWifiState = INVALID; 75 /* Fall through */ 76 default: 77 return; 78 } 79 mWifiState = state; 80 informWifiStateBatteryStats(reportState); 81 } 82 return; 83 } 84 } 85