markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.connectivity.tethering; |
| 18 | |
| 19 | import android.app.Service; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.net.ConnectivityManager; |
| 23 | import android.net.ITetherInternalCallback; |
| 24 | import android.net.ITetheringConnector; |
| 25 | import android.net.NetworkRequest; |
| 26 | import android.net.util.SharedLog; |
| 27 | import android.os.HandlerThread; |
| 28 | import android.os.IBinder; |
| 29 | import android.os.Looper; |
| 30 | import android.os.ResultReceiver; |
| 31 | import android.os.SystemProperties; |
| 32 | import android.provider.Settings; |
| 33 | |
| 34 | import androidx.annotation.NonNull; |
| 35 | import androidx.annotation.Nullable; |
| 36 | |
| 37 | import com.android.internal.annotations.VisibleForTesting; |
| 38 | |
| 39 | import java.io.FileDescriptor; |
| 40 | import 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 | */ |
| 47 | public 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 | } |