Avoid potential negative array index access to cached text.
[LibreOffice.git] / android / source / src / java / org / libreoffice / ThumbnailCreator.java
blobc0c097747c695a4166079611dd80b7d06334f551
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9 package org.libreoffice;
11 import android.graphics.Bitmap;
12 import android.graphics.Color;
13 import android.graphics.drawable.ColorDrawable;
14 import android.graphics.drawable.Drawable;
15 import android.widget.ImageView;
17 import java.lang.ref.WeakReference;
19 /**
20 * Create thumbnails for the parts of the document.
22 public class ThumbnailCreator {
23 private static final String LOG_TAG = ThumbnailCreator.class.getSimpleName();
24 private static final int THUMBNAIL_SIZE = 256;
26 private static boolean needsThumbnailCreation(int partNumber, ImageView imageView) {
27 ThumbnailCreationTask thumbnailCreationTask = currentThumbnailCreationTask(imageView);
29 if (thumbnailCreationTask == null) {
30 return true;
33 if (thumbnailCreationTask.partNumber != partNumber) {
34 thumbnailCreationTask.cancel();
35 return true;
36 } else {
37 return false;
41 private static ThumbnailCreationTask currentThumbnailCreationTask(ImageView imageView) {
42 if (imageView == null) {
43 return null;
45 Drawable drawable = imageView.getDrawable();
46 if (drawable instanceof ThumbnailDrawable) {
47 return ((ThumbnailDrawable) drawable).thumbnailCreationTask.get();
48 } else {
49 return null;
53 public void createThumbnail(int partNumber, ImageView imageView) {
54 if (needsThumbnailCreation(partNumber, imageView)) {
55 ThumbnailCreationTask task = new ThumbnailCreationTask(imageView, partNumber);
56 ThumbnailDrawable thumbnailDrawable = new ThumbnailDrawable(task);
57 imageView.setImageDrawable(thumbnailDrawable);
58 imageView.setMinimumHeight(THUMBNAIL_SIZE);
59 LOKitShell.sendThumbnailEvent(task);
63 static class ThumbnailDrawable extends ColorDrawable {
64 public final WeakReference<ThumbnailCreationTask> thumbnailCreationTask;
66 public ThumbnailDrawable(ThumbnailCreationTask thumbnailCreationTask) {
67 super(Color.WHITE);
68 this.thumbnailCreationTask = new WeakReference<ThumbnailCreationTask>(thumbnailCreationTask);
72 class ThumbnailCreationTask{
73 private final WeakReference<ImageView> imageViewReference;
74 private final int partNumber;
75 private boolean cancelled = false;
77 public ThumbnailCreationTask(ImageView imageView, int partNumber) {
78 imageViewReference = new WeakReference<ImageView>(imageView);
79 this.partNumber = partNumber;
82 public void cancel() {
83 cancelled = true;
86 public Bitmap getThumbnail(TileProvider tileProvider) {
87 int currentPart = tileProvider.getCurrentPartNumber();
88 tileProvider.changePart(partNumber);
89 final Bitmap bitmap = tileProvider.thumbnail(THUMBNAIL_SIZE);
90 tileProvider.changePart(currentPart);
91 return bitmap;
94 private void changeBitmap(Bitmap bitmap) {
95 if (cancelled) {
96 bitmap = null;
99 if (imageViewReference == null) {
100 return;
102 ImageView imageView = imageViewReference.get();
103 ThumbnailCreationTask thumbnailCreationTask = currentThumbnailCreationTask(imageView);
104 if (this == thumbnailCreationTask) {
105 imageView.setImageBitmap(bitmap);
109 public void applyBitmap(final Bitmap bitmap) {
110 // run on UI thread
111 LibreOfficeApplication.getMainHandler().post(new Runnable() {
112 @Override
113 public void run() {
114 changeBitmap(bitmap);
121 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */