Kaydet (Commit) 2897374f authored tarafından Tomaž Vajngerl's avatar Tomaž Vajngerl

android: cleanly load/close a document when activity starts/stops

Change-Id: I2206a2b6818c030ba756f2b4d014a20d46f4106e
üst 18c052d6
...@@ -12,17 +12,24 @@ public class LOEvent { ...@@ -12,17 +12,24 @@ public class LOEvent {
public static final int VIEWPORT = 3; public static final int VIEWPORT = 3;
public static final int DRAW = 4; public static final int DRAW = 4;
public static final int CHANGE_PART = 5; public static final int CHANGE_PART = 5;
public static final int LOAD = 6;
public int mType; public int mType;
ViewportMetrics viewportMetrics;
private ViewportMetrics mViewportMetrics; private ViewportMetrics mViewportMetrics;
private String mTypeString; private String mTypeString;
private int mPartIndex; private int mPartIndex;
private String mFilename;
public LOEvent(int type, int widthPixels, int heightPixels, int tileWidth, int tileHeight) { public LOEvent(int type, int widthPixels, int heightPixels, int tileWidth, int tileHeight) {
mType = type; mType = type;
mTypeString = "Size Changed: " + widthPixels + " " + heightPixels; mTypeString = "Size Changed: " + widthPixels + " " + heightPixels;
} }
public LOEvent(int type, String filename) {
mType = type;
mFilename = filename;
}
public LOEvent(int type, IntSize tileSize) { public LOEvent(int type, IntSize tileSize) {
mType = type; mType = type;
mTypeString = "Tile size"; mTypeString = "Tile size";
...@@ -65,6 +72,10 @@ public class LOEvent { ...@@ -65,6 +72,10 @@ public class LOEvent {
return new LOEvent(CHANGE_PART, part); return new LOEvent(CHANGE_PART, part);
} }
public static LOEvent load(String inputFile) {
return new LOEvent(LOAD, inputFile);
}
public String getTypeString() { public String getTypeString() {
return mTypeString; return mTypeString;
} }
...@@ -76,4 +87,9 @@ public class LOEvent { ...@@ -76,4 +87,9 @@ public class LOEvent {
public int getPartIndex() { public int getPartIndex() {
return mPartIndex; return mPartIndex;
} }
public String getFilename() {
return mFilename;
}
} }
...@@ -38,4 +38,5 @@ public class LOKitShell { ...@@ -38,4 +38,5 @@ public class LOKitShell {
public static Handler getMainHandler() { public static Handler getMainHandler() {
return LibreOfficeMainActivity.mAppContext.mMainHandler; return LibreOfficeMainActivity.mAppContext.mMainHandler;
} }
} }
...@@ -22,21 +22,19 @@ public class LOKitThread extends Thread { ...@@ -22,21 +22,19 @@ public class LOKitThread extends Thread {
private LibreOfficeMainActivity mApplication; private LibreOfficeMainActivity mApplication;
private TileProvider mTileProvider; private TileProvider mTileProvider;
private ViewportMetrics mViewportMetrics; private ViewportMetrics mViewportMetrics;
private String mInputFile;
private Rect mOldRect; private Rect mOldRect;
private boolean mCheckboardImageSet = false; private boolean mCheckboardImageSet = false;
LOKitThread(String inputFile) { public LOKitThread() {
mInputFile = inputFile;
} }
RectF normlizeRect(ImmutableViewportMetrics metrics) { private RectF normlizeRect(ImmutableViewportMetrics metrics) {
RectF rect = metrics.getViewport(); RectF rect = metrics.getViewport();
float zoomFactor = metrics.zoomFactor; float zoomFactor = metrics.zoomFactor;
return new RectF(rect.left / zoomFactor, rect.top / zoomFactor, rect.right / zoomFactor, rect.bottom / zoomFactor); return new RectF(rect.left / zoomFactor, rect.top / zoomFactor, rect.right / zoomFactor, rect.bottom / zoomFactor);
} }
Rect roundToTileSize(RectF input, int tileSize) { private Rect roundToTileSize(RectF input, int tileSize) {
int minX = (Math.round(input.left) / tileSize) * tileSize; int minX = (Math.round(input.left) / tileSize) * tileSize;
int minY = (Math.round(input.top) / tileSize) * tileSize; int minY = (Math.round(input.top) / tileSize) * tileSize;
int maxX = ((Math.round(input.right) / tileSize) + 1) * tileSize; int maxX = ((Math.round(input.right) / tileSize) + 1) * tileSize;
...@@ -44,7 +42,7 @@ public class LOKitThread extends Thread { ...@@ -44,7 +42,7 @@ public class LOKitThread extends Thread {
return new Rect(minX, minY, maxX, maxY); return new Rect(minX, minY, maxX, maxY);
} }
Rect inflate(Rect rect, int inflateSize) { private Rect inflate(Rect rect, int inflateSize) {
Rect newRect = new Rect(rect); Rect newRect = new Rect(rect);
newRect.left -= inflateSize; newRect.left -= inflateSize;
newRect.left = newRect.left < 0 ? 0 : newRect.left; newRect.left = newRect.left < 0 ? 0 : newRect.left;
...@@ -130,41 +128,49 @@ public class LOKitThread extends Thread { ...@@ -130,41 +128,49 @@ public class LOKitThread extends Thread {
LOKitShell.sendEvent(LOEvent.draw(new Rect())); LOKitShell.sendEvent(LOEvent.draw(new Rect()));
} }
private boolean initialize() { private boolean load(String filename) {
mApplication = LibreOfficeMainActivity.mAppContext; if (mApplication == null) {
mTileProvider = new LOKitTileProvider(mApplication.getLayerController(), mInputFile); mApplication = LibreOfficeMainActivity.mAppContext;
}
if (mTileProvider != null) {
mTileProvider.close();
}
mTileProvider = new LOKitTileProvider(mApplication.getLayerController(), filename);
boolean isReady = mTileProvider.isReady(); boolean isReady = mTileProvider.isReady();
if (isReady) if (isReady) {
{ updateCheckbardImage();
if (!mCheckboardImageSet) {
Log.i(LOGTAG, "Generate thumbnail!");
Bitmap bitmap = mTileProvider.thumbnail();
Log.i(LOGTAG, "Done generate thumbnail!");
if (bitmap != null) {
Log.i(LOGTAG, "Setting checkboard image!");
mApplication.getLayerController().getView().changeCheckerboardBitmap(bitmap);
Log.i(LOGTAG, "Done setting checkboard image!!");
mCheckboardImageSet = true;
}
}
} }
return isReady; return isReady;
} }
private void updateCheckbardImage() {
if (!mCheckboardImageSet) {
Log.i(LOGTAG, "Generate thumbnail!");
Bitmap bitmap = mTileProvider.thumbnail();
Log.i(LOGTAG, "Done generate thumbnail!");
if (bitmap != null) {
Log.i(LOGTAG, "Setting checkboard image!");
mApplication.getLayerController().getView().changeCheckerboardBitmap(bitmap);
Log.i(LOGTAG, "Done setting checkboard image!!");
mCheckboardImageSet = true;
}
}
}
public void run() { public void run() {
if (initialize()) { try {
try { while (true) {
boolean drawn = false; processEvent(mEventQueue.take());
while (true) {
processEvent(mEventQueue.take());
}
} catch (InterruptedException ex) {
} }
} catch (InterruptedException ex) {
} }
} }
private void processEvent(LOEvent event) throws InterruptedException { private void processEvent(LOEvent event) throws InterruptedException {
switch (event.mType) { switch (event.mType) {
case LOEvent.LOAD:
load(event.getFilename());
break;
case LOEvent.VIEWPORT: case LOEvent.VIEWPORT:
mViewportMetrics = event.getViewport(); mViewportMetrics = event.getViewport();
draw(); draw();
......
...@@ -21,6 +21,7 @@ public class LOKitTileProvider implements TileProvider { ...@@ -21,6 +21,7 @@ public class LOKitTileProvider implements TileProvider {
private final LayerController mLayerController; private final LayerController mLayerController;
private final double mTileWidth; private final double mTileWidth;
private final double mTileHeight; private final double mTileHeight;
private final String mInputFile;
private double mDPI; private double mDPI;
private double mWidthTwip; private double mWidthTwip;
...@@ -37,6 +38,7 @@ public class LOKitTileProvider implements TileProvider { ...@@ -37,6 +38,7 @@ public class LOKitTileProvider implements TileProvider {
mOffice = new Office(LibreOfficeKit.getLibreOfficeKitHandle()); mOffice = new Office(LibreOfficeKit.getLibreOfficeKitHandle());
mInputFile = input;
mDocument = mOffice.documentLoad(input); mDocument = mOffice.documentLoad(input);
if (checkDocument()) { if (checkDocument()) {
...@@ -110,7 +112,11 @@ public class LOKitTileProvider implements TileProvider { ...@@ -110,7 +112,11 @@ public class LOKitTileProvider implements TileProvider {
ByteBuffer buffer = ByteBuffer.allocateDirect(TILE_SIZE * TILE_SIZE * 4); ByteBuffer buffer = ByteBuffer.allocateDirect(TILE_SIZE * TILE_SIZE * 4);
Bitmap bitmap = Bitmap.createBitmap(TILE_SIZE, TILE_SIZE, Bitmap.Config.ARGB_8888); Bitmap bitmap = Bitmap.createBitmap(TILE_SIZE, TILE_SIZE, Bitmap.Config.ARGB_8888);
mDocument.paintTile(buffer, TILE_SIZE, TILE_SIZE, (int) pixelToTwip(x, mDPI), (int) pixelToTwip(y, mDPI), (int) mTileWidth, (int) mTileHeight); if (mDocument != null) {
mDocument.paintTile(buffer, TILE_SIZE, TILE_SIZE, (int) pixelToTwip(x, mDPI), (int) pixelToTwip(y, mDPI), (int) mTileWidth, (int) mTileHeight);
} else {
Log.e(LOGTAG, "Document is null!!");
}
bitmap.copyPixelsFromBuffer(buffer); bitmap.copyPixelsFromBuffer(buffer);
...@@ -146,6 +152,12 @@ public class LOKitTileProvider implements TileProvider { ...@@ -146,6 +152,12 @@ public class LOKitTileProvider implements TileProvider {
return bitmap; return bitmap;
} }
@Override
public void close() {
Log.i(LOGTAG, "Document destroyed: " + mInputFile);
mDocument.destroy();
}
@Override @Override
public void changePart(int partIndex) { public void changePart(int partIndex) {
mDocument.setPart(partIndex); mDocument.setPart(partIndex);
......
...@@ -116,22 +116,19 @@ public class LibreOfficeMainActivity extends Activity { ...@@ -116,22 +116,19 @@ public class LibreOfficeMainActivity extends Activity {
mDrawerList.setOnItemClickListener(new DocumentPartClickListener()); mDrawerList.setOnItemClickListener(new DocumentPartClickListener());
} }
if (mLayerController == null) { mLayerController = new LayerController(this);
mLayerController = new LayerController(this); mLayerClient = new GeckoLayerClient(this);
mLayerController.setLayerClient(mLayerClient);
Log.e(LOGTAG, "### Creating GeckoSoftwareLayerClient"); mGeckoLayout.addView(mLayerController.getView(), 0);
mLayerClient = new GeckoLayerClient(this);
Log.e(LOGTAG, "### Done creating GeckoSoftwareLayerClient");
mLayerController.setLayerClient(mLayerClient);
mGeckoLayout.addView(mLayerController.getView(), 0);
}
if (sLOKitThread == null) { if (sLOKitThread == null) {
sLOKitThread = new LOKitThread(inputFile); sLOKitThread = new LOKitThread();
sLOKitThread.start(); sLOKitThread.start();
} }
sLOKitThread.mEventQueue.clear();
LOKitShell.sendEvent(LOEvent.load(inputFile));
Log.w(LOGTAG, "UI almost up"); Log.w(LOGTAG, "UI almost up");
} }
......
...@@ -66,6 +66,10 @@ public class MockTileProvider implements TileProvider { ...@@ -66,6 +66,10 @@ public class MockTileProvider implements TileProvider {
return layerController.getDrawable("dummy_page"); return layerController.getDrawable("dummy_page");
} }
@Override
public void close() {
}
@Override @Override
public void changePart(int partIndex) { public void changePart(int partIndex) {
......
...@@ -17,4 +17,6 @@ public interface TileProvider { ...@@ -17,4 +17,6 @@ public interface TileProvider {
void changePart(int partIndex); void changePart(int partIndex);
Bitmap thumbnail(); Bitmap thumbnail();
void close();
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment