1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/com/squareup/picasso/Dispatcher.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,315 @@ 1.4 +/* 1.5 + * Copyright (C) 2013 Square, Inc. 1.6 + * 1.7 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.8 + * you may not use this file except in compliance with the License. 1.9 + * You may obtain a copy of the License at 1.10 + * 1.11 + * http://www.apache.org/licenses/LICENSE-2.0 1.12 + * 1.13 + * Unless required by applicable law or agreed to in writing, software 1.14 + * distributed under the License is distributed on an "AS IS" BASIS, 1.15 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.16 + * See the License for the specific language governing permissions and 1.17 + * limitations under the License. 1.18 + */ 1.19 +package com.squareup.picasso; 1.20 + 1.21 +import android.Manifest; 1.22 +import android.content.BroadcastReceiver; 1.23 +import android.content.Context; 1.24 +import android.content.Intent; 1.25 +import android.content.IntentFilter; 1.26 +import android.net.ConnectivityManager; 1.27 +import android.net.NetworkInfo; 1.28 +import android.os.Bundle; 1.29 +import android.os.Handler; 1.30 +import android.os.HandlerThread; 1.31 +import android.os.Looper; 1.32 +import android.os.Message; 1.33 +import java.util.ArrayList; 1.34 +import java.util.LinkedHashMap; 1.35 +import java.util.List; 1.36 +import java.util.Map; 1.37 +import java.util.concurrent.ExecutorService; 1.38 + 1.39 +import static android.content.Context.CONNECTIVITY_SERVICE; 1.40 +import static android.content.Intent.ACTION_AIRPLANE_MODE_CHANGED; 1.41 +import static android.net.ConnectivityManager.CONNECTIVITY_ACTION; 1.42 +import static android.os.Process.THREAD_PRIORITY_BACKGROUND; 1.43 +import static com.squareup.picasso.BitmapHunter.forRequest; 1.44 + 1.45 +class Dispatcher { 1.46 + private static final int RETRY_DELAY = 500; 1.47 + private static final int AIRPLANE_MODE_ON = 1; 1.48 + private static final int AIRPLANE_MODE_OFF = 0; 1.49 + 1.50 + static final int REQUEST_SUBMIT = 1; 1.51 + static final int REQUEST_CANCEL = 2; 1.52 + static final int REQUEST_GCED = 3; 1.53 + static final int HUNTER_COMPLETE = 4; 1.54 + static final int HUNTER_RETRY = 5; 1.55 + static final int HUNTER_DECODE_FAILED = 6; 1.56 + static final int HUNTER_DELAY_NEXT_BATCH = 7; 1.57 + static final int HUNTER_BATCH_COMPLETE = 8; 1.58 + static final int NETWORK_STATE_CHANGE = 9; 1.59 + static final int AIRPLANE_MODE_CHANGE = 10; 1.60 + 1.61 + private static final String DISPATCHER_THREAD_NAME = "Dispatcher"; 1.62 + private static final int BATCH_DELAY = 200; // ms 1.63 + 1.64 + final DispatcherThread dispatcherThread; 1.65 + final Context context; 1.66 + final ExecutorService service; 1.67 + final Downloader downloader; 1.68 + final Map<String, BitmapHunter> hunterMap; 1.69 + final Handler handler; 1.70 + final Handler mainThreadHandler; 1.71 + final Cache cache; 1.72 + final Stats stats; 1.73 + final List<BitmapHunter> batch; 1.74 + final NetworkBroadcastReceiver receiver; 1.75 + 1.76 + NetworkInfo networkInfo; 1.77 + boolean airplaneMode; 1.78 + 1.79 + Dispatcher(Context context, ExecutorService service, Handler mainThreadHandler, 1.80 + Downloader downloader, Cache cache, Stats stats) { 1.81 + this.dispatcherThread = new DispatcherThread(); 1.82 + this.dispatcherThread.start(); 1.83 + this.context = context; 1.84 + this.service = service; 1.85 + this.hunterMap = new LinkedHashMap<String, BitmapHunter>(); 1.86 + this.handler = new DispatcherHandler(dispatcherThread.getLooper(), this); 1.87 + this.downloader = downloader; 1.88 + this.mainThreadHandler = mainThreadHandler; 1.89 + this.cache = cache; 1.90 + this.stats = stats; 1.91 + this.batch = new ArrayList<BitmapHunter>(4); 1.92 + this.airplaneMode = Utils.isAirplaneModeOn(this.context); 1.93 + this.receiver = new NetworkBroadcastReceiver(this.context); 1.94 + receiver.register(); 1.95 + } 1.96 + 1.97 + void shutdown() { 1.98 + service.shutdown(); 1.99 + dispatcherThread.quit(); 1.100 + receiver.unregister(); 1.101 + } 1.102 + 1.103 + void dispatchSubmit(Action action) { 1.104 + handler.sendMessage(handler.obtainMessage(REQUEST_SUBMIT, action)); 1.105 + } 1.106 + 1.107 + void dispatchCancel(Action action) { 1.108 + handler.sendMessage(handler.obtainMessage(REQUEST_CANCEL, action)); 1.109 + } 1.110 + 1.111 + void dispatchComplete(BitmapHunter hunter) { 1.112 + handler.sendMessage(handler.obtainMessage(HUNTER_COMPLETE, hunter)); 1.113 + } 1.114 + 1.115 + void dispatchRetry(BitmapHunter hunter) { 1.116 + handler.sendMessageDelayed(handler.obtainMessage(HUNTER_RETRY, hunter), RETRY_DELAY); 1.117 + } 1.118 + 1.119 + void dispatchFailed(BitmapHunter hunter) { 1.120 + handler.sendMessage(handler.obtainMessage(HUNTER_DECODE_FAILED, hunter)); 1.121 + } 1.122 + 1.123 + void dispatchNetworkStateChange(NetworkInfo info) { 1.124 + handler.sendMessage(handler.obtainMessage(NETWORK_STATE_CHANGE, info)); 1.125 + } 1.126 + 1.127 + void dispatchAirplaneModeChange(boolean airplaneMode) { 1.128 + handler.sendMessage(handler.obtainMessage(AIRPLANE_MODE_CHANGE, 1.129 + airplaneMode ? AIRPLANE_MODE_ON : AIRPLANE_MODE_OFF, 0)); 1.130 + } 1.131 + 1.132 + void performSubmit(Action action) { 1.133 + BitmapHunter hunter = hunterMap.get(action.getKey()); 1.134 + if (hunter != null) { 1.135 + hunter.attach(action); 1.136 + return; 1.137 + } 1.138 + 1.139 + if (service.isShutdown()) { 1.140 + return; 1.141 + } 1.142 + 1.143 + hunter = forRequest(context, action.getPicasso(), this, cache, stats, action, downloader); 1.144 + hunter.future = service.submit(hunter); 1.145 + hunterMap.put(action.getKey(), hunter); 1.146 + } 1.147 + 1.148 + void performCancel(Action action) { 1.149 + String key = action.getKey(); 1.150 + BitmapHunter hunter = hunterMap.get(key); 1.151 + if (hunter != null) { 1.152 + hunter.detach(action); 1.153 + if (hunter.cancel()) { 1.154 + hunterMap.remove(key); 1.155 + } 1.156 + } 1.157 + } 1.158 + 1.159 + void performRetry(BitmapHunter hunter) { 1.160 + if (hunter.isCancelled()) return; 1.161 + 1.162 + if (service.isShutdown()) { 1.163 + performError(hunter); 1.164 + return; 1.165 + } 1.166 + 1.167 + if (hunter.shouldRetry(airplaneMode, networkInfo)) { 1.168 + hunter.future = service.submit(hunter); 1.169 + } else { 1.170 + performError(hunter); 1.171 + } 1.172 + } 1.173 + 1.174 + void performComplete(BitmapHunter hunter) { 1.175 + if (!hunter.shouldSkipMemoryCache()) { 1.176 + cache.set(hunter.getKey(), hunter.getResult()); 1.177 + } 1.178 + hunterMap.remove(hunter.getKey()); 1.179 + batch(hunter); 1.180 + } 1.181 + 1.182 + void performBatchComplete() { 1.183 + List<BitmapHunter> copy = new ArrayList<BitmapHunter>(batch); 1.184 + batch.clear(); 1.185 + mainThreadHandler.sendMessage(mainThreadHandler.obtainMessage(HUNTER_BATCH_COMPLETE, copy)); 1.186 + } 1.187 + 1.188 + void performError(BitmapHunter hunter) { 1.189 + hunterMap.remove(hunter.getKey()); 1.190 + batch(hunter); 1.191 + } 1.192 + 1.193 + void performAirplaneModeChange(boolean airplaneMode) { 1.194 + this.airplaneMode = airplaneMode; 1.195 + } 1.196 + 1.197 + void performNetworkStateChange(NetworkInfo info) { 1.198 + networkInfo = info; 1.199 + if (service instanceof PicassoExecutorService) { 1.200 + ((PicassoExecutorService) service).adjustThreadCount(info); 1.201 + } 1.202 + } 1.203 + 1.204 + private void batch(BitmapHunter hunter) { 1.205 + if (hunter.isCancelled()) { 1.206 + return; 1.207 + } 1.208 + batch.add(hunter); 1.209 + if (!handler.hasMessages(HUNTER_DELAY_NEXT_BATCH)) { 1.210 + handler.sendEmptyMessageDelayed(HUNTER_DELAY_NEXT_BATCH, BATCH_DELAY); 1.211 + } 1.212 + } 1.213 + 1.214 + private static class DispatcherHandler extends Handler { 1.215 + private final Dispatcher dispatcher; 1.216 + 1.217 + public DispatcherHandler(Looper looper, Dispatcher dispatcher) { 1.218 + super(looper); 1.219 + this.dispatcher = dispatcher; 1.220 + } 1.221 + 1.222 + @Override public void handleMessage(final Message msg) { 1.223 + switch (msg.what) { 1.224 + case REQUEST_SUBMIT: { 1.225 + Action action = (Action) msg.obj; 1.226 + dispatcher.performSubmit(action); 1.227 + break; 1.228 + } 1.229 + case REQUEST_CANCEL: { 1.230 + Action action = (Action) msg.obj; 1.231 + dispatcher.performCancel(action); 1.232 + break; 1.233 + } 1.234 + case HUNTER_COMPLETE: { 1.235 + BitmapHunter hunter = (BitmapHunter) msg.obj; 1.236 + dispatcher.performComplete(hunter); 1.237 + break; 1.238 + } 1.239 + case HUNTER_RETRY: { 1.240 + BitmapHunter hunter = (BitmapHunter) msg.obj; 1.241 + dispatcher.performRetry(hunter); 1.242 + break; 1.243 + } 1.244 + case HUNTER_DECODE_FAILED: { 1.245 + BitmapHunter hunter = (BitmapHunter) msg.obj; 1.246 + dispatcher.performError(hunter); 1.247 + break; 1.248 + } 1.249 + case HUNTER_DELAY_NEXT_BATCH: { 1.250 + dispatcher.performBatchComplete(); 1.251 + break; 1.252 + } 1.253 + case NETWORK_STATE_CHANGE: { 1.254 + NetworkInfo info = (NetworkInfo) msg.obj; 1.255 + dispatcher.performNetworkStateChange(info); 1.256 + break; 1.257 + } 1.258 + case AIRPLANE_MODE_CHANGE: { 1.259 + dispatcher.performAirplaneModeChange(msg.arg1 == AIRPLANE_MODE_ON); 1.260 + break; 1.261 + } 1.262 + default: 1.263 + Picasso.HANDLER.post(new Runnable() { 1.264 + @Override public void run() { 1.265 + throw new AssertionError("Unknown handler message received: " + msg.what); 1.266 + } 1.267 + }); 1.268 + } 1.269 + } 1.270 + } 1.271 + 1.272 + static class DispatcherThread extends HandlerThread { 1.273 + DispatcherThread() { 1.274 + super(Utils.THREAD_PREFIX + DISPATCHER_THREAD_NAME, THREAD_PRIORITY_BACKGROUND); 1.275 + } 1.276 + } 1.277 + 1.278 + private class NetworkBroadcastReceiver extends BroadcastReceiver { 1.279 + private static final String EXTRA_AIRPLANE_STATE = "state"; 1.280 + 1.281 + private final ConnectivityManager connectivityManager; 1.282 + 1.283 + NetworkBroadcastReceiver(Context context) { 1.284 + connectivityManager = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE); 1.285 + } 1.286 + 1.287 + void register() { 1.288 + boolean shouldScanState = service instanceof PicassoExecutorService && // 1.289 + Utils.hasPermission(context, Manifest.permission.ACCESS_NETWORK_STATE); 1.290 + IntentFilter filter = new IntentFilter(); 1.291 + filter.addAction(ACTION_AIRPLANE_MODE_CHANGED); 1.292 + if (shouldScanState) { 1.293 + filter.addAction(CONNECTIVITY_ACTION); 1.294 + } 1.295 + context.registerReceiver(this, filter); 1.296 + } 1.297 + 1.298 + void unregister() { 1.299 + context.unregisterReceiver(this); 1.300 + } 1.301 + 1.302 + @Override public void onReceive(Context context, Intent intent) { 1.303 + // On some versions of Android this may be called with a null Intent 1.304 + if (null == intent) { 1.305 + return; 1.306 + } 1.307 + 1.308 + String action = intent.getAction(); 1.309 + Bundle extras = intent.getExtras(); 1.310 + 1.311 + if (ACTION_AIRPLANE_MODE_CHANGED.equals(action)) { 1.312 + dispatchAirplaneModeChange(extras.getBoolean(EXTRA_AIRPLANE_STATE, false)); 1.313 + } else if (CONNECTIVITY_ACTION.equals(action)) { 1.314 + dispatchNetworkStateChange(connectivityManager.getActiveNetworkInfo()); 1.315 + } 1.316 + } 1.317 + } 1.318 +}