blob: 418d6915d4b3d164833adc451a86cc183f8ef954 [file] [log] [blame]
Lorenzo Colittiae5cb712020-01-08 00:04:09 +09001/*
2 * Copyright (C) 2020 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
17package android.net;
18
19import android.annotation.NonNull;
20import android.annotation.Nullable;
21import android.annotation.RequiresPermission;
22import android.annotation.SystemApi;
23import android.content.Context;
24import android.os.Handler;
25import android.os.Looper;
26import android.os.Message;
27import android.os.Messenger;
28import android.util.Log;
29
30/**
31 * Base class for network providers such as telephony or Wi-Fi. NetworkProviders connect the device
32 * to networks and makes them available to to the core network stack by creating
33 * {@link NetworkAgent}s. The networks can then provide connectivity to apps and can be interacted
34 * with via networking APIs such as {@link ConnectivityManager}.
35 *
36 * Subclasses should implement {@link #onNetworkRequested} and {@link #onRequestWithdrawn} to
37 * receive {@link NetworkRequest}s sent by the system and by apps. A network that is not the
38 * best (highest-scoring) network for any request is generally not used by the system, and torn
39 * down.
40 *
41 * @hide
42 */
43@SystemApi
44public class NetworkProvider {
45 /**
46 * {@code providerId} value that indicates the absence of a provider. It is the providerId of
47 * any NetworkProvider that is not currently registered, and of any NetworkRequest that is not
48 * currently being satisfied by a network.
49 */
50 public static final int ID_NONE = -1;
51
52 /**
53 * A hardcoded ID for NetworkAgents representing VPNs. These agents are not created by any
54 * provider, so they use this constant for clarity instead of NONE.
55 * @hide only used by ConnectivityService.
56 */
57 public static final int ID_VPN = -2;
58
59 /**
60 * The first providerId value that will be allocated.
61 * @hide only used by ConnectivityService.
62 */
63 public static final int FIRST_PROVIDER_ID = 1;
64
65 /** @hide only used by ConnectivityService */
66 public static final int CMD_REQUEST_NETWORK = 1;
67 /** @hide only used by ConnectivityService */
68 public static final int CMD_CANCEL_REQUEST = 2;
69
70 private final Messenger mMessenger;
71 private final String mName;
72 private final ConnectivityManager mCm;
73
74 private int mProviderId = ID_NONE;
75
76 /**
77 * Constructs a new NetworkProvider.
78 *
79 * @param looper the Looper on which to run {@link #onNetworkRequested} and
80 * {@link #onRequestWithdrawn}.
81 * @param name the name of the listener, used only for debugging.
82 *
83 * @hide
84 */
85 @SystemApi
86 public NetworkProvider(@NonNull Context context, @NonNull Looper looper, @NonNull String name) {
87 mCm = ConnectivityManager.from(context);
88
89 Handler handler = new Handler(looper) {
90 @Override
91 public void handleMessage(Message m) {
92 switch (m.what) {
93 case CMD_REQUEST_NETWORK:
94 onNetworkRequested((NetworkRequest) m.obj, m.arg1, m.arg2);
95 break;
96 case CMD_CANCEL_REQUEST:
97 onRequestWithdrawn((NetworkRequest) m.obj);
98 break;
99 default:
100 Log.e(mName, "Unhandled message: " + m.what);
101 }
102 }
103 };
104 mMessenger = new Messenger(handler);
105 mName = name;
106 }
107
108 // TODO: consider adding a register() method so ConnectivityManager does not need to call this.
Aaron Huangf68546e2020-03-12 17:52:33 +0800109 /** @hide */
Lorenzo Colittiae5cb712020-01-08 00:04:09 +0900110 public @Nullable Messenger getMessenger() {
111 return mMessenger;
112 }
113
Aaron Huangf68546e2020-03-12 17:52:33 +0800114 /** @hide */
Lorenzo Colittiae5cb712020-01-08 00:04:09 +0900115 public @NonNull String getName() {
116 return mName;
117 }
118
119 /**
120 * Returns the ID of this provider. This is known only once the provider is registered via
121 * {@link ConnectivityManager#registerNetworkProvider()}, otherwise the ID is {@link #ID_NONE}.
122 * This ID must be used when registering any {@link NetworkAgent}s.
123 */
124 public int getProviderId() {
125 return mProviderId;
126 }
127
128 /** @hide */
129 public void setProviderId(int providerId) {
130 mProviderId = providerId;
131 }
132
133 /**
134 * Called when a NetworkRequest is received. The request may be a new request or an existing
135 * request with a different score.
136 *
137 * @param request the NetworkRequest being received
138 * @param score the score of the network currently satisfying the request, or 0 if none.
139 * @param providerId the ID of the provider that created the network currently satisfying this
140 * request, or {@link #ID_NONE} if none.
141 *
142 * @hide
143 */
144 @SystemApi
145 public void onNetworkRequested(@NonNull NetworkRequest request, int score, int providerId) {}
146
147 /**
148 * Called when a NetworkRequest is withdrawn.
149 * @hide
150 */
151 @SystemApi
152 public void onRequestWithdrawn(@NonNull NetworkRequest request) {}
153
154 /**
155 * Asserts that no provider will ever be able to satisfy the specified request. The provider
156 * must only call this method if it knows that it is the only provider on the system capable of
157 * satisfying this request, and that the request cannot be satisfied. The application filing the
158 * request will receive an {@link NetworkCallback#onUnavailable()} callback.
159 *
160 * @param request the request that cannot be fulfilled
161 * @hide
162 */
163 @SystemApi
164 @RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
165 public void declareNetworkRequestUnfulfillable(@NonNull NetworkRequest request) {
166 mCm.declareNetworkRequestUnfulfillable(request);
167 }
168}