Remove ConnOnActivityStartTest.

This is not needed anymore. We added a similar
test in CTS.

Bug: 226299593
Test: treehugger
Change-Id: Idb0d1ff5f897af91a84cb124d2be9d18ee7e75a8
This commit is contained in:
Sudheer Shanka
2022-04-27 17:12:22 -07:00
parent c85e97a7a1
commit ae884781b7
11 changed files with 0 additions and 760 deletions

View File

@@ -19,9 +19,6 @@ android_test {
"src/**/*.java",
"src/**/*.kt",
"aidl/com/android/servicestests/aidl/INetworkStateObserver.aidl",
"aidl/com/android/servicestests/aidl/ICmdReceiverService.aidl",
"test-apps/JobTestApp/src/**/*.java",
"test-apps/SuspendTestApp/src/**/*.java",
@@ -67,10 +64,6 @@ android_test {
"ActivityContext",
],
aidl: {
local_include_dirs: ["aidl"],
},
libs: [
"android.hardware.power-V1-java",
"android.hardware.tv.cec-V1.0-java",

View File

@@ -28,7 +28,6 @@
<option name="install-arg" value="-t" />
<option name="test-file-name" value="FrameworksServicesTests.apk" />
<option name="test-file-name" value="JobTestApp.apk" />
<option name="test-file-name" value="ConnTestApp.apk" />
<option name="test-file-name" value="SuspendTestApp.apk" />
<option name="test-file-name" value="SimpleServiceTestApp1.apk" />
<option name="test-file-name" value="SimpleServiceTestApp2.apk" />

View File

@@ -1,31 +0,0 @@
// 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 {
// See: http://go/android-license-faq
// A large-scale-change added 'default_applicable_licenses' to import
// all of the 'license_kinds' from "frameworks_base_license"
// to get the below license kinds:
// SPDX-license-identifier-Apache-2.0
default_applicable_licenses: ["frameworks_base_license"],
}
java_library {
name: "servicestests-aidl",
sdk_version: "current",
srcs: [
"com/android/servicestests/aidl/INetworkStateObserver.aidl",
"com/android/servicestests/aidl/ICmdReceiverService.aidl",
],
}

View File

@@ -1,21 +0,0 @@
/*
* 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.servicestests.aidl;
interface ICmdReceiverService {
void finishActivity();
}

View File

@@ -1,27 +0,0 @@
/*
* 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.servicestests.aidl;
oneway interface INetworkStateObserver {
/**
* {@param resultData} will be in the format
* NetinfoState|NetinfoDetailedState|RealConnectionCheck|RealConnectionCheckDetails|Netinfo.
* For detailed info, see
* servicestests/test-apps/ConnTestApp/.../ConnTestActivity#checkNetworkStatus
*/
void onNetworkStateChecked(String resultData);
}

View File

