Set parole_duration=0 for CtsHostsideNetworkTests.

Some of the tests in CtsHostsideNetworkTests module will put the
test app in App standby mode and check the network availability.
If the device enters into parole state during the test, then the test
fails. Set the parole_duration to 0 so that it won't parole state
won't affect these tests.

Bug: 63140218
Test: cts-tradefed run singleCommand cts-dev -m CtsHostsideNetworkTests -t \
      com.android.cts.net.HostsideRestrictBackgroundNetworkTests

Change-Id: I8158eba74f03e82566de1de9f94b981b17175df5
diff --git a/tests/cts/hostside/AndroidTest.xml b/tests/cts/hostside/AndroidTest.xml
index ad7f81b..4a2e2e3 100644
--- a/tests/cts/hostside/AndroidTest.xml
+++ b/tests/cts/hostside/AndroidTest.xml
@@ -15,6 +15,7 @@
 -->
 <configuration description="Config for CTS net host test cases">
     <option name="config-descriptor:metadata" key="component" value="networking" />
+    <target_preparer class="com.android.cts.net.NetPolicyTestsPreparer" />
     <test class="com.android.compatibility.common.tradefed.testtype.JarHostTest" >
         <option name="jar" value="CtsHostsideNetworkTests.jar" />
         <option name="runtime-hint" value="3m56s" />
diff --git a/tests/cts/hostside/src/com/android/cts/net/NetPolicyTestsPreparer.java b/tests/cts/hostside/src/com/android/cts/net/NetPolicyTestsPreparer.java
new file mode 100644
index 0000000..9b19554
--- /dev/null
+++ b/tests/cts/hostside/src/com/android/cts/net/NetPolicyTestsPreparer.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.cts.net;
+
+import com.android.tradefed.build.IBuildInfo;
+import com.android.tradefed.device.DeviceNotAvailableException;
+import com.android.tradefed.device.ITestDevice;
+import com.android.tradefed.log.LogUtil;
+import com.android.tradefed.targetprep.ITargetCleaner;
+import com.android.tradefed.targetprep.ITargetPreparer;
+
+public class NetPolicyTestsPreparer implements ITargetPreparer, ITargetCleaner {
+    private final static String KEY_PAROLE_DURATION = "parole_duration";
+    private final static String DESIRED_PAROLE_DURATION = "0";
+
+    private boolean mAppIdleConstsUpdated;
+    private String mOriginalAppIdleConsts;
+
+    @Override
+    public void setUp(ITestDevice device, IBuildInfo buildInfo) throws DeviceNotAvailableException {
+        updateParoleDuration(device);
+        LogUtil.CLog.d("Original app_idle_constants: " + mOriginalAppIdleConsts);
+    }
+
+    @Override
+    public void tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable throwable)
+            throws DeviceNotAvailableException {
+        if (mAppIdleConstsUpdated) {
+            executeCmd(device, "settings put global app_idle_constants " + mOriginalAppIdleConsts);
+        }
+    }
+
+    /**
+     * Updates parole_duration with the desired value.
+     */
+    private void updateParoleDuration(ITestDevice device) throws DeviceNotAvailableException {
+        mOriginalAppIdleConsts = executeCmd(device, "settings get global app_idle_constants");
+        String newAppIdleConstants;
+        final String newConstant = KEY_PAROLE_DURATION + "=" + DESIRED_PAROLE_DURATION;
+        if (mOriginalAppIdleConsts == null || "null".equals(mOriginalAppIdleConsts)) {
+            // app_idle_constants is initially empty, so just assign the desired value.
+            newAppIdleConstants = newConstant;
+        } else if (mOriginalAppIdleConsts.contains(KEY_PAROLE_DURATION)) {
+            // app_idle_constants contains parole_duration, so replace it with the desired value.
+            newAppIdleConstants = mOriginalAppIdleConsts.replaceAll(
+                    KEY_PAROLE_DURATION + "=\\d+", newConstant);
+        } else {
+            // app_idle_constants didn't have parole_duration, so append the desired value.
+            newAppIdleConstants = mOriginalAppIdleConsts + "," + newConstant;
+        }
+        executeCmd(device, "settings put global app_idle_constants " + newAppIdleConstants);
+        mAppIdleConstsUpdated = true;
+    }
+
+    private String executeCmd(ITestDevice device, String cmd)
+            throws DeviceNotAvailableException {
+        final String output = device.executeShellCommand(cmd).trim();
+        LogUtil.CLog.d("Output for '%s': %s", cmd, output);
+        return output;
+    }
+}