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