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 {
public static final int VIEWPORT = 3;
public static final int DRAW = 4;
public static final int CHANGE_PART = 5;
public static final int LOAD = 6;
public int mType;
ViewportMetrics viewportMetrics;
private ViewportMetrics mViewportMetrics;
private String mTypeString;
private int mPartIndex;
private String mFilename;
public LOEvent(int type, int widthPixels, int heightPixels, int tileWidth, int tileHeight) {
mType = type;
mTypeString = "Size Changed: " + widthPixels + " " + heightPixels;
}
public LOEvent(int type, String filename) {
mType = type;
mFilename = filename;
}
public LOEvent(int type, IntSize tileSize) {
mType = type;
mTypeString = "Tile size";
......@@ -65,6 +72,10 @@ public class LOEvent {
return new LOEvent(CHANGE_PART, part);
}
public static LOEvent load(String inputFile) {
return new LOEvent(LOAD, inputFile);
}
public String getTypeString() {
return mTypeString;
}
......@@ -76,4 +87,9 @@ public class LOEvent {
public int getPartIndex() {
return mPartIndex;
}
public String getFilename() {
return mFilename;
}
}
......@@ -38,4 +38,5 @@ public class LOKitShell {
public static Handler getMainHandler() {
return LibreOfficeMainActivity.mAppContext.mMainHandler;
}
}
......@@ -22,21 +22,19 @@ public class LOKitThread extends Thread {
private LibreOfficeMainActivity mApplication;
private TileProvider mTileProvider;
private ViewportMetrics mViewportMetrics;
private String mInputFile;
private Rect mOldRect;
private boolean mCheckboardImageSet = false;
LOKitThread(String inputFile) {
mInputFile = inputFile;
public LOKitThread() {
}
RectF normlizeRect(ImmutableViewportMetrics metrics) {
private RectF normlizeRect(ImmutableViewportMetrics metrics) {
RectF rect = metrics.getViewport();
float zoomFactor = metrics.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 minY = (Math.round(input.top) / tileSize) * tileSize;
int maxX = ((Math.round(input.right) / tileSize) + 1) * tileSize;
......@@ -44,7 +42,7 @@ public class LOKitThread extends Thread {
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);
newRect.left -= inflateSize;
newRect.left = newRect.left < 0 ? 0 : newRect.left;
......@@ -130,12 +128,22 @@ public class LOKitThread extends Thread {
LOKitShell.sendEvent(LOEvent.draw(new Rect()));
}
private boolean initialize() {
private boolean load(String filename) {
if (mApplication == null) {
mApplication = LibreOfficeMainActivity.mAppContext;
mTileProvider = new LOKitTileProvider(mApplication.getLayerController(), mInputFile);
}
if (mTileProvider != null) {
mTileProvider.close();
}
mTileProvider = new LOKitTileProvider(mApplication.getLayerController(), filename);
boolean isReady = mTileProvider.isReady();
if (isReady)
{
if (isReady) {
updateCheckbardImage();
}
return isReady;
}
private void updateCheckbardImage() {
if (!mCheckboardImageSet) {
Log.i(LOGTAG, "Generate thumbnail!");
Bitmap bitmap = mTileProvider.thumbnail();
......@@ -148,23 +156,21 @@ public class LOKitThread extends Thread {
}
}
}
return isReady;
}
public void run() {
if (initialize()) {
try {
boolean drawn = false;
while (true) {
processEvent(mEventQueue.take());
}
} catch (InterruptedException ex) {
}
}
}
private void processEvent(LOEvent event) throws InterruptedException {
switch (event.mType) {
case LOEvent.LOAD:
load(event.getFilename());
break;
case LOEvent.VIEWPORT:
mViewportMetrics = event.getViewport();
draw();
......
......@@ -21,6 +21,7 @@ public class LOKitTileProvider implements TileProvider {
private final LayerController mLayerController;
private final double mTileWidth;
private final double mTileHeight;
private final String mInputFile;
private double mDPI;
private double mWidthTwip;
......@@ -37,6 +38,7 @@ public class LOKitTileProvider implements TileProvider {
mOffice = new Office(LibreOfficeKit.getLibreOfficeKitHandle());
mInputFile = input;
mDocument = mOffice.documentLoad(input);
if (checkDocument()) {
......@@ -110,7 +112,11 @@ public class LOKitTileProvider implements TileProvider {
ByteBuffer buffer = ByteBuffer.allocateDirect(TILE_SIZE * TILE_SIZE * 4);
Bitmap bitmap = Bitmap.createBitmap(TILE_SIZE, TILE_SIZE, Bitmap.Config.ARGB_8888);
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);
......@@ -146,6 +152,12 @@ public class LOKitTileProvider implements TileProvider {
return bitmap;
}
@Override
public void close() {
Log.i(LOGTAG, "Document destroyed: " + mInputFile);
mDocument.destroy();
}
@Override
public void changePart(int partIndex) {
mDocument.setPart(partIndex);
......
......@@ -116,22 +116,19 @@ public class LibreOfficeMainActivity extends Activity {
mDrawerList.setOnItemClickListener(new DocumentPartClickListener());
}
if (mLayerController == null) {
mLayerController = new LayerController(this);
Log.e(LOGTAG, "### Creating GeckoSoftwareLayerClient");
mLayerClient = new GeckoLayerClient(this);
Log.e(LOGTAG, "### Done creating GeckoSoftwareLayerClient");
mLayerController.setLayerClient(mLayerClient);
mGeckoLayout.addView(mLayerController.getView(), 0);
}
if (sLOKitThread == null) {
sLOKitThread = new LOKitThread(inputFile);
sLOKitThread = new LOKitThread();
sLOKitThread.start();
}
sLOKitThread.mEventQueue.clear();
LOKitShell.sendEvent(LOEvent.load(inputFile));
Log.w(LOGTAG, "UI almost up");
}
......
......@@ -66,6 +66,10 @@ public class MockTileProvider implements TileProvider {
return layerController.getDrawable("dummy_page");
}
@Override
public void close() {
}
@Override
public void changePart(int partIndex) {
......
......@@ -17,4 +17,6 @@ public interface TileProvider {
void changePart(int partIndex);
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