Print uid when running network checks.

BUG: 27570398
Change-Id: I4dcbd4e41641c406c687a0e29e2637581d91b17c
This commit is contained in:
Felipe Leme
2016-03-09 12:28:36 -08:00
parent b3cc6ef3ae
commit f448ccd08d
2 changed files with 21 additions and 4 deletions

View File

@@ -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);
}
}
}

View File

@@ -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<String> 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);