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 package com.android.tradefed.device; 17 18 import com.android.ddmlib.AdbCommandRejectedException; 19 import com.android.ddmlib.CollectingOutputReceiver; 20 import com.android.ddmlib.IDevice; 21 import com.android.ddmlib.ShellCommandUnresponsiveException; 22 import com.android.ddmlib.TimeoutException; 23 import com.android.tradefed.log.LogUtil.CLog; 24 25 import java.io.IOException; 26 import java.util.concurrent.TimeUnit; 27 28 /** 29 * Helper class for monitoring the state of a {@link IDevice}. 30 */ 31 public class DeviceStateMonitor extends NativeDeviceStateMonitor { 32 33 public DeviceStateMonitor(IDeviceManager mgr, IDevice device, boolean fastbootEnabled) { 34 super(mgr, device, fastbootEnabled); 35 } 36 37 /** 38 * Waits for the device package manager to be responsive. 39 * 40 * @param waitTime time in ms to wait before giving up 41 * @return <code>true</code> if package manage becomes responsive before waitTime expires. 42 * <code>false</code> otherwise 43 */ 44 protected boolean waitForPmResponsive(final long waitTime) { 45 CLog.i("Waiting %d ms for device %s package manager", 46 waitTime, getSerialNumber()); 47 long startTime = System.currentTimeMillis(); 48 int counter = 1; 49 while (System.currentTimeMillis() - startTime < waitTime) { 50 final CollectingOutputReceiver receiver = createOutputReceiver(); 51 final String cmd = "pm path android"; 52 try { 53 getIDevice().executeShellCommand(cmd, receiver, MAX_OP_TIME, TimeUnit.MILLISECONDS); 54 String output = receiver.getOutput(); 55 CLog.v("%s returned %s", cmd, output); 56 if (output.contains("package:")) { 57 return true; 58 } 59 } catch (IOException e) { 60 CLog.i("%s on device %s failed: %s", cmd, getSerialNumber(), e.getMessage()); 61 } catch (TimeoutException e) { 62 CLog.i("%s on device %s failed: timeout", cmd, getSerialNumber()); 63 } catch (AdbCommandRejectedException e) { 64 CLog.i("%s on device %s failed: %s", cmd, getSerialNumber(), e.getMessage()); 65 } catch (ShellCommandUnresponsiveException e) { 66 CLog.i("%s on device %s failed: %s", cmd, getSerialNumber(), e.getMessage()); 67 } 68 getRunUtil().sleep(Math.min(getCheckPollTime() * counter, MAX_CHECK_POLL_TIME)); 69 counter++; 70 } 71 CLog.w("Device %s package manager is unresponsive", getSerialNumber()); 72 return false; 73 } 74 75 /** 76 * {@inheritDoc} 77 */ 78 @Override 79 protected boolean postOnlineCheck(final long waitTime) { 80 long startTime = System.currentTimeMillis(); 81 if (!waitForPmResponsive(waitTime)) { 82 return false; 83 } 84 long elapsedTime = System.currentTimeMillis() - startTime; 85 return super.postOnlineCheck(waitTime - elapsedTime); 86 } 87 } 88