1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
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
;
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) {
33 if (thumbnailCreationTask
.partNumber
!= partNumber
) {
34 thumbnailCreationTask
.cancel();
41 private static ThumbnailCreationTask
currentThumbnailCreationTask(ImageView imageView
) {
42 if (imageView
== null) {
45 Drawable drawable
= imageView
.getDrawable();
46 if (drawable
instanceof ThumbnailDrawable
) {
47 return ((ThumbnailDrawable
) drawable
).thumbnailCreationTask
.get();
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
) {
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() {
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
);
94 private void changeBitmap(Bitmap bitmap
) {
99 if (imageViewReference
== null) {
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
) {
111 LibreOfficeApplication
.getMainHandler().post(new Runnable() {
114 changeBitmap(bitmap
);
121 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */