/** * Run the test over device given. * * @param device the device to run the test. */ public void run(final TestDevice device) throws DeviceDisconnectedException, ADBServerNeedRestartException { if ((getName() == null) || (getName().length() == 0)) { return; } if (TestSession.exceedsMaxCount()) { throw new ADBServerNeedRestartException("Test count reached overflow point"); } else { TestSession.incTestCount(); } mTestStop = false; mDevice = device; mTimeOutTimer = new HostTimer(new TimeOutTask(this), HostConfig.Ints.individualStartTimeoutMs.value()); mTimeOutTimer.start(); mProgressObserver = new ProgressObserver(); mProgressObserver.start(); setStartTime(System.currentTimeMillis()); String testFullName = getFullName(); print(testFullName + "..."); runImpl(); synchronized (mTimeOutTimer) { if (!mTestStop) { try { mTimeOutTimer.waitOn(); } catch (InterruptedException e) { Log.d("time out object interrupted"); } } mProgressObserver.stop(); if (mTimeOutTimer.isTimeOut()) { return; } else { // not caused by timer timing out // need to cancel timer mTimeOutTimer.cancel(false); } } setResult(mResult); }
/** {@inheritDoc} */ public void notifyTestingDeviceDisconnected() { Log.d("Test.notifyTestingDeviceDisconnected() is called"); if (mProgressObserver != null) { mProgressObserver.stop(); } if (mTimeOutTimer != null) { synchronized (mTimeOutTimer) { mTimeOutTimer.cancel(false); mTimeOutTimer.sendNotify(); } } }
/** * Copy an input stream to an output stream. * * @param bufferSize the size of the buffer * @param progress the progress observer it could be null * @param in the input stream * @param out the output stream * @param canStop if true, the copy can be stopped by interrupting the thread * @return <code>true</code> if the copy was done, <code>false</code> if it was interrupted * @throws IOException IOException If an I/O error occurs */ public static boolean copyStream( int bufferSize, ProgressObserver progress, InputStream in, OutputStream out, boolean canStop) throws IOException { byte[] buffer = new byte[bufferSize]; int n; long copied = 0L; while (-1 != (n = in.read(buffer))) { out.write(buffer, 0, n); copied += n; if (progress != null) progress.setValue(copied); if (canStop && Thread.interrupted()) return false; } return true; }
protected synchronized void notifyComplete(Object result) { _isDone = true; for (ProgressObserver po : _progressObservers) { po.onComplete(_requestCode, result); } }