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