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