|
89 | 89 | */ |
90 | 90 | public final class ThumbnailsCacheManager { |
91 | 91 | private static final int READ_TIMEOUT = 40000; |
| 92 | + private static final int[] VIDEO_PREVIEW_SIZES = {1024, 512, 256, 128, 64}; |
92 | 93 | private static final int CONNECTION_TIMEOUT = 5000; |
93 | 94 |
|
94 | 95 | /** Cache key prefix {@code <prefix><remoteId>}; sized from {@link #getScreenDimension()}. */ |
@@ -1271,6 +1272,52 @@ public static void generateThumbnailFromOCFile(OCFile file, User user, Context c |
1271 | 1272 | } |
1272 | 1273 | } |
1273 | 1274 |
|
| 1275 | + private static Bitmap downloadVideoPreview(OCFile file) { |
| 1276 | + for (int size : VIDEO_PREVIEW_SIZES) { |
| 1277 | + Bitmap preview = downloadPreview(file, |
| 1278 | + OwnCloudClientExtensionsKt.getVideoPreviewEndpoint(mClient, |
| 1279 | + file.getLocalId(), |
| 1280 | + size)); |
| 1281 | + if (preview != null) { |
| 1282 | + return preview; |
| 1283 | + } |
| 1284 | + } |
| 1285 | + |
| 1286 | + return null; |
| 1287 | + } |
| 1288 | + |
| 1289 | + private static Bitmap downloadPreview(OCFile file, String uri) { |
| 1290 | + Log_OC.d(TAG, "generating resized image: " + file.getFileName() + " URI: " + uri); |
| 1291 | + |
| 1292 | + GetMethod getMethod = null; |
| 1293 | + |
| 1294 | + try { |
| 1295 | + getMethod = new GetMethod(uri); |
| 1296 | + getMethod.getParams().setSoTimeout(READ_TIMEOUT); |
| 1297 | + |
| 1298 | + int status = mClient.executeMethod(getMethod); |
| 1299 | + if (status != HttpStatus.SC_OK) { |
| 1300 | + Log_OC.e(TAG, "cannot generate thumbnail not supported file type, status: " + status |
| 1301 | + + " file: " + file.getRemotePath()); |
| 1302 | + mClient.exhaustResponse(getMethod.getResponseBodyAsStream()); |
| 1303 | + return null; |
| 1304 | + } |
| 1305 | + |
| 1306 | + try (InputStream inputStream = getMethod.getResponseBodyAsStream()) { |
| 1307 | + Bitmap preview = BitmapFactory.decodeStream(inputStream); |
| 1308 | + Log_OC.d(TAG, "resized image generated"); |
| 1309 | + return preview; |
| 1310 | + } |
| 1311 | + } catch (Exception e) { |
| 1312 | + Log_OC.e(TAG, "doResizedBitmap: ", e); |
| 1313 | + return null; |
| 1314 | + } finally { |
| 1315 | + if (getMethod != null) { |
| 1316 | + getMethod.releaseConnection(); |
| 1317 | + } |
| 1318 | + } |
| 1319 | + } |
| 1320 | + |
1274 | 1321 | @VisibleForTesting |
1275 | 1322 | public static void clearCache() { |
1276 | 1323 | synchronized (mThumbnailsDiskCacheLock) { |
@@ -1310,41 +1357,20 @@ public static Bitmap doResizedImageInBackground(OCFile file, FileDataStorageMana |
1310 | 1357 | file.setUpdateThumbnailNeeded(false); |
1311 | 1358 | } |
1312 | 1359 | } else if (mClient != null) { |
1313 | | - GetMethod getMethod = null; |
1314 | | - |
1315 | | - try { |
1316 | | - String uri = MimeTypeUtil.isVideo(file) |
1317 | | - ? OwnCloudClientExtensionsKt.getVideoPreviewEndpoint(mClient, file.getLocalId()) |
1318 | | - : OwnCloudClientExtensionsKt.getPreviewEndpoint(mClient, file.getLocalId(), pxW, pxH); |
1319 | | - Log_OC.d(TAG, "generating resized image: " + file.getFileName() + " URI: " + uri); |
1320 | | - |
1321 | | - getMethod = new GetMethod(uri); |
1322 | | - getMethod.getParams().setSoTimeout(READ_TIMEOUT); |
1323 | | - |
1324 | | - int status = mClient.executeMethod(getMethod); |
1325 | | - if (status == HttpStatus.SC_OK) { |
1326 | | - try (InputStream inputStream = getMethod.getResponseBodyAsStream()) { |
1327 | | - thumbnail = BitmapFactory.decodeStream(inputStream); |
1328 | | - Log_OC.d(TAG, "resized image generated"); |
1329 | | - } |
1330 | | - } else { |
1331 | | - Log_OC.e(TAG, "cannot generate thumbnail not supported file type, status: " + status + " file: " + file.getRemotePath()); |
1332 | | - mClient.exhaustResponse(getMethod.getResponseBodyAsStream()); |
1333 | | - } |
1334 | | - |
1335 | | - if (thumbnail != null && PNG_MIMETYPE.equalsIgnoreCase(file.getMimeType())) { |
1336 | | - thumbnail = handlePNG(thumbnail, thumbnail.getWidth(), thumbnail.getHeight()); |
1337 | | - } |
| 1360 | + thumbnail = MimeTypeUtil.isVideo(file) |
| 1361 | + ? downloadVideoPreview(file) |
| 1362 | + : downloadPreview(file, |
| 1363 | + OwnCloudClientExtensionsKt.getPreviewEndpoint(mClient, |
| 1364 | + file.getLocalId(), |
| 1365 | + pxW, |
| 1366 | + pxH)); |
| 1367 | + |
| 1368 | + if (thumbnail != null && PNG_MIMETYPE.equalsIgnoreCase(file.getMimeType())) { |
| 1369 | + thumbnail = handlePNG(thumbnail, thumbnail.getWidth(), thumbnail.getHeight()); |
| 1370 | + } |
1338 | 1371 |
|
1339 | | - if (thumbnail != null) { |
1340 | | - addBitmapToCache(imageKey, thumbnail); |
1341 | | - } |
1342 | | - } catch (Exception e) { |
1343 | | - Log_OC.e(TAG, "doResizedBitmap: ", e); |
1344 | | - } finally { |
1345 | | - if (getMethod != null) { |
1346 | | - getMethod.releaseConnection(); |
1347 | | - } |
| 1372 | + if (thumbnail != null) { |
| 1373 | + addBitmapToCache(imageKey, thumbnail); |
1348 | 1374 | } |
1349 | 1375 | } |
1350 | 1376 |
|
|
0 commit comments