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