blob: 456f2f7f27f5978c96d4e8592a458f063ac88978 [file] [log] [blame]
markchien0df2ebc42019-09-30 14:40:57 +08001/*
2 * Copyright (C) 2019 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 com.android.server.connectivity.tethering;
18
19import android.app.Service;
20import android.content.Context;
21import android.content.Intent;
22import android.net.ConnectivityManager;
23import android.net.ITetherInternalCallback;
24import android.net.ITetheringConnector;
25import android.net.NetworkRequest;
26import android.net.util.SharedLog;
27import android.os.HandlerThread;
28import android.os.IBinder;
29import android.os.Looper;
30import android.os.ResultReceiver;
31import android.os.SystemProperties;
32import android.provider.Settings;
33
34import androidx.annotation.NonNull;
35import androidx.annotation.Nullable;
36
37import com.android.internal.annotations.VisibleForTesting;
38
39import java.io.FileDescriptor;
40import java.io.PrintWriter;
41
42/**
43 * Android service used to manage tethering.
44 *
45 * <p>The service returns a binder for the system server to communicate with the tethering.
46 */
47public class TetheringService extends Service {
48 private static final String TAG = TetheringService.class.getSimpleName();
49
50 private final SharedLog mLog = new SharedLog(TAG);
51 private TetheringConnector mConnector;
52 private Context mContext;
53 private TetheringDependencies mDeps;
54 private Tethering mTethering;
55
56 @Override
57 public void onCreate() {
58 mLog.mark("onCreate");
59 mDeps = getTetheringDependencies();
60 mContext = mDeps.getContext();
61 mTethering = makeTethering(mDeps);
62 }
63
64 /**
65 * Make a reference to Tethering object.
66 */
67 @VisibleForTesting
68 public Tethering makeTethering(TetheringDependencies deps) {
69 return new Tethering(deps);
70 }
71
72 /**
73 * Create a binder connector for the system server to communicate with the tethering.
74 */
75 private synchronized IBinder makeConnector() {
76 if (mConnector == null) {
77 mConnector = new TetheringConnector(mTethering);
78 }
79 return mConnector;
80 }
81
82 @NonNull
83 @Override
84 public IBinder onBind(Intent intent) {
85 mLog.mark("onBind");
86 return makeConnector();
87 }
88
89 private static class TetheringConnector extends ITetheringConnector.Stub {
90 private final Tethering mService;
91
92 TetheringConnector(Tethering tether) {
93 mService = tether;
94 }
95
96 @Override
97 public void tether(String iface) {
98 mService.tether(iface);
99 }
100
101 @Override
102 public void untether(String iface) {
103 mService.untether(iface);
104 }
105
106 @Override
107 public void setUsbTethering(boolean enable) {
108 mService.setUsbTethering(enable);
109 }
110
111 @Override
112 public void startTethering(int type, ResultReceiver receiver, boolean showProvisioningUi) {
113 mService.startTethering(type, receiver, showProvisioningUi);
114 }
115
116 @Override
117 public void stopTethering(int type) {
118 mService.stopTethering(type);
119 }
120
121 @Override
122 public void requestLatestTetheringEntitlementResult(int type, ResultReceiver receiver,
123 boolean showEntitlementUi) {
124 mService.requestLatestTetheringEntitlementResult(type, receiver, showEntitlementUi);
125 }
126
127 @Override
128 public void registerTetherInternalCallback(ITetherInternalCallback callback) {
129 mService.registerTetherInternalCallback(callback);
130 }
131 }
132
133 @Override
134 protected void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter writer,
135 @Nullable String[] args) {
136 mTethering.dump(fd, writer, args);
137 }
138
139 /**
140 * An injection method for testing.
141 */
142 @VisibleForTesting
143 public TetheringDependencies getTetheringDependencies() {
144 if (mDeps == null) {
145 mDeps = new TetheringDependencies() {
146 @Override
147 public NetworkRequest getDefaultNetworkRequest() {
148 ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(
149 Context.CONNECTIVITY_SERVICE);
150 return cm.getDefaultRequest();
151 }
152
153 @Override
154 public Looper getTetheringLooper() {
155 final HandlerThread tetherThread = new HandlerThread("android.tethering");
156 tetherThread.start();
157 return tetherThread.getLooper();
158 }
159
160 @Override
161 public boolean isTetheringSupported() {
162 int defaultVal =
163 SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1;
164 boolean tetherSupported = Settings.Global.getInt(mContext.getContentResolver(),
165 Settings.Global.TETHER_SUPPORTED, defaultVal) != 0;
166 return tetherSupported;
167 }
168
169 @Override
170 public Context getContext() {
171 return TetheringService.this;
172 }
173 };
174 }
175
176 return mDeps;
177 }
178}