diff options
author | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2014-12-22 21:07:39 +0900 |
---|---|---|
committer | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2014-12-22 21:21:19 +0900 |
commit | c6fd07e0e46fa6ecba38cf7bd2f0e0312eaf7600 (patch) | |
tree | 4922688509728cfbf9b31c73158cda3bbad5d8a1 | |
parent | 2b54b6c29a832722e277d8cc3eae4b169b41c515 (diff) |
fdo#85849 on-demand create thumbnails of tiles in the sidebar
Change-Id: I0bef7bff0553c24b9d699845e70fbb128301a0fe
11 files changed, 158 insertions, 33 deletions
diff --git a/android/experimental/LOAndroid3/src/java/org/libreoffice/DocumentPartView.java b/android/experimental/LOAndroid3/src/java/org/libreoffice/DocumentPartView.java index c5f6e338eb5c..768b4af26eb0 100644 --- a/android/experimental/LOAndroid3/src/java/org/libreoffice/DocumentPartView.java +++ b/android/experimental/LOAndroid3/src/java/org/libreoffice/DocumentPartView.java @@ -1,27 +1,11 @@ package org.libreoffice; - -import android.graphics.Bitmap; - public class DocumentPartView { - private final int partIndex; - private final String partName; - private final Bitmap thumbnail; + public final int partIndex; + public final String partName; - public DocumentPartView(int partIndex, String partName, Bitmap thumbnail) { + public DocumentPartView(int partIndex, String partName) { this.partIndex = partIndex; this.partName = partName; - this.thumbnail = thumbnail; - } - - public String getPartName() { - return partName; - } - - public int getPartIndex() { - return partIndex; - } - public Bitmap getThumbnail() { - return thumbnail; } } diff --git a/android/experimental/LOAndroid3/src/java/org/libreoffice/DocumentPartViewListAdapter.java b/android/experimental/LOAndroid3/src/java/org/libreoffice/DocumentPartViewListAdapter.java index d25cccb0e46e..0dbc9cd8f079 100644 --- a/android/experimental/LOAndroid3/src/java/org/libreoffice/DocumentPartViewListAdapter.java +++ b/android/experimental/LOAndroid3/src/java/org/libreoffice/DocumentPartViewListAdapter.java @@ -1,7 +1,6 @@ package org.libreoffice; import android.app.Activity; -import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -15,10 +14,12 @@ public class DocumentPartViewListAdapter extends ArrayAdapter<DocumentPartView> private static final String LOGTAG = DocumentPartViewListAdapter.class.getSimpleName(); private final Activity activity; + private final ThumbnailCreator thumbnailCollector; public DocumentPartViewListAdapter(Activity activity, int resource, List<DocumentPartView> objects) { super(activity, resource, objects); this.activity = activity; + this.thumbnailCollector = new ThumbnailCreator(); } @Override @@ -30,15 +31,10 @@ public class DocumentPartViewListAdapter extends ArrayAdapter<DocumentPartView> DocumentPartView documentPartView = getItem(position); TextView textView = (TextView) view.findViewById(R.id.text); - textView.setText(documentPartView.getPartName()); - Log.i(LOGTAG, "getView - " + documentPartView.getPartName()); + textView.setText(documentPartView.partName); ImageView imageView = (ImageView) view.findViewById(R.id.image); - if (documentPartView.getThumbnail() != null) { - imageView.setImageBitmap(documentPartView.getThumbnail()); - } else { - imageView.setImageResource(R.drawable.writer); - } + thumbnailCollector.createThumbnail(position, imageView); return view; } diff --git a/android/experimental/LOAndroid3/src/java/org/libreoffice/LOEvent.java b/android/experimental/LOAndroid3/src/java/org/libreoffice/LOEvent.java index ced5ebd49daf..cb71bb5084d5 100644 --- a/android/experimental/LOAndroid3/src/java/org/libreoffice/LOEvent.java +++ b/android/experimental/LOAndroid3/src/java/org/libreoffice/LOEvent.java @@ -12,10 +12,12 @@ public class LOEvent implements Comparable<LOEvent> { public static final int CLOSE = 5; public static final int REDRAW = 6; public static final int TILE_REQUEST = 7; + public static final int THUMBNAIL = 8; public final int mType; public int mPriority = 0; + public ThumbnailCreator.ThumbnailCreationTask mTask; public String mTypeString; public int mPartIndex; public String mFilename; @@ -55,6 +57,11 @@ public class LOEvent implements Comparable<LOEvent> { mTypeString = "Change part"; } + public LOEvent(int type, ThumbnailCreator.ThumbnailCreationTask task) { + mType = type; + mTask = task; + } + public String getTypeString() { if (mTypeString == null) { return "Event type: " + mType; diff --git a/android/experimental/LOAndroid3/src/java/org/libreoffice/LOEventFactory.java b/android/experimental/LOAndroid3/src/java/org/libreoffice/LOEventFactory.java index a43720355df6..62c5a36f2653 100644 --- a/android/experimental/LOAndroid3/src/java/org/libreoffice/LOEventFactory.java +++ b/android/experimental/LOAndroid3/src/java/org/libreoffice/LOEventFactory.java @@ -32,4 +32,8 @@ public class LOEventFactory { public static LOEvent tileRequest(ComposedTileLayer composedTileLayer, TileIdentifier tileRequest) { return new LOEvent(LOEvent.TILE_REQUEST, composedTileLayer, tileRequest); } + + public static LOEvent thumbnail(ThumbnailCreator.ThumbnailCreationTask task) { + return new LOEvent(LOEvent.THUMBNAIL, task); + } } diff --git a/android/experimental/LOAndroid3/src/java/org/libreoffice/LOKitShell.java b/android/experimental/LOAndroid3/src/java/org/libreoffice/LOKitShell.java index 888e5884a096..80916278c282 100644 --- a/android/experimental/LOAndroid3/src/java/org/libreoffice/LOKitShell.java +++ b/android/experimental/LOAndroid3/src/java/org/libreoffice/LOKitShell.java @@ -54,4 +54,8 @@ public class LOKitShell { LibreOfficeMainActivity.mAppContext.getWindowManager().getDefaultDisplay().getMetrics(metrics); return metrics; } + + public static void sendThumbnailEvent(ThumbnailCreator.ThumbnailCreationTask task) { + LOKitShell.sendEvent(LOEventFactory.thumbnail(task)); + } } diff --git a/android/experimental/LOAndroid3/src/java/org/libreoffice/LOKitThread.java b/android/experimental/LOAndroid3/src/java/org/libreoffice/LOKitThread.java index 2b837383f4d9..8099c7397384 100644 --- a/android/experimental/LOAndroid3/src/java/org/libreoffice/LOKitThread.java +++ b/android/experimental/LOAndroid3/src/java/org/libreoffice/LOKitThread.java @@ -1,14 +1,13 @@ package org.libreoffice; +import android.graphics.Bitmap; import android.graphics.PointF; -import android.graphics.RectF; import android.util.Log; import org.mozilla.gecko.gfx.CairoImage; import org.mozilla.gecko.gfx.ComposedTileLayer; import org.mozilla.gecko.gfx.GeckoLayerClient; import org.mozilla.gecko.gfx.ImmutableViewportMetrics; -import org.mozilla.gecko.gfx.JavaPanZoomController; import org.mozilla.gecko.gfx.SubTile; import java.util.concurrent.PriorityBlockingQueue; @@ -116,9 +115,16 @@ public class LOKitThread extends Thread { case LOEvent.TILE_REQUEST: tileRequest(event.mComposedTileLayer, event.mTileId); break; + case LOEvent.THUMBNAIL: + createThumbnail(event.mTask); } } + private void createThumbnail(final ThumbnailCreator.ThumbnailCreationTask task) { + final Bitmap bitmap = task.getThumbnail(mTileProvider); + task.applyBitmap(bitmap); + } + public void queueEvent(LOEvent event) { mEventQueue.add(event); } diff --git a/android/experimental/LOAndroid3/src/java/org/libreoffice/LOKitTileProvider.java b/android/experimental/LOAndroid3/src/java/org/libreoffice/LOKitTileProvider.java index 848ec7b747d5..bda810d15277 100644 --- a/android/experimental/LOAndroid3/src/java/org/libreoffice/LOKitTileProvider.java +++ b/android/experimental/LOAndroid3/src/java/org/libreoffice/LOKitTileProvider.java @@ -82,7 +82,7 @@ public class LOKitTileProvider implements TileProvider { mDocument.setPart(i); resetDocumentSize(); - final DocumentPartView partView = new DocumentPartView(i, partName, thumbnail(128)); + final DocumentPartView partView = new DocumentPartView(i, partName); LibreOfficeMainActivity.mAppContext.getDocumentPartView().add(partView); } } else { @@ -257,6 +257,11 @@ public class LOKitTileProvider implements TileProvider { mDocument.setPart(partIndex); resetDocumentSize(); } + + @Override + public int getCurrentPartNumber() { + return mDocument.getPart(); + } } // vim:set shiftwidth=4 softtabstop=4 expandtab: diff --git a/android/experimental/LOAndroid3/src/java/org/libreoffice/LibreOfficeMainActivity.java b/android/experimental/LOAndroid3/src/java/org/libreoffice/LibreOfficeMainActivity.java index 062f44c4fc46..b83422c066a5 100644 --- a/android/experimental/LOAndroid3/src/java/org/libreoffice/LibreOfficeMainActivity.java +++ b/android/experimental/LOAndroid3/src/java/org/libreoffice/LibreOfficeMainActivity.java @@ -217,7 +217,7 @@ public class LibreOfficeMainActivity extends LOAbout { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { DocumentPartView partView = mDocumentPartViewListAdapter.getItem(position); - LOKitShell.sendEvent(LOEventFactory.changePart(partView.getPartIndex())); + LOKitShell.sendEvent(LOEventFactory.changePart(partView.partIndex)); mDrawerLayout.closeDrawer(mDrawerList); } } diff --git a/android/experimental/LOAndroid3/src/java/org/libreoffice/MockTileProvider.java b/android/experimental/LOAndroid3/src/java/org/libreoffice/MockTileProvider.java index e6070479671e..b8d7a3c305a0 100644 --- a/android/experimental/LOAndroid3/src/java/org/libreoffice/MockTileProvider.java +++ b/android/experimental/LOAndroid3/src/java/org/libreoffice/MockTileProvider.java @@ -18,8 +18,13 @@ public class MockTileProvider implements TileProvider { for (int i = 0; i < 5; i++) { String partName = "Part " + i; - DocumentPartView partView = new DocumentPartView(i, partName, null); - LibreOfficeMainActivity.mAppContext.getDocumentPartViewListAdapter().add(partView); + final DocumentPartView partView = new DocumentPartView(i, partName); + LibreOfficeMainActivity.mAppContext.mMainHandler.post(new Runnable() { + @Override + public void run() { + LibreOfficeMainActivity.mAppContext.getDocumentPartViewListAdapter().add(partView); + } + }); } LibreOfficeMainActivity.mAppContext.mMainHandler.post(new Runnable() { @Override @@ -70,6 +75,10 @@ public class MockTileProvider implements TileProvider { @Override public void changePart(int partIndex) { + } + @Override + public int getCurrentPartNumber() { + return 0; } } diff --git a/android/experimental/LOAndroid3/src/java/org/libreoffice/ThumbnailCreator.java b/android/experimental/LOAndroid3/src/java/org/libreoffice/ThumbnailCreator.java new file mode 100644 index 000000000000..dc027452de25 --- /dev/null +++ b/android/experimental/LOAndroid3/src/java/org/libreoffice/ThumbnailCreator.java @@ -0,0 +1,108 @@ +package org.libreoffice; + +import android.graphics.Bitmap; +import android.graphics.Color; +import android.graphics.drawable.ColorDrawable; +import android.graphics.drawable.Drawable; +import android.widget.ImageView; + +import java.lang.ref.WeakReference; + +public class ThumbnailCreator { + private static final String LOG_TAG = ThumbnailCreator.class.getSimpleName(); + private static final int THUMBNAIL_SIZE = 256; + + private static boolean needsThumbnailCreation(int partNumber, ImageView imageView) { + ThumbnailCreationTask thumbnailCreationTask = currentThumbnailCreationTask(imageView); + + if (thumbnailCreationTask == null) { + return true; + } + + if (thumbnailCreationTask.partNumber != partNumber) { + thumbnailCreationTask.cancel(); + return true; + } else { + return false; + } + } + + private static ThumbnailCreationTask currentThumbnailCreationTask(ImageView imageView) { + if (imageView == null) { + return null; + } + Drawable drawable = imageView.getDrawable(); + if (drawable instanceof ThumbnailDrawable) { + return ((ThumbnailDrawable) drawable).thumbnailCreationTask.get(); + } else { + return null; + } + } + + public void createThumbnail(int partNumber, ImageView imageView) { + if (needsThumbnailCreation(partNumber, imageView)) { + ThumbnailCreationTask task = new ThumbnailCreationTask(imageView, partNumber); + ThumbnailDrawable thumbnailDrawable = new ThumbnailDrawable(task); + imageView.setImageDrawable(thumbnailDrawable); + imageView.setMinimumHeight(THUMBNAIL_SIZE); + LOKitShell.sendThumbnailEvent(task); + } + } + + static class ThumbnailDrawable extends ColorDrawable { + public final WeakReference<ThumbnailCreationTask> thumbnailCreationTask; + + public ThumbnailDrawable(ThumbnailCreationTask thumbnailCreationTask) { + super(Color.WHITE); + this.thumbnailCreationTask = new WeakReference<ThumbnailCreationTask>(thumbnailCreationTask); + } + } + + class ThumbnailCreationTask{ + private final WeakReference<ImageView> imageViewReference; + private int partNumber; + private boolean cancelled = false; + + public ThumbnailCreationTask(ImageView imageView, int partNumber) { + imageViewReference = new WeakReference<ImageView>(imageView); + this.partNumber = partNumber; + } + + public void cancel() { + cancelled = true; + } + + public Bitmap getThumbnail(TileProvider tileProvider) { + int currentPart = tileProvider.getCurrentPartNumber(); + tileProvider.changePart(partNumber); + final Bitmap bitmap = tileProvider.thumbnail(THUMBNAIL_SIZE); + tileProvider.changePart(currentPart); + return bitmap; + } + + private void changeBitmap(Bitmap bitmap) { + if (cancelled) { + bitmap = null; + } + + if (imageViewReference == null) { + return; + } + ImageView imageView = imageViewReference.get(); + ThumbnailCreationTask thumbnailCreationTask = currentThumbnailCreationTask(imageView); + if (this == thumbnailCreationTask) { + imageView.setImageBitmap(bitmap); + } + } + + public void applyBitmap(final Bitmap bitmap) { + // run on UI thread + LibreOfficeMainActivity.mAppContext.mMainHandler.post(new Runnable() { + @Override + public void run() { + changeBitmap(bitmap); + } + }); + } + } +} diff --git a/android/experimental/LOAndroid3/src/java/org/libreoffice/TileProvider.java b/android/experimental/LOAndroid3/src/java/org/libreoffice/TileProvider.java index 12a47f6c7f39..7142bc31310c 100644 --- a/android/experimental/LOAndroid3/src/java/org/libreoffice/TileProvider.java +++ b/android/experimental/LOAndroid3/src/java/org/libreoffice/TileProvider.java @@ -17,6 +17,8 @@ public interface TileProvider { void changePart(int partIndex); + int getCurrentPartNumber(); + Bitmap thumbnail(int size); void close(); |