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 performReceive(Intent intent, int resultCode, String data, Bundle extras, boolean ordered, boolean sticky, int sendingUser)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) Logger.out.println("Network state changed: " 48 + "type=" + ni.getType() + ", state=" + ni.getState()); 49 updateNetworkStats(); 50 if (NetworkInfo.State.CONNECTED == ni.getState()) { 51 if (LDEBUG) Logger.out.println("Network connected"); 52 mLastNetworkType = ni.getType(); 53 } else if (NetworkInfo.State.DISCONNECTED == ni.getState()) { 54 if (LDEBUG) Logger.out.println("Network not connected"); 55 mLastNetworkType = -1; // unknown since we're disconnected 56 } 57 mEventTime = SystemClock.elapsedRealtime(); 58 } 59 updateNetworkStats()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) Logger.out.println("Adding to mobile: " + delta); 66 mMobileElapsedTime += delta; 67 break; 68 case ConnectivityManager.TYPE_WIFI: 69 if (LDEBUG) Logger.out.println("Adding to wifi: " + delta); 70 mWifiElapsedTime += delta; 71 break; 72 default: 73 if (LDEBUG) Logger.out.println("Unaccounted for: " + delta); 74 break; 75 } 76 mElapsedTime = timeNow - mCollectionStartTime; 77 } 78 start()79 public void start() { 80 mWifiElapsedTime = 0; 81 mMobileElapsedTime = 0; 82 mElapsedTime = 0; 83 mEventTime = mCollectionStartTime = SystemClock.elapsedRealtime(); 84 } 85 register(IActivityManager am)86 public void register(IActivityManager am) throws RemoteException { 87 if (LDEBUG) Logger.out.println("registering Receiver"); 88 am.registerReceiverWithFeature(null, null, null, null, this, filter, null, 89 UserHandle.USER_ALL, 0); 90 } 91 unregister(IActivityManager am)92 public void unregister(IActivityManager am) throws RemoteException { 93 if (LDEBUG) Logger.out.println("unregistering Receiver"); 94 am.unregisterReceiver(this); 95 } 96 stop()97 public void stop() { 98 updateNetworkStats(); 99 } 100 dump()101 public void dump() { 102 Logger.out.println("## Network stats: elapsed time=" + mElapsedTime + "ms (" 103 + mMobileElapsedTime + "ms mobile, " 104 + mWifiElapsedTime + "ms wifi, " 105 + (mElapsedTime - mMobileElapsedTime - mWifiElapsedTime) + "ms not connected)"); 106 } 107 } 108