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