Update a few target preparers

- Move a few methods to AfwTestTargetPreparer
- Don't throw exception while trying to connect wifi

Change-Id: Icec5358fe6b2d9aa10fdbd5ba0b5f40341530e0a
diff --git a/tools/tradefed-host/src/com/android/afwtest/tradefed/targetprep/AfwTestFactoryReset.java b/tools/tradefed-host/src/com/android/afwtest/tradefed/targetprep/AfwTestFactoryReset.java
index 4c849f2..5ee82fa 100644
--- a/tools/tradefed-host/src/com/android/afwtest/tradefed/targetprep/AfwTestFactoryReset.java
+++ b/tools/tradefed-host/src/com/android/afwtest/tradefed/targetprep/AfwTestFactoryReset.java
@@ -25,8 +25,6 @@
 import com.android.tradefed.log.LogUtil.CLog;
 import com.android.tradefed.targetprep.ITargetPreparer;
 import com.android.tradefed.targetprep.TargetSetupError;
-import com.android.tradefed.util.CommandResult;
-import com.android.tradefed.util.CommandStatus;
 
 import java.util.concurrent.TimeUnit;
 
@@ -177,38 +175,5 @@
         }
         return result;
     }
-
-    /**
-     * Wipes device via fastboot.
-     *
-     * @param device test device
-     */
-    private void wipeDevice(ITestDevice device)
-            throws DeviceNotAvailableException, TargetSetupError {
-
-        CLog.i(String.format("Wiping device %s".format(device.getSerialNumber())));
-
-        device.rebootIntoBootloader();
-        fastbootFormat(device, "cache");
-        fastbootFormat(device, "userdata");
-        device.executeFastbootCommand("reboot");
-    }
-
-    /**
-     * Performs fastboot erase/format operation on certain partition
-     *
-     * @param device test device
-     * @param partition android partition, e.g. userdata, cache, system
-     */
-    private void fastbootFormat(ITestDevice device, String partition)
-            throws DeviceNotAvailableException, TargetSetupError {
-
-        CLog.i("Attempting: fastboot format %s", partition);
-        CommandResult r = device.executeLongFastbootCommand("format", partition);
-        if (r.getStatus() != CommandStatus.SUCCESS) {
-            throw new TargetSetupError(
-                    String.format("format %s failed: %s", partition, r.getStderr()));
-        }
-    }
 }
 
diff --git a/tools/tradefed-host/src/com/android/afwtest/tradefed/targetprep/AfwTestTargetPreparer.java b/tools/tradefed-host/src/com/android/afwtest/tradefed/targetprep/AfwTestTargetPreparer.java
index b066374..3316e6b 100644
--- a/tools/tradefed-host/src/com/android/afwtest/tradefed/targetprep/AfwTestTargetPreparer.java
+++ b/tools/tradefed-host/src/com/android/afwtest/tradefed/targetprep/AfwTestTargetPreparer.java
@@ -22,18 +22,23 @@
 import com.android.tradefed.device.DeviceNotAvailableException;
 import com.android.tradefed.device.ITestDevice;
 import com.android.tradefed.device.PackageInfo;
+import com.android.tradefed.log.LogUtil.CLog;
 import com.android.tradefed.targetprep.TargetSetupError;
+import com.android.tradefed.util.CommandResult;
+import com.android.tradefed.util.CommandStatus;
 import com.android.tradefed.util.RunUtil;
 
 import java.io.File;
 import java.util.concurrent.TimeUnit;
 
+
 /**
  * Abstract class to keep common functionality.
  */
 public abstract class AfwTestTargetPreparer {
 
     private CtsBuildHelper mBuildHelper;
+    private Long mTimeoutSecs = TimeUnit.MINUTES.toSeconds(5);
 
     /**
      * Gets an instance of {@link CtsBuildHelper}.
@@ -135,5 +140,43 @@
 
         return TestConfig.getInstance().getTimeoutSize();
     }
+
+    /**
+     * Wipes device via fastboot.
+     *
+     * <p>
+     * Refers to com.android.tradefed.targetprep.DeviceWiper
+     * </p>
+     *
+     * @param device test device
+     */
+    protected void wipeDevice(ITestDevice device)
+            throws DeviceNotAvailableException, TargetSetupError {
+
+        CLog.i(String.format("Wiping device %s".format(device.getSerialNumber())));
+
+        device.rebootIntoBootloader();
+        fastbootFormat(device, "cache");
+        fastbootFormat(device, "userdata");
+        device.executeFastbootCommand("reboot");
+    }
+
+
+    /**
+     * Performs fastboot erase/format operation on certain partition
+     *
+     * @param device test device
+     * @param partition android partition, e.g. userdata, cache, system
+     */
+    protected void fastbootFormat(ITestDevice device, String partition)
+            throws DeviceNotAvailableException, TargetSetupError {
+
+        CLog.i("Attempting: fastboot format %s", partition);
+        CommandResult r = device.executeLongFastbootCommand("format", partition);
+        if (r.getStatus() != CommandStatus.SUCCESS) {
+            throw new TargetSetupError(
+                    String.format("format %s failed: %s", partition, r.getStderr()));
+        }
+    }
 }
 
diff --git a/tools/tradefed-host/src/com/android/afwtest/tradefed/targetprep/AfwTestWifiPreparer.java b/tools/tradefed-host/src/com/android/afwtest/tradefed/targetprep/AfwTestWifiPreparer.java
index 1a039a4..ffa9def 100644
--- a/tools/tradefed-host/src/com/android/afwtest/tradefed/targetprep/AfwTestWifiPreparer.java
+++ b/tools/tradefed-host/src/com/android/afwtest/tradefed/targetprep/AfwTestWifiPreparer.java
@@ -91,8 +91,13 @@
 
         // Implement retry.
         for (int i = 0; i < mWifiAttempts; ++i) {
-            if (device.connectToWifiNetworkIfNeeded(wifiSsid, wifiPassword)) {
-                return;
+            try {
+
+                if (device.connectToWifiNetworkIfNeeded(wifiSsid, wifiPassword)) {
+                    return;
+                }
+            } catch(Exception e) {
+                CLog.e("Error trying to connect to Wifi:", e);
             }
             boolean lastAttempt = (i + 1) == mWifiAttempts;
             if (!lastAttempt) {