1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.networkstack.apishim.common;
18 
19 import android.net.ConnectivityManager.NetworkCallback;
20 import android.net.NetworkRequest;
21 import android.os.Handler;
22 import android.util.Range;
23 
24 import androidx.annotation.NonNull;
25 
26 import java.util.Collection;
27 
28 /**
29  * Interface used to access API methods in {@link android.net.ConnectivityManager}, with
30  * appropriate fallbacks if the methods are not yet part of the released API.
31  *
32  * <p>This interface makes it easier for callers to use ConnectivityManagerShimImpl, as it's more
33  * obvious what methods must be implemented on each API level, and it abstracts from callers the
34  * need to reference classes that have different implementations (which also does not work well
35  * with IDEs).
36  */
37 public interface ConnectivityManagerShim {
38     /** See android.net.ConnectivityManager#requestBackgroundNetwork */
requestBackgroundNetwork(@onNull NetworkRequest request, @NonNull NetworkCallback networkCallback, @NonNull Handler handler)39     void requestBackgroundNetwork(@NonNull NetworkRequest request,
40             @NonNull NetworkCallback networkCallback, @NonNull Handler handler)
41             throws UnsupportedApiLevelException;
42 
43     /** See android.net.ConnectivityManager#registerSystemDefaultNetworkCallback */
registerSystemDefaultNetworkCallback( @onNull NetworkCallback networkCallback, @NonNull Handler handler)44     void registerSystemDefaultNetworkCallback(
45             @NonNull NetworkCallback networkCallback, @NonNull Handler handler)
46             throws UnsupportedApiLevelException;
47 
48     /** See android.net.ConnectivityManager#registerDefaultNetworkCallbackForUid */
registerDefaultNetworkCallbackForUid( int uid, @NonNull NetworkCallback networkCallback, @NonNull Handler handler)49     default void registerDefaultNetworkCallbackForUid(
50             int uid, @NonNull NetworkCallback networkCallback, @NonNull Handler handler)
51             throws UnsupportedApiLevelException {
52         throw new UnsupportedApiLevelException("Only supported starting from API 31");
53     }
54 
55     /** See android.net.ConnectivityManager#setLegacyLockdownVpnEnabled */
setLegacyLockdownVpnEnabled(boolean enabled)56     default void setLegacyLockdownVpnEnabled(boolean enabled) throws UnsupportedApiLevelException {
57         throw new UnsupportedApiLevelException("Only supported starting from API 31");
58     }
59 
60     /** See android.net.ConnectivityManager#setRequireVpnForUids */
setRequireVpnForUids(boolean requireVpn, Collection<Range<Integer>> ranges)61     default void setRequireVpnForUids(boolean requireVpn, Collection<Range<Integer>> ranges)
62             throws UnsupportedApiLevelException {
63         throw new UnsupportedApiLevelException("Only supported starting from API 31");
64     }
65 }
66