Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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; |
| 18 | |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame^] | 19 | import static android.hardware.usb.UsbManager.USB_CONFIGURED; |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 20 | import static android.hardware.usb.UsbManager.USB_CONNECTED; |
| 21 | import static android.hardware.usb.UsbManager.USB_FUNCTION_RNDIS; |
Erik Kline | 93c4afa | 2017-06-04 11:36:01 +0900 | [diff] [blame] | 22 | import static android.net.ConnectivityManager.getNetworkTypeName; |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 23 | import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_INTERFACE_NAME; |
| 24 | import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_MODE; |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 25 | import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_STATE; |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 26 | import static android.net.wifi.WifiManager.IFACE_IP_MODE_CONFIGURATION_ERROR; |
| 27 | import static android.net.wifi.WifiManager.IFACE_IP_MODE_LOCAL_ONLY; |
| 28 | import static android.net.wifi.WifiManager.IFACE_IP_MODE_TETHERED; |
| 29 | import static android.net.wifi.WifiManager.IFACE_IP_MODE_UNSPECIFIED; |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 30 | import static android.net.wifi.WifiManager.WIFI_AP_STATE_DISABLED; |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 31 | import static com.android.server.ConnectivityService.SHORT_ARG; |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 32 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 33 | import android.app.Notification; |
| 34 | import android.app.NotificationManager; |
| 35 | import android.app.PendingIntent; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 36 | import android.bluetooth.BluetoothAdapter; |
| 37 | import android.bluetooth.BluetoothPan; |
| 38 | import android.bluetooth.BluetoothProfile; |
| 39 | import android.bluetooth.BluetoothProfile.ServiceListener; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 40 | import android.content.BroadcastReceiver; |
Robert Greenwalt | 2ffe412 | 2014-12-12 12:22:31 -0800 | [diff] [blame] | 41 | import android.content.ComponentName; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 42 | import android.content.Context; |
| 43 | import android.content.Intent; |
| 44 | import android.content.IntentFilter; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 45 | import android.content.pm.PackageManager; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 46 | import android.content.res.Resources; |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 47 | import android.hardware.usb.UsbManager; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 48 | import android.net.ConnectivityManager; |
Christopher Wiley | deebfec | 2016-09-16 11:14:36 -0700 | [diff] [blame] | 49 | import android.net.INetworkPolicyManager; |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 50 | import android.net.INetworkStatsService; |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 51 | import android.net.IpPrefix; |
Robert Greenwalt | 05d8936 | 2011-01-23 16:04:05 -0800 | [diff] [blame] | 52 | import android.net.LinkProperties; |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 53 | import android.net.Network; |
Erik Kline | 0c9cb2b | 2015-11-20 17:47:08 +0900 | [diff] [blame] | 54 | import android.net.NetworkCapabilities; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 55 | import android.net.NetworkInfo; |
Erik Kline | 0c9cb2b | 2015-11-20 17:47:08 +0900 | [diff] [blame] | 56 | import android.net.NetworkRequest; |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 57 | import android.net.NetworkState; |
Robert Greenwalt | e590373 | 2011-02-22 16:00:42 -0800 | [diff] [blame] | 58 | import android.net.NetworkUtils; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 59 | import android.net.RouteInfo; |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 60 | import android.net.util.SharedLog; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 61 | import android.net.wifi.WifiManager; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 62 | import android.os.Binder; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 63 | import android.os.Bundle; |
Erik Kline | f4b6e34 | 2017-04-25 19:19:59 +0900 | [diff] [blame] | 64 | import android.os.Handler; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 65 | import android.os.INetworkManagementService; |
Robert Greenwalt | dfadaea | 2010-03-11 15:03:08 -0800 | [diff] [blame] | 66 | import android.os.Looper; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 67 | import android.os.Message; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 68 | import android.os.Parcel; |
Christopher Wiley | deebfec | 2016-09-16 11:14:36 -0700 | [diff] [blame] | 69 | import android.os.RemoteException; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 70 | import android.os.ResultReceiver; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 71 | import android.os.UserHandle; |
Jeremy Klein | 15f3d21 | 2016-01-24 17:01:09 -0800 | [diff] [blame] | 72 | import android.provider.Settings; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 73 | import android.telephony.CarrierConfigManager; |
Junda Liu | 3853158 | 2014-10-28 13:52:51 -0700 | [diff] [blame] | 74 | import android.telephony.TelephonyManager; |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 75 | import android.text.TextUtils; |
Christopher Wiley | e949039 | 2016-05-26 15:57:29 -0700 | [diff] [blame] | 76 | import android.util.ArrayMap; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 77 | import android.util.Log; |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 78 | import android.util.SparseArray; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 79 | |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 80 | import com.android.internal.annotations.VisibleForTesting; |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 81 | import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 82 | import com.android.internal.notification.SystemNotificationChannels; |
Robert Greenwalt | 2ffe412 | 2014-12-12 12:22:31 -0800 | [diff] [blame] | 83 | import com.android.internal.telephony.IccCardConstants; |
Robert Greenwalt | 2ffe412 | 2014-12-12 12:22:31 -0800 | [diff] [blame] | 84 | import com.android.internal.telephony.TelephonyIntents; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 85 | import com.android.internal.util.DumpUtils; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 86 | import com.android.internal.util.IndentingPrintWriter; |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 87 | import com.android.internal.util.MessageUtils; |
| 88 | import com.android.internal.util.Protocol; |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 89 | import com.android.internal.util.State; |
| 90 | import com.android.internal.util.StateMachine; |
Christopher Wiley | e03fb44 | 2016-05-18 13:45:20 -0700 | [diff] [blame] | 91 | import com.android.server.connectivity.tethering.IControlsTethering; |
Erik Kline | 1eb8c69 | 2016-07-08 17:21:26 +0900 | [diff] [blame] | 92 | import com.android.server.connectivity.tethering.IPv6TetheringCoordinator; |
Erik Kline | e0cce21 | 2017-03-06 14:05:23 +0900 | [diff] [blame] | 93 | import com.android.server.connectivity.tethering.OffloadController; |
Erik Kline | 227648f | 2017-01-20 20:26:10 +0900 | [diff] [blame] | 94 | import com.android.server.connectivity.tethering.SimChangeListener; |
Mitchell Wills | 7040b4e | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 95 | import com.android.server.connectivity.tethering.TetherInterfaceStateMachine; |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 96 | import com.android.server.connectivity.tethering.TetheringConfiguration; |
Erik Kline | 47222fc | 2017-04-30 19:36:15 +0900 | [diff] [blame] | 97 | import com.android.server.connectivity.tethering.TetheringDependencies; |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 98 | import com.android.server.connectivity.tethering.UpstreamNetworkMonitor; |
Lorenzo Colitti | f4e90ea | 2013-10-31 23:30:47 +0900 | [diff] [blame] | 99 | import com.android.server.net.BaseNetworkObserver; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 100 | |
| 101 | import java.io.FileDescriptor; |
| 102 | import java.io.PrintWriter; |
Robert Greenwalt | 4f74d55 | 2011-12-19 16:59:31 -0800 | [diff] [blame] | 103 | import java.net.Inet4Address; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 104 | import java.net.InetAddress; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 105 | import java.util.ArrayList; |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 106 | import java.util.Arrays; |
Robert Greenwalt | ccf83af1 | 2011-06-02 17:30:47 -0700 | [diff] [blame] | 107 | import java.util.Collection; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 108 | import java.util.HashMap; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 109 | import java.util.HashSet; |
Robert Greenwalt | ccf83af1 | 2011-06-02 17:30:47 -0700 | [diff] [blame] | 110 | import java.util.Iterator; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 111 | import java.util.Map; |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 112 | import java.util.Set; |
Robert Greenwalt | 2ffe412 | 2014-12-12 12:22:31 -0800 | [diff] [blame] | 113 | import java.util.concurrent.atomic.AtomicInteger; |
| 114 | |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 115 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 116 | /** |
| 117 | * @hide |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 118 | * |
Christopher Wiley | 3b1d922 | 2016-05-20 16:44:04 -0700 | [diff] [blame] | 119 | * This class holds much of the business logic to allow Android devices |
| 120 | * to act as IP gateways via USB, BT, and WiFi interfaces. |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 121 | */ |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 122 | public class Tethering extends BaseNetworkObserver { |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 123 | |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 124 | private final static String TAG = Tethering.class.getSimpleName(); |
Joe Onorato | 12acbd7 | 2016-02-01 17:49:31 -0800 | [diff] [blame] | 125 | private final static boolean DBG = false; |
Robert Greenwalt | fd1be2b | 2011-11-11 12:30:19 -0800 | [diff] [blame] | 126 | private final static boolean VDBG = false; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 127 | |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 128 | protected static final String DISABLE_PROVISIONING_SYSPROP_KEY = "net.tethering.noprovisioning"; |
| 129 | |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 130 | private static final Class[] messageClasses = { |
Mitchell Wills | 7040b4e | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 131 | Tethering.class, TetherMasterSM.class, TetherInterfaceStateMachine.class |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 132 | }; |
| 133 | private static final SparseArray<String> sMagicDecoderRing = |
| 134 | MessageUtils.findMessageNames(messageClasses); |
| 135 | |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 136 | // {@link ComponentName} of the Service used to run tether provisioning. |
| 137 | private static final ComponentName TETHER_SERVICE = ComponentName.unflattenFromString(Resources |
| 138 | .getSystem().getString(com.android.internal.R.string.config_wifi_tether_enable)); |
| 139 | |
| 140 | private static class TetherState { |
| 141 | public final TetherInterfaceStateMachine stateMachine; |
| 142 | public int lastState; |
| 143 | public int lastError; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 144 | |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 145 | public TetherState(TetherInterfaceStateMachine sm) { |
| 146 | stateMachine = sm; |
| 147 | // Assume all state machines start out available and with no errors. |
| 148 | lastState = IControlsTethering.STATE_AVAILABLE; |
| 149 | lastError = ConnectivityManager.TETHER_ERROR_NO_ERROR; |
| 150 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 151 | |
| 152 | public boolean isCurrentlyServing() { |
| 153 | switch (lastState) { |
| 154 | case IControlsTethering.STATE_TETHERED: |
Erik Kline | ca41be7 | 2017-04-20 22:59:16 +0900 | [diff] [blame] | 155 | case IControlsTethering.STATE_LOCAL_ONLY: |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 156 | return true; |
| 157 | default: |
| 158 | return false; |
| 159 | } |
| 160 | } |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 161 | } |
Robert Greenwalt | ccf83af1 | 2011-06-02 17:30:47 -0700 | [diff] [blame] | 162 | |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 163 | private final SharedLog mLog = new SharedLog(TAG); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 164 | |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 165 | // used to synchronize public access to members |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 166 | private final Object mPublicSync; |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 167 | private final Context mContext; |
| 168 | private final ArrayMap<String, TetherState> mTetherStates; |
| 169 | private final BroadcastReceiver mStateReceiver; |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 170 | private final INetworkManagementService mNMService; |
| 171 | private final INetworkStatsService mStatsService; |
Christopher Wiley | deebfec | 2016-09-16 11:14:36 -0700 | [diff] [blame] | 172 | private final INetworkPolicyManager mPolicyManager; |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 173 | private final Looper mLooper; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 174 | private final MockableSystemProperties mSystemProperties; |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 175 | private final StateMachine mTetherMasterSM; |
Erik Kline | e0cce21 | 2017-03-06 14:05:23 +0900 | [diff] [blame] | 176 | private final OffloadController mOffloadController; |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 177 | private final UpstreamNetworkMonitor mUpstreamNetworkMonitor; |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 178 | // TODO: Figure out how to merge this and other downstream-tracking objects |
| 179 | // into a single coherent structure. |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 180 | private final HashSet<TetherInterfaceStateMachine> mForwardedDownstreams; |
Erik Kline | 227648f | 2017-01-20 20:26:10 +0900 | [diff] [blame] | 181 | private final SimChangeListener mSimChange; |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 182 | |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 183 | private volatile TetheringConfiguration mConfig; |
| 184 | private String mCurrentUpstreamIface; |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 185 | private Notification.Builder mTetheredNotificationBuilder; |
| 186 | private int mLastNotificationId; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 187 | |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 188 | private boolean mRndisEnabled; // track the RNDIS function enabled state |
| 189 | private boolean mUsbTetherRequested; // true if USB tethering should be started |
| 190 | // when RNDIS is enabled |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 191 | // True iff. WiFi tethering should be started when soft AP is ready. |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 192 | private boolean mWifiTetherRequested; |
| 193 | |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 194 | public Tethering(Context context, INetworkManagementService nmService, |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 195 | INetworkStatsService statsService, INetworkPolicyManager policyManager, |
Erik Kline | 47222fc | 2017-04-30 19:36:15 +0900 | [diff] [blame] | 196 | Looper looper, MockableSystemProperties systemProperties, |
| 197 | TetheringDependencies deps) { |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 198 | mLog.mark("constructed"); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 199 | mContext = context; |
Chia-chi Yeh | c933830 | 2011-05-11 16:35:13 -0700 | [diff] [blame] | 200 | mNMService = nmService; |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 201 | mStatsService = statsService; |
Christopher Wiley | deebfec | 2016-09-16 11:14:36 -0700 | [diff] [blame] | 202 | mPolicyManager = policyManager; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 203 | mLooper = looper; |
| 204 | mSystemProperties = systemProperties; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 205 | |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 206 | mPublicSync = new Object(); |
| 207 | |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 208 | mTetherStates = new ArrayMap<>(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 209 | |
Robert Greenwalt | dfadaea | 2010-03-11 15:03:08 -0800 | [diff] [blame] | 210 | mTetherMasterSM = new TetherMasterSM("TetherMaster", mLooper); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 211 | mTetherMasterSM.start(); |
| 212 | |
Erik Kline | f4b6e34 | 2017-04-25 19:19:59 +0900 | [diff] [blame] | 213 | final Handler smHandler = mTetherMasterSM.getHandler(); |
| 214 | mOffloadController = new OffloadController(smHandler, |
| 215 | deps.getOffloadHardwareInterface(smHandler, mLog), |
Erik Kline | 92c4db0 | 2017-05-31 10:21:32 +0900 | [diff] [blame] | 216 | mContext.getContentResolver(), |
Erik Kline | f4b6e34 | 2017-04-25 19:19:59 +0900 | [diff] [blame] | 217 | mLog); |
Erik Kline | ebdb8c8 | 2017-01-10 17:37:54 +0900 | [diff] [blame] | 218 | mUpstreamNetworkMonitor = new UpstreamNetworkMonitor( |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 219 | mContext, mTetherMasterSM, mLog, TetherMasterSM.EVENT_UPSTREAM_CALLBACK ); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 220 | mForwardedDownstreams = new HashSet<>(); |
Erik Kline | 227648f | 2017-01-20 20:26:10 +0900 | [diff] [blame] | 221 | mSimChange = new SimChangeListener( |
| 222 | mContext, mTetherMasterSM.getHandler(), () -> reevaluateSimCardProvisioning()); |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 223 | |
Robert Greenwalt | bb51d9f | 2010-03-12 10:23:35 -0800 | [diff] [blame] | 224 | mStateReceiver = new StateReceiver(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 225 | IntentFilter filter = new IntentFilter(); |
Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 226 | filter.addAction(UsbManager.ACTION_USB_STATE); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 227 | filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 228 | filter.addAction(WifiManager.WIFI_AP_STATE_CHANGED_ACTION); |
Robert Greenwalt | c13368b | 2013-07-18 14:24:42 -0700 | [diff] [blame] | 229 | filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 230 | mContext.registerReceiver(mStateReceiver, filter, null, mTetherMasterSM.getHandler()); |
Robert Greenwalt | bb51d9f | 2010-03-12 10:23:35 -0800 | [diff] [blame] | 231 | |
| 232 | filter = new IntentFilter(); |
Robert Greenwalt | 030f5e1 | 2010-03-10 16:41:03 -0800 | [diff] [blame] | 233 | filter.addAction(Intent.ACTION_MEDIA_SHARED); |
| 234 | filter.addAction(Intent.ACTION_MEDIA_UNSHARED); |
Robert Greenwalt | bb51d9f | 2010-03-12 10:23:35 -0800 | [diff] [blame] | 235 | filter.addDataScheme("file"); |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 236 | mContext.registerReceiver(mStateReceiver, filter, null, mTetherMasterSM.getHandler()); |
Robert Greenwalt | bb51d9f | 2010-03-12 10:23:35 -0800 | [diff] [blame] | 237 | |
Robert Greenwalt | 49348e7 | 2011-10-21 16:54:26 -0700 | [diff] [blame] | 238 | // load device config info |
| 239 | updateConfiguration(); |
Robert Greenwalt | 49348e7 | 2011-10-21 16:54:26 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Sreeram Ramachandran | 62a6131 | 2014-07-24 16:05:17 -0700 | [diff] [blame] | 242 | // We can't do this once in the Tethering() constructor and cache the value, because the |
| 243 | // CONNECTIVITY_SERVICE is registered only after the Tethering() constructor has completed. |
| 244 | private ConnectivityManager getConnectivityManager() { |
| 245 | return (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 246 | } |
| 247 | |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 248 | private WifiManager getWifiManager() { |
| 249 | return (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); |
| 250 | } |
| 251 | |
Erik Kline | 93c4afa | 2017-06-04 11:36:01 +0900 | [diff] [blame] | 252 | // NOTE: This is always invoked on the mLooper thread. |
Erik Kline | d781fba | 2017-01-23 13:01:58 +0900 | [diff] [blame] | 253 | private void updateConfiguration() { |
Erik Kline | 6bd7453 | 2017-05-19 10:10:41 +0900 | [diff] [blame] | 254 | mConfig = new TetheringConfiguration(mContext, mLog); |
Erik Kline | 93c4afa | 2017-06-04 11:36:01 +0900 | [diff] [blame] | 255 | mUpstreamNetworkMonitor.updateMobileRequiresDun(mConfig.isDunRequired); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Erik Kline | 0c9cb2b | 2015-11-20 17:47:08 +0900 | [diff] [blame] | 258 | @Override |
Mike J. Chen | 6143f5f | 2011-06-23 15:17:51 -0700 | [diff] [blame] | 259 | public void interfaceStatusChanged(String iface, boolean up) { |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 260 | // Never called directly: only called from interfaceLinkStateChanged. |
| 261 | // See NetlinkHandler.cpp:71. |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 262 | if (VDBG) Log.d(TAG, "interfaceStatusChanged " + iface + ", " + up); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 263 | synchronized (mPublicSync) { |
Mike J. Chen | 6143f5f | 2011-06-23 15:17:51 -0700 | [diff] [blame] | 264 | if (up) { |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 265 | maybeTrackNewInterfaceLocked(iface); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 266 | } else { |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 267 | if (ifaceNameToType(iface) == ConnectivityManager.TETHERING_BLUETOOTH) { |
| 268 | stopTrackingInterfaceLocked(iface); |
Christopher Wiley | d30aaeb | 2016-07-08 09:33:50 -0700 | [diff] [blame] | 269 | } else { |
| 270 | // Ignore usb0 down after enabling RNDIS. |
| 271 | // We will handle disconnect in interfaceRemoved. |
| 272 | // Similarly, ignore interface down for WiFi. We monitor WiFi AP status |
| 273 | // through the WifiManager.WIFI_AP_STATE_CHANGED_ACTION intent. |
| 274 | if (VDBG) Log.d(TAG, "ignore interface down for " + iface); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 278 | } |
| 279 | |
Erik Kline | 0c9cb2b | 2015-11-20 17:47:08 +0900 | [diff] [blame] | 280 | @Override |
Mike J. Chen | f59c7d0 | 2011-06-23 15:33:15 -0700 | [diff] [blame] | 281 | public void interfaceLinkStateChanged(String iface, boolean up) { |
Irfan Sheriff | 23eb297 | 2011-07-22 15:21:10 -0700 | [diff] [blame] | 282 | interfaceStatusChanged(iface, up); |
Mike J. Chen | f59c7d0 | 2011-06-23 15:33:15 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 285 | private int ifaceNameToType(String iface) { |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 286 | final TetheringConfiguration cfg = mConfig; |
| 287 | |
| 288 | if (cfg.isWifi(iface)) { |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 289 | return ConnectivityManager.TETHERING_WIFI; |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 290 | } else if (cfg.isUsb(iface)) { |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 291 | return ConnectivityManager.TETHERING_USB; |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 292 | } else if (cfg.isBluetooth(iface)) { |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 293 | return ConnectivityManager.TETHERING_BLUETOOTH; |
| 294 | } |
| 295 | return ConnectivityManager.TETHERING_INVALID; |
| 296 | } |
| 297 | |
Erik Kline | 0c9cb2b | 2015-11-20 17:47:08 +0900 | [diff] [blame] | 298 | @Override |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 299 | public void interfaceAdded(String iface) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 300 | if (VDBG) Log.d(TAG, "interfaceAdded " + iface); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 301 | synchronized (mPublicSync) { |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 302 | maybeTrackNewInterfaceLocked(iface); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 303 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 304 | } |
| 305 | |
Erik Kline | 0c9cb2b | 2015-11-20 17:47:08 +0900 | [diff] [blame] | 306 | @Override |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 307 | public void interfaceRemoved(String iface) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 308 | if (VDBG) Log.d(TAG, "interfaceRemoved " + iface); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 309 | synchronized (mPublicSync) { |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 310 | stopTrackingInterfaceLocked(iface); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 314 | public void startTethering(int type, ResultReceiver receiver, boolean showProvisioningUi) { |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 315 | if (!isTetherProvisioningRequired()) { |
| 316 | enableTetheringInternal(type, true, receiver); |
| 317 | return; |
| 318 | } |
| 319 | |
| 320 | if (showProvisioningUi) { |
| 321 | runUiTetherProvisioningAndEnable(type, receiver); |
| 322 | } else { |
| 323 | runSilentTetherProvisioningAndEnable(type, receiver); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | public void stopTethering(int type) { |
| 328 | enableTetheringInternal(type, false, null); |
| 329 | if (isTetherProvisioningRequired()) { |
| 330 | cancelTetherProvisioningRechecks(type); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Check if the device requires a provisioning check in order to enable tethering. |
| 336 | * |
| 337 | * @return a boolean - {@code true} indicating tether provisioning is required by the carrier. |
| 338 | */ |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 339 | @VisibleForTesting |
| 340 | protected boolean isTetherProvisioningRequired() { |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 341 | String[] provisionApp = mContext.getResources().getStringArray( |
| 342 | com.android.internal.R.array.config_mobile_hotspot_provision_app); |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 343 | if (mSystemProperties.getBoolean(DISABLE_PROVISIONING_SYSPROP_KEY, false) |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 344 | || provisionApp == null) { |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | // Check carrier config for entitlement checks |
| 349 | final CarrierConfigManager configManager = (CarrierConfigManager) mContext |
| 350 | .getSystemService(Context.CARRIER_CONFIG_SERVICE); |
Rebecca Silberstein | b07f140 | 2016-10-04 13:53:33 -0700 | [diff] [blame] | 351 | if (configManager != null && configManager.getConfig() != null) { |
| 352 | // we do have a CarrierConfigManager and it has a config. |
| 353 | boolean isEntitlementCheckRequired = configManager.getConfig().getBoolean( |
| 354 | CarrierConfigManager.KEY_REQUIRE_ENTITLEMENT_CHECKS_BOOL); |
| 355 | if (!isEntitlementCheckRequired) { |
| 356 | return false; |
| 357 | } |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 358 | } |
| 359 | return (provisionApp.length == 2); |
| 360 | } |
| 361 | |
Erik Kline | 227648f | 2017-01-20 20:26:10 +0900 | [diff] [blame] | 362 | // Used by the SIM card change observation code. |
| 363 | // TODO: De-duplicate above code. |
| 364 | private boolean hasMobileHotspotProvisionApp() { |
| 365 | try { |
| 366 | if (!mContext.getResources().getString(com.android.internal.R.string. |
| 367 | config_mobile_hotspot_provision_app_no_ui).isEmpty()) { |
| 368 | Log.d(TAG, "re-evaluate provisioning"); |
| 369 | return true; |
| 370 | } |
| 371 | } catch (Resources.NotFoundException e) {} |
| 372 | Log.d(TAG, "no prov-check needed for new SIM"); |
| 373 | return false; |
| 374 | } |
| 375 | |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 376 | /** |
| 377 | * Enables or disables tethering for the given type. This should only be called once |
| 378 | * provisioning has succeeded or is not necessary. It will also schedule provisioning rechecks |
| 379 | * for the specified interface. |
| 380 | */ |
| 381 | private void enableTetheringInternal(int type, boolean enable, ResultReceiver receiver) { |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 382 | boolean isProvisioningRequired = enable && isTetherProvisioningRequired(); |
| 383 | int result; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 384 | switch (type) { |
| 385 | case ConnectivityManager.TETHERING_WIFI: |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 386 | result = setWifiTethering(enable); |
| 387 | if (isProvisioningRequired && result == ConnectivityManager.TETHER_ERROR_NO_ERROR) { |
| 388 | scheduleProvisioningRechecks(type); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 389 | } |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 390 | sendTetherResult(receiver, result); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 391 | break; |
| 392 | case ConnectivityManager.TETHERING_USB: |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 393 | result = setUsbTethering(enable); |
| 394 | if (isProvisioningRequired && result == ConnectivityManager.TETHER_ERROR_NO_ERROR) { |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 395 | scheduleProvisioningRechecks(type); |
| 396 | } |
| 397 | sendTetherResult(receiver, result); |
| 398 | break; |
| 399 | case ConnectivityManager.TETHERING_BLUETOOTH: |
| 400 | setBluetoothTethering(enable, receiver); |
| 401 | break; |
| 402 | default: |
| 403 | Log.w(TAG, "Invalid tether type."); |
| 404 | sendTetherResult(receiver, ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | private void sendTetherResult(ResultReceiver receiver, int result) { |
| 409 | if (receiver != null) { |
| 410 | receiver.send(result, null); |
| 411 | } |
| 412 | } |
| 413 | |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 414 | private int setWifiTethering(final boolean enable) { |
Erik Kline | 256be78 | 2017-04-29 13:20:04 +0900 | [diff] [blame] | 415 | int rval = ConnectivityManager.TETHER_ERROR_MASTER_ERROR; |
| 416 | final long ident = Binder.clearCallingIdentity(); |
| 417 | try { |
| 418 | synchronized (mPublicSync) { |
| 419 | mWifiTetherRequested = enable; |
| 420 | final WifiManager mgr = getWifiManager(); |
| 421 | if ((enable && mgr.startSoftAp(null /* use existing wifi config */)) || |
| 422 | (!enable && mgr.stopSoftAp())) { |
| 423 | rval = ConnectivityManager.TETHER_ERROR_NO_ERROR; |
| 424 | } |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 425 | } |
Erik Kline | 256be78 | 2017-04-29 13:20:04 +0900 | [diff] [blame] | 426 | } finally { |
| 427 | Binder.restoreCallingIdentity(ident); |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 428 | } |
Erik Kline | 256be78 | 2017-04-29 13:20:04 +0900 | [diff] [blame] | 429 | return rval; |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 432 | private void setBluetoothTethering(final boolean enable, final ResultReceiver receiver) { |
| 433 | final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); |
| 434 | if (adapter == null || !adapter.isEnabled()) { |
| 435 | Log.w(TAG, "Tried to enable bluetooth tethering with null or disabled adapter. null: " + |
| 436 | (adapter == null)); |
| 437 | sendTetherResult(receiver, ConnectivityManager.TETHER_ERROR_SERVICE_UNAVAIL); |
| 438 | return; |
| 439 | } |
| 440 | |
| 441 | adapter.getProfileProxy(mContext, new ServiceListener() { |
| 442 | @Override |
| 443 | public void onServiceDisconnected(int profile) { } |
| 444 | |
| 445 | @Override |
| 446 | public void onServiceConnected(int profile, BluetoothProfile proxy) { |
| 447 | ((BluetoothPan) proxy).setBluetoothTethering(enable); |
| 448 | // TODO: Enabling bluetooth tethering can fail asynchronously here. |
| 449 | // We should figure out a way to bubble up that failure instead of sending success. |
Jeremy Klein | 45ae04a | 2016-02-05 10:37:32 -0800 | [diff] [blame] | 450 | int result = ((BluetoothPan) proxy).isTetheringOn() == enable ? |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 451 | ConnectivityManager.TETHER_ERROR_NO_ERROR : |
| 452 | ConnectivityManager.TETHER_ERROR_MASTER_ERROR; |
| 453 | sendTetherResult(receiver, result); |
| 454 | if (enable && isTetherProvisioningRequired()) { |
| 455 | scheduleProvisioningRechecks(ConnectivityManager.TETHERING_BLUETOOTH); |
| 456 | } |
| 457 | adapter.closeProfileProxy(BluetoothProfile.PAN, proxy); |
| 458 | } |
| 459 | }, BluetoothProfile.PAN); |
| 460 | } |
| 461 | |
| 462 | private void runUiTetherProvisioningAndEnable(int type, ResultReceiver receiver) { |
Jeremy Klein | 15f3d21 | 2016-01-24 17:01:09 -0800 | [diff] [blame] | 463 | ResultReceiver proxyReceiver = getProxyReceiver(type, receiver); |
| 464 | sendUiTetherProvisionIntent(type, proxyReceiver); |
| 465 | } |
| 466 | |
| 467 | private void sendUiTetherProvisionIntent(int type, ResultReceiver receiver) { |
| 468 | Intent intent = new Intent(Settings.ACTION_TETHER_PROVISIONING); |
| 469 | intent.putExtra(ConnectivityManager.EXTRA_ADD_TETHER_TYPE, type); |
| 470 | intent.putExtra(ConnectivityManager.EXTRA_PROVISION_CALLBACK, receiver); |
| 471 | final long ident = Binder.clearCallingIdentity(); |
| 472 | try { |
| 473 | mContext.startActivityAsUser(intent, UserHandle.CURRENT); |
| 474 | } finally { |
| 475 | Binder.restoreCallingIdentity(ident); |
| 476 | } |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /** |
Erik Kline | fdd4111 | 2017-01-12 17:02:51 +0900 | [diff] [blame] | 480 | * Creates a proxy {@link ResultReceiver} which enables tethering if the provisioning result |
| 481 | * is successful before firing back up to the wrapped receiver. |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 482 | * |
| 483 | * @param type The type of tethering being enabled. |
| 484 | * @param receiver A ResultReceiver which will be called back with an int resultCode. |
| 485 | * @return The proxy receiver. |
| 486 | */ |
| 487 | private ResultReceiver getProxyReceiver(final int type, final ResultReceiver receiver) { |
| 488 | ResultReceiver rr = new ResultReceiver(null) { |
| 489 | @Override |
| 490 | protected void onReceiveResult(int resultCode, Bundle resultData) { |
| 491 | // If provisioning is successful, enable tethering, otherwise just send the error. |
| 492 | if (resultCode == ConnectivityManager.TETHER_ERROR_NO_ERROR) { |
| 493 | enableTetheringInternal(type, true, receiver); |
| 494 | } else { |
| 495 | sendTetherResult(receiver, resultCode); |
| 496 | } |
| 497 | } |
| 498 | }; |
| 499 | |
| 500 | // The following is necessary to avoid unmarshalling issues when sending the receiver |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 501 | // across processes. |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 502 | Parcel parcel = Parcel.obtain(); |
| 503 | rr.writeToParcel(parcel,0); |
| 504 | parcel.setDataPosition(0); |
| 505 | ResultReceiver receiverForSending = ResultReceiver.CREATOR.createFromParcel(parcel); |
| 506 | parcel.recycle(); |
| 507 | return receiverForSending; |
| 508 | } |
| 509 | |
| 510 | private void scheduleProvisioningRechecks(int type) { |
| 511 | Intent intent = new Intent(); |
| 512 | intent.putExtra(ConnectivityManager.EXTRA_ADD_TETHER_TYPE, type); |
| 513 | intent.putExtra(ConnectivityManager.EXTRA_SET_ALARM, true); |
| 514 | intent.setComponent(TETHER_SERVICE); |
| 515 | final long ident = Binder.clearCallingIdentity(); |
| 516 | try { |
| 517 | mContext.startServiceAsUser(intent, UserHandle.CURRENT); |
| 518 | } finally { |
| 519 | Binder.restoreCallingIdentity(ident); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | private void runSilentTetherProvisioningAndEnable(int type, ResultReceiver receiver) { |
| 524 | ResultReceiver proxyReceiver = getProxyReceiver(type, receiver); |
| 525 | sendSilentTetherProvisionIntent(type, proxyReceiver); |
| 526 | } |
| 527 | |
| 528 | private void sendSilentTetherProvisionIntent(int type, ResultReceiver receiver) { |
| 529 | Intent intent = new Intent(); |
| 530 | intent.putExtra(ConnectivityManager.EXTRA_ADD_TETHER_TYPE, type); |
| 531 | intent.putExtra(ConnectivityManager.EXTRA_RUN_PROVISION, true); |
| 532 | intent.putExtra(ConnectivityManager.EXTRA_PROVISION_CALLBACK, receiver); |
| 533 | intent.setComponent(TETHER_SERVICE); |
| 534 | final long ident = Binder.clearCallingIdentity(); |
| 535 | try { |
| 536 | mContext.startServiceAsUser(intent, UserHandle.CURRENT); |
| 537 | } finally { |
| 538 | Binder.restoreCallingIdentity(ident); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | private void cancelTetherProvisioningRechecks(int type) { |
| 543 | if (getConnectivityManager().isTetheringSupported()) { |
| 544 | Intent intent = new Intent(); |
| 545 | intent.putExtra(ConnectivityManager.EXTRA_REM_TETHER_TYPE, type); |
| 546 | intent.setComponent(TETHER_SERVICE); |
| 547 | final long ident = Binder.clearCallingIdentity(); |
| 548 | try { |
| 549 | mContext.startServiceAsUser(intent, UserHandle.CURRENT); |
| 550 | } finally { |
| 551 | Binder.restoreCallingIdentity(ident); |
| 552 | } |
| 553 | } |
| 554 | } |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 555 | |
Erik Kline | 227648f | 2017-01-20 20:26:10 +0900 | [diff] [blame] | 556 | // Used by the SIM card change observation code. |
| 557 | // TODO: De-duplicate with above code, where possible. |
| 558 | private void startProvisionIntent(int tetherType) { |
| 559 | final Intent startProvIntent = new Intent(); |
| 560 | startProvIntent.putExtra(ConnectivityManager.EXTRA_ADD_TETHER_TYPE, tetherType); |
| 561 | startProvIntent.putExtra(ConnectivityManager.EXTRA_RUN_PROVISION, true); |
| 562 | startProvIntent.setComponent(TETHER_SERVICE); |
| 563 | mContext.startServiceAsUser(startProvIntent, UserHandle.CURRENT); |
| 564 | } |
| 565 | |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 566 | public int tether(String iface) { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 567 | return tether(iface, IControlsTethering.STATE_TETHERED); |
| 568 | } |
| 569 | |
| 570 | private int tether(String iface, int requestedState) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 571 | if (DBG) Log.d(TAG, "Tethering " + iface); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 572 | synchronized (mPublicSync) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 573 | TetherState tetherState = mTetherStates.get(iface); |
| 574 | if (tetherState == null) { |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 575 | Log.e(TAG, "Tried to Tether an unknown iface: " + iface + ", ignoring"); |
Christopher Wiley | 4312a4c | 2016-05-23 13:58:00 -0700 | [diff] [blame] | 576 | return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE; |
| 577 | } |
| 578 | // Ignore the error status of the interface. If the interface is available, |
| 579 | // the errors are referring to past tethering attempts anyway. |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 580 | if (tetherState.lastState != IControlsTethering.STATE_AVAILABLE) { |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 581 | Log.e(TAG, "Tried to Tether an unavailable iface: " + iface + ", ignoring"); |
Christopher Wiley | 4312a4c | 2016-05-23 13:58:00 -0700 | [diff] [blame] | 582 | return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE; |
Christopher Wiley | 4312a4c | 2016-05-23 13:58:00 -0700 | [diff] [blame] | 583 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 584 | // NOTE: If a CMD_TETHER_REQUESTED message is already in the TISM's |
| 585 | // queue but not yet processed, this will be a no-op and it will not |
| 586 | // return an error. |
| 587 | // |
| 588 | // TODO: reexamine the threading and messaging model. |
| 589 | tetherState.stateMachine.sendMessage( |
| 590 | TetherInterfaceStateMachine.CMD_TETHER_REQUESTED, requestedState); |
Christopher Wiley | 4312a4c | 2016-05-23 13:58:00 -0700 | [diff] [blame] | 591 | return ConnectivityManager.TETHER_ERROR_NO_ERROR; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 592 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 593 | } |
| 594 | |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 595 | public int untether(String iface) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 596 | if (DBG) Log.d(TAG, "Untethering " + iface); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 597 | synchronized (mPublicSync) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 598 | TetherState tetherState = mTetherStates.get(iface); |
| 599 | if (tetherState == null) { |
Christopher Wiley | 4312a4c | 2016-05-23 13:58:00 -0700 | [diff] [blame] | 600 | Log.e(TAG, "Tried to Untether an unknown iface :" + iface + ", ignoring"); |
| 601 | return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE; |
| 602 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 603 | if (!tetherState.isCurrentlyServing()) { |
| 604 | Log.e(TAG, "Tried to untether an inactive iface :" + iface + ", ignoring"); |
Christopher Wiley | 4312a4c | 2016-05-23 13:58:00 -0700 | [diff] [blame] | 605 | return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE; |
| 606 | } |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 607 | tetherState.stateMachine.sendMessage( |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 608 | TetherInterfaceStateMachine.CMD_TETHER_UNREQUESTED); |
Christopher Wiley | 4312a4c | 2016-05-23 13:58:00 -0700 | [diff] [blame] | 609 | return ConnectivityManager.TETHER_ERROR_NO_ERROR; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 610 | } |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 611 | } |
| 612 | |
Felipe Leme | 70c8b9b | 2016-04-25 14:41:31 -0700 | [diff] [blame] | 613 | public void untetherAll() { |
Christopher Wiley | fa6a486 | 2016-09-19 08:58:06 -0700 | [diff] [blame] | 614 | stopTethering(ConnectivityManager.TETHERING_WIFI); |
| 615 | stopTethering(ConnectivityManager.TETHERING_USB); |
| 616 | stopTethering(ConnectivityManager.TETHERING_BLUETOOTH); |
Felipe Leme | 70c8b9b | 2016-04-25 14:41:31 -0700 | [diff] [blame] | 617 | } |
| 618 | |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 619 | public int getLastTetherError(String iface) { |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 620 | synchronized (mPublicSync) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 621 | TetherState tetherState = mTetherStates.get(iface); |
| 622 | if (tetherState == null) { |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 623 | Log.e(TAG, "Tried to getLastTetherError on an unknown iface :" + iface + |
| 624 | ", ignoring"); |
| 625 | return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE; |
| 626 | } |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 627 | return tetherState.lastError; |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 628 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 629 | } |
| 630 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 631 | // TODO: Figure out how to update for local hotspot mode interfaces. |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 632 | private void sendTetherStateChangedBroadcast() { |
Sreeram Ramachandran | 62a6131 | 2014-07-24 16:05:17 -0700 | [diff] [blame] | 633 | if (!getConnectivityManager().isTetheringSupported()) return; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 634 | |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 635 | final ArrayList<String> availableList = new ArrayList<>(); |
| 636 | final ArrayList<String> tetherList = new ArrayList<>(); |
| 637 | final ArrayList<String> localOnlyList = new ArrayList<>(); |
| 638 | final ArrayList<String> erroredList = new ArrayList<>(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 639 | |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 640 | boolean wifiTethered = false; |
| 641 | boolean usbTethered = false; |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 642 | boolean bluetoothTethered = false; |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 643 | |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 644 | final TetheringConfiguration cfg = mConfig; |
| 645 | |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 646 | synchronized (mPublicSync) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 647 | for (int i = 0; i < mTetherStates.size(); i++) { |
| 648 | TetherState tetherState = mTetherStates.valueAt(i); |
| 649 | String iface = mTetherStates.keyAt(i); |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 650 | if (tetherState.lastError != ConnectivityManager.TETHER_ERROR_NO_ERROR) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 651 | erroredList.add(iface); |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 652 | } else if (tetherState.lastState == IControlsTethering.STATE_AVAILABLE) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 653 | availableList.add(iface); |
Erik Kline | ca41be7 | 2017-04-20 22:59:16 +0900 | [diff] [blame] | 654 | } else if (tetherState.lastState == IControlsTethering.STATE_LOCAL_ONLY) { |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 655 | localOnlyList.add(iface); |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 656 | } else if (tetherState.lastState == IControlsTethering.STATE_TETHERED) { |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 657 | if (cfg.isUsb(iface)) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 658 | usbTethered = true; |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 659 | } else if (cfg.isWifi(iface)) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 660 | wifiTethered = true; |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 661 | } else if (cfg.isBluetooth(iface)) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 662 | bluetoothTethered = true; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 663 | } |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 664 | tetherList.add(iface); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 665 | } |
| 666 | } |
| 667 | } |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 668 | final Intent bcast = new Intent(ConnectivityManager.ACTION_TETHER_STATE_CHANGED); |
| 669 | bcast.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING | |
Robert Greenwalt | 98c79e5 | 2011-07-28 11:51:11 -0700 | [diff] [blame] | 670 | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 671 | bcast.putStringArrayListExtra(ConnectivityManager.EXTRA_AVAILABLE_TETHER, availableList); |
| 672 | bcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ACTIVE_LOCAL_ONLY, localOnlyList); |
| 673 | bcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ACTIVE_TETHER, tetherList); |
| 674 | bcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ERRORED_TETHER, erroredList); |
| 675 | mContext.sendStickyBroadcastAsUser(bcast, UserHandle.ALL); |
Robert Greenwalt | fd1be2b | 2011-11-11 12:30:19 -0800 | [diff] [blame] | 676 | if (DBG) { |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 677 | Log.d(TAG, String.format( |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 678 | "sendTetherStateChangedBroadcast %s=[%s] %s=[%s] %s=[%s] %s=[%s]", |
| 679 | "avail", TextUtils.join(",", availableList), |
| 680 | "local_only", TextUtils.join(",", localOnlyList), |
| 681 | "tether", TextUtils.join(",", tetherList), |
| 682 | "error", TextUtils.join(",", erroredList))); |
Robert Greenwalt | 924cc94 | 2010-06-28 10:26:19 -0700 | [diff] [blame] | 683 | } |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 684 | |
| 685 | if (usbTethered) { |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 686 | if (wifiTethered || bluetoothTethered) { |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 687 | showTetheredNotification(SystemMessage.NOTE_TETHER_GENERAL); |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 688 | } else { |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 689 | showTetheredNotification(SystemMessage.NOTE_TETHER_USB); |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 690 | } |
| 691 | } else if (wifiTethered) { |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 692 | if (bluetoothTethered) { |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 693 | showTetheredNotification(SystemMessage.NOTE_TETHER_GENERAL); |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 694 | } else { |
Vinit Deshpande | 2576a80 | 2014-11-18 13:56:15 -0800 | [diff] [blame] | 695 | /* We now have a status bar icon for WifiTethering, so drop the notification */ |
| 696 | clearTetheredNotification(); |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 697 | } |
| 698 | } else if (bluetoothTethered) { |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 699 | showTetheredNotification(SystemMessage.NOTE_TETHER_BLUETOOTH); |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 700 | } else { |
| 701 | clearTetheredNotification(); |
| 702 | } |
| 703 | } |
| 704 | |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 705 | private void showTetheredNotification(int id) { |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 706 | NotificationManager notificationManager = |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame^] | 707 | (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 708 | if (notificationManager == null) { |
| 709 | return; |
| 710 | } |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 711 | int icon = 0; |
| 712 | switch(id) { |
| 713 | case SystemMessage.NOTE_TETHER_USB: |
| 714 | icon = com.android.internal.R.drawable.stat_sys_tether_usb; |
| 715 | break; |
| 716 | case SystemMessage.NOTE_TETHER_BLUETOOTH: |
| 717 | icon = com.android.internal.R.drawable.stat_sys_tether_bluetooth; |
| 718 | break; |
| 719 | case SystemMessage.NOTE_TETHER_GENERAL: |
| 720 | default: |
| 721 | icon = com.android.internal.R.drawable.stat_sys_tether_general; |
| 722 | break; |
| 723 | } |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 724 | |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 725 | if (mLastNotificationId != 0) { |
| 726 | if (mLastNotificationId == icon) { |
Robert Greenwalt | db3fe9e | 2010-03-18 16:28:30 -0700 | [diff] [blame] | 727 | return; |
| 728 | } |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 729 | notificationManager.cancelAsUser(null, mLastNotificationId, |
Robert Greenwalt | 3cab6b0 | 2012-10-04 16:44:26 -0700 | [diff] [blame] | 730 | UserHandle.ALL); |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 731 | mLastNotificationId = 0; |
Robert Greenwalt | db3fe9e | 2010-03-18 16:28:30 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 734 | Intent intent = new Intent(); |
| 735 | intent.setClassName("com.android.settings", "com.android.settings.TetherSettings"); |
| 736 | intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); |
| 737 | |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 738 | PendingIntent pi = PendingIntent.getActivityAsUser(mContext, 0, intent, 0, |
| 739 | null, UserHandle.CURRENT); |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 740 | |
| 741 | Resources r = Resources.getSystem(); |
| 742 | CharSequence title = r.getText(com.android.internal.R.string.tethered_notification_title); |
| 743 | CharSequence message = r.getText(com.android.internal.R.string. |
| 744 | tethered_notification_message); |
| 745 | |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 746 | if (mTetheredNotificationBuilder == null) { |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 747 | mTetheredNotificationBuilder = |
| 748 | new Notification.Builder(mContext, SystemNotificationChannels.NETWORK_STATUS); |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 749 | mTetheredNotificationBuilder.setWhen(0) |
| 750 | .setOngoing(true) |
| 751 | .setColor(mContext.getColor( |
| 752 | com.android.internal.R.color.system_notification_accent_color)) |
| 753 | .setVisibility(Notification.VISIBILITY_PUBLIC) |
| 754 | .setCategory(Notification.CATEGORY_STATUS); |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 755 | } |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 756 | mTetheredNotificationBuilder.setSmallIcon(icon) |
| 757 | .setContentTitle(title) |
| 758 | .setContentText(message) |
| 759 | .setContentIntent(pi); |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 760 | mLastNotificationId = id; |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 761 | |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 762 | notificationManager.notifyAsUser(null, mLastNotificationId, |
zhouzhijie | 130d459 | 2017-05-18 10:02:59 +0800 | [diff] [blame] | 763 | mTetheredNotificationBuilder.buildInto(new Notification()), UserHandle.ALL); |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | private void clearTetheredNotification() { |
| 767 | NotificationManager notificationManager = |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame^] | 768 | (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 769 | if (notificationManager != null && mLastNotificationId != 0) { |
| 770 | notificationManager.cancelAsUser(null, mLastNotificationId, |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 771 | UserHandle.ALL); |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 772 | mLastNotificationId = 0; |
Robert Greenwalt | a599fe7c | 2010-03-08 18:30:14 -0800 | [diff] [blame] | 773 | } |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 774 | } |
| 775 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 776 | private class StateReceiver extends BroadcastReceiver { |
Nick Kralevich | 70c117a | 2014-05-27 15:30:02 -0700 | [diff] [blame] | 777 | @Override |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 778 | public void onReceive(Context content, Intent intent) { |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 779 | final String action = intent.getAction(); |
| 780 | if (action == null) return; |
| 781 | |
Mike Lockwood | 770126a | 2010-12-09 22:30:37 -0800 | [diff] [blame] | 782 | if (action.equals(UsbManager.ACTION_USB_STATE)) { |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 783 | handleUsbAction(intent); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 784 | } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) { |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 785 | handleConnectivityAction(intent); |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 786 | } else if (action.equals(WifiManager.WIFI_AP_STATE_CHANGED_ACTION)) { |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 787 | handleWifiApAction(intent); |
Robert Greenwalt | c13368b | 2013-07-18 14:24:42 -0700 | [diff] [blame] | 788 | } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) { |
| 789 | updateConfiguration(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 790 | } |
| 791 | } |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 792 | |
| 793 | private void handleConnectivityAction(Intent intent) { |
| 794 | final NetworkInfo networkInfo = (NetworkInfo)intent.getParcelableExtra( |
| 795 | ConnectivityManager.EXTRA_NETWORK_INFO); |
| 796 | if (networkInfo == null || |
| 797 | networkInfo.getDetailedState() == NetworkInfo.DetailedState.FAILED) { |
| 798 | return; |
| 799 | } |
| 800 | |
| 801 | if (VDBG) Log.d(TAG, "Tethering got CONNECTIVITY_ACTION: " + networkInfo.toString()); |
| 802 | mTetherMasterSM.sendMessage(TetherMasterSM.CMD_UPSTREAM_CHANGED); |
| 803 | } |
| 804 | |
| 805 | private void handleUsbAction(Intent intent) { |
| 806 | final boolean usbConnected = intent.getBooleanExtra(USB_CONNECTED, false); |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame^] | 807 | final boolean usbConfigured = intent.getBooleanExtra(USB_CONFIGURED, false); |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 808 | final boolean rndisEnabled = intent.getBooleanExtra(USB_FUNCTION_RNDIS, false); |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame^] | 809 | |
| 810 | mLog.log(String.format("USB bcast connected:%s configured:%s rndis:%s", |
| 811 | usbConnected, usbConfigured, rndisEnabled)); |
| 812 | |
| 813 | // There are three types of ACTION_USB_STATE: |
| 814 | // |
| 815 | // - DISCONNECTED (USB_CONNECTED and USB_CONFIGURED are 0) |
| 816 | // Meaning: USB connection has ended either because of |
| 817 | // software reset or hard unplug. |
| 818 | // |
| 819 | // - CONNECTED (USB_CONNECTED is 1, USB_CONFIGURED is 0) |
| 820 | // Meaning: the first stage of USB protocol handshake has |
| 821 | // occurred but it is not complete. |
| 822 | // |
| 823 | // - CONFIGURED (USB_CONNECTED and USB_CONFIGURED are 1) |
| 824 | // Meaning: the USB handshake is completely done and all the |
| 825 | // functions are ready to use. |
| 826 | // |
| 827 | // For more explanation, see b/62552150 . |
| 828 | if (usbConnected && !usbConfigured) { |
| 829 | // Nothing for us to do here. |
| 830 | // TODO: consider ignoring DISCONNECTED broadcasts as well. |
| 831 | return; |
| 832 | } |
| 833 | |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 834 | synchronized (Tethering.this.mPublicSync) { |
| 835 | mRndisEnabled = rndisEnabled; |
| 836 | // start tethering if we have a request pending |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame^] | 837 | if (usbConfigured && mRndisEnabled && mUsbTetherRequested) { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 838 | tetherMatchingInterfaces( |
| 839 | IControlsTethering.STATE_TETHERED, |
| 840 | ConnectivityManager.TETHERING_USB); |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 841 | } |
| 842 | mUsbTetherRequested = false; |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | private void handleWifiApAction(Intent intent) { |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 847 | final int curState = intent.getIntExtra(EXTRA_WIFI_AP_STATE, WIFI_AP_STATE_DISABLED); |
| 848 | final String ifname = intent.getStringExtra(EXTRA_WIFI_AP_INTERFACE_NAME); |
| 849 | final int ipmode = intent.getIntExtra(EXTRA_WIFI_AP_MODE, IFACE_IP_MODE_UNSPECIFIED); |
| 850 | |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 851 | synchronized (Tethering.this.mPublicSync) { |
| 852 | switch (curState) { |
| 853 | case WifiManager.WIFI_AP_STATE_ENABLING: |
| 854 | // We can see this state on the way to both enabled and failure states. |
| 855 | break; |
| 856 | case WifiManager.WIFI_AP_STATE_ENABLED: |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 857 | enableWifiIpServingLocked(ifname, ipmode); |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 858 | break; |
| 859 | case WifiManager.WIFI_AP_STATE_DISABLED: |
| 860 | case WifiManager.WIFI_AP_STATE_DISABLING: |
| 861 | case WifiManager.WIFI_AP_STATE_FAILED: |
| 862 | default: |
Erik Kline | 562e0c1 | 2017-06-09 16:36:29 +0900 | [diff] [blame] | 863 | disableWifiIpServingLocked(ifname, curState); |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 864 | break; |
Erik Kline | 2e88b5e | 2017-01-18 11:57:45 +0900 | [diff] [blame] | 865 | } |
| 866 | } |
| 867 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 868 | } |
| 869 | |
Erik Kline | 562e0c1 | 2017-06-09 16:36:29 +0900 | [diff] [blame] | 870 | private void disableWifiIpServingLocked(String ifname, int apState) { |
| 871 | mLog.log("Canceling WiFi tethering request - AP_STATE=" + apState); |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 872 | |
Erik Kline | 562e0c1 | 2017-06-09 16:36:29 +0900 | [diff] [blame] | 873 | // Regardless of whether we requested this transition, the AP has gone |
| 874 | // down. Don't try to tether again unless we're requested to do so. |
| 875 | // TODO: Remove this altogether, once Wi-Fi reliably gives us an |
| 876 | // interface name with every broadcast. |
| 877 | mWifiTetherRequested = false; |
| 878 | |
| 879 | if (!TextUtils.isEmpty(ifname)) { |
| 880 | final TetherState ts = mTetherStates.get(ifname); |
| 881 | if (ts != null) { |
| 882 | ts.stateMachine.unwanted(); |
| 883 | return; |
| 884 | } |
| 885 | } |
| 886 | |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 887 | for (int i = 0; i < mTetherStates.size(); i++) { |
| 888 | TetherInterfaceStateMachine tism = mTetherStates.valueAt(i).stateMachine; |
| 889 | if (tism.interfaceType() == ConnectivityManager.TETHERING_WIFI) { |
Erik Kline | 562e0c1 | 2017-06-09 16:36:29 +0900 | [diff] [blame] | 890 | tism.unwanted(); |
| 891 | return; |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 892 | } |
| 893 | } |
Erik Kline | 562e0c1 | 2017-06-09 16:36:29 +0900 | [diff] [blame] | 894 | |
| 895 | mLog.log("Error disabling Wi-Fi IP serving; " + |
| 896 | (TextUtils.isEmpty(ifname) ? "no interface name specified" |
| 897 | : "specified interface: " + ifname)); |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | private void enableWifiIpServingLocked(String ifname, int wifiIpMode) { |
| 901 | // Map wifiIpMode values to IControlsTethering serving states, inferring |
| 902 | // from mWifiTetherRequested as a final "best guess". |
| 903 | final int ipServingMode; |
| 904 | switch (wifiIpMode) { |
| 905 | case IFACE_IP_MODE_TETHERED: |
| 906 | ipServingMode = IControlsTethering.STATE_TETHERED; |
| 907 | break; |
| 908 | case IFACE_IP_MODE_LOCAL_ONLY: |
| 909 | ipServingMode = IControlsTethering.STATE_LOCAL_ONLY; |
| 910 | break; |
| 911 | default: |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 912 | mLog.e("Cannot enable IP serving in unknown WiFi mode: " + wifiIpMode); |
| 913 | return; |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | if (!TextUtils.isEmpty(ifname)) { |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 917 | maybeTrackNewInterfaceLocked(ifname, ConnectivityManager.TETHERING_WIFI); |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 918 | changeInterfaceState(ifname, ipServingMode); |
| 919 | } else { |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 920 | mLog.e(String.format( |
| 921 | "Cannot enable IP serving in mode %s on missing interface name", |
| 922 | ipServingMode)); |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 923 | } |
| 924 | } |
| 925 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 926 | // TODO: Consider renaming to something more accurate in its description. |
| 927 | // This method: |
| 928 | // - allows requesting either tethering or local hotspot serving states |
| 929 | // - handles both enabling and disabling serving states |
| 930 | // - only tethers the first matching interface in listInterfaces() |
| 931 | // order of a given type |
| 932 | private void tetherMatchingInterfaces(int requestedState, int interfaceType) { |
| 933 | if (VDBG) { |
| 934 | Log.d(TAG, "tetherMatchingInterfaces(" + requestedState + ", " + interfaceType + ")"); |
| 935 | } |
Mike Lockwood | 3c2a2f6 | 2011-06-08 15:10:26 -0700 | [diff] [blame] | 936 | |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 937 | String[] ifaces = null; |
Robert Greenwalt | 65ae29b | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 938 | try { |
Chia-chi Yeh | c933830 | 2011-05-11 16:35:13 -0700 | [diff] [blame] | 939 | ifaces = mNMService.listInterfaces(); |
Robert Greenwalt | 65ae29b | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 940 | } catch (Exception e) { |
Mike Lockwood | 3c2a2f6 | 2011-06-08 15:10:26 -0700 | [diff] [blame] | 941 | Log.e(TAG, "Error listing Interfaces", e); |
Robert Greenwalt | 65ae29b | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 942 | return; |
| 943 | } |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 944 | String chosenIface = null; |
| 945 | if (ifaces != null) { |
| 946 | for (String iface : ifaces) { |
| 947 | if (ifaceNameToType(iface) == interfaceType) { |
| 948 | chosenIface = iface; |
| 949 | break; |
Robert Greenwalt | 65ae29b | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 950 | } |
| 951 | } |
| 952 | } |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 953 | if (chosenIface == null) { |
| 954 | Log.e(TAG, "could not find iface of type " + interfaceType); |
| 955 | return; |
| 956 | } |
| 957 | |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 958 | changeInterfaceState(chosenIface, requestedState); |
| 959 | } |
| 960 | |
| 961 | private void changeInterfaceState(String ifname, int requestedState) { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 962 | final int result; |
| 963 | switch (requestedState) { |
| 964 | case IControlsTethering.STATE_UNAVAILABLE: |
| 965 | case IControlsTethering.STATE_AVAILABLE: |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 966 | result = untether(ifname); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 967 | break; |
| 968 | case IControlsTethering.STATE_TETHERED: |
Erik Kline | ca41be7 | 2017-04-20 22:59:16 +0900 | [diff] [blame] | 969 | case IControlsTethering.STATE_LOCAL_ONLY: |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 970 | result = tether(ifname, requestedState); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 971 | break; |
| 972 | default: |
| 973 | Log.wtf(TAG, "Unknown interface state: " + requestedState); |
| 974 | return; |
| 975 | } |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 976 | if (result != ConnectivityManager.TETHER_ERROR_NO_ERROR) { |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 977 | Log.e(TAG, "unable start or stop tethering on iface " + ifname); |
Christopher Wiley | 5c0b10a | 2016-05-31 14:43:08 -0700 | [diff] [blame] | 978 | return; |
| 979 | } |
Robert Greenwalt | 65ae29b | 2010-02-18 11:25:54 -0800 | [diff] [blame] | 980 | } |
| 981 | |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 982 | public TetheringConfiguration getTetheringConfiguration() { |
| 983 | return mConfig; |
| 984 | } |
| 985 | |
Erik Kline | d781fba | 2017-01-23 13:01:58 +0900 | [diff] [blame] | 986 | public boolean hasTetherableConfiguration() { |
| 987 | final TetheringConfiguration cfg = mConfig; |
| 988 | final boolean hasDownstreamConfiguration = |
| 989 | (cfg.tetherableUsbRegexs.length != 0) || |
| 990 | (cfg.tetherableWifiRegexs.length != 0) || |
| 991 | (cfg.tetherableBluetoothRegexs.length != 0); |
| 992 | final boolean hasUpstreamConfiguration = !cfg.preferredUpstreamIfaceTypes.isEmpty(); |
| 993 | |
| 994 | return hasDownstreamConfiguration && hasUpstreamConfiguration; |
| 995 | } |
| 996 | |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 997 | // TODO - update callers to use getTetheringConfiguration(), |
| 998 | // which has only final members. |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 999 | public String[] getTetherableUsbRegexs() { |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 1000 | return copy(mConfig.tetherableUsbRegexs); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1001 | } |
| 1002 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1003 | public String[] getTetherableWifiRegexs() { |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 1004 | return copy(mConfig.tetherableWifiRegexs); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1005 | } |
| 1006 | |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 1007 | public String[] getTetherableBluetoothRegexs() { |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 1008 | return copy(mConfig.tetherableBluetoothRegexs); |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 1011 | public int setUsbTethering(boolean enable) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1012 | if (VDBG) Log.d(TAG, "setUsbTethering(" + enable + ")"); |
Erik Kline | c438e30 | 2017-07-04 22:02:49 +0900 | [diff] [blame^] | 1013 | UsbManager usbManager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE); |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 1014 | |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 1015 | synchronized (mPublicSync) { |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 1016 | if (enable) { |
| 1017 | if (mRndisEnabled) { |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 1018 | final long ident = Binder.clearCallingIdentity(); |
| 1019 | try { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1020 | tetherMatchingInterfaces(IControlsTethering.STATE_TETHERED, |
| 1021 | ConnectivityManager.TETHERING_USB); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 1022 | } finally { |
| 1023 | Binder.restoreCallingIdentity(ident); |
| 1024 | } |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 1025 | } else { |
| 1026 | mUsbTetherRequested = true; |
Jerry Zhang | 935372b | 2016-10-12 15:49:32 -0700 | [diff] [blame] | 1027 | usbManager.setCurrentFunction(UsbManager.USB_FUNCTION_RNDIS, false); |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 1028 | } |
| 1029 | } else { |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 1030 | final long ident = Binder.clearCallingIdentity(); |
| 1031 | try { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1032 | tetherMatchingInterfaces(IControlsTethering.STATE_AVAILABLE, |
| 1033 | ConnectivityManager.TETHERING_USB); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 1034 | } finally { |
| 1035 | Binder.restoreCallingIdentity(ident); |
| 1036 | } |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 1037 | if (mRndisEnabled) { |
Jerry Zhang | 935372b | 2016-10-12 15:49:32 -0700 | [diff] [blame] | 1038 | usbManager.setCurrentFunction(null, false); |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 1039 | } |
| 1040 | mUsbTetherRequested = false; |
| 1041 | } |
| 1042 | } |
| 1043 | return ConnectivityManager.TETHER_ERROR_NO_ERROR; |
| 1044 | } |
| 1045 | |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1046 | // TODO review API - figure out how to delete these entirely. |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1047 | public String[] getTetheredIfaces() { |
| 1048 | ArrayList<String> list = new ArrayList<String>(); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 1049 | synchronized (mPublicSync) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1050 | for (int i = 0; i < mTetherStates.size(); i++) { |
| 1051 | TetherState tetherState = mTetherStates.valueAt(i); |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 1052 | if (tetherState.lastState == IControlsTethering.STATE_TETHERED) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1053 | list.add(mTetherStates.keyAt(i)); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1054 | } |
| 1055 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1056 | } |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1057 | return list.toArray(new String[list.size()]); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | public String[] getTetherableIfaces() { |
| 1061 | ArrayList<String> list = new ArrayList<String>(); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 1062 | synchronized (mPublicSync) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1063 | for (int i = 0; i < mTetherStates.size(); i++) { |
| 1064 | TetherState tetherState = mTetherStates.valueAt(i); |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 1065 | if (tetherState.lastState == IControlsTethering.STATE_AVAILABLE) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1066 | list.add(mTetherStates.keyAt(i)); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1067 | } |
| 1068 | } |
| 1069 | } |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1070 | return list.toArray(new String[list.size()]); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1071 | } |
| 1072 | |
Robert Greenwalt | 9c7e2c2 | 2014-06-23 14:53:42 -0700 | [diff] [blame] | 1073 | public String[] getTetheredDhcpRanges() { |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 1074 | return mConfig.dhcpRanges; |
Robert Greenwalt | 9c7e2c2 | 2014-06-23 14:53:42 -0700 | [diff] [blame] | 1075 | } |
| 1076 | |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1077 | public String[] getErroredIfaces() { |
| 1078 | ArrayList<String> list = new ArrayList<String>(); |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 1079 | synchronized (mPublicSync) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1080 | for (int i = 0; i < mTetherStates.size(); i++) { |
| 1081 | TetherState tetherState = mTetherStates.valueAt(i); |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 1082 | if (tetherState.lastError != ConnectivityManager.TETHER_ERROR_NO_ERROR) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1083 | list.add(mTetherStates.keyAt(i)); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1084 | } |
| 1085 | } |
| 1086 | } |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1087 | return list.toArray(new String[list.size()]); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1088 | } |
| 1089 | |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 1090 | private void maybeLogMessage(State state, int what) { |
| 1091 | if (DBG) { |
| 1092 | Log.d(TAG, state.getName() + " got " + |
| 1093 | sMagicDecoderRing.get(what, Integer.toString(what))); |
| 1094 | } |
| 1095 | } |
| 1096 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1097 | private boolean upstreamWanted() { |
| 1098 | if (!mForwardedDownstreams.isEmpty()) return true; |
| 1099 | |
| 1100 | synchronized (mPublicSync) { |
| 1101 | return mUsbTetherRequested || mWifiTetherRequested; |
| 1102 | } |
| 1103 | } |
| 1104 | |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1105 | // Needed because the canonical source of upstream truth is just the |
| 1106 | // upstream interface name, |mCurrentUpstreamIface|. This is ripe for |
| 1107 | // future simplification, once the upstream Network is canonical. |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1108 | private boolean pertainsToCurrentUpstream(NetworkState ns) { |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1109 | if (ns != null && ns.linkProperties != null && mCurrentUpstreamIface != null) { |
| 1110 | for (String ifname : ns.linkProperties.getAllInterfaceNames()) { |
| 1111 | if (mCurrentUpstreamIface.equals(ifname)) { |
| 1112 | return true; |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1113 | } |
| 1114 | } |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1115 | } |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1116 | return false; |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1117 | } |
| 1118 | |
Erik Kline | 227648f | 2017-01-20 20:26:10 +0900 | [diff] [blame] | 1119 | private void reevaluateSimCardProvisioning() { |
| 1120 | if (!hasMobileHotspotProvisionApp()) return; |
| 1121 | |
| 1122 | ArrayList<Integer> tethered = new ArrayList<>(); |
| 1123 | synchronized (mPublicSync) { |
| 1124 | for (int i = 0; i < mTetherStates.size(); i++) { |
| 1125 | TetherState tetherState = mTetherStates.valueAt(i); |
| 1126 | if (tetherState.lastState != IControlsTethering.STATE_TETHERED) { |
| 1127 | continue; // Skip interfaces that aren't tethered. |
| 1128 | } |
| 1129 | String iface = mTetherStates.keyAt(i); |
| 1130 | int interfaceType = ifaceNameToType(iface); |
| 1131 | if (interfaceType != ConnectivityManager.TETHERING_INVALID) { |
| 1132 | tethered.add(interfaceType); |
| 1133 | } |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | for (int tetherType : tethered) { |
| 1138 | startProvisionIntent(tetherType); |
| 1139 | } |
| 1140 | } |
| 1141 | |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1142 | private void startOffloadController() { |
| 1143 | mOffloadController.start(); |
| 1144 | mOffloadController.updateExemptPrefixes( |
| 1145 | mUpstreamNetworkMonitor.getOffloadExemptPrefixes()); |
| 1146 | } |
| 1147 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1148 | class TetherMasterSM extends StateMachine { |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 1149 | private static final int BASE_MASTER = Protocol.BASE_TETHERING; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1150 | // an interface SM has requested Tethering/Local Hotspot |
| 1151 | static final int EVENT_IFACE_SERVING_STATE_ACTIVE = BASE_MASTER + 1; |
| 1152 | // an interface SM has unrequested Tethering/Local Hotspot |
| 1153 | static final int EVENT_IFACE_SERVING_STATE_INACTIVE = BASE_MASTER + 2; |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1154 | // upstream connection change - do the right thing |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 1155 | static final int CMD_UPSTREAM_CHANGED = BASE_MASTER + 3; |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1156 | // we don't have a valid upstream conn, check again after a delay |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 1157 | static final int CMD_RETRY_UPSTREAM = BASE_MASTER + 4; |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1158 | // Events from NetworkCallbacks that we process on the master state |
| 1159 | // machine thread on behalf of the UpstreamNetworkMonitor. |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1160 | static final int EVENT_UPSTREAM_CALLBACK = BASE_MASTER + 5; |
Yohei, Oshima | 977aad5 | 2016-12-08 13:39:20 +0900 | [diff] [blame] | 1161 | // we treated the error and want now to clear it |
| 1162 | static final int CMD_CLEAR_ERROR = BASE_MASTER + 6; |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1163 | static final int EVENT_IFACE_UPDATE_LINKPROPERTIES = BASE_MASTER + 7; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1164 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1165 | private State mInitialState; |
| 1166 | private State mTetherModeAliveState; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1167 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1168 | private State mSetIpForwardingEnabledErrorState; |
| 1169 | private State mSetIpForwardingDisabledErrorState; |
| 1170 | private State mStartTetheringErrorState; |
| 1171 | private State mStopTetheringErrorState; |
| 1172 | private State mSetDnsForwardersErrorState; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1173 | |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1174 | // This list is a little subtle. It contains all the interfaces that currently are |
| 1175 | // requesting tethering, regardless of whether these interfaces are still members of |
| 1176 | // mTetherStates. This allows us to maintain the following predicates: |
| 1177 | // |
| 1178 | // 1) mTetherStates contains the set of all currently existing, tetherable, link state up |
| 1179 | // interfaces. |
| 1180 | // 2) mNotifyList contains all state machines that may have outstanding tethering state |
| 1181 | // that needs to be torn down. |
| 1182 | // |
| 1183 | // Because we excise interfaces immediately from mTetherStates, we must maintain mNotifyList |
| 1184 | // so that the garbage collector does not clean up the state machine before it has a chance |
| 1185 | // to tear itself down. |
Erik Kline | 1eb8c69 | 2016-07-08 17:21:26 +0900 | [diff] [blame] | 1186 | private final ArrayList<TetherInterfaceStateMachine> mNotifyList; |
| 1187 | private final IPv6TetheringCoordinator mIPv6TetheringCoordinator; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1188 | |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1189 | private static final int UPSTREAM_SETTLE_TIME_MS = 10000; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1190 | |
Robert Greenwalt | dfadaea | 2010-03-11 15:03:08 -0800 | [diff] [blame] | 1191 | TetherMasterSM(String name, Looper looper) { |
| 1192 | super(name, looper); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1193 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1194 | mInitialState = new InitialState(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1195 | mTetherModeAliveState = new TetherModeAliveState(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1196 | mSetIpForwardingEnabledErrorState = new SetIpForwardingEnabledErrorState(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1197 | mSetIpForwardingDisabledErrorState = new SetIpForwardingDisabledErrorState(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1198 | mStartTetheringErrorState = new StartTetheringErrorState(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1199 | mStopTetheringErrorState = new StopTetheringErrorState(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1200 | mSetDnsForwardersErrorState = new SetDnsForwardersErrorState(); |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1201 | |
| 1202 | addState(mInitialState); |
| 1203 | addState(mTetherModeAliveState); |
| 1204 | addState(mSetIpForwardingEnabledErrorState); |
| 1205 | addState(mSetIpForwardingDisabledErrorState); |
| 1206 | addState(mStartTetheringErrorState); |
| 1207 | addState(mStopTetheringErrorState); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1208 | addState(mSetDnsForwardersErrorState); |
| 1209 | |
Mitchell Wills | 7040b4e | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 1210 | mNotifyList = new ArrayList<>(); |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 1211 | mIPv6TetheringCoordinator = new IPv6TetheringCoordinator(mNotifyList, mLog); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1212 | setInitialState(mInitialState); |
| 1213 | } |
| 1214 | |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1215 | class InitialState extends State { |
| 1216 | @Override |
| 1217 | public boolean processMessage(Message message) { |
| 1218 | maybeLogMessage(this, message.what); |
| 1219 | switch (message.what) { |
| 1220 | case EVENT_IFACE_SERVING_STATE_ACTIVE: |
| 1221 | TetherInterfaceStateMachine who = (TetherInterfaceStateMachine)message.obj; |
| 1222 | if (VDBG) Log.d(TAG, "Tether Mode requested by " + who); |
| 1223 | handleInterfaceServingStateActive(message.arg1, who); |
| 1224 | transitionTo(mTetherModeAliveState); |
| 1225 | break; |
| 1226 | case EVENT_IFACE_SERVING_STATE_INACTIVE: |
| 1227 | who = (TetherInterfaceStateMachine)message.obj; |
| 1228 | if (VDBG) Log.d(TAG, "Tether Mode unrequested by " + who); |
| 1229 | handleInterfaceServingStateInactive(who); |
| 1230 | break; |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1231 | case EVENT_IFACE_UPDATE_LINKPROPERTIES: |
| 1232 | // Silently ignore these for now. |
| 1233 | break; |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1234 | default: |
| 1235 | return NOT_HANDLED; |
| 1236 | } |
| 1237 | return HANDLED; |
| 1238 | } |
| 1239 | } |
| 1240 | |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1241 | protected boolean turnOnMasterTetherSettings() { |
| 1242 | final TetheringConfiguration cfg = mConfig; |
| 1243 | try { |
| 1244 | mNMService.setIpForwardingEnabled(true); |
| 1245 | } catch (Exception e) { |
| 1246 | mLog.e(e); |
| 1247 | transitionTo(mSetIpForwardingEnabledErrorState); |
Robert Greenwalt | d70a3d4 | 2010-02-23 18:15:29 -0800 | [diff] [blame] | 1248 | return false; |
| 1249 | } |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1250 | // TODO: Randomize DHCPv4 ranges, especially in hotspot mode. |
| 1251 | try { |
| 1252 | // TODO: Find a more accurate method name (startDHCPv4()?). |
| 1253 | mNMService.startTethering(cfg.dhcpRanges); |
| 1254 | } catch (Exception e) { |
Robert Greenwalt | d70a3d4 | 2010-02-23 18:15:29 -0800 | [diff] [blame] | 1255 | try { |
Chia-chi Yeh | c933830 | 2011-05-11 16:35:13 -0700 | [diff] [blame] | 1256 | mNMService.stopTethering(); |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1257 | mNMService.startTethering(cfg.dhcpRanges); |
| 1258 | } catch (Exception ee) { |
| 1259 | mLog.e(ee); |
| 1260 | transitionTo(mStartTetheringErrorState); |
Robert Greenwalt | d70a3d4 | 2010-02-23 18:15:29 -0800 | [diff] [blame] | 1261 | return false; |
| 1262 | } |
Robert Greenwalt | d70a3d4 | 2010-02-23 18:15:29 -0800 | [diff] [blame] | 1263 | } |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1264 | mLog.log("SET master tether settings: ON"); |
| 1265 | return true; |
| 1266 | } |
Robert Greenwalt | 1039872 | 2010-12-17 15:20:36 -0800 | [diff] [blame] | 1267 | |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1268 | protected boolean turnOffMasterTetherSettings() { |
| 1269 | try { |
| 1270 | mNMService.stopTethering(); |
| 1271 | } catch (Exception e) { |
| 1272 | mLog.e(e); |
| 1273 | transitionTo(mStopTetheringErrorState); |
| 1274 | return false; |
Erik Kline | 14f7faf | 2017-02-14 19:03:09 +0900 | [diff] [blame] | 1275 | } |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1276 | try { |
| 1277 | mNMService.setIpForwardingEnabled(false); |
| 1278 | } catch (Exception e) { |
| 1279 | mLog.e(e); |
| 1280 | transitionTo(mSetIpForwardingDisabledErrorState); |
| 1281 | return false; |
| 1282 | } |
| 1283 | transitionTo(mInitialState); |
| 1284 | mLog.log("SET master tether settings: OFF"); |
| 1285 | return true; |
| 1286 | } |
Erik Kline | 14f7faf | 2017-02-14 19:03:09 +0900 | [diff] [blame] | 1287 | |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1288 | protected void chooseUpstreamType(boolean tryCell) { |
| 1289 | updateConfiguration(); // TODO - remove? |
Erik Kline | 1e2897d | 2017-06-09 17:08:52 +0900 | [diff] [blame] | 1290 | |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1291 | final NetworkState ns = mUpstreamNetworkMonitor.selectPreferredUpstreamType( |
| 1292 | mConfig.preferredUpstreamIfaceTypes); |
| 1293 | if (ns == null) { |
| 1294 | if (tryCell) { |
| 1295 | mUpstreamNetworkMonitor.registerMobileNetworkRequest(); |
| 1296 | // We think mobile should be coming up; don't set a retry. |
| 1297 | } else { |
| 1298 | sendMessageDelayed(CMD_RETRY_UPSTREAM, UPSTREAM_SETTLE_TIME_MS); |
Erik Kline | 1e2897d | 2017-06-09 17:08:52 +0900 | [diff] [blame] | 1299 | } |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1300 | } |
| 1301 | setUpstreamNetwork(ns); |
| 1302 | } |
| 1303 | |
| 1304 | protected void setUpstreamNetwork(NetworkState ns) { |
| 1305 | String iface = null; |
| 1306 | if (ns != null && ns.linkProperties != null) { |
| 1307 | // Find the interface with the default IPv4 route. It may be the |
| 1308 | // interface described by linkProperties, or one of the interfaces |
| 1309 | // stacked on top of it. |
| 1310 | Log.i(TAG, "Finding IPv4 upstream interface on: " + ns.linkProperties); |
| 1311 | RouteInfo ipv4Default = RouteInfo.selectBestRoute( |
| 1312 | ns.linkProperties.getAllRoutes(), Inet4Address.ANY); |
| 1313 | if (ipv4Default != null) { |
| 1314 | iface = ipv4Default.getInterface(); |
| 1315 | Log.i(TAG, "Found interface " + ipv4Default.getInterface()); |
| 1316 | } else { |
| 1317 | Log.i(TAG, "No IPv4 upstream interface, giving up."); |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1318 | } |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1319 | } |
Robert Greenwalt | ccf83af1 | 2011-06-02 17:30:47 -0700 | [diff] [blame] | 1320 | |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1321 | if (iface != null) { |
| 1322 | setDnsForwarders(ns.network, ns.linkProperties); |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1323 | } |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1324 | notifyTetheredOfNewUpstreamIface(iface); |
| 1325 | if (ns != null && pertainsToCurrentUpstream(ns)) { |
| 1326 | // If we already have NetworkState for this network examine |
| 1327 | // it immediately, because there likely will be no second |
| 1328 | // EVENT_ON_AVAILABLE (it was already received). |
| 1329 | handleNewUpstreamNetworkState(ns); |
| 1330 | } else if (mCurrentUpstreamIface == null) { |
| 1331 | // There are no available upstream networks, or none that |
| 1332 | // have an IPv4 default route (current metric for success). |
| 1333 | handleNewUpstreamNetworkState(null); |
| 1334 | } |
| 1335 | } |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1336 | |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1337 | protected void setDnsForwarders(final Network network, final LinkProperties lp) { |
| 1338 | // TODO: Set v4 and/or v6 DNS per available connectivity. |
| 1339 | String[] dnsServers = mConfig.defaultIPv4DNS; |
| 1340 | final Collection<InetAddress> dnses = lp.getDnsServers(); |
| 1341 | // TODO: Properly support the absence of DNS servers. |
| 1342 | if (dnses != null && !dnses.isEmpty()) { |
| 1343 | // TODO: remove this invocation of NetworkUtils.makeStrings(). |
| 1344 | dnsServers = NetworkUtils.makeStrings(dnses); |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1345 | } |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1346 | try { |
| 1347 | mNMService.setDnsForwarders(network, dnsServers); |
| 1348 | mLog.log(String.format( |
| 1349 | "SET DNS forwarders: network=%s dnsServers=%s", |
| 1350 | network, Arrays.toString(dnsServers))); |
| 1351 | } catch (Exception e) { |
| 1352 | // TODO: Investigate how this can fail and what exactly |
| 1353 | // happens if/when such failures occur. |
| 1354 | mLog.e("setting DNS forwarders failed, " + e); |
| 1355 | transitionTo(mSetDnsForwardersErrorState); |
| 1356 | } |
| 1357 | } |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1358 | |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1359 | protected void notifyTetheredOfNewUpstreamIface(String ifaceName) { |
| 1360 | if (DBG) Log.d(TAG, "Notifying tethered with upstream=" + ifaceName); |
| 1361 | mCurrentUpstreamIface = ifaceName; |
| 1362 | for (TetherInterfaceStateMachine sm : mNotifyList) { |
| 1363 | sm.sendMessage(TetherInterfaceStateMachine.CMD_TETHER_CONNECTION_CHANGED, |
| 1364 | ifaceName); |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1365 | } |
Robert Greenwalt | d70a3d4 | 2010-02-23 18:15:29 -0800 | [diff] [blame] | 1366 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1367 | |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1368 | protected void handleNewUpstreamNetworkState(NetworkState ns) { |
| 1369 | mIPv6TetheringCoordinator.updateUpstreamNetworkState(ns); |
| 1370 | mOffloadController.setUpstreamLinkProperties((ns != null) ? ns.linkProperties : null); |
| 1371 | } |
| 1372 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1373 | private void handleInterfaceServingStateActive(int mode, TetherInterfaceStateMachine who) { |
| 1374 | if (mNotifyList.indexOf(who) < 0) { |
| 1375 | mNotifyList.add(who); |
| 1376 | mIPv6TetheringCoordinator.addActiveDownstream(who, mode); |
| 1377 | } |
| 1378 | |
| 1379 | if (mode == IControlsTethering.STATE_TETHERED) { |
| 1380 | mForwardedDownstreams.add(who); |
| 1381 | } else { |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1382 | mOffloadController.removeDownstreamInterface(who.interfaceName()); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1383 | mForwardedDownstreams.remove(who); |
| 1384 | } |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 1385 | |
| 1386 | // If this is a Wi-Fi interface, notify WifiManager of the active serving state. |
| 1387 | if (who.interfaceType() == ConnectivityManager.TETHERING_WIFI) { |
| 1388 | final WifiManager mgr = getWifiManager(); |
| 1389 | final String iface = who.interfaceName(); |
| 1390 | switch (mode) { |
| 1391 | case IControlsTethering.STATE_TETHERED: |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1392 | mgr.updateInterfaceIpState(iface, IFACE_IP_MODE_TETHERED); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 1393 | break; |
| 1394 | case IControlsTethering.STATE_LOCAL_ONLY: |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1395 | mgr.updateInterfaceIpState(iface, IFACE_IP_MODE_LOCAL_ONLY); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 1396 | break; |
| 1397 | default: |
| 1398 | Log.wtf(TAG, "Unknown active serving mode: " + mode); |
| 1399 | break; |
| 1400 | } |
| 1401 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | private void handleInterfaceServingStateInactive(TetherInterfaceStateMachine who) { |
| 1405 | mNotifyList.remove(who); |
| 1406 | mIPv6TetheringCoordinator.removeActiveDownstream(who); |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1407 | mOffloadController.removeDownstreamInterface(who.interfaceName()); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1408 | mForwardedDownstreams.remove(who); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 1409 | |
| 1410 | // If this is a Wi-Fi interface, tell WifiManager of any errors. |
| 1411 | if (who.interfaceType() == ConnectivityManager.TETHERING_WIFI) { |
| 1412 | if (who.lastError() != ConnectivityManager.TETHER_ERROR_NO_ERROR) { |
| 1413 | getWifiManager().updateInterfaceIpState( |
Erik Kline | 2efb827 | 2017-05-31 15:53:53 +0900 | [diff] [blame] | 1414 | who.interfaceName(), IFACE_IP_MODE_CONFIGURATION_ERROR); |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 1415 | } |
| 1416 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1417 | } |
| 1418 | |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1419 | private void handleUpstreamNetworkMonitorCallback(int arg1, Object o) { |
| 1420 | if (arg1 == UpstreamNetworkMonitor.NOTIFY_EXEMPT_PREFIXES) { |
| 1421 | mOffloadController.updateExemptPrefixes((Set<IpPrefix>) o); |
| 1422 | return; |
| 1423 | } |
| 1424 | |
| 1425 | final NetworkState ns = (NetworkState) o; |
| 1426 | |
| 1427 | if (ns == null || !pertainsToCurrentUpstream(ns)) { |
| 1428 | // TODO: In future, this is where upstream evaluation and selection |
| 1429 | // could be handled for notifications which include sufficient data. |
| 1430 | // For example, after CONNECTIVITY_ACTION listening is removed, here |
| 1431 | // is where we could observe a Wi-Fi network becoming available and |
| 1432 | // passing validation. |
| 1433 | if (mCurrentUpstreamIface == null) { |
| 1434 | // If we have no upstream interface, try to run through upstream |
| 1435 | // selection again. If, for example, IPv4 connectivity has shown up |
| 1436 | // after IPv6 (e.g., 464xlat became available) we want the chance to |
| 1437 | // notice and act accordingly. |
| 1438 | chooseUpstreamType(false); |
| 1439 | } |
| 1440 | return; |
| 1441 | } |
| 1442 | |
| 1443 | switch (arg1) { |
| 1444 | case UpstreamNetworkMonitor.EVENT_ON_AVAILABLE: |
| 1445 | // The default network changed, or DUN connected |
| 1446 | // before this callback was processed. Updates |
| 1447 | // for the current NetworkCapabilities and |
| 1448 | // LinkProperties have been requested (default |
| 1449 | // request) or are being sent shortly (DUN). Do |
| 1450 | // nothing until they arrive; if no updates |
| 1451 | // arrive there's nothing to do. |
| 1452 | break; |
| 1453 | case UpstreamNetworkMonitor.EVENT_ON_CAPABILITIES: |
| 1454 | handleNewUpstreamNetworkState(ns); |
| 1455 | break; |
| 1456 | case UpstreamNetworkMonitor.EVENT_ON_LINKPROPERTIES: |
| 1457 | setDnsForwarders(ns.network, ns.linkProperties); |
| 1458 | handleNewUpstreamNetworkState(ns); |
| 1459 | break; |
| 1460 | case UpstreamNetworkMonitor.EVENT_ON_LOST: |
| 1461 | // TODO: Re-evaluate possible upstreams. Currently upstream |
| 1462 | // reevaluation is triggered via received CONNECTIVITY_ACTION |
| 1463 | // broadcasts that result in being passed a |
| 1464 | // TetherMasterSM.CMD_UPSTREAM_CHANGED. |
| 1465 | handleNewUpstreamNetworkState(null); |
| 1466 | break; |
| 1467 | default: |
| 1468 | mLog.e("Unknown arg1 value: " + arg1); |
| 1469 | break; |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | class TetherModeAliveState extends State { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1474 | boolean mUpstreamWanted = false; |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1475 | boolean mTryCell = true; |
Erik Kline | e0cce21 | 2017-03-06 14:05:23 +0900 | [diff] [blame] | 1476 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1477 | @Override |
| 1478 | public void enter() { |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1479 | // If turning on master tether settings fails, we have already |
| 1480 | // transitioned to an error state; exit early. |
| 1481 | if (!turnOnMasterTetherSettings()) { |
| 1482 | return; |
| 1483 | } |
| 1484 | |
Erik Kline | 227648f | 2017-01-20 20:26:10 +0900 | [diff] [blame] | 1485 | mSimChange.startListening(); |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1486 | mUpstreamNetworkMonitor.start(); |
Robert Greenwalt | 4f74d55 | 2011-12-19 16:59:31 -0800 | [diff] [blame] | 1487 | |
Erik Kline | f4b6e34 | 2017-04-25 19:19:59 +0900 | [diff] [blame] | 1488 | // TODO: De-duplicate with updateUpstreamWanted() below. |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1489 | if (upstreamWanted()) { |
| 1490 | mUpstreamWanted = true; |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1491 | startOffloadController(); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1492 | chooseUpstreamType(true); |
| 1493 | mTryCell = false; |
| 1494 | } |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1495 | } |
Erik Kline | fa37b2f | 2016-08-02 18:27:03 +0900 | [diff] [blame] | 1496 | |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1497 | @Override |
| 1498 | public void exit() { |
Erik Kline | e0cce21 | 2017-03-06 14:05:23 +0900 | [diff] [blame] | 1499 | mOffloadController.stop(); |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1500 | mUpstreamNetworkMonitor.stop(); |
Erik Kline | 227648f | 2017-01-20 20:26:10 +0900 | [diff] [blame] | 1501 | mSimChange.stopListening(); |
Robert Greenwalt | b37f0c6 | 2010-03-25 14:50:34 -0700 | [diff] [blame] | 1502 | notifyTetheredOfNewUpstreamIface(null); |
Erik Kline | fa37b2f | 2016-08-02 18:27:03 +0900 | [diff] [blame] | 1503 | handleNewUpstreamNetworkState(null); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1504 | } |
Erik Kline | fa37b2f | 2016-08-02 18:27:03 +0900 | [diff] [blame] | 1505 | |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1506 | private boolean updateUpstreamWanted() { |
| 1507 | final boolean previousUpstreamWanted = mUpstreamWanted; |
| 1508 | mUpstreamWanted = upstreamWanted(); |
Erik Kline | f4b6e34 | 2017-04-25 19:19:59 +0900 | [diff] [blame] | 1509 | if (mUpstreamWanted != previousUpstreamWanted) { |
| 1510 | if (mUpstreamWanted) { |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1511 | startOffloadController(); |
Erik Kline | f4b6e34 | 2017-04-25 19:19:59 +0900 | [diff] [blame] | 1512 | } else { |
| 1513 | mOffloadController.stop(); |
| 1514 | } |
| 1515 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1516 | return previousUpstreamWanted; |
| 1517 | } |
| 1518 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1519 | @Override |
| 1520 | public boolean processMessage(Message message) { |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 1521 | maybeLogMessage(this, message.what); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1522 | boolean retValue = true; |
| 1523 | switch (message.what) { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1524 | case EVENT_IFACE_SERVING_STATE_ACTIVE: { |
Mitchell Wills | 7040b4e | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 1525 | TetherInterfaceStateMachine who = (TetherInterfaceStateMachine)message.obj; |
Robert Greenwalt | 68ea9b0 | 2012-05-10 16:58:16 -0700 | [diff] [blame] | 1526 | if (VDBG) Log.d(TAG, "Tether Mode requested by " + who); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1527 | handleInterfaceServingStateActive(message.arg1, who); |
Mitchell Wills | 7040b4e | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 1528 | who.sendMessage(TetherInterfaceStateMachine.CMD_TETHER_CONNECTION_CHANGED, |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1529 | mCurrentUpstreamIface); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1530 | // If there has been a change and an upstream is now |
| 1531 | // desired, kick off the selection process. |
| 1532 | final boolean previousUpstreamWanted = updateUpstreamWanted(); |
| 1533 | if (!previousUpstreamWanted && mUpstreamWanted) { |
| 1534 | chooseUpstreamType(true); |
| 1535 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1536 | break; |
Erik Kline | 6e29bf0 | 2016-08-15 16:16:18 +0900 | [diff] [blame] | 1537 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1538 | case EVENT_IFACE_SERVING_STATE_INACTIVE: { |
Erik Kline | 6e29bf0 | 2016-08-15 16:16:18 +0900 | [diff] [blame] | 1539 | TetherInterfaceStateMachine who = (TetherInterfaceStateMachine)message.obj; |
Robert Greenwalt | 68ea9b0 | 2012-05-10 16:58:16 -0700 | [diff] [blame] | 1540 | if (VDBG) Log.d(TAG, "Tether Mode unrequested by " + who); |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1541 | handleInterfaceServingStateInactive(who); |
| 1542 | |
| 1543 | if (mNotifyList.isEmpty()) { |
Erik Kline | 47222fc | 2017-04-30 19:36:15 +0900 | [diff] [blame] | 1544 | // This transitions us out of TetherModeAliveState, |
| 1545 | // either to InitialState or an error state. |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1546 | turnOffMasterTetherSettings(); |
| 1547 | break; |
| 1548 | } |
| 1549 | |
| 1550 | if (DBG) { |
| 1551 | Log.d(TAG, "TetherModeAlive still has " + mNotifyList.size() + |
| 1552 | " live requests:"); |
| 1553 | for (TetherInterfaceStateMachine o : mNotifyList) { |
| 1554 | Log.d(TAG, " " + o); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1555 | } |
| 1556 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1557 | // If there has been a change and an upstream is no |
| 1558 | // longer desired, release any mobile requests. |
| 1559 | final boolean previousUpstreamWanted = updateUpstreamWanted(); |
| 1560 | if (previousUpstreamWanted && !mUpstreamWanted) { |
| 1561 | mUpstreamNetworkMonitor.releaseMobileNetworkRequest(); |
| 1562 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1563 | break; |
Erik Kline | 6e29bf0 | 2016-08-15 16:16:18 +0900 | [diff] [blame] | 1564 | } |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1565 | case EVENT_IFACE_UPDATE_LINKPROPERTIES: { |
| 1566 | final LinkProperties newLp = (LinkProperties) message.obj; |
| 1567 | if (message.arg1 == IControlsTethering.STATE_TETHERED) { |
| 1568 | mOffloadController.notifyDownstreamLinkProperties(newLp); |
| 1569 | } else { |
| 1570 | mOffloadController.removeDownstreamInterface(newLp.getInterfaceName()); |
| 1571 | } |
| 1572 | break; |
| 1573 | } |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1574 | case CMD_UPSTREAM_CHANGED: |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1575 | updateUpstreamWanted(); |
| 1576 | if (!mUpstreamWanted) break; |
| 1577 | |
Erik Kline | fb41343 | 2017-02-14 18:26:04 +0900 | [diff] [blame] | 1578 | // Need to try DUN immediately if Wi-Fi goes down. |
| 1579 | chooseUpstreamType(true); |
| 1580 | mTryCell = false; |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1581 | break; |
Kazuhiro Ondo | 01758e8 | 2011-04-30 20:10:57 -0500 | [diff] [blame] | 1582 | case CMD_RETRY_UPSTREAM: |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1583 | updateUpstreamWanted(); |
| 1584 | if (!mUpstreamWanted) break; |
| 1585 | |
Kazuhiro Ondo | 01758e8 | 2011-04-30 20:10:57 -0500 | [diff] [blame] | 1586 | chooseUpstreamType(mTryCell); |
| 1587 | mTryCell = !mTryCell; |
| 1588 | break; |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1589 | case EVENT_UPSTREAM_CALLBACK: { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1590 | updateUpstreamWanted(); |
Erik Kline | 3a5278f | 2017-06-24 19:29:10 +0900 | [diff] [blame] | 1591 | if (mUpstreamWanted) { |
| 1592 | handleUpstreamNetworkMonitorCallback(message.arg1, message.obj); |
Erik Kline | 6ff17f7 | 2015-12-10 20:42:12 +0900 | [diff] [blame] | 1593 | } |
| 1594 | break; |
Erik Kline | 00019f4 | 2016-06-30 19:31:46 +0900 | [diff] [blame] | 1595 | } |
Kazuhiro Ondo | 01758e8 | 2011-04-30 20:10:57 -0500 | [diff] [blame] | 1596 | default: |
| 1597 | retValue = false; |
| 1598 | break; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1599 | } |
| 1600 | return retValue; |
| 1601 | } |
| 1602 | } |
| 1603 | |
Wink Saville | 64c42ca | 2011-04-18 14:55:10 -0700 | [diff] [blame] | 1604 | class ErrorState extends State { |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 1605 | private int mErrorNotification; |
| 1606 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1607 | @Override |
| 1608 | public boolean processMessage(Message message) { |
| 1609 | boolean retValue = true; |
| 1610 | switch (message.what) { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1611 | case EVENT_IFACE_SERVING_STATE_ACTIVE: |
Mitchell Wills | 7040b4e | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 1612 | TetherInterfaceStateMachine who = (TetherInterfaceStateMachine)message.obj; |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1613 | who.sendMessage(mErrorNotification); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1614 | break; |
Yohei, Oshima | 977aad5 | 2016-12-08 13:39:20 +0900 | [diff] [blame] | 1615 | case CMD_CLEAR_ERROR: |
| 1616 | mErrorNotification = ConnectivityManager.TETHER_ERROR_NO_ERROR; |
| 1617 | transitionTo(mInitialState); |
| 1618 | break; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1619 | default: |
| 1620 | retValue = false; |
| 1621 | } |
| 1622 | return retValue; |
| 1623 | } |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 1624 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1625 | void notify(int msgType) { |
| 1626 | mErrorNotification = msgType; |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1627 | for (TetherInterfaceStateMachine sm : mNotifyList) { |
Robert Greenwalt | 6a1967c | 2010-03-17 11:19:57 -0700 | [diff] [blame] | 1628 | sm.sendMessage(msgType); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1629 | } |
| 1630 | } |
| 1631 | |
| 1632 | } |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 1633 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1634 | class SetIpForwardingEnabledErrorState extends ErrorState { |
| 1635 | @Override |
| 1636 | public void enter() { |
| 1637 | Log.e(TAG, "Error in setIpForwardingEnabled"); |
Mitchell Wills | 7040b4e | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 1638 | notify(TetherInterfaceStateMachine.CMD_IP_FORWARDING_ENABLE_ERROR); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1639 | } |
| 1640 | } |
| 1641 | |
| 1642 | class SetIpForwardingDisabledErrorState extends ErrorState { |
| 1643 | @Override |
| 1644 | public void enter() { |
| 1645 | Log.e(TAG, "Error in setIpForwardingDisabled"); |
Mitchell Wills | 7040b4e | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 1646 | notify(TetherInterfaceStateMachine.CMD_IP_FORWARDING_DISABLE_ERROR); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1647 | } |
| 1648 | } |
| 1649 | |
| 1650 | class StartTetheringErrorState extends ErrorState { |
| 1651 | @Override |
| 1652 | public void enter() { |
| 1653 | Log.e(TAG, "Error in startTethering"); |
Mitchell Wills | 7040b4e | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 1654 | notify(TetherInterfaceStateMachine.CMD_START_TETHERING_ERROR); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1655 | try { |
Chia-chi Yeh | c933830 | 2011-05-11 16:35:13 -0700 | [diff] [blame] | 1656 | mNMService.setIpForwardingEnabled(false); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1657 | } catch (Exception e) {} |
| 1658 | } |
| 1659 | } |
| 1660 | |
| 1661 | class StopTetheringErrorState extends ErrorState { |
| 1662 | @Override |
| 1663 | public void enter() { |
| 1664 | Log.e(TAG, "Error in stopTethering"); |
Mitchell Wills | 7040b4e | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 1665 | notify(TetherInterfaceStateMachine.CMD_STOP_TETHERING_ERROR); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1666 | try { |
Chia-chi Yeh | c933830 | 2011-05-11 16:35:13 -0700 | [diff] [blame] | 1667 | mNMService.setIpForwardingEnabled(false); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1668 | } catch (Exception e) {} |
| 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | class SetDnsForwardersErrorState extends ErrorState { |
| 1673 | @Override |
| 1674 | public void enter() { |
| 1675 | Log.e(TAG, "Error in setDnsForwarders"); |
Mitchell Wills | 7040b4e | 2016-05-23 16:40:10 -0700 | [diff] [blame] | 1676 | notify(TetherInterfaceStateMachine.CMD_SET_DNS_FORWARDERS_ERROR); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1677 | try { |
Chia-chi Yeh | c933830 | 2011-05-11 16:35:13 -0700 | [diff] [blame] | 1678 | mNMService.stopTethering(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1679 | } catch (Exception e) {} |
| 1680 | try { |
Chia-chi Yeh | c933830 | 2011-05-11 16:35:13 -0700 | [diff] [blame] | 1681 | mNMService.setIpForwardingEnabled(false); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1682 | } catch (Exception e) {} |
| 1683 | } |
| 1684 | } |
| 1685 | } |
| 1686 | |
Christopher Wiley | 499a57a | 2016-05-16 16:19:07 -0700 | [diff] [blame] | 1687 | @Override |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 1688 | public void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
Christopher Wiley | 499a57a | 2016-05-16 16:19:07 -0700 | [diff] [blame] | 1689 | // Binder.java closes the resource for us. |
| 1690 | @SuppressWarnings("resource") |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 1691 | final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " "); |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 1692 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1693 | |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 1694 | pw.println("Tethering:"); |
| 1695 | pw.increaseIndent(); |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 1696 | |
Erik Kline | 9db1b54 | 2017-03-16 14:10:27 +0900 | [diff] [blame] | 1697 | pw.println("Configuration:"); |
| 1698 | pw.increaseIndent(); |
| 1699 | final TetheringConfiguration cfg = mConfig; |
| 1700 | cfg.dump(pw); |
| 1701 | pw.decreaseIndent(); |
| 1702 | |
| 1703 | synchronized (mPublicSync) { |
Robert Greenwalt | b445362 | 2011-11-03 16:01:40 -0700 | [diff] [blame] | 1704 | pw.println("Tether state:"); |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 1705 | pw.increaseIndent(); |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1706 | for (int i = 0; i < mTetherStates.size(); i++) { |
| 1707 | final String iface = mTetherStates.keyAt(i); |
| 1708 | final TetherState tetherState = mTetherStates.valueAt(i); |
| 1709 | pw.print(iface + " - "); |
| 1710 | |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 1711 | switch (tetherState.lastState) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1712 | case IControlsTethering.STATE_UNAVAILABLE: |
| 1713 | pw.print("UnavailableState"); |
| 1714 | break; |
| 1715 | case IControlsTethering.STATE_AVAILABLE: |
| 1716 | pw.print("AvailableState"); |
| 1717 | break; |
| 1718 | case IControlsTethering.STATE_TETHERED: |
| 1719 | pw.print("TetheredState"); |
| 1720 | break; |
Erik Kline | ca41be7 | 2017-04-20 22:59:16 +0900 | [diff] [blame] | 1721 | case IControlsTethering.STATE_LOCAL_ONLY: |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1722 | pw.print("LocalHotspotState"); |
| 1723 | break; |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1724 | default: |
| 1725 | pw.print("UnknownState"); |
| 1726 | break; |
| 1727 | } |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 1728 | pw.println(" - lastError = " + tetherState.lastError); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1729 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1730 | pw.println("Upstream wanted: " + upstreamWanted()); |
Erik Kline | ee363c4 | 2017-05-29 09:11:03 +0900 | [diff] [blame] | 1731 | pw.println("Current upstream interface: " + mCurrentUpstreamIface); |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 1732 | pw.decreaseIndent(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1733 | } |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1734 | |
| 1735 | pw.println("Log:"); |
| 1736 | pw.increaseIndent(); |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 1737 | if (argsContain(args, SHORT_ARG)) { |
| 1738 | pw.println("<log removed for brevity>"); |
| 1739 | } else { |
| 1740 | mLog.dump(fd, pw, args); |
| 1741 | } |
Erik Kline | 1fdc2e2 | 2017-05-08 17:56:35 +0900 | [diff] [blame] | 1742 | pw.decreaseIndent(); |
| 1743 | |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 1744 | pw.decreaseIndent(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1745 | } |
Christopher Wiley | e03fb44 | 2016-05-18 13:45:20 -0700 | [diff] [blame] | 1746 | |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 1747 | private static boolean argsContain(String[] args, String target) { |
| 1748 | for (String arg : args) { |
Erik Kline | ee363c4 | 2017-05-29 09:11:03 +0900 | [diff] [blame] | 1749 | if (target.equals(arg)) return true; |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 1750 | } |
| 1751 | return false; |
| 1752 | } |
| 1753 | |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1754 | private IControlsTethering makeControlCallback(String ifname) { |
| 1755 | return new IControlsTethering() { |
| 1756 | @Override |
| 1757 | public void updateInterfaceState( |
| 1758 | TetherInterfaceStateMachine who, int state, int lastError) { |
| 1759 | notifyInterfaceStateChange(ifname, who, state, lastError); |
| 1760 | } |
| 1761 | |
| 1762 | @Override |
| 1763 | public void updateLinkProperties( |
| 1764 | TetherInterfaceStateMachine who, LinkProperties newLp) { |
| 1765 | notifyLinkPropertiesChanged(ifname, who, newLp); |
| 1766 | } |
| 1767 | }; |
| 1768 | } |
| 1769 | |
| 1770 | // TODO: Move into TetherMasterSM. |
| 1771 | private void notifyInterfaceStateChange( |
| 1772 | String iface, TetherInterfaceStateMachine who, int state, int error) { |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1773 | synchronized (mPublicSync) { |
Erik Kline | 216af6d | 2017-04-27 20:57:23 +0900 | [diff] [blame] | 1774 | final TetherState tetherState = mTetherStates.get(iface); |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 1775 | if (tetherState != null && tetherState.stateMachine.equals(who)) { |
| 1776 | tetherState.lastState = state; |
| 1777 | tetherState.lastError = error; |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1778 | } else { |
| 1779 | if (DBG) Log.d(TAG, "got notification from stale iface " + iface); |
| 1780 | } |
| 1781 | } |
| 1782 | |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 1783 | mLog.log(String.format("OBSERVED iface=%s state=%s error=%s", iface, state, error)); |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1784 | |
Christopher Wiley | deebfec | 2016-09-16 11:14:36 -0700 | [diff] [blame] | 1785 | try { |
| 1786 | // Notify that we're tethering (or not) this interface. |
| 1787 | // This is how data saver for instance knows if the user explicitly |
| 1788 | // turned on tethering (thus keeping us from being in data saver mode). |
| 1789 | mPolicyManager.onTetheringChanged(iface, state == IControlsTethering.STATE_TETHERED); |
| 1790 | } catch (RemoteException e) { |
| 1791 | // Not really very much we can do here. |
| 1792 | } |
| 1793 | |
Yohei, Oshima | 977aad5 | 2016-12-08 13:39:20 +0900 | [diff] [blame] | 1794 | // If TetherMasterSM is in ErrorState, TetherMasterSM stays there. |
| 1795 | // Thus we give a chance for TetherMasterSM to recover to InitialState |
| 1796 | // by sending CMD_CLEAR_ERROR |
| 1797 | if (error == ConnectivityManager.TETHER_ERROR_MASTER_ERROR) { |
| 1798 | mTetherMasterSM.sendMessage(TetherMasterSM.CMD_CLEAR_ERROR, who); |
| 1799 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1800 | int which; |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1801 | switch (state) { |
| 1802 | case IControlsTethering.STATE_UNAVAILABLE: |
| 1803 | case IControlsTethering.STATE_AVAILABLE: |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1804 | which = TetherMasterSM.EVENT_IFACE_SERVING_STATE_INACTIVE; |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1805 | break; |
| 1806 | case IControlsTethering.STATE_TETHERED: |
Erik Kline | ca41be7 | 2017-04-20 22:59:16 +0900 | [diff] [blame] | 1807 | case IControlsTethering.STATE_LOCAL_ONLY: |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1808 | which = TetherMasterSM.EVENT_IFACE_SERVING_STATE_ACTIVE; |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1809 | break; |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1810 | default: |
| 1811 | Log.wtf(TAG, "Unknown interface state: " + state); |
| 1812 | return; |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1813 | } |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 1814 | mTetherMasterSM.sendMessage(which, state, 0, who); |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1815 | sendTetherStateChangedBroadcast(); |
| 1816 | } |
| 1817 | |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1818 | private void notifyLinkPropertiesChanged(String iface, TetherInterfaceStateMachine who, |
| 1819 | LinkProperties newLp) { |
| 1820 | final int state; |
| 1821 | synchronized (mPublicSync) { |
| 1822 | final TetherState tetherState = mTetherStates.get(iface); |
| 1823 | if (tetherState != null && tetherState.stateMachine.equals(who)) { |
| 1824 | state = tetherState.lastState; |
| 1825 | } else { |
| 1826 | mLog.log("got notification from stale iface " + iface); |
| 1827 | return; |
| 1828 | } |
| 1829 | } |
| 1830 | |
Erik Kline | 7fd696c | 2017-06-12 18:20:08 +0900 | [diff] [blame] | 1831 | mLog.log(String.format( |
| 1832 | "OBSERVED LinkProperties update iface=%s state=%s lp=%s", |
| 1833 | iface, IControlsTethering.getStateString(state), newLp)); |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1834 | final int which = TetherMasterSM.EVENT_IFACE_UPDATE_LINKPROPERTIES; |
| 1835 | mTetherMasterSM.sendMessage(which, state, 0, newLp); |
| 1836 | } |
| 1837 | |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 1838 | private void maybeTrackNewInterfaceLocked(final String iface) { |
| 1839 | // If we don't care about this type of interface, ignore. |
| 1840 | final int interfaceType = ifaceNameToType(iface); |
| 1841 | if (interfaceType == ConnectivityManager.TETHERING_INVALID) { |
| 1842 | mLog.log(iface + " is not a tetherable iface, ignoring"); |
| 1843 | return; |
| 1844 | } |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 1845 | maybeTrackNewInterfaceLocked(iface, interfaceType); |
| 1846 | } |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 1847 | |
Erik Kline | a9cde8b | 2017-06-20 21:18:31 +0900 | [diff] [blame] | 1848 | private void maybeTrackNewInterfaceLocked(final String iface, int interfaceType) { |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 1849 | // If we have already started a TISM for this interface, skip. |
| 1850 | if (mTetherStates.containsKey(iface)) { |
| 1851 | mLog.log("active iface (" + iface + ") reported as added, ignoring"); |
| 1852 | return; |
| 1853 | } |
| 1854 | |
| 1855 | mLog.log("adding TetheringInterfaceStateMachine for: " + iface); |
| 1856 | final TetherState tetherState = new TetherState( |
| 1857 | new TetherInterfaceStateMachine( |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1858 | iface, mLooper, interfaceType, mLog, mNMService, mStatsService, |
Erik Kline | a8e2e79 | 2017-06-13 21:32:10 +0900 | [diff] [blame] | 1859 | makeControlCallback(iface))); |
Christopher Wiley | d985dde | 2016-05-31 10:44:35 -0700 | [diff] [blame] | 1860 | mTetherStates.put(iface, tetherState); |
Hugo Benichi | b55fb22 | 2017-03-10 14:20:57 +0900 | [diff] [blame] | 1861 | tetherState.stateMachine.start(); |
Christopher Wiley | e03fb44 | 2016-05-18 13:45:20 -0700 | [diff] [blame] | 1862 | } |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 1863 | |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 1864 | private void stopTrackingInterfaceLocked(final String iface) { |
| 1865 | final TetherState tetherState = mTetherStates.get(iface); |
| 1866 | if (tetherState == null) { |
| 1867 | mLog.log("attempting to remove unknown iface (" + iface + "), ignoring"); |
| 1868 | return; |
| 1869 | } |
Erik Kline | 6e9a101 | 2017-06-06 19:24:21 +0900 | [diff] [blame] | 1870 | tetherState.stateMachine.stop(); |
Erik Kline | 4dd9bb8 | 2017-04-26 11:11:07 +0900 | [diff] [blame] | 1871 | mLog.log("removing TetheringInterfaceStateMachine for: " + iface); |
| 1872 | mTetherStates.remove(iface); |
| 1873 | } |
| 1874 | |
Erik Kline | 3e75665 | 2017-01-17 13:42:19 +0900 | [diff] [blame] | 1875 | private static String[] copy(String[] strarray) { |
| 1876 | return Arrays.copyOf(strarray, strarray.length); |
| 1877 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1878 | } |