mobile/android/thirdparty/com/squareup/picasso/PicassoExecutorService.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * Copyright (C) 2013 Square, Inc.
michael@0 3 *
michael@0 4 * Licensed under the Apache License, Version 2.0 (the "License");
michael@0 5 * you may not use this file except in compliance with the License.
michael@0 6 * You may obtain a copy of the License at
michael@0 7 *
michael@0 8 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 9 *
michael@0 10 * Unless required by applicable law or agreed to in writing, software
michael@0 11 * distributed under the License is distributed on an "AS IS" BASIS,
michael@0 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0 13 * See the License for the specific language governing permissions and
michael@0 14 * limitations under the License.
michael@0 15 */
michael@0 16 package com.squareup.picasso;
michael@0 17
michael@0 18 import android.net.ConnectivityManager;
michael@0 19 import android.net.NetworkInfo;
michael@0 20 import android.telephony.TelephonyManager;
michael@0 21 import java.util.concurrent.LinkedBlockingQueue;
michael@0 22 import java.util.concurrent.ThreadPoolExecutor;
michael@0 23 import java.util.concurrent.TimeUnit;
michael@0 24
michael@0 25 /**
michael@0 26 * The default {@link java.util.concurrent.ExecutorService} used for new {@link Picasso} instances.
michael@0 27 * <p/>
michael@0 28 * Exists as a custom type so that we can differentiate the use of defaults versus a user-supplied
michael@0 29 * instance.
michael@0 30 */
michael@0 31 class PicassoExecutorService extends ThreadPoolExecutor {
michael@0 32 private static final int DEFAULT_THREAD_COUNT = 3;
michael@0 33
michael@0 34 PicassoExecutorService() {
michael@0 35 super(DEFAULT_THREAD_COUNT, DEFAULT_THREAD_COUNT, 0, TimeUnit.MILLISECONDS,
michael@0 36 new LinkedBlockingQueue<Runnable>(), new Utils.PicassoThreadFactory());
michael@0 37 }
michael@0 38
michael@0 39 void adjustThreadCount(NetworkInfo info) {
michael@0 40 if (info == null || !info.isConnectedOrConnecting()) {
michael@0 41 setThreadCount(DEFAULT_THREAD_COUNT);
michael@0 42 return;
michael@0 43 }
michael@0 44 switch (info.getType()) {
michael@0 45 case ConnectivityManager.TYPE_WIFI:
michael@0 46 case ConnectivityManager.TYPE_WIMAX:
michael@0 47 case ConnectivityManager.TYPE_ETHERNET:
michael@0 48 setThreadCount(4);
michael@0 49 break;
michael@0 50 case ConnectivityManager.TYPE_MOBILE:
michael@0 51 switch (info.getSubtype()) {
michael@0 52 case TelephonyManager.NETWORK_TYPE_LTE: // 4G
michael@0 53 case TelephonyManager.NETWORK_TYPE_HSPAP:
michael@0 54 case TelephonyManager.NETWORK_TYPE_EHRPD:
michael@0 55 setThreadCount(3);
michael@0 56 break;
michael@0 57 case TelephonyManager.NETWORK_TYPE_UMTS: // 3G
michael@0 58 case TelephonyManager.NETWORK_TYPE_CDMA:
michael@0 59 case TelephonyManager.NETWORK_TYPE_EVDO_0:
michael@0 60 case TelephonyManager.NETWORK_TYPE_EVDO_A:
michael@0 61 case TelephonyManager.NETWORK_TYPE_EVDO_B:
michael@0 62 setThreadCount(2);
michael@0 63 break;
michael@0 64 case TelephonyManager.NETWORK_TYPE_GPRS: // 2G
michael@0 65 case TelephonyManager.NETWORK_TYPE_EDGE:
michael@0 66 setThreadCount(1);
michael@0 67 break;
michael@0 68 default:
michael@0 69 setThreadCount(DEFAULT_THREAD_COUNT);
michael@0 70 }
michael@0 71 break;
michael@0 72 default:
michael@0 73 setThreadCount(DEFAULT_THREAD_COUNT);
michael@0 74 }
michael@0 75 }
michael@0 76
michael@0 77 private void setThreadCount(int threadCount) {
michael@0 78 setCorePoolSize(threadCount);
michael@0 79 setMaximumPoolSize(threadCount);
michael@0 80 }
michael@0 81 }

mercurial