Asserts foreground apps always have network access.

BUG: 28473659
Change-Id: Iea6933a4630ff2e9c00a2d2e9e4a6f1a51de70f2
diff --git a/tests/cts/hostside/app2/AndroidManifest.xml b/tests/cts/hostside/app2/AndroidManifest.xml
index 9ce5781..9c4884b 100644
--- a/tests/cts/hostside/app2/AndroidManifest.xml
+++ b/tests/cts/hostside/app2/AndroidManifest.xml
@@ -31,6 +31,7 @@
          test app.
     -->
     <application>
+        <activity android:name=".MyActivity" android:exported="true"/>
         <service android:name=".MyService" android:exported="true"/>
         <service android:name=".MyForegroundService" android:exported="true"/>
 
@@ -38,6 +39,7 @@
             <intent-filter>
                 <action android:name="android.net.conn.RESTRICT_BACKGROUND_CHANGED" />
                 <action android:name="com.android.cts.net.hostside.app2.action.GET_COUNTERS" />
+                <action android:name="com.android.cts.net.hostside.app2.action.GET_RESTRICT_BACKGROUND_STATUS" />
                 <action android:name="com.android.cts.net.hostside.app2.action.CHECK_NETWORK" />
             </intent-filter>
         </receiver>
diff --git a/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/Common.java b/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/Common.java
index d247c31..ed58184 100644
--- a/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/Common.java
+++ b/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/Common.java
@@ -28,6 +28,8 @@
     static final String DYNAMIC_RECEIVER = "DynamicReceiver";
     static final String ACTION_GET_COUNTERS =
             "com.android.cts.net.hostside.app2.action.GET_COUNTERS";
+    static final String ACTION_GET_RESTRICT_BACKGROUND_STATUS =
+            "com.android.cts.net.hostside.app2.action.GET_RESTRICT_BACKGROUND_STATUS";
     static final String ACTION_CHECK_NETWORK =
             "com.android.cts.net.hostside.app2.action.CHECK_NETWORK";
     static final String ACTION_RECEIVER_READY =
diff --git a/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyActivity.java b/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyActivity.java
new file mode 100644
index 0000000..7c6b504
--- /dev/null
+++ b/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyActivity.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2016 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.hostside.app2;
+
+import static com.android.cts.net.hostside.app2.Common.TAG;
+import android.app.Activity;
+import android.util.Log;
+
+/**
+ * Activity used to bring process to foreground.
+ */
+public class MyActivity extends Activity {
+
+    @Override
+    protected void onStart() {
+        super.onStart();
+        Log.d(TAG, "MyActivity.onStart()");
+    }
+
+    @Override
+    protected void onDestroy() {
+        Log.d(TAG, "MyActivity.onDestroy()");
+        super.onDestroy();
+    }
+}
diff --git a/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyBroadcastReceiver.java b/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyBroadcastReceiver.java
index e8a959c..b876276 100644
--- a/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyBroadcastReceiver.java
+++ b/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyBroadcastReceiver.java
@@ -19,6 +19,7 @@
 import static android.net.ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED;
 import static com.android.cts.net.hostside.app2.Common.ACTION_CHECK_NETWORK;
 import static com.android.cts.net.hostside.app2.Common.ACTION_GET_COUNTERS;
+import static com.android.cts.net.hostside.app2.Common.ACTION_GET_RESTRICT_BACKGROUND_STATUS;
 import static com.android.cts.net.hostside.app2.Common.ACTION_RECEIVER_READY;
 import static com.android.cts.net.hostside.app2.Common.EXTRA_ACTION;
 import static com.android.cts.net.hostside.app2.Common.EXTRA_RECEIVER_NAME;
@@ -73,6 +74,9 @@
             case ACTION_GET_COUNTERS:
                 setResultDataFromCounter(context, intent);
                 break;
+            case ACTION_GET_RESTRICT_BACKGROUND_STATUS:
+                getRestrictBackgroundStatus(context, intent);
+                break;
             case ACTION_CHECK_NETWORK:
                 checkNetwork(context, intent);
                 break;
@@ -101,31 +105,30 @@
         return value;
     }
 
+    private void getRestrictBackgroundStatus(Context context, Intent intent) {
+        final ConnectivityManager cm = (ConnectivityManager) context
+                .getSystemService(Context.CONNECTIVITY_SERVICE);
+        final int apiStatus = cm.getRestrictBackgroundStatus();
+        Log.d(TAG, "getRestrictBackgroundStatus: returning " + apiStatus);
+        setResultData(Integer.toString(apiStatus));
+    }
+
     private void checkNetwork(final Context context, Intent intent) {
         final ConnectivityManager cm = (ConnectivityManager) context
                 .getSystemService(Context.CONNECTIVITY_SERVICE);
 
-        final StringBuilder data = new StringBuilder();
-        final int apiStatus = cm.getRestrictBackgroundStatus();
-        String netStatus;
+        String netStatus = null;
         try {
             netStatus = checkNetworkStatus(context, cm);
         } catch (InterruptedException e) {
             Log.e(TAG, "Timeout checking network status");
-            setResultData(null);
-            return;
         }
-        data.append(apiStatus).append(RESULT_SEPARATOR);
-        if (netStatus != null) {
-            data.append(netStatus);
-        }
-        Log.d(TAG, "checkNetwork: returning " + data);
-        setResultData(data.toString());
+        Log.d(TAG, "checkNetwork(): returning " + netStatus);
+        setResultData(netStatus);
     }
 
 
     private static final String NETWORK_STATUS_TEMPLATE = "%s|%s|%s|%s|%s";
-
     /**
      * Checks whether the network is available and return a string which can then be send as a
      * result data for the ordered broadcast.