@@ -1,420 +0,0 @@
/*
* 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.server.net;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.SystemClock;
import android.support.test.uiautomator.UiDevice;
import android.text.TextUtils;
import android.util.Log;
import androidx.test.InstrumentationRegistry;
import androidx.test.filters.LargeTest;
import androidx.test.runner.AndroidJUnit4;
import com.android.servicestests.aidl.ICmdReceiverService;
import com.android.servicestests.aidl.INetworkStateObserver;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
/**
* Tests for verifying network availability on activity start.
*
* To run the tests, use
*
* runtest -c com.android.server.net.ConnOnActivityStartTest frameworks-services
*
* or the following steps:
*
* Build: m FrameworksServicesTests
* Install: adb install -r \
* ${ANDROID_PRODUCT_OUT}/data/app/FrameworksServicesTests/FrameworksServicesTests.apk
* Run: adb shell am instrument -e class com.android.server.net.ConnOnActivityStartTest -w \
* com.android.frameworks.servicestests/androidx.test.runner.AndroidJUnitRunner
*/
@LargeTest
@RunWith(AndroidJUnit4.class)
public class ConnOnActivityStartTest {
private static final String TAG = ConnOnActivityStartTest.class.getSimpleName();
private static final String TEST_PKG = "com.android.servicestests.apps.conntestapp";
private static final String TEST_ACTIVITY_CLASS = TEST_PKG + ".ConnTestActivity";
private static final String TEST_SERVICE_CLASS = TEST_PKG + ".CmdReceiverService";
private static final String EXTRA_NETWORK_STATE_OBSERVER = TEST_PKG + ".observer";
private static final long BATTERY_OFF_TIMEOUT_MS = 2000; // 2 sec
private static final long BATTERY_OFF_CHECK_INTERVAL_MS = 200; // 0.2 sec
private static final long NETWORK_CHECK_TIMEOUT_MS = 4000; // 4 sec
private static final long SCREEN_ON_DELAY_MS = 2000; // 2 sec
private static final long BIND_SERVICE_TIMEOUT_SEC = 4;
private static final int REPEAT_TEST_COUNT = 5;
private static Context mContext;
private static UiDevice mUiDevice;
private static int mTestPkgUid;
private static BatteryManager mBatteryManager;
private static ServiceConnection mServiceConnection;
private static ICmdReceiverService mCmdReceiverService;
@BeforeClass
public static void setUpOnce() throws Exception {
mContext = InstrumentationRegistry.getContext();
mUiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
mContext.getPackageManager().setApplicationEnabledSetting(TEST_PKG,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0);
mTestPkgUid = mContext.getPackageManager().getPackageUid(TEST_PKG, 0);
mBatteryManager = (BatteryManager) mContext.getSystemService(Context.BATTERY_SERVICE);
bindService();
}
@AfterClass
public static void tearDownOnce() throws Exception {
batteryReset();
unbindService();
}
private static void bindService() throws Exception {
final CountDownLatch bindLatch = new CountDownLatch(1);
mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i(TAG, "Service connected");
mCmdReceiverService = ICmdReceiverService.Stub.asInterface(service);
bindLatch.countDown();
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i(TAG, "Service disconnected");
}
};
final Intent intent = new Intent()
.setComponent(new ComponentName(TEST_PKG, TEST_SERVICE_CLASS));
// Needs to use BIND_ALLOW_OOM_MANAGEMENT and BIND_NOT_FOREGROUND so that the test app
// does not run in the same process state as this app.
mContext.bindService(intent, mServiceConnection,
Context.BIND_AUTO_CREATE
| Context.BIND_ALLOW_OOM_MANAGEMENT
| Context.BIND_NOT_FOREGROUND);
if (!bindLatch.await(BIND_SERVICE_TIMEOUT_SEC, TimeUnit.SECONDS)) {
fail("Timed out waiting for the service to bind in " + mTestPkgUid);
}
}
private static void unbindService() {
if (mCmdReceiverService != null) {
mContext.unbindService(mServiceConnection);
}
}
@Test
public void testStartActivity_batterySaver() throws Exception {
setBatterySaverMode(true);
try {
testConnOnActivityStart("testStartActivity_batterySaver");
} finally {
setBatterySaverMode(false);
}
}
@Test
public void testStartActivity_dataSaver() throws Exception {
setDataSaverMode(true);
try {
testConnOnActivityStart("testStartActivity_dataSaver");
} finally {
setDataSaverMode(false);
}
}
@Test
public void testStartActivity_dozeMode() throws Exception {
setDozeMode(true);
try {
testConnOnActivityStart("testStartActivity_dozeMode");
} finally {
setDozeMode(false);
}
}
@Test
public void testStartActivity_appStandby() throws Exception {
try{
turnBatteryOn();
setAppIdle(true);
turnScreenOn();
startActivityAndCheckNetworkAccess();
} finally {
turnBatteryOff();
finishActivity();
setAppIdle(false);
}
}
@Test
public void testStartActivity_backgroundRestrict() throws Exception {
updateRestrictBackgroundBlacklist(true);
try {
testConnOnActivityStart("testStartActivity_backgroundRestrict");
} finally {
updateRestrictBackgroundBlacklist(false);
}
}
private void testConnOnActivityStart(String testName) throws Exception {
for (int i = 1; i <= REPEAT_TEST_COUNT; ++i) {
try {
Log.d(TAG, testName + " Start #" + i);
turnScreenOn();
startActivityAndCheckNetworkAccess();
} finally {
finishActivity();
Log.d(TAG, testName + " end #" + i);
}
}
}
// TODO: Some of these methods are also used in CTS, so instead of duplicating code,
// create a static library which can be used by both servicestests and cts.
private void setBatterySaverMode(boolean enabled) throws Exception {
if (enabled) {
turnBatteryOn();
executeCommand("settings put global low_power 1");
} else {
executeCommand("settings put global low_power 0");
turnBatteryOff();
}
final String result = executeCommand("settings get global low_power");
assertEquals(enabled ? "1" : "0", result);
}
private void setDataSaverMode(boolean enabled) throws Exception {
executeCommand("cmd netpolicy set restrict-background " + enabled);
final String output = executeCommand("cmd netpolicy get restrict-background");
final String expectedSuffix = enabled ? "enabled" : "disabled";
assertTrue("output '" + output + "' should end with '" + expectedSuffix + "'",
output.endsWith(expectedSuffix));
}
private void setDozeMode(boolean enabled) throws Exception {
if (enabled) {
turnBatteryOn();
turnScreenOff();
executeCommand("dumpsys deviceidle force-idle deep");
} else {
turnScreenOn();
turnBatteryOff();
executeCommand("dumpsys deviceidle unforce");
}
assertDelayedCommandResult("dumpsys deviceidle get deep", enabled ? "IDLE" : "ACTIVE",
5 /* maxTries */, 500 /* napTimeMs */);
}
private void setAppIdle(boolean enabled) throws Exception {
executeCommand("am set-inactive " + TEST_PKG + " " + enabled);
assertDelayedCommandResult("am get-inactive " + TEST_PKG, "Idle=" + enabled,
15 /* maxTries */, 2000 /* napTimeMs */);
}
private void updateRestrictBackgroundBlacklist(boolean add) throws Exception {
if (add) {
executeCommand("cmd netpolicy add restrict-background-blacklist " + mTestPkgUid);
} else {
executeCommand("cmd netpolicy remove restrict-background-blacklist " + mTestPkgUid);
}
assertRestrictBackground("restrict-background-blacklist", mTestPkgUid, add);
}
private void assertRestrictBackground(String list, int uid, boolean expected) throws Exception {
final int maxTries = 5;
boolean actual = false;
final String expectedUid = Integer.toString(uid);
String uids = "";
for (int i = 1; i <= maxTries; i++) {
final String output = executeCommand("cmd netpolicy list " + list);
uids = output.split(":")[1];
for (String candidate : uids.split(" ")) {
actual = candidate.trim().equals(expectedUid);
if (expected == actual) {
return;
}
}
Log.v(TAG, list + " check for uid " + uid + " doesn't match yet (expected "
+ expected + ", got " + actual + "); sleeping 1s before polling again");
SystemClock.sleep(1000);
}
fail(list + " check for uid " + uid + " failed: expected " + expected + ", got " + actual
+ ". Full list: " + uids);
}
private void turnBatteryOn() throws Exception {
executeCommand("cmd battery unplug");
executeCommand("cmd battery set status " + BatteryManager.BATTERY_STATUS_NOT_CHARGING);
assertBatteryOn();
}
private void assertBatteryOn() throws Exception {
final long endTime = SystemClock.uptimeMillis() + BATTERY_OFF_TIMEOUT_MS;
while (mBatteryManager.isCharging() && SystemClock.uptimeMillis() < endTime) {
SystemClock.sleep(BATTERY_OFF_CHECK_INTERVAL_MS);
}
assertFalse("Power should be disconnected", mBatteryManager.isCharging());
}
private void turnBatteryOff() throws Exception {
executeCommand("cmd battery set ac " + BatteryManager.BATTERY_PLUGGED_AC);
executeCommand("cmd battery set status " + BatteryManager.BATTERY_STATUS_CHARGING);
}
private static void batteryReset() throws Exception {
executeCommand("cmd battery reset");
}
private void turnScreenOff() throws Exception {
executeCommand("input keyevent KEYCODE_SLEEP");
}
private void turnScreenOn() throws Exception {
executeCommand("input keyevent KEYCODE_WAKEUP");
executeCommand("wm dismiss-keyguard");
// Wait for screen-on state to propagate through the system.
SystemClock.sleep(SCREEN_ON_DELAY_MS);
}
private static String executeCommand(String cmd) throws IOException {
final String result = executeSilentCommand(cmd);
Log.d(TAG, String.format("Result for '%s': %s", cmd, result));
return result;
}
private static String executeSilentCommand(String cmd) throws IOException {
return mUiDevice.executeShellCommand(cmd).trim();
}
private void assertDelayedCommandResult(String cmd, String expectedResult,
int maxTries, int napTimeMs) throws Exception {
String result = "";
for (int i = 1; i <= maxTries; ++i) {
result = executeCommand(cmd);
if (expectedResult.equals(result)) {
return;
}
Log.v(TAG, "Command '" + cmd + "' returned '" + result + " instead of '"
+ expectedResult + "' on attempt #" + i
+ "; sleeping " + napTimeMs + "ms before trying again");
SystemClock.sleep(napTimeMs);
}
fail("Command '" + cmd + "' did not return '" + expectedResult + "' after "
+ maxTries + " attempts. Last result: '" + result + "'");
}
private void startActivityAndCheckNetworkAccess() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final Intent launchIntent = new Intent().setComponent(
new ComponentName(TEST_PKG, TEST_ACTIVITY_CLASS));
final Bundle extras = new Bundle();
final String[] errors = new String[] {null};
extras.putBinder(EXTRA_NETWORK_STATE_OBSERVER, new INetworkStateObserver.Stub() {
@Override
public void onNetworkStateChecked(String resultData) {
errors[0] = resultData;
latch.countDown();
}
});
launchIntent.putExtras(extras)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(launchIntent);
if (latch.await(NETWORK_CHECK_TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
if (errors[0] != null) {
fail("Network not available for test app " + mTestPkgUid + ". " + errors[0]);
}
} else {
fail("Timed out waiting for network availability status from test app " + mTestPkgUid);
}
}
private static void fail(String msg) throws Exception {
dumpOnFailure();
Assert.fail(msg);
}
private static void dumpOnFailure() throws Exception {
dump("network_management");
dump("netpolicy");
dumpUsageStats();
}
private static void dumpUsageStats() throws Exception {
final String output = executeSilentCommand("dumpsys usagestats");
final StringBuilder sb = new StringBuilder();
final TextUtils.SimpleStringSplitter splitter = new TextUtils.SimpleStringSplitter('\n');
splitter.setString(output);
String str;
while (splitter.hasNext()) {
str = splitter.next();
if (str.contains("package=") && !str.contains(TEST_PKG)) {
continue;
}
if (str.trim().startsWith("config=") || str.trim().startsWith("time=")) {
continue;
}
sb.append(str).append('\n');
}
dump("usagestats", sb.toString());
}
private static void dump(String service) throws Exception {
dump(service, executeSilentCommand("dumpsys " + service));
}
private static void dump(String service, String dump) throws Exception {
Log.d(TAG, ">>> Begin dump " + service);
Log.printlns(Log.LOG_ID_MAIN, Log.DEBUG, TAG, dump, null);
Log.d(TAG, "<<< End dump " + service);
}
private void finishActivity() throws Exception {
mCmdReceiverService.finishActivity();
}
}

