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 f5f5fafa31..668669bc2e 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 @@ -15,6 +15,10 @@ */ package com.android.cts.net.hostside.app2; +import android.content.Context; +import android.content.pm.PackageManager.NameNotFoundException; +import android.util.Log; + public final class Common { static final String TAG = "CtsNetApp2"; @@ -35,4 +39,13 @@ public final class Common { static final char RESULT_SEPARATOR = ';'; static final String STATUS_NETWORK_UNAVAILABLE_PREFIX = "NetworkUnavailable:"; static final String STATUS_NETWORK_AVAILABLE_PREFIX = "NetworkAvailable:"; + + static int getUid(Context context) { + final String packageName = context.getPackageName(); + try { + return context.getPackageManager().getPackageUid(packageName, 0); + } catch (NameNotFoundException e) { + throw new IllegalStateException("Could not get UID for " + packageName, e); + } + } } 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 94ec6af7ee..07f717b192 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 @@ -27,6 +27,7 @@ import static com.android.cts.net.hostside.app2.Common.RESULT_SEPARATOR; import static com.android.cts.net.hostside.app2.Common.STATUS_NETWORK_AVAILABLE_PREFIX; import static com.android.cts.net.hostside.app2.Common.STATUS_NETWORK_UNAVAILABLE_PREFIX; import static com.android.cts.net.hostside.app2.Common.TAG; +import static com.android.cts.net.hostside.app2.Common.getUid; import java.net.HttpURLConnection; import java.net.URL; @@ -110,7 +111,7 @@ public class MyBroadcastReceiver extends BroadcastReceiver { final int apiStatus = cm.getRestrictBackgroundStatus(); String netStatus; try { - netStatus = checkNetworkStatus(cm); + netStatus = checkNetworkStatus(context, cm); } catch (InterruptedException e) { Log.e(TAG, "Timeout checking network status"); setResultData(null); @@ -124,7 +125,8 @@ public class MyBroadcastReceiver extends BroadcastReceiver { setResultData(data.toString()); } - private String checkNetworkStatus(final ConnectivityManager cm) throws InterruptedException { + private String checkNetworkStatus(final Context context, final ConnectivityManager cm) + throws InterruptedException { final LinkedBlockingQueue result = new LinkedBlockingQueue<>(1); new Thread(new Runnable() { @@ -134,7 +136,7 @@ public class MyBroadcastReceiver extends BroadcastReceiver { final String address = "http://example.com"; final NetworkInfo networkInfo = cm.getActiveNetworkInfo(); Log.d(TAG, "Running checkNetworkStatus() on thread " - + Thread.currentThread().getName() + + Thread.currentThread().getName() + " for UID " + getUid(context) + "\n\tactiveNetworkInfo: " + networkInfo + "\n\tURL: " + address); String prefix = STATUS_NETWORK_AVAILABLE_PREFIX; try { @@ -151,7 +153,9 @@ public class MyBroadcastReceiver extends BroadcastReceiver { Log.d(TAG, "Exception getting " + address + ": " + e); prefix = STATUS_NETWORK_UNAVAILABLE_PREFIX + "Exception " + e + ":"; } - result.offer(prefix + networkInfo); + final String netInfo = prefix + networkInfo; + Log.d(TAG, "Offering " + netInfo); + result.offer(netInfo); } }, mName).start(); return result.poll(NETWORK_TIMEOUT_MS * 2, TimeUnit.MILLISECONDS);