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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/com/squareup/picasso/PicassoExecutorService.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,81 @@
     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.net.ConnectivityManager;
    1.22 +import android.net.NetworkInfo;
    1.23 +import android.telephony.TelephonyManager;
    1.24 +import java.util.concurrent.LinkedBlockingQueue;
    1.25 +import java.util.concurrent.ThreadPoolExecutor;
    1.26 +import java.util.concurrent.TimeUnit;
    1.27 +
    1.28 +/**
    1.29 + * The default {@link java.util.concurrent.ExecutorService} used for new {@link Picasso} instances.
    1.30 + * <p/>
    1.31 + * Exists as a custom type so that we can differentiate the use of defaults versus a user-supplied
    1.32 + * instance.
    1.33 + */
    1.34 +class PicassoExecutorService extends ThreadPoolExecutor {
    1.35 +  private static final int DEFAULT_THREAD_COUNT = 3;
    1.36 +
    1.37 +  PicassoExecutorService() {
    1.38 +    super(DEFAULT_THREAD_COUNT, DEFAULT_THREAD_COUNT, 0, TimeUnit.MILLISECONDS,
    1.39 +        new LinkedBlockingQueue<Runnable>(), new Utils.PicassoThreadFactory());
    1.40 +  }
    1.41 +
    1.42 +  void adjustThreadCount(NetworkInfo info) {
    1.43 +    if (info == null || !info.isConnectedOrConnecting()) {
    1.44 +      setThreadCount(DEFAULT_THREAD_COUNT);
    1.45 +      return;
    1.46 +    }
    1.47 +    switch (info.getType()) {
    1.48 +      case ConnectivityManager.TYPE_WIFI:
    1.49 +      case ConnectivityManager.TYPE_WIMAX:
    1.50 +      case ConnectivityManager.TYPE_ETHERNET:
    1.51 +        setThreadCount(4);
    1.52 +        break;
    1.53 +      case ConnectivityManager.TYPE_MOBILE:
    1.54 +        switch (info.getSubtype()) {
    1.55 +          case TelephonyManager.NETWORK_TYPE_LTE:  // 4G
    1.56 +          case TelephonyManager.NETWORK_TYPE_HSPAP:
    1.57 +          case TelephonyManager.NETWORK_TYPE_EHRPD:
    1.58 +            setThreadCount(3);
    1.59 +            break;
    1.60 +          case TelephonyManager.NETWORK_TYPE_UMTS: // 3G
    1.61 +          case TelephonyManager.NETWORK_TYPE_CDMA:
    1.62 +          case TelephonyManager.NETWORK_TYPE_EVDO_0:
    1.63 +          case TelephonyManager.NETWORK_TYPE_EVDO_A:
    1.64 +          case TelephonyManager.NETWORK_TYPE_EVDO_B:
    1.65 +            setThreadCount(2);
    1.66 +            break;
    1.67 +          case TelephonyManager.NETWORK_TYPE_GPRS: // 2G
    1.68 +          case TelephonyManager.NETWORK_TYPE_EDGE:
    1.69 +            setThreadCount(1);
    1.70 +            break;
    1.71 +          default:
    1.72 +            setThreadCount(DEFAULT_THREAD_COUNT);
    1.73 +        }
    1.74 +        break;
    1.75 +      default:
    1.76 +        setThreadCount(DEFAULT_THREAD_COUNT);
    1.77 +    }
    1.78 +  }
    1.79 +
    1.80 +  private void setThreadCount(int threadCount) {
    1.81 +    setCorePoolSize(threadCount);
    1.82 +    setMaximumPoolSize(threadCount);
    1.83 +  }
    1.84 +}

mercurial