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