|
1 /* |
|
2 * Copyright (C) 2013 Square, Inc. |
|
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 */ |
|
16 package com.squareup.picasso; |
|
17 |
|
18 import android.net.ConnectivityManager; |
|
19 import android.net.NetworkInfo; |
|
20 import android.telephony.TelephonyManager; |
|
21 import java.util.concurrent.LinkedBlockingQueue; |
|
22 import java.util.concurrent.ThreadPoolExecutor; |
|
23 import java.util.concurrent.TimeUnit; |
|
24 |
|
25 /** |
|
26 * The default {@link java.util.concurrent.ExecutorService} used for new {@link Picasso} instances. |
|
27 * <p/> |
|
28 * Exists as a custom type so that we can differentiate the use of defaults versus a user-supplied |
|
29 * instance. |
|
30 */ |
|
31 class PicassoExecutorService extends ThreadPoolExecutor { |
|
32 private static final int DEFAULT_THREAD_COUNT = 3; |
|
33 |
|
34 PicassoExecutorService() { |
|
35 super(DEFAULT_THREAD_COUNT, DEFAULT_THREAD_COUNT, 0, TimeUnit.MILLISECONDS, |
|
36 new LinkedBlockingQueue<Runnable>(), new Utils.PicassoThreadFactory()); |
|
37 } |
|
38 |
|
39 void adjustThreadCount(NetworkInfo info) { |
|
40 if (info == null || !info.isConnectedOrConnecting()) { |
|
41 setThreadCount(DEFAULT_THREAD_COUNT); |
|
42 return; |
|
43 } |
|
44 switch (info.getType()) { |
|
45 case ConnectivityManager.TYPE_WIFI: |
|
46 case ConnectivityManager.TYPE_WIMAX: |
|
47 case ConnectivityManager.TYPE_ETHERNET: |
|
48 setThreadCount(4); |
|
49 break; |
|
50 case ConnectivityManager.TYPE_MOBILE: |
|
51 switch (info.getSubtype()) { |
|
52 case TelephonyManager.NETWORK_TYPE_LTE: // 4G |
|
53 case TelephonyManager.NETWORK_TYPE_HSPAP: |
|
54 case TelephonyManager.NETWORK_TYPE_EHRPD: |
|
55 setThreadCount(3); |
|
56 break; |
|
57 case TelephonyManager.NETWORK_TYPE_UMTS: // 3G |
|
58 case TelephonyManager.NETWORK_TYPE_CDMA: |
|
59 case TelephonyManager.NETWORK_TYPE_EVDO_0: |
|
60 case TelephonyManager.NETWORK_TYPE_EVDO_A: |
|
61 case TelephonyManager.NETWORK_TYPE_EVDO_B: |
|
62 setThreadCount(2); |
|
63 break; |
|
64 case TelephonyManager.NETWORK_TYPE_GPRS: // 2G |
|
65 case TelephonyManager.NETWORK_TYPE_EDGE: |
|
66 setThreadCount(1); |
|
67 break; |
|
68 default: |
|
69 setThreadCount(DEFAULT_THREAD_COUNT); |
|
70 } |
|
71 break; |
|
72 default: |
|
73 setThreadCount(DEFAULT_THREAD_COUNT); |
|
74 } |
|
75 } |
|
76 |
|
77 private void setThreadCount(int threadCount) { |
|
78 setCorePoolSize(threadCount); |
|
79 setMaximumPoolSize(threadCount); |
|
80 } |
|
81 } |