Skip to content

Commit 496a9d2

Browse files
lcolittiAndroid (Google) Code Review
authored andcommitted
Merge "Stop supporting legacy ConnectivityManager routing methods in M." into mnc-dev
2 parents 02565c3 + ffc42b0 commit 496a9d2

3 files changed

Lines changed: 74 additions & 5 deletions

File tree

core/java/android/app/SystemServiceRegistry.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,12 @@ public ClipboardManager createService(ContextImpl ctx) {
220220
SYSTEM_SERVICE_NAMES.put(android.text.ClipboardManager.class, Context.CLIPBOARD_SERVICE);
221221

222222
registerService(Context.CONNECTIVITY_SERVICE, ConnectivityManager.class,
223-
new StaticServiceFetcher<ConnectivityManager>() {
223+
new StaticOuterContextServiceFetcher<ConnectivityManager>() {
224224
@Override
225-
public ConnectivityManager createService() {
225+
public ConnectivityManager createService(Context context) {
226226
IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
227-
return new ConnectivityManager(IConnectivityManager.Stub.asInterface(b));
227+
IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
228+
return new ConnectivityManager(context, service);
228229
}});
229230

230231
registerService(Context.COUNTRY_DETECTOR, CountryDetector.class,
@@ -793,4 +794,30 @@ public final T getService(ContextImpl unused) {
793794

794795
public abstract T createService();
795796
}
797+
798+
/**
799+
* Like StaticServiceFetcher, creates only one instance of the service per process, but when
800+
* creating the service for the first time, passes it the outer context of the creating
801+
* component.
802+
*
803+
* TODO: Is this safe in the case where multiple applications share the same process?
804+
* TODO: Delete this once its only user (ConnectivityManager) is known to work well in the
805+
* case where multiple application components each have their own ConnectivityManager object.
806+
*/
807+
static abstract class StaticOuterContextServiceFetcher<T> implements ServiceFetcher<T> {
808+
private T mCachedInstance;
809+
810+
@Override
811+
public final T getService(ContextImpl ctx) {
812+
synchronized (StaticOuterContextServiceFetcher.this) {
813+
if (mCachedInstance == null) {
814+
mCachedInstance = createService(ctx.getOuterContext());
815+
}
816+
return mCachedInstance;
817+
}
818+
}
819+
820+
public abstract T createService(Context applicationContext);
821+
}
822+
796823
}

core/java/android/net/ConnectivityManager.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import android.app.PendingIntent;
2323
import android.content.Context;
2424
import android.content.Intent;
25+
import android.content.pm.PackageManager;
2526
import android.net.NetworkUtils;
2627
import android.os.Binder;
2728
import android.os.Build.VERSION_CODES;
@@ -33,6 +34,7 @@
3334
import android.os.Looper;
3435
import android.os.Message;
3536
import android.os.Messenger;
37+
import android.os.Process;
3638
import android.os.RemoteException;
3739
import android.os.ServiceManager;
3840
import android.provider.Settings;
@@ -490,6 +492,8 @@ public class ConnectivityManager {
490492
*/
491493
private static ConnectivityManager sInstance;
492494

495+
private final Context mContext;
496+
493497
private INetworkManagementService mNMService;
494498

495499
/**
@@ -892,8 +896,11 @@ public NetworkCapabilities getNetworkCapabilities(Network network) {
892896
*
893897
* @deprecated Deprecated in favor of the cleaner
894898
* {@link #requestNetwork(NetworkRequest, NetworkCallback)} API.
899+
* In {@link VERSION_CODES#MNC}, and above, this method is unsupported and will
900+
* throw {@code UnsupportedOperationException} if called.
895901
*/
896902
public int startUsingNetworkFeature(int networkType, String feature) {
903+
checkLegacyRoutingApiAccess();
897904
NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
898905
if (netCap == null) {
899906
Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " +
@@ -939,8 +946,11 @@ public int startUsingNetworkFeature(int networkType, String feature) {
939946
* always indicates failure.
940947
*
941948
* @deprecated Deprecated in favor of the cleaner {@link #unregisterNetworkCallback} API.
949+
* In {@link VERSION_CODES#MNC}, and above, this method is unsupported and will
950+
* throw {@code UnsupportedOperationException} if called.
942951
*/
943952
public int stopUsingNetworkFeature(int networkType, String feature) {
953+
checkLegacyRoutingApiAccess();
944954
NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
945955
if (netCap == null) {
946956
Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " +
@@ -1218,6 +1228,8 @@ private boolean removeRequestForFeature(NetworkCapabilities netCap) {
12181228
* @deprecated Deprecated in favor of the
12191229
* {@link #requestNetwork(NetworkRequest, NetworkCallback)},
12201230
* {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} API.
1231+
* In {@link VERSION_CODES#MNC}, and above, this method is unsupported and will
1232+
* throw {@code UnsupportedOperationException} if called.
12211233
*/
12221234
public boolean requestRouteToHost(int networkType, int hostAddress) {
12231235
return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress));
@@ -1238,6 +1250,7 @@ public boolean requestRouteToHost(int networkType, int hostAddress) {
12381250
* {@link #bindProcessToNetwork} API.
12391251
*/
12401252
public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
1253+
checkLegacyRoutingApiAccess();
12411254
try {
12421255
return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress());
12431256
} catch (RemoteException e) {
@@ -1416,7 +1429,8 @@ public boolean isDefaultNetworkActive() {
14161429
/**
14171430
* {@hide}
14181431
*/
1419-
public ConnectivityManager(IConnectivityManager service) {
1432+
public ConnectivityManager(Context context, IConnectivityManager service) {
1433+
mContext = checkNotNull(context, "missing context");
14201434
mService = checkNotNull(service, "missing IConnectivityManager");
14211435
sInstance = this;
14221436
}
@@ -2703,6 +2717,34 @@ public static Network getProcessDefaultNetwork() {
27032717
return new Network(netId);
27042718
}
27052719

2720+
private void unsupportedStartingFrom(int version) {
2721+
if (Process.myUid() == Process.SYSTEM_UID) {
2722+
// The getApplicationInfo() call we make below is not supported in system context, and
2723+
// we want to allow the system to use these APIs anyway.
2724+
return;
2725+
}
2726+
2727+
if (mContext.getApplicationInfo().targetSdkVersion >= version) {
2728+
throw new UnsupportedOperationException(
2729+
"This method is not supported in target SDK version " + version + " and above");
2730+
}
2731+
}
2732+
2733+
// Checks whether the calling app can use the legacy routing API (startUsingNetworkFeature,
2734+
// stopUsingNetworkFeature, requestRouteToHost), and if not throw UnsupportedOperationException.
2735+
// TODO: convert the existing system users (Tethering, GpsLocationProvider) to the new APIs and
2736+
// remove these exemptions. Note that this check is not secure, and apps can still access these
2737+
// functions by accessing ConnectivityService directly. However, it should be clear that doing
2738+
// so is unsupported and may break in the future. http://b/22728205
2739+
private void checkLegacyRoutingApiAccess() {
2740+
if (mContext.checkCallingOrSelfPermission("com.android.permission.INJECT_OMADM_SETTINGS")
2741+
== PackageManager.PERMISSION_GRANTED) {
2742+
return;
2743+
}
2744+
2745+
unsupportedStartingFrom(VERSION_CODES.MNC);
2746+
}
2747+
27062748
/**
27072749
* Binds host resolutions performed by this process to {@code network}.
27082750
* {@link #bindProcessToNetwork} takes precedence over this setting.

services/tests/servicestests/src/com/android/server/ConnectivityServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ public void setUp() throws Exception {
463463
mService = new WrappedConnectivityService(
464464
mServiceContext, mNetManager, mStatsService, mPolicyService);
465465
mService.systemReady();
466-
mCm = new ConnectivityManager(mService);
466+
mCm = new ConnectivityManager(getContext(), mService);
467467
}
468468

469469
private int transportToLegacyType(int transport) {

0 commit comments

Comments
 (0)