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