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