View File

@@ -1,40 +0,0 @@
// 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 {
// See: http://go/android-license-faq
// A large-scale-change added 'default_applicable_licenses' to import
// all of the 'license_kinds' from "frameworks_base_license"
// to get the below license kinds:
// SPDX-license-identifier-Apache-2.0
default_applicable_licenses: ["frameworks_base_license"],
}
android_test_helper_app {
name: "ConnTestApp",
test_suites: ["device-tests"],
static_libs: ["servicestests-aidl"],
srcs: ["**/*.java"],
platform_apis: true,
certificate: "platform",
dex_preopt: {
enabled: false,
},
optimize: {
enabled: false,
},
}

View File

@@ -1,30 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 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.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.servicestests.apps.conntestapp">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.OBSERVE_NETWORK_POLICY" />
<application>
<activity android:name=".ConnTestActivity"
android:exported="true" />
<service android:name=".CmdReceiverService"
android:exported="true" />
</application>
</manifest>

View File

@@ -1 +0,0 @@
include /services/core/java/com/android/server/net/OWNERS

View File

@@ -1,36 +0,0 @@
/*
* 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.servicestests.apps.conntestapp;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import com.android.servicestests.aidl.ICmdReceiverService;
public class CmdReceiverService extends Service {
private ICmdReceiverService.Stub mBinder = new ICmdReceiverService.Stub() {
@Override
public void finishActivity() {
ConnTestActivity.finishSelf();
}
};
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}

View File

@@ -1,146 +0,0 @@
/*
* 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.servicestests.apps.conntestapp;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.INetworkPolicyManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.INetworkManagementService;
import android.os.Process;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;
import com.android.internal.annotations.GuardedBy;
import com.android.servicestests.aidl.INetworkStateObserver;
public class ConnTestActivity extends Activity {
private static final String TAG = ConnTestActivity.class.getSimpleName();
private static final String TEST_PKG = ConnTestActivity.class.getPackage().getName();
private static final String ACTION_FINISH_ACTIVITY = TEST_PKG + ".FINISH";
private static final String EXTRA_NETWORK_STATE_OBSERVER = TEST_PKG + ".observer";
private static final Object INSTANCE_LOCK = new Object();
@GuardedBy("instanceLock")
private static Activity sInstance;
private BroadcastReceiver finishCommandReceiver = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
synchronized (INSTANCE_LOCK) {
sInstance = this;
}
Log.i(TAG, "onCreate called");
notifyNetworkStateObserver();
finishCommandReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "finish command received");
ConnTestActivity.this.finish();
}
};
registerReceiver(finishCommandReceiver, new IntentFilter(ACTION_FINISH_ACTIVITY));
}
@Override
public void onResume() {
super.onResume();
Log.i(TAG, "onResume called");
}
@Override
public void onStop() {
Log.i(TAG, "onStop called");
if (finishCommandReceiver != null) {
unregisterReceiver(finishCommandReceiver);
finishCommandReceiver = null;
}
super.onStop();
}
@Override
public void finish() {
synchronized (INSTANCE_LOCK) {
sInstance = null;
}
Log.i(TAG, "finish called");
super.finish();
}
public static void finishSelf() {
synchronized (INSTANCE_LOCK) {
if (sInstance != null) {
sInstance.finish();
}
}
}
private void notifyNetworkStateObserver() {
if (getIntent() == null) {
return;
}
final Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
}
final INetworkStateObserver observer = INetworkStateObserver.Stub.asInterface(
extras.getBinder(EXTRA_NETWORK_STATE_OBSERVER));
if (observer != null) {
AsyncTask.execute(() -> {
try {
observer.onNetworkStateChecked(checkNetworkStatus());
} catch (RemoteException e) {
Log.e(TAG, "Error occured while notifying the observer: " + e);
}
});
}
}
/**
* Checks whether the network is restricted.
*
* @return null if network is not restricted, otherwise an error message.
*/
private String checkNetworkStatus() {
final INetworkManagementService nms = INetworkManagementService.Stub.asInterface(
ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
final INetworkPolicyManager npms = INetworkPolicyManager.Stub.asInterface(
ServiceManager.getService(Context.NETWORK_POLICY_SERVICE));
try {
final boolean restrictedByFwRules = nms.isNetworkRestricted(Process.myUid());
final boolean restrictedByUidRules = npms.isUidNetworkingBlocked(Process.myUid(), true);
if (restrictedByFwRules || restrictedByUidRules) {
return "Network is restricted by fwRules: " + restrictedByFwRules
+ " and uidRules: " + restrictedByUidRules;
}
return null;
} catch (RemoteException e) {
return "Error talking to system server: " + e;
}
}
}