2222import android .app .PendingIntent ;
2323import android .content .Context ;
2424import android .content .Intent ;
25+ import android .content .pm .PackageManager ;
2526import android .net .NetworkUtils ;
2627import android .os .Binder ;
2728import android .os .Build .VERSION_CODES ;
3334import android .os .Looper ;
3435import android .os .Message ;
3536import android .os .Messenger ;
37+ import android .os .Process ;
3638import android .os .RemoteException ;
3739import android .os .ServiceManager ;
3840import 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.
0 commit comments