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.annotation.IntDef; 20 import android.os.BatteryStatsManager; 21 import android.util.ArrayMap; 22 import android.util.Log; 23 24 import java.lang.annotation.Retention; 25 import java.lang.annotation.RetentionPolicy; 26 import java.util.Map; 27 import java.util.concurrent.RejectedExecutionException; 28 29 /** 30 * This class is used to track WifiState to update BatteryStats 31 */ 32 public class WifiStateTracker { 33 private static final String TAG = "WifiStateTracker"; 34 35 @IntDef(value = { 36 INVALID, 37 SCAN_MODE, 38 DISCONNECTED, 39 CONNECTED, 40 SOFT_AP, 41 }) 42 @Retention(RetentionPolicy.SOURCE) 43 public @interface WifiStateTrackerState {} 44 45 public static final int INVALID = -1; 46 public static final int SCAN_MODE = 1; // unused 47 public static final int DISCONNECTED = 2; 48 public static final int CONNECTED = 3; 49 public static final int SOFT_AP = 4; // unused 50 51 /** 52 * String key: interface name 53 * int value: last wifi state, one of the {@link WifiStateTrackerState} values. 54 */ 55 private final Map<String, Integer> mIfaceToWifiState = new ArrayMap<>(); 56 private final BatteryStatsManager mBatteryStatsManager; 57 WifiStateTracker(BatteryStatsManager batteryStatsManager)58 public WifiStateTracker(BatteryStatsManager batteryStatsManager) { 59 mBatteryStatsManager = batteryStatsManager; 60 } 61 62 @WifiStateTrackerState 63 private int mLastReportedState = INVALID; 64 informWifiStateBatteryStats(@ifiStateTrackerState int state)65 private void informWifiStateBatteryStats(@WifiStateTrackerState int state) { 66 if (state == mLastReportedState) { 67 return; 68 } 69 mLastReportedState = state; 70 71 int batteryStatsState = internalToBatteryStatsWifiState(state); 72 if (batteryStatsState == INVALID) { 73 return; 74 } 75 76 try { 77 mBatteryStatsManager.reportWifiState(batteryStatsState, null); 78 } catch (RejectedExecutionException e) { 79 Log.e(TAG, "Battery stats executor is being shutdown " + e.getMessage()); 80 } 81 } 82 83 /** 84 * Reports the most active state among Wifi ifaces. e.g. Connected is more active than 85 * disconnected. 86 */ 87 @WifiStateTrackerState getHighestPriorityState()88 private int getHighestPriorityState() { 89 int highest = INVALID; 90 for (int i : mIfaceToWifiState.values()) { 91 highest = Math.max(highest, i); 92 } 93 return highest; 94 } 95 96 /** 97 * Inform the WifiState to this tracker to translate into the 98 * WifiState corresponding to BatteryStatsManager. 99 * @param ifaceName iface name whose state has changed 100 * @param state state corresponding to the ClientModeImpl state, one of the 101 * {@link WifiStateTrackerState} values. 102 */ updateState(String ifaceName, @WifiStateTrackerState int state)103 public void updateState(String ifaceName, @WifiStateTrackerState int state) { 104 mIfaceToWifiState.put(ifaceName, state); 105 int highestPriorityState = getHighestPriorityState(); 106 informWifiStateBatteryStats(highestPriorityState); 107 } 108 internalToBatteryStatsWifiState(@ifiStateTrackerState int internal)109 private static int internalToBatteryStatsWifiState(@WifiStateTrackerState int internal) { 110 switch (internal) { 111 case SCAN_MODE: 112 return BatteryStatsManager.WIFI_STATE_OFF_SCANNING; 113 case DISCONNECTED: 114 return BatteryStatsManager.WIFI_STATE_ON_DISCONNECTED; 115 case CONNECTED: 116 return BatteryStatsManager.WIFI_STATE_ON_CONNECTED_STA; 117 case SOFT_AP: 118 return BatteryStatsManager.WIFI_STATE_SOFT_AP; 119 case INVALID: 120 default: 121 return INVALID; 122 } 123 } 124 } 125