1 /** 2 ** Copyright 2007, 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.commands.monkey; 18 19 import android.app.IActivityManager; 20 import android.content.IIntentReceiver; 21 import android.content.Intent; 22 import android.content.IntentFilter; 23 import android.net.ConnectivityManager; 24 import android.net.NetworkInfo; 25 import android.os.Bundle; 26 import android.os.RemoteException; 27 import android.os.SystemClock; 28 import android.os.UserHandle; 29 30 /** 31 * Class for monitoring network connectivity during monkey runs. 32 */ 33 public class MonkeyNetworkMonitor extends IIntentReceiver.Stub { 34 private static final boolean LDEBUG = false; 35 private final IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); 36 private long mCollectionStartTime; // time we started collecting data 37 private long mEventTime; // time of last event (connect, disconnect, etc.) 38 private int mLastNetworkType = -1; // unknown 39 private long mWifiElapsedTime = 0; // accumulated time spent on wifi since start() 40 private long mMobileElapsedTime = 0; // accumulated time spent on mobile since start() 41 private long mElapsedTime = 0; // amount of time spent between start() and stop() 42 43 public void performReceive(Intent intent, int resultCode, String data, Bundle extras, 44 boolean ordered, boolean sticky, int sendingUser) throws RemoteException { 45 NetworkInfo ni = (NetworkInfo) intent.getParcelableExtra( 46 ConnectivityManager.EXTRA_NETWORK_INFO); 47 if (LDEBUG) System.out.println("Network state changed: " 48 + "type=" + ni.getType() + ", state=" + ni.getState()); 49 updateNetworkStats(); 50 if (NetworkInfo.State.CONNECTED == ni.getState()) { 51 if (LDEBUG) System.out.println("Network connected"); 52 mLastNetworkType = ni.getType(); 53 } else if (NetworkInfo.State.DISCONNECTED == ni.getState()) { 54 if (LDEBUG) System.out.println("Network not connected"); 55 mLastNetworkType = -1; // unknown since we're disconnected 56 } 57 mEventTime = SystemClock.elapsedRealtime(); 58 } 59 60 private void updateNetworkStats() { 61 long timeNow = SystemClock.elapsedRealtime(); 62 long delta = timeNow - mEventTime; 63 switch (mLastNetworkType) { 64 case ConnectivityManager.TYPE_MOBILE: 65 if (LDEBUG) System.out.println("Adding to mobile: " + delta); 66 mMobileElapsedTime += delta; 67 break; 68 case ConnectivityManager.TYPE_WIFI: 69 if (LDEBUG) System.out.println("Adding to wifi: " + delta); 70 mWifiElapsedTime += delta; 71 break; 72 default: 73 if (LDEBUG) System.out.println("Unaccounted for: " + delta); 74 break; 75 } 76 mElapsedTime = timeNow - mCollectionStartTime; 77 } 78 79 public void start() { 80 mWifiElapsedTime = 0; 81 mMobileElapsedTime = 0; 82 mElapsedTime = 0; 83 mEventTime = mCollectionStartTime = SystemClock.elapsedRealtime(); 84 } 85 86 public void register(IActivityManager am) throws RemoteException { 87 if (LDEBUG) System.out.println("registering Receiver"); 88 am.registerReceiver(null, null, this, filter, null, UserHandle.USER_ALL); 89 } 90 91 public void unregister(IActivityManager am) throws RemoteException { 92 if (LDEBUG) System.out.println("unregistering Receiver"); 93 am.unregisterReceiver(this); 94 } 95 96 public void stop() { 97 updateNetworkStats(); 98 } 99 100 public void dump() { 101 System.out.println("## Network stats: elapsed time=" + mElapsedTime + "ms (" 102 + mMobileElapsedTime + "ms mobile, " 103 + mWifiElapsedTime + "ms wifi, " 104 + (mElapsedTime - mMobileElapsedTime - mWifiElapsedTime) + "ms not connected)"); 105 } 106 }