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