michael@0: /* michael@0: * Copyright (C) 2013 Square, Inc. michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: package com.squareup.picasso; michael@0: michael@0: import android.net.ConnectivityManager; michael@0: import android.net.NetworkInfo; michael@0: import android.telephony.TelephonyManager; michael@0: import java.util.concurrent.LinkedBlockingQueue; michael@0: import java.util.concurrent.ThreadPoolExecutor; michael@0: import java.util.concurrent.TimeUnit; michael@0: michael@0: /** michael@0: * The default {@link java.util.concurrent.ExecutorService} used for new {@link Picasso} instances. michael@0: *

michael@0: * Exists as a custom type so that we can differentiate the use of defaults versus a user-supplied michael@0: * instance. michael@0: */ michael@0: class PicassoExecutorService extends ThreadPoolExecutor { michael@0: private static final int DEFAULT_THREAD_COUNT = 3; michael@0: michael@0: PicassoExecutorService() { michael@0: super(DEFAULT_THREAD_COUNT, DEFAULT_THREAD_COUNT, 0, TimeUnit.MILLISECONDS, michael@0: new LinkedBlockingQueue(), new Utils.PicassoThreadFactory()); michael@0: } michael@0: michael@0: void adjustThreadCount(NetworkInfo info) { michael@0: if (info == null || !info.isConnectedOrConnecting()) { michael@0: setThreadCount(DEFAULT_THREAD_COUNT); michael@0: return; michael@0: } michael@0: switch (info.getType()) { michael@0: case ConnectivityManager.TYPE_WIFI: michael@0: case ConnectivityManager.TYPE_WIMAX: michael@0: case ConnectivityManager.TYPE_ETHERNET: michael@0: setThreadCount(4); michael@0: break; michael@0: case ConnectivityManager.TYPE_MOBILE: michael@0: switch (info.getSubtype()) { michael@0: case TelephonyManager.NETWORK_TYPE_LTE: // 4G michael@0: case TelephonyManager.NETWORK_TYPE_HSPAP: michael@0: case TelephonyManager.NETWORK_TYPE_EHRPD: michael@0: setThreadCount(3); michael@0: break; michael@0: case TelephonyManager.NETWORK_TYPE_UMTS: // 3G michael@0: case TelephonyManager.NETWORK_TYPE_CDMA: michael@0: case TelephonyManager.NETWORK_TYPE_EVDO_0: michael@0: case TelephonyManager.NETWORK_TYPE_EVDO_A: michael@0: case TelephonyManager.NETWORK_TYPE_EVDO_B: michael@0: setThreadCount(2); michael@0: break; michael@0: case TelephonyManager.NETWORK_TYPE_GPRS: // 2G michael@0: case TelephonyManager.NETWORK_TYPE_EDGE: michael@0: setThreadCount(1); michael@0: break; michael@0: default: michael@0: setThreadCount(DEFAULT_THREAD_COUNT); michael@0: } michael@0: break; michael@0: default: michael@0: setThreadCount(DEFAULT_THREAD_COUNT); michael@0: } michael@0: } michael@0: michael@0: private void setThreadCount(int threadCount) { michael@0: setCorePoolSize(threadCount); michael@0: setMaximumPoolSize(threadCount); michael@0: } michael@0: }