Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2013, 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 | */ |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 16 | package com.android.server.connectivity; |
| 17 | |
| 18 | import android.app.AlarmManager; |
| 19 | import android.app.PendingIntent; |
| 20 | import android.content.BroadcastReceiver; |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 21 | import android.content.ComponentName; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 22 | import android.content.ContentResolver; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
| 25 | import android.content.IntentFilter; |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 26 | import android.content.ServiceConnection; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 27 | import android.net.ProxyProperties; |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 28 | import android.os.Handler; |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 29 | import android.os.IBinder; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 30 | import android.os.RemoteException; |
| 31 | import android.os.ServiceManager; |
| 32 | import android.os.SystemClock; |
| 33 | import android.os.SystemProperties; |
| 34 | import android.provider.Settings; |
| 35 | import android.text.TextUtils; |
| 36 | import android.util.Log; |
| 37 | |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 38 | import com.android.internal.annotations.GuardedBy; |
Jason Monk | 6f8a68f | 2013-08-23 19:21:25 -0400 | [diff] [blame] | 39 | import com.android.net.IProxyCallback; |
| 40 | import com.android.net.IProxyPortListener; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 41 | import com.android.net.IProxyService; |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 42 | import com.android.server.IoThread; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 43 | |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 44 | import libcore.io.Streams; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 45 | |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 46 | import java.io.IOException; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 47 | import java.net.URL; |
| 48 | import java.net.URLConnection; |
| 49 | |
| 50 | /** |
| 51 | * @hide |
| 52 | */ |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 53 | public class PacManager { |
Jason Monk | da205a7 | 2013-08-21 15:57:30 -0400 | [diff] [blame] | 54 | public static final String PAC_PACKAGE = "com.android.pacprocessor"; |
| 55 | public static final String PAC_SERVICE = "com.android.pacprocessor.PacService"; |
| 56 | public static final String PAC_SERVICE_NAME = "com.android.net.IProxyService"; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 57 | |
Jason Monk | da205a7 | 2013-08-21 15:57:30 -0400 | [diff] [blame] | 58 | public static final String PROXY_PACKAGE = "com.android.proxyhandler"; |
| 59 | public static final String PROXY_SERVICE = "com.android.proxyhandler.ProxyService"; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 60 | |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 61 | private static final String TAG = "PacManager"; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 62 | |
| 63 | private static final String ACTION_PAC_REFRESH = "android.net.proxy.PAC_REFRESH"; |
| 64 | |
| 65 | private static final String DEFAULT_DELAYS = "8 32 120 14400 43200"; |
| 66 | private static final int DELAY_1 = 0; |
| 67 | private static final int DELAY_4 = 3; |
| 68 | private static final int DELAY_LONG = 4; |
| 69 | |
| 70 | /** Keep these values up-to-date with ProxyService.java */ |
| 71 | public static final String KEY_PROXY = "keyProxy"; |
| 72 | private String mCurrentPac; |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 73 | @GuardedBy("mProxyLock") |
| 74 | private String mPacUrl; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 75 | |
| 76 | private AlarmManager mAlarmManager; |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 77 | @GuardedBy("mProxyLock") |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 78 | private IProxyService mProxyService; |
| 79 | private PendingIntent mPacRefreshIntent; |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 80 | private ServiceConnection mConnection; |
Jason Monk | da205a7 | 2013-08-21 15:57:30 -0400 | [diff] [blame] | 81 | private ServiceConnection mProxyConnection; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 82 | private Context mContext; |
| 83 | |
| 84 | private int mCurrentDelay; |
Jason Monk | 6f8a68f | 2013-08-23 19:21:25 -0400 | [diff] [blame] | 85 | private int mLastPort; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 86 | |
Jason Monk | d443479 | 2013-09-23 13:32:39 -0400 | [diff] [blame] | 87 | private boolean mHasSentBroadcast; |
| 88 | private boolean mHasDownloaded; |
| 89 | |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 90 | private Handler mConnectivityHandler; |
| 91 | private int mProxyMessage; |
| 92 | |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 93 | /** |
| 94 | * Used for locking when setting mProxyService and all references to mPacUrl or mCurrentPac. |
| 95 | */ |
| 96 | private final Object mProxyLock = new Object(); |
| 97 | |
| 98 | private Runnable mPacDownloader = new Runnable() { |
| 99 | @Override |
| 100 | public void run() { |
| 101 | String file; |
| 102 | synchronized (mProxyLock) { |
| 103 | if (mPacUrl == null) return; |
| 104 | try { |
| 105 | file = get(mPacUrl); |
| 106 | } catch (IOException ioe) { |
| 107 | file = null; |
| 108 | Log.w(TAG, "Failed to load PAC file: " + ioe); |
| 109 | } |
| 110 | } |
| 111 | if (file != null) { |
| 112 | synchronized (mProxyLock) { |
| 113 | if (!file.equals(mCurrentPac)) { |
| 114 | setCurrentProxyScript(file); |
| 115 | } |
| 116 | } |
Jason Monk | d443479 | 2013-09-23 13:32:39 -0400 | [diff] [blame] | 117 | mHasDownloaded = true; |
| 118 | sendProxyIfNeeded(); |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 119 | longSchedule(); |
| 120 | } else { |
| 121 | reschedule(); |
| 122 | } |
| 123 | } |
| 124 | }; |
| 125 | |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 126 | class PacRefreshIntentReceiver extends BroadcastReceiver { |
| 127 | public void onReceive(Context context, Intent intent) { |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 128 | IoThread.getHandler().post(mPacDownloader); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 132 | public PacManager(Context context, Handler handler, int proxyMessage) { |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 133 | mContext = context; |
Jason Monk | 6f8a68f | 2013-08-23 19:21:25 -0400 | [diff] [blame] | 134 | mLastPort = -1; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 135 | |
| 136 | mPacRefreshIntent = PendingIntent.getBroadcast( |
| 137 | context, 0, new Intent(ACTION_PAC_REFRESH), 0); |
| 138 | context.registerReceiver(new PacRefreshIntentReceiver(), |
| 139 | new IntentFilter(ACTION_PAC_REFRESH)); |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 140 | mConnectivityHandler = handler; |
| 141 | mProxyMessage = proxyMessage; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | private AlarmManager getAlarmManager() { |
| 145 | if (mAlarmManager == null) { |
| 146 | mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE); |
| 147 | } |
| 148 | return mAlarmManager; |
| 149 | } |
| 150 | |
Jason Monk | 6f8a68f | 2013-08-23 19:21:25 -0400 | [diff] [blame] | 151 | /** |
| 152 | * Updates the PAC Manager with current Proxy information. This is called by |
| 153 | * the ConnectivityService directly before a broadcast takes place to allow |
| 154 | * the PacManager to indicate that the broadcast should not be sent and the |
| 155 | * PacManager will trigger a new broadcast when it is ready. |
| 156 | * |
| 157 | * @param proxy Proxy information that is about to be broadcast. |
| 158 | * @return Returns true when the broadcast should not be sent |
| 159 | */ |
| 160 | public synchronized boolean setCurrentProxyScriptUrl(ProxyProperties proxy) { |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 161 | if (!TextUtils.isEmpty(proxy.getPacFileUrl())) { |
Jason Monk | 90760c8 | 2013-10-14 18:24:13 -0400 | [diff] [blame] | 162 | if (proxy.getPacFileUrl().equals(mPacUrl) && (proxy.getPort() > 0)) { |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 163 | // Allow to send broadcast, nothing to do. |
| 164 | return false; |
| 165 | } |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 166 | synchronized (mProxyLock) { |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 167 | mPacUrl = proxy.getPacFileUrl(); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 168 | } |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 169 | mCurrentDelay = DELAY_1; |
Jason Monk | d443479 | 2013-09-23 13:32:39 -0400 | [diff] [blame] | 170 | mHasSentBroadcast = false; |
| 171 | mHasDownloaded = false; |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 172 | getAlarmManager().cancel(mPacRefreshIntent); |
| 173 | bind(); |
Jason Monk | 6f8a68f | 2013-08-23 19:21:25 -0400 | [diff] [blame] | 174 | return true; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 175 | } else { |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 176 | getAlarmManager().cancel(mPacRefreshIntent); |
| 177 | synchronized (mProxyLock) { |
| 178 | mPacUrl = null; |
| 179 | mCurrentPac = null; |
| 180 | if (mProxyService != null) { |
| 181 | try { |
| 182 | mProxyService.stopPacSystem(); |
| 183 | } catch (RemoteException e) { |
| 184 | Log.w(TAG, "Failed to stop PAC service", e); |
| 185 | } finally { |
| 186 | unbind(); |
| 187 | } |
| 188 | } |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 189 | } |
Jason Monk | 6f8a68f | 2013-08-23 19:21:25 -0400 | [diff] [blame] | 190 | return false; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Does a post and reports back the status code. |
| 196 | * |
| 197 | * @throws IOException |
| 198 | */ |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 199 | private static String get(String urlString) throws IOException { |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 200 | URL url = new URL(urlString); |
| 201 | URLConnection urlConnection = url.openConnection(java.net.Proxy.NO_PROXY); |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 202 | return new String(Streams.readFully(urlConnection.getInputStream())); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | private int getNextDelay(int currentDelay) { |
| 206 | if (++currentDelay > DELAY_4) { |
| 207 | return DELAY_4; |
| 208 | } |
| 209 | return currentDelay; |
| 210 | } |
| 211 | |
| 212 | private void longSchedule() { |
| 213 | mCurrentDelay = DELAY_1; |
| 214 | setDownloadIn(DELAY_LONG); |
| 215 | } |
| 216 | |
| 217 | private void reschedule() { |
| 218 | mCurrentDelay = getNextDelay(mCurrentDelay); |
| 219 | setDownloadIn(mCurrentDelay); |
| 220 | } |
| 221 | |
| 222 | private String getPacChangeDelay() { |
| 223 | final ContentResolver cr = mContext.getContentResolver(); |
| 224 | |
| 225 | /** Check system properties for the default value then use secure settings value, if any. */ |
| 226 | String defaultDelay = SystemProperties.get( |
| 227 | "conn." + Settings.Global.PAC_CHANGE_DELAY, |
| 228 | DEFAULT_DELAYS); |
| 229 | String val = Settings.Global.getString(cr, Settings.Global.PAC_CHANGE_DELAY); |
| 230 | return (val == null) ? defaultDelay : val; |
| 231 | } |
| 232 | |
| 233 | private long getDownloadDelay(int delayIndex) { |
| 234 | String[] list = getPacChangeDelay().split(" "); |
| 235 | if (delayIndex < list.length) { |
| 236 | return Long.parseLong(list[delayIndex]); |
| 237 | } |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | private void setDownloadIn(int delayIndex) { |
| 242 | long delay = getDownloadDelay(delayIndex); |
| 243 | long timeTillTrigger = 1000 * delay + SystemClock.elapsedRealtime(); |
| 244 | getAlarmManager().set(AlarmManager.ELAPSED_REALTIME, timeTillTrigger, mPacRefreshIntent); |
| 245 | } |
| 246 | |
| 247 | private boolean setCurrentProxyScript(String script) { |
Wink Saville | a48ad8b | 2013-08-10 11:22:31 -0700 | [diff] [blame] | 248 | if (mProxyService == null) { |
| 249 | Log.e(TAG, "setCurrentProxyScript: no proxy service"); |
| 250 | return false; |
| 251 | } |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 252 | try { |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 253 | mProxyService.setPacFile(script); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 254 | mCurrentPac = script; |
| 255 | } catch (RemoteException e) { |
| 256 | Log.e(TAG, "Unable to set PAC file", e); |
| 257 | } |
| 258 | return true; |
| 259 | } |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 260 | |
| 261 | private void bind() { |
| 262 | if (mContext == null) { |
| 263 | Log.e(TAG, "No context for binding"); |
| 264 | return; |
| 265 | } |
| 266 | Intent intent = new Intent(); |
Jason Monk | da205a7 | 2013-08-21 15:57:30 -0400 | [diff] [blame] | 267 | intent.setClassName(PAC_PACKAGE, PAC_SERVICE); |
Jason Monk | 6f8a68f | 2013-08-23 19:21:25 -0400 | [diff] [blame] | 268 | // Already bound no need to bind again. |
Jason Monk | bc018d8 | 2013-09-17 16:37:38 -0400 | [diff] [blame] | 269 | if ((mProxyConnection != null) && (mConnection != null)) { |
Jason Monk | 6f8a68f | 2013-08-23 19:21:25 -0400 | [diff] [blame] | 270 | if (mLastPort != -1) { |
| 271 | sendPacBroadcast(new ProxyProperties(mPacUrl, mLastPort)); |
| 272 | } else { |
| 273 | Log.e(TAG, "Received invalid port from Local Proxy," |
| 274 | + " PAC will not be operational"); |
| 275 | } |
| 276 | return; |
| 277 | } |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 278 | mConnection = new ServiceConnection() { |
| 279 | @Override |
| 280 | public void onServiceDisconnected(ComponentName component) { |
| 281 | synchronized (mProxyLock) { |
| 282 | mProxyService = null; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | @Override |
| 287 | public void onServiceConnected(ComponentName component, IBinder binder) { |
| 288 | synchronized (mProxyLock) { |
| 289 | try { |
Jason Monk | da205a7 | 2013-08-21 15:57:30 -0400 | [diff] [blame] | 290 | Log.d(TAG, "Adding service " + PAC_SERVICE_NAME + " " |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 291 | + binder.getInterfaceDescriptor()); |
| 292 | } catch (RemoteException e1) { |
| 293 | Log.e(TAG, "Remote Exception", e1); |
| 294 | } |
Jason Monk | da205a7 | 2013-08-21 15:57:30 -0400 | [diff] [blame] | 295 | ServiceManager.addService(PAC_SERVICE_NAME, binder); |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 296 | mProxyService = IProxyService.Stub.asInterface(binder); |
| 297 | if (mProxyService == null) { |
| 298 | Log.e(TAG, "No proxy service"); |
| 299 | } else { |
| 300 | try { |
| 301 | mProxyService.startPacSystem(); |
| 302 | } catch (RemoteException e) { |
| 303 | Log.e(TAG, "Unable to reach ProxyService - PAC will not be started", e); |
| 304 | } |
| 305 | IoThread.getHandler().post(mPacDownloader); |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | }; |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 310 | mContext.bindService(intent, mConnection, |
| 311 | Context.BIND_AUTO_CREATE | Context.BIND_NOT_FOREGROUND | Context.BIND_NOT_VISIBLE); |
Jason Monk | da205a7 | 2013-08-21 15:57:30 -0400 | [diff] [blame] | 312 | |
| 313 | intent = new Intent(); |
| 314 | intent.setClassName(PROXY_PACKAGE, PROXY_SERVICE); |
| 315 | mProxyConnection = new ServiceConnection() { |
| 316 | @Override |
| 317 | public void onServiceDisconnected(ComponentName component) { |
| 318 | } |
| 319 | |
| 320 | @Override |
| 321 | public void onServiceConnected(ComponentName component, IBinder binder) { |
Jason Monk | 6f8a68f | 2013-08-23 19:21:25 -0400 | [diff] [blame] | 322 | IProxyCallback callbackService = IProxyCallback.Stub.asInterface(binder); |
| 323 | if (callbackService != null) { |
| 324 | try { |
| 325 | callbackService.getProxyPort(new IProxyPortListener.Stub() { |
| 326 | @Override |
| 327 | public void setProxyPort(int port) throws RemoteException { |
Jason Monk | d443479 | 2013-09-23 13:32:39 -0400 | [diff] [blame] | 328 | if (mLastPort != -1) { |
| 329 | // Always need to send if port changed |
| 330 | mHasSentBroadcast = false; |
| 331 | } |
Jason Monk | 6f8a68f | 2013-08-23 19:21:25 -0400 | [diff] [blame] | 332 | mLastPort = port; |
| 333 | if (port != -1) { |
| 334 | Log.d(TAG, "Local proxy is bound on " + port); |
Jason Monk | d443479 | 2013-09-23 13:32:39 -0400 | [diff] [blame] | 335 | sendProxyIfNeeded(); |
Jason Monk | 6f8a68f | 2013-08-23 19:21:25 -0400 | [diff] [blame] | 336 | } else { |
| 337 | Log.e(TAG, "Received invalid port from Local Proxy," |
| 338 | + " PAC will not be operational"); |
| 339 | } |
| 340 | } |
| 341 | }); |
| 342 | } catch (RemoteException e) { |
| 343 | e.printStackTrace(); |
| 344 | } |
| 345 | } |
Jason Monk | da205a7 | 2013-08-21 15:57:30 -0400 | [diff] [blame] | 346 | } |
| 347 | }; |
| 348 | mContext.bindService(intent, mProxyConnection, |
| 349 | Context.BIND_AUTO_CREATE | Context.BIND_NOT_FOREGROUND | Context.BIND_NOT_VISIBLE); |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | private void unbind() { |
Jason Monk | bc018d8 | 2013-09-17 16:37:38 -0400 | [diff] [blame] | 353 | if (mConnection != null) { |
| 354 | mContext.unbindService(mConnection); |
| 355 | mConnection = null; |
| 356 | } |
| 357 | if (mProxyConnection != null) { |
| 358 | mContext.unbindService(mProxyConnection); |
| 359 | mProxyConnection = null; |
| 360 | } |
| 361 | mProxyService = null; |
Jason Monk | d443479 | 2013-09-23 13:32:39 -0400 | [diff] [blame] | 362 | mLastPort = -1; |
Jason Monk | 6f8a68f | 2013-08-23 19:21:25 -0400 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | private void sendPacBroadcast(ProxyProperties proxy) { |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 366 | mConnectivityHandler.sendMessage(mConnectivityHandler.obtainMessage(mProxyMessage, proxy)); |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 367 | } |
Jason Monk | d443479 | 2013-09-23 13:32:39 -0400 | [diff] [blame] | 368 | |
| 369 | private synchronized void sendProxyIfNeeded() { |
| 370 | if (!mHasDownloaded || (mLastPort == -1)) { |
| 371 | return; |
| 372 | } |
| 373 | if (!mHasSentBroadcast) { |
| 374 | sendPacBroadcast(new ProxyProperties(mPacUrl, mLastPort)); |
| 375 | mHasSentBroadcast = true; |
| 376 | } |
| 377 | } |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 378 | } |