1 /* 2 * Copyright (C) 2016 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.compatibility.common.util; 18 19 import com.android.ddmlib.Log.LogLevel; 20 import com.android.tradefed.device.DeviceNotAvailableException; 21 import com.android.tradefed.device.ITestDevice; 22 import com.android.tradefed.log.LogUtil.CLog; 23 import com.android.tradefed.result.ITestInvocationListener; 24 import com.android.tradefed.util.RunUtil; 25 26 /** 27 * Utility functions related to device state monitoring during compatibility test. 28 */ 29 public class MonitoringUtils { 30 31 private static final long CONNECTIVITY_CHECK_TIME_MS = 20 * 1000; 32 private static final long CONNECTIVITY_CHECK_INTERVAL_MS = 5 * 1000; 33 checkDeviceConnectivity(ITestDevice device)34 public static boolean checkDeviceConnectivity(ITestDevice device) 35 throws DeviceNotAvailableException { 36 long start = System.currentTimeMillis(); 37 while (System.currentTimeMillis() - start < CONNECTIVITY_CHECK_TIME_MS) { 38 if (device.checkConnectivity()) { 39 CLog.i("Wifi Connectivity: passed check."); 40 return true; 41 } else { 42 CLog.logAndDisplay( 43 LogLevel.INFO, 44 "Wifi Connectivity check failed on %s. (Is your device connected to Wifi?)" 45 + ", retrying in %dms", 46 device.getSerialNumber(), 47 CONNECTIVITY_CHECK_INTERVAL_MS); 48 RunUtil.getDefault().sleep(CONNECTIVITY_CHECK_INTERVAL_MS); 49 } 50 } 51 return false; 52 } 53 checkDeviceConnectivity(ITestDevice device, ITestInvocationListener listener, String tag)54 public static void checkDeviceConnectivity(ITestDevice device, ITestInvocationListener listener, 55 String tag) throws DeviceNotAvailableException { 56 if (!checkDeviceConnectivity(device)) { 57 CLog.w("Wifi Connectivity: check failed. (Is your device connected to Wifi?)"); 58 device.logBugreport(String.format("bugreport-connectivity-%s", tag), listener); 59 } 60 } 61 } 62