|
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.annotation.TargetApi; |
|
19 import android.app.ActivityManager; |
|
20 import android.content.ContentResolver; |
|
21 import android.content.Context; |
|
22 import android.content.pm.PackageManager; |
|
23 import android.content.res.Resources; |
|
24 import android.graphics.Bitmap; |
|
25 import android.os.Looper; |
|
26 import android.os.Process; |
|
27 import android.os.StatFs; |
|
28 import android.provider.Settings; |
|
29 import java.io.ByteArrayOutputStream; |
|
30 import java.io.File; |
|
31 import java.io.FileNotFoundException; |
|
32 import java.io.IOException; |
|
33 import java.io.InputStream; |
|
34 import java.util.List; |
|
35 import java.util.concurrent.ThreadFactory; |
|
36 |
|
37 import static android.content.Context.ACTIVITY_SERVICE; |
|
38 import static android.content.pm.ApplicationInfo.FLAG_LARGE_HEAP; |
|
39 import static android.os.Build.VERSION.SDK_INT; |
|
40 import static android.os.Build.VERSION_CODES.HONEYCOMB; |
|
41 import static android.os.Build.VERSION_CODES.HONEYCOMB_MR1; |
|
42 import static android.os.Process.THREAD_PRIORITY_BACKGROUND; |
|
43 import static android.provider.Settings.System.AIRPLANE_MODE_ON; |
|
44 |
|
45 final class Utils { |
|
46 static final String THREAD_PREFIX = "Picasso-"; |
|
47 static final String THREAD_IDLE_NAME = THREAD_PREFIX + "Idle"; |
|
48 static final int DEFAULT_READ_TIMEOUT = 20 * 1000; // 20s |
|
49 static final int DEFAULT_CONNECT_TIMEOUT = 15 * 1000; // 15s |
|
50 private static final String PICASSO_CACHE = "picasso-cache"; |
|
51 private static final int KEY_PADDING = 50; // Determined by exact science. |
|
52 private static final int MIN_DISK_CACHE_SIZE = 5 * 1024 * 1024; // 5MB |
|
53 private static final int MAX_DISK_CACHE_SIZE = 50 * 1024 * 1024; // 50MB |
|
54 |
|
55 /* WebP file header |
|
56 0 1 2 3 |
|
57 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 |
|
58 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
|
59 | 'R' | 'I' | 'F' | 'F' | |
|
60 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
|
61 | File Size | |
|
62 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
|
63 | 'W' | 'E' | 'B' | 'P' | |
|
64 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
|
65 */ |
|
66 private static final int WEBP_FILE_HEADER_SIZE = 12; |
|
67 private static final String WEBP_FILE_HEADER_RIFF = "RIFF"; |
|
68 private static final String WEBP_FILE_HEADER_WEBP = "WEBP"; |
|
69 |
|
70 private Utils() { |
|
71 // No instances. |
|
72 } |
|
73 |
|
74 static int getBitmapBytes(Bitmap bitmap) { |
|
75 int result; |
|
76 if (SDK_INT >= HONEYCOMB_MR1) { |
|
77 result = BitmapHoneycombMR1.getByteCount(bitmap); |
|
78 } else { |
|
79 result = bitmap.getRowBytes() * bitmap.getHeight(); |
|
80 } |
|
81 if (result < 0) { |
|
82 throw new IllegalStateException("Negative size: " + bitmap); |
|
83 } |
|
84 return result; |
|
85 } |
|
86 |
|
87 static void checkNotMain() { |
|
88 if (Looper.getMainLooper().getThread() == Thread.currentThread()) { |
|
89 throw new IllegalStateException("Method call should not happen from the main thread."); |
|
90 } |
|
91 } |
|
92 |
|
93 static String createKey(Request data) { |
|
94 StringBuilder builder; |
|
95 |
|
96 if (data.uri != null) { |
|
97 String path = data.uri.toString(); |
|
98 builder = new StringBuilder(path.length() + KEY_PADDING); |
|
99 builder.append(path); |
|
100 } else { |
|
101 builder = new StringBuilder(KEY_PADDING); |
|
102 builder.append(data.resourceId); |
|
103 } |
|
104 builder.append('\n'); |
|
105 |
|
106 if (data.rotationDegrees != 0) { |
|
107 builder.append("rotation:").append(data.rotationDegrees); |
|
108 if (data.hasRotationPivot) { |
|
109 builder.append('@').append(data.rotationPivotX).append('x').append(data.rotationPivotY); |
|
110 } |
|
111 builder.append('\n'); |
|
112 } |
|
113 if (data.targetWidth != 0) { |
|
114 builder.append("resize:").append(data.targetWidth).append('x').append(data.targetHeight); |
|
115 builder.append('\n'); |
|
116 } |
|
117 if (data.centerCrop) { |
|
118 builder.append("centerCrop\n"); |
|
119 } else if (data.centerInside) { |
|
120 builder.append("centerInside\n"); |
|
121 } |
|
122 |
|
123 if (data.transformations != null) { |
|
124 //noinspection ForLoopReplaceableByForEach |
|
125 for (int i = 0, count = data.transformations.size(); i < count; i++) { |
|
126 builder.append(data.transformations.get(i).key()); |
|
127 builder.append('\n'); |
|
128 } |
|
129 } |
|
130 |
|
131 return builder.toString(); |
|
132 } |
|
133 |
|
134 static void closeQuietly(InputStream is) { |
|
135 if (is == null) return; |
|
136 try { |
|
137 is.close(); |
|
138 } catch (IOException ignored) { |
|
139 } |
|
140 } |
|
141 |
|
142 /** Returns {@code true} if header indicates the response body was loaded from the disk cache. */ |
|
143 static boolean parseResponseSourceHeader(String header) { |
|
144 if (header == null) { |
|
145 return false; |
|
146 } |
|
147 String[] parts = header.split(" ", 2); |
|
148 if ("CACHE".equals(parts[0])) { |
|
149 return true; |
|
150 } |
|
151 if (parts.length == 1) { |
|
152 return false; |
|
153 } |
|
154 try { |
|
155 return "CONDITIONAL_CACHE".equals(parts[0]) && Integer.parseInt(parts[1]) == 304; |
|
156 } catch (NumberFormatException e) { |
|
157 return false; |
|
158 } |
|
159 } |
|
160 |
|
161 static Downloader createDefaultDownloader(Context context) { |
|
162 return new UrlConnectionDownloader(context); |
|
163 } |
|
164 |
|
165 static File createDefaultCacheDir(Context context) { |
|
166 File cache = new File(context.getApplicationContext().getCacheDir(), PICASSO_CACHE); |
|
167 if (!cache.exists()) { |
|
168 cache.mkdirs(); |
|
169 } |
|
170 return cache; |
|
171 } |
|
172 |
|
173 static long calculateDiskCacheSize(File dir) { |
|
174 long size = MIN_DISK_CACHE_SIZE; |
|
175 |
|
176 try { |
|
177 StatFs statFs = new StatFs(dir.getAbsolutePath()); |
|
178 long available = ((long) statFs.getBlockCount()) * statFs.getBlockSize(); |
|
179 // Target 2% of the total space. |
|
180 size = available / 50; |
|
181 } catch (IllegalArgumentException ignored) { |
|
182 } |
|
183 |
|
184 // Bound inside min/max size for disk cache. |
|
185 return Math.max(Math.min(size, MAX_DISK_CACHE_SIZE), MIN_DISK_CACHE_SIZE); |
|
186 } |
|
187 |
|
188 static int calculateMemoryCacheSize(Context context) { |
|
189 ActivityManager am = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE); |
|
190 boolean largeHeap = (context.getApplicationInfo().flags & FLAG_LARGE_HEAP) != 0; |
|
191 int memoryClass = am.getMemoryClass(); |
|
192 if (largeHeap && SDK_INT >= HONEYCOMB) { |
|
193 memoryClass = ActivityManagerHoneycomb.getLargeMemoryClass(am); |
|
194 } |
|
195 // Target ~15% of the available heap. |
|
196 return 1024 * 1024 * memoryClass / 7; |
|
197 } |
|
198 |
|
199 static boolean isAirplaneModeOn(Context context) { |
|
200 ContentResolver contentResolver = context.getContentResolver(); |
|
201 return Settings.System.getInt(contentResolver, AIRPLANE_MODE_ON, 0) != 0; |
|
202 } |
|
203 |
|
204 static boolean hasPermission(Context context, String permission) { |
|
205 return context.checkCallingOrSelfPermission(permission) == PackageManager.PERMISSION_GRANTED; |
|
206 } |
|
207 |
|
208 static byte[] toByteArray(InputStream input) throws IOException { |
|
209 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
|
210 byte[] buffer = new byte[1024 * 4]; |
|
211 int n = 0; |
|
212 while (-1 != (n = input.read(buffer))) { |
|
213 byteArrayOutputStream.write(buffer, 0, n); |
|
214 } |
|
215 return byteArrayOutputStream.toByteArray(); |
|
216 } |
|
217 |
|
218 static boolean isWebPFile(InputStream stream) throws IOException { |
|
219 byte[] fileHeaderBytes = new byte[WEBP_FILE_HEADER_SIZE]; |
|
220 boolean isWebPFile = false; |
|
221 if (stream.read(fileHeaderBytes, 0, WEBP_FILE_HEADER_SIZE) == WEBP_FILE_HEADER_SIZE) { |
|
222 // If a file's header starts with RIFF and end with WEBP, the file is a WebP file |
|
223 isWebPFile = WEBP_FILE_HEADER_RIFF.equals(new String(fileHeaderBytes, 0, 4, "US-ASCII")) |
|
224 && WEBP_FILE_HEADER_WEBP.equals(new String(fileHeaderBytes, 8, 4, "US-ASCII")); |
|
225 } |
|
226 return isWebPFile; |
|
227 } |
|
228 |
|
229 static int getResourceId(Resources resources, Request data) throws FileNotFoundException { |
|
230 if (data.resourceId != 0 || data.uri == null) { |
|
231 return data.resourceId; |
|
232 } |
|
233 |
|
234 String pkg = data.uri.getAuthority(); |
|
235 if (pkg == null) throw new FileNotFoundException("No package provided: " + data.uri); |
|
236 |
|
237 int id; |
|
238 List<String> segments = data.uri.getPathSegments(); |
|
239 if (segments == null || segments.isEmpty()) { |
|
240 throw new FileNotFoundException("No path segments: " + data.uri); |
|
241 } else if (segments.size() == 1) { |
|
242 try { |
|
243 id = Integer.parseInt(segments.get(0)); |
|
244 } catch (NumberFormatException e) { |
|
245 throw new FileNotFoundException("Last path segment is not a resource ID: " + data.uri); |
|
246 } |
|
247 } else if (segments.size() == 2) { |
|
248 String type = segments.get(0); |
|
249 String name = segments.get(1); |
|
250 |
|
251 id = resources.getIdentifier(name, type, pkg); |
|
252 } else { |
|
253 throw new FileNotFoundException("More than two path segments: " + data.uri); |
|
254 } |
|
255 return id; |
|
256 } |
|
257 |
|
258 static Resources getResources(Context context, Request data) throws FileNotFoundException { |
|
259 if (data.resourceId != 0 || data.uri == null) { |
|
260 return context.getResources(); |
|
261 } |
|
262 |
|
263 String pkg = data.uri.getAuthority(); |
|
264 if (pkg == null) throw new FileNotFoundException("No package provided: " + data.uri); |
|
265 try { |
|
266 PackageManager pm = context.getPackageManager(); |
|
267 return pm.getResourcesForApplication(pkg); |
|
268 } catch (PackageManager.NameNotFoundException e) { |
|
269 throw new FileNotFoundException("Unable to obtain resources for package: " + data.uri); |
|
270 } |
|
271 } |
|
272 |
|
273 @TargetApi(HONEYCOMB) |
|
274 private static class ActivityManagerHoneycomb { |
|
275 static int getLargeMemoryClass(ActivityManager activityManager) { |
|
276 return activityManager.getLargeMemoryClass(); |
|
277 } |
|
278 } |
|
279 |
|
280 static class PicassoThreadFactory implements ThreadFactory { |
|
281 @SuppressWarnings("NullableProblems") |
|
282 public Thread newThread(Runnable r) { |
|
283 return new PicassoThread(r); |
|
284 } |
|
285 } |
|
286 |
|
287 private static class PicassoThread extends Thread { |
|
288 public PicassoThread(Runnable r) { |
|
289 super(r); |
|
290 } |
|
291 |
|
292 @Override public void run() { |
|
293 Process.setThreadPriority(THREAD_PRIORITY_BACKGROUND); |
|
294 super.run(); |
|
295 } |
|
296 } |
|
297 |
|
298 @TargetApi(HONEYCOMB_MR1) |
|
299 private static class BitmapHoneycombMR1 { |
|
300 static int getByteCount(Bitmap bitmap) { |
|
301 return bitmap.getByteCount(); |
|
302 } |
|
303 } |
|
304 } |