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