mobile/android/thirdparty/com/squareup/picasso/Utils.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/Utils.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,304 @@
     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.annotation.TargetApi;
    1.22 +import android.app.ActivityManager;
    1.23 +import android.content.ContentResolver;
    1.24 +import android.content.Context;
    1.25 +import android.content.pm.PackageManager;
    1.26 +import android.content.res.Resources;
    1.27 +import android.graphics.Bitmap;
    1.28 +import android.os.Looper;
    1.29 +import android.os.Process;
    1.30 +import android.os.StatFs;
    1.31 +import android.provider.Settings;
    1.32 +import java.io.ByteArrayOutputStream;
    1.33 +import java.io.File;
    1.34 +import java.io.FileNotFoundException;
    1.35 +import java.io.IOException;
    1.36 +import java.io.InputStream;
    1.37 +import java.util.List;
    1.38 +import java.util.concurrent.ThreadFactory;
    1.39 +
    1.40 +import static android.content.Context.ACTIVITY_SERVICE;
    1.41 +import static android.content.pm.ApplicationInfo.FLAG_LARGE_HEAP;
    1.42 +import static android.os.Build.VERSION.SDK_INT;
    1.43 +import static android.os.Build.VERSION_CODES.HONEYCOMB;
    1.44 +import static android.os.Build.VERSION_CODES.HONEYCOMB_MR1;
    1.45 +import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
    1.46 +import static android.provider.Settings.System.AIRPLANE_MODE_ON;
    1.47 +
    1.48 +final class Utils {
    1.49 +  static final String THREAD_PREFIX = "Picasso-";
    1.50 +  static final String THREAD_IDLE_NAME = THREAD_PREFIX + "Idle";
    1.51 +  static final int DEFAULT_READ_TIMEOUT = 20 * 1000; // 20s
    1.52 +  static final int DEFAULT_CONNECT_TIMEOUT = 15 * 1000; // 15s
    1.53 +  private static final String PICASSO_CACHE = "picasso-cache";
    1.54 +  private static final int KEY_PADDING = 50; // Determined by exact science.
    1.55 +  private static final int MIN_DISK_CACHE_SIZE = 5 * 1024 * 1024; // 5MB
    1.56 +  private static final int MAX_DISK_CACHE_SIZE = 50 * 1024 * 1024; // 50MB
    1.57 +
    1.58 +  /* WebP file header
    1.59 +     0                   1                   2                   3
    1.60 +     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
    1.61 +    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    1.62 +    |      'R'      |      'I'      |      'F'      |      'F'      |
    1.63 +    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    1.64 +    |                           File Size                           |
    1.65 +    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    1.66 +    |      'W'      |      'E'      |      'B'      |      'P'      |
    1.67 +    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    1.68 +  */
    1.69 +  private static final int WEBP_FILE_HEADER_SIZE = 12;
    1.70 +  private static final String WEBP_FILE_HEADER_RIFF = "RIFF";
    1.71 +  private static final String WEBP_FILE_HEADER_WEBP = "WEBP";
    1.72 +
    1.73 +  private Utils() {
    1.74 +    // No instances.
    1.75 +  }
    1.76 +
    1.77 +  static int getBitmapBytes(Bitmap bitmap) {
    1.78 +    int result;
    1.79 +    if (SDK_INT >= HONEYCOMB_MR1) {
    1.80 +      result = BitmapHoneycombMR1.getByteCount(bitmap);
    1.81 +    } else {
    1.82 +      result = bitmap.getRowBytes() * bitmap.getHeight();
    1.83 +    }
    1.84 +    if (result < 0) {
    1.85 +      throw new IllegalStateException("Negative size: " + bitmap);
    1.86 +    }
    1.87 +    return result;
    1.88 +  }
    1.89 +
    1.90 +  static void checkNotMain() {
    1.91 +    if (Looper.getMainLooper().getThread() == Thread.currentThread()) {
    1.92 +      throw new IllegalStateException("Method call should not happen from the main thread.");
    1.93 +    }
    1.94 +  }
    1.95 +
    1.96 +  static String createKey(Request data) {
    1.97 +    StringBuilder builder;
    1.98 +
    1.99 +    if (data.uri != null) {
   1.100 +      String path = data.uri.toString();
   1.101 +      builder = new StringBuilder(path.length() + KEY_PADDING);
   1.102 +      builder.append(path);
   1.103 +    } else {
   1.104 +      builder = new StringBuilder(KEY_PADDING);
   1.105 +      builder.append(data.resourceId);
   1.106 +    }
   1.107 +    builder.append('\n');
   1.108 +
   1.109 +    if (data.rotationDegrees != 0) {
   1.110 +      builder.append("rotation:").append(data.rotationDegrees);
   1.111 +      if (data.hasRotationPivot) {
   1.112 +        builder.append('@').append(data.rotationPivotX).append('x').append(data.rotationPivotY);
   1.113 +      }
   1.114 +      builder.append('\n');
   1.115 +    }
   1.116 +    if (data.targetWidth != 0) {
   1.117 +      builder.append("resize:").append(data.targetWidth).append('x').append(data.targetHeight);
   1.118 +      builder.append('\n');
   1.119 +    }
   1.120 +    if (data.centerCrop) {
   1.121 +      builder.append("centerCrop\n");
   1.122 +    } else if (data.centerInside) {
   1.123 +      builder.append("centerInside\n");
   1.124 +    }
   1.125 +
   1.126 +    if (data.transformations != null) {
   1.127 +      //noinspection ForLoopReplaceableByForEach
   1.128 +      for (int i = 0, count = data.transformations.size(); i < count; i++) {
   1.129 +        builder.append(data.transformations.get(i).key());
   1.130 +        builder.append('\n');
   1.131 +      }
   1.132 +    }
   1.133 +
   1.134 +    return builder.toString();
   1.135 +  }
   1.136 +
   1.137 +  static void closeQuietly(InputStream is) {
   1.138 +    if (is == null) return;
   1.139 +    try {
   1.140 +      is.close();
   1.141 +    } catch (IOException ignored) {
   1.142 +    }
   1.143 +  }
   1.144 +
   1.145 +  /** Returns {@code true} if header indicates the response body was loaded from the disk cache. */
   1.146 +  static boolean parseResponseSourceHeader(String header) {
   1.147 +    if (header == null) {
   1.148 +      return false;
   1.149 +    }
   1.150 +    String[] parts = header.split(" ", 2);
   1.151 +    if ("CACHE".equals(parts[0])) {
   1.152 +      return true;
   1.153 +    }
   1.154 +    if (parts.length == 1) {
   1.155 +      return false;
   1.156 +    }
   1.157 +    try {
   1.158 +      return "CONDITIONAL_CACHE".equals(parts[0]) && Integer.parseInt(parts[1]) == 304;
   1.159 +    } catch (NumberFormatException e) {
   1.160 +      return false;
   1.161 +    }
   1.162 +  }
   1.163 +
   1.164 +  static Downloader createDefaultDownloader(Context context) {
   1.165 +    return new UrlConnectionDownloader(context);
   1.166 +  }
   1.167 +
   1.168 +  static File createDefaultCacheDir(Context context) {
   1.169 +    File cache = new File(context.getApplicationContext().getCacheDir(), PICASSO_CACHE);
   1.170 +    if (!cache.exists()) {
   1.171 +      cache.mkdirs();
   1.172 +    }
   1.173 +    return cache;
   1.174 +  }
   1.175 +
   1.176 +  static long calculateDiskCacheSize(File dir) {
   1.177 +    long size = MIN_DISK_CACHE_SIZE;
   1.178 +
   1.179 +    try {
   1.180 +      StatFs statFs = new StatFs(dir.getAbsolutePath());
   1.181 +      long available = ((long) statFs.getBlockCount()) * statFs.getBlockSize();
   1.182 +      // Target 2% of the total space.
   1.183 +      size = available / 50;
   1.184 +    } catch (IllegalArgumentException ignored) {
   1.185 +    }
   1.186 +
   1.187 +    // Bound inside min/max size for disk cache.
   1.188 +    return Math.max(Math.min(size, MAX_DISK_CACHE_SIZE), MIN_DISK_CACHE_SIZE);
   1.189 +  }
   1.190 +
   1.191 +  static int calculateMemoryCacheSize(Context context) {
   1.192 +    ActivityManager am = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
   1.193 +    boolean largeHeap = (context.getApplicationInfo().flags & FLAG_LARGE_HEAP) != 0;
   1.194 +    int memoryClass = am.getMemoryClass();
   1.195 +    if (largeHeap && SDK_INT >= HONEYCOMB) {
   1.196 +      memoryClass = ActivityManagerHoneycomb.getLargeMemoryClass(am);
   1.197 +    }
   1.198 +    // Target ~15% of the available heap.
   1.199 +    return 1024 * 1024 * memoryClass / 7;
   1.200 +  }
   1.201 +
   1.202 +  static boolean isAirplaneModeOn(Context context) {
   1.203 +    ContentResolver contentResolver = context.getContentResolver();
   1.204 +    return Settings.System.getInt(contentResolver, AIRPLANE_MODE_ON, 0) != 0;
   1.205 +  }
   1.206 +
   1.207 +  static boolean hasPermission(Context context, String permission) {
   1.208 +    return context.checkCallingOrSelfPermission(permission) == PackageManager.PERMISSION_GRANTED;
   1.209 +  }
   1.210 +
   1.211 +  static byte[] toByteArray(InputStream input) throws IOException {
   1.212 +    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
   1.213 +    byte[] buffer = new byte[1024 * 4];
   1.214 +    int n = 0;
   1.215 +    while (-1 != (n = input.read(buffer))) {
   1.216 +      byteArrayOutputStream.write(buffer, 0, n);
   1.217 +    }
   1.218 +    return byteArrayOutputStream.toByteArray();
   1.219 +  }
   1.220 +
   1.221 +  static boolean isWebPFile(InputStream stream) throws IOException {
   1.222 +    byte[] fileHeaderBytes = new byte[WEBP_FILE_HEADER_SIZE];
   1.223 +    boolean isWebPFile = false;
   1.224 +    if (stream.read(fileHeaderBytes, 0, WEBP_FILE_HEADER_SIZE) == WEBP_FILE_HEADER_SIZE) {
   1.225 +      // If a file's header starts with RIFF and end with WEBP, the file is a WebP file
   1.226 +      isWebPFile = WEBP_FILE_HEADER_RIFF.equals(new String(fileHeaderBytes, 0, 4, "US-ASCII"))
   1.227 +          && WEBP_FILE_HEADER_WEBP.equals(new String(fileHeaderBytes, 8, 4, "US-ASCII"));
   1.228 +    }
   1.229 +    return isWebPFile;
   1.230 +  }
   1.231 +
   1.232 +  static int getResourceId(Resources resources, Request data) throws FileNotFoundException {
   1.233 +    if (data.resourceId != 0 || data.uri == null) {
   1.234 +      return data.resourceId;
   1.235 +    }
   1.236 +
   1.237 +    String pkg = data.uri.getAuthority();
   1.238 +    if (pkg == null) throw new FileNotFoundException("No package provided: " + data.uri);
   1.239 +
   1.240 +    int id;
   1.241 +    List<String> segments = data.uri.getPathSegments();
   1.242 +    if (segments == null || segments.isEmpty()) {
   1.243 +      throw new FileNotFoundException("No path segments: " + data.uri);
   1.244 +    } else if (segments.size() == 1) {
   1.245 +      try {
   1.246 +        id = Integer.parseInt(segments.get(0));
   1.247 +      } catch (NumberFormatException e) {
   1.248 +        throw new FileNotFoundException("Last path segment is not a resource ID: " + data.uri);
   1.249 +      }
   1.250 +    } else if (segments.size() == 2) {
   1.251 +      String type = segments.get(0);
   1.252 +      String name = segments.get(1);
   1.253 +
   1.254 +      id = resources.getIdentifier(name, type, pkg);
   1.255 +    } else {
   1.256 +      throw new FileNotFoundException("More than two path segments: " + data.uri);
   1.257 +    }
   1.258 +    return id;
   1.259 +  }
   1.260 +
   1.261 +  static Resources getResources(Context context, Request data) throws FileNotFoundException {
   1.262 +    if (data.resourceId != 0 || data.uri == null) {
   1.263 +      return context.getResources();
   1.264 +    }
   1.265 +
   1.266 +    String pkg = data.uri.getAuthority();
   1.267 +    if (pkg == null) throw new FileNotFoundException("No package provided: " + data.uri);
   1.268 +    try {
   1.269 +      PackageManager pm = context.getPackageManager();
   1.270 +      return pm.getResourcesForApplication(pkg);
   1.271 +    } catch (PackageManager.NameNotFoundException e) {
   1.272 +      throw new FileNotFoundException("Unable to obtain resources for package: " + data.uri);
   1.273 +    }
   1.274 +  }
   1.275 +
   1.276 +  @TargetApi(HONEYCOMB)
   1.277 +  private static class ActivityManagerHoneycomb {
   1.278 +    static int getLargeMemoryClass(ActivityManager activityManager) {
   1.279 +      return activityManager.getLargeMemoryClass();
   1.280 +    }
   1.281 +  }
   1.282 +
   1.283 +  static class PicassoThreadFactory implements ThreadFactory {
   1.284 +    @SuppressWarnings("NullableProblems")
   1.285 +    public Thread newThread(Runnable r) {
   1.286 +      return new PicassoThread(r);
   1.287 +    }
   1.288 +  }
   1.289 +
   1.290 +  private static class PicassoThread extends Thread {
   1.291 +    public PicassoThread(Runnable r) {
   1.292 +      super(r);
   1.293 +    }
   1.294 +
   1.295 +    @Override public void run() {
   1.296 +      Process.setThreadPriority(THREAD_PRIORITY_BACKGROUND);
   1.297 +      super.run();
   1.298 +    }
   1.299 +  }
   1.300 +
   1.301 +  @TargetApi(HONEYCOMB_MR1)
   1.302 +  private static class BitmapHoneycombMR1 {
   1.303 +    static int getByteCount(Bitmap bitmap) {
   1.304 +      return bitmap.getByteCount();
   1.305 +    }
   1.306 +  }
   1.307 +}

mercurial