1 /*
2  * Copyright (C) 2017 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.tethering;
18 
19 import android.app.usage.NetworkStatsManager;
20 import android.bluetooth.BluetoothAdapter;
21 import android.content.Context;
22 import android.net.INetd;
23 import android.net.NetworkRequest;
24 import android.net.ip.IpServer;
25 import android.net.util.SharedLog;
26 import android.os.Handler;
27 import android.os.IBinder;
28 import android.os.Looper;
29 import android.os.SystemProperties;
30 import android.text.TextUtils;
31 
32 import androidx.annotation.NonNull;
33 
34 import com.android.internal.util.StateMachine;
35 
36 import java.util.ArrayList;
37 
38 
39 /**
40  * Capture tethering dependencies, for injection.
41  *
42  * @hide
43  */
44 public abstract class TetheringDependencies {
45     /**
46      * Get a reference to the BpfCoordinator to be used by tethering.
47      */
getBpfCoordinator( @onNull BpfCoordinator.Dependencies deps)48     public @NonNull BpfCoordinator getBpfCoordinator(
49             @NonNull BpfCoordinator.Dependencies deps) {
50         return new BpfCoordinator(deps);
51     }
52 
53     /**
54      * Get a reference to the offload hardware interface to be used by tethering.
55      */
getOffloadHardwareInterface(Handler h, SharedLog log)56     public OffloadHardwareInterface getOffloadHardwareInterface(Handler h, SharedLog log) {
57         return new OffloadHardwareInterface(h, log);
58     }
59 
60     /**
61      * Get a reference to the offload controller to be used by tethering.
62      */
63     @NonNull
getOffloadController(@onNull Handler h, @NonNull SharedLog log, @NonNull OffloadController.Dependencies deps)64     public OffloadController getOffloadController(@NonNull Handler h,
65             @NonNull SharedLog log, @NonNull OffloadController.Dependencies deps) {
66         final NetworkStatsManager statsManager =
67                 (NetworkStatsManager) getContext().getSystemService(Context.NETWORK_STATS_SERVICE);
68         return new OffloadController(h, getOffloadHardwareInterface(h, log),
69                 getContext().getContentResolver(), statsManager, log, deps);
70     }
71 
72 
73     /**
74      * Get a reference to the UpstreamNetworkMonitor to be used by tethering.
75      */
getUpstreamNetworkMonitor(Context ctx, StateMachine target, SharedLog log, int what)76     public UpstreamNetworkMonitor getUpstreamNetworkMonitor(Context ctx, StateMachine target,
77             SharedLog log, int what) {
78         return new UpstreamNetworkMonitor(ctx, target, log, what);
79     }
80 
81     /**
82      * Get a reference to the IPv6TetheringCoordinator to be used by tethering.
83      */
getIPv6TetheringCoordinator( ArrayList<IpServer> notifyList, SharedLog log)84     public IPv6TetheringCoordinator getIPv6TetheringCoordinator(
85             ArrayList<IpServer> notifyList, SharedLog log) {
86         return new IPv6TetheringCoordinator(notifyList, log);
87     }
88 
89     /**
90      * Get dependencies to be used by IpServer.
91      */
getIpServerDependencies()92     public abstract IpServer.Dependencies getIpServerDependencies();
93 
94     /**
95      * Indicates whether tethering is supported on the device.
96      */
isTetheringSupported()97     public boolean isTetheringSupported() {
98         return true;
99     }
100 
101     /**
102      * Get the NetworkRequest that should be fulfilled by the default network.
103      */
getDefaultNetworkRequest()104     public abstract NetworkRequest getDefaultNetworkRequest();
105 
106     /**
107      * Get a reference to the EntitlementManager to be used by tethering.
108      */
getEntitlementManager(Context ctx, Handler h, SharedLog log, Runnable callback)109     public EntitlementManager getEntitlementManager(Context ctx, Handler h, SharedLog log,
110             Runnable callback) {
111         return new EntitlementManager(ctx, h, log, callback);
112     }
113 
114     /**
115      * Generate a new TetheringConfiguration according to input sub Id.
116      */
generateTetheringConfiguration(Context ctx, SharedLog log, int subId)117     public TetheringConfiguration generateTetheringConfiguration(Context ctx, SharedLog log,
118             int subId) {
119         return new TetheringConfiguration(ctx, log, subId);
120     }
121 
122     /**
123      * Get a reference to INetd to be used by tethering.
124      */
getINetd(Context context)125     public INetd getINetd(Context context) {
126         return INetd.Stub.asInterface(
127                 (IBinder) context.getSystemService(Context.NETD_SERVICE));
128     }
129 
130     /**
131      * Get a reference to the TetheringNotificationUpdater to be used by tethering.
132      */
getNotificationUpdater(@onNull final Context ctx, @NonNull final Looper looper)133     public TetheringNotificationUpdater getNotificationUpdater(@NonNull final Context ctx,
134             @NonNull final Looper looper) {
135         return new TetheringNotificationUpdater(ctx, looper);
136     }
137 
138     /**
139      * Get tethering thread looper.
140      */
getTetheringLooper()141     public abstract Looper getTetheringLooper();
142 
143     /**
144      *  Get Context of TetheringSerice.
145      */
getContext()146     public abstract Context getContext();
147 
148     /**
149      * Get a reference to BluetoothAdapter to be used by tethering.
150      */
getBluetoothAdapter()151     public abstract BluetoothAdapter getBluetoothAdapter();
152 
153     /**
154      * Get SystemProperties which indicate whether tethering is denied.
155      */
isTetheringDenied()156     public boolean isTetheringDenied() {
157         return TextUtils.equals(SystemProperties.get("ro.tether.denied"), "true");
158     }
159 }
160