am da9a22bc: Save/restore the picture in a background thread.

Merge commit 'da9a22bc5cfd87d80d5afa53fa8c1d29b0407a07' into froyo-plus-aosp

* commit 'da9a22bc5cfd87d80d5afa53fa8c1d29b0407a07':
  Save/restore the picture in a background thread.
This commit is contained in:
Patrick Scott
2010-04-09 06:37:11 -07:00
committed by Android Git Automerger

View File

@@ -1274,30 +1274,57 @@ public class WebView extends AbsoluteLayout
* overwritten with this WebView's picture data. * overwritten with this WebView's picture data.
* @return True if the picture was successfully saved. * @return True if the picture was successfully saved.
*/ */
public boolean savePicture(Bundle b, File dest) { public boolean savePicture(Bundle b, final File dest) {
if (dest == null || b == null) { if (dest == null || b == null) {
return false; return false;
} }
final Picture p = capturePicture(); final Picture p = capturePicture();
try { // Use a temporary file while writing to ensure the destination file
final FileOutputStream out = new FileOutputStream(dest); // contains valid data.
p.writeToStream(out); final File temp = new File(dest.getPath() + ".writing");
out.close(); new Thread(new Runnable() {
// now update the bundle public void run() {
b.putInt("scrollX", mScrollX); try {
b.putInt("scrollY", mScrollY); FileOutputStream out = new FileOutputStream(temp);
b.putFloat("scale", mActualScale); p.writeToStream(out);
b.putFloat("textwrapScale", mTextWrapScale); out.close();
b.putBoolean("overview", mInZoomOverview); // Writing the picture succeeded, rename the temporary file
return true; // to the destination.
} catch (FileNotFoundException e){ temp.renameTo(dest);
e.printStackTrace(); } catch (Exception e) {
} catch (IOException e) { // too late to do anything about it.
e.printStackTrace(); } finally {
} catch (RuntimeException e) { temp.delete();
e.printStackTrace(); }
} }
return false; }).start();
// now update the bundle
b.putInt("scrollX", mScrollX);
b.putInt("scrollY", mScrollY);
b.putFloat("scale", mActualScale);
b.putFloat("textwrapScale", mTextWrapScale);
b.putBoolean("overview", mInZoomOverview);
return true;
}
private void restoreHistoryPictureFields(Picture p, Bundle b) {
int sx = b.getInt("scrollX", 0);
int sy = b.getInt("scrollY", 0);
float scale = b.getFloat("scale", 1.0f);
mDrawHistory = true;
mHistoryPicture = p;
mScrollX = sx;
mScrollY = sy;
mHistoryWidth = Math.round(p.getWidth() * scale);
mHistoryHeight = Math.round(p.getHeight() * scale);
// as getWidth() / getHeight() of the view are not available yet, set up
// mActualScale, so that when onSizeChanged() is called, the rest will
// be set correctly
mActualScale = scale;
mInvActualScale = 1 / scale;
mTextWrapScale = b.getFloat("textwrapScale", scale);
mInZoomOverview = b.getBoolean("overview");
invalidate();
} }
/** /**
@@ -1311,42 +1338,35 @@ public class WebView extends AbsoluteLayout
if (src == null || b == null) { if (src == null || b == null) {
return false; return false;
} }
if (src.exists()) { if (!src.exists()) {
Picture p = null; return false;
try {
final FileInputStream in = new FileInputStream(src);
p = Picture.createFromStream(in);
in.close();
} catch (FileNotFoundException e){
e.printStackTrace();
} catch (RuntimeException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (p != null) {
int sx = b.getInt("scrollX", 0);
int sy = b.getInt("scrollY", 0);
float scale = b.getFloat("scale", 1.0f);
mDrawHistory = true;
mHistoryPicture = p;
mScrollX = sx;
mScrollY = sy;
mHistoryWidth = Math.round(p.getWidth() * scale);
mHistoryHeight = Math.round(p.getHeight() * scale);
// as getWidth() / getHeight() of the view are not
// available yet, set up mActualScale, so that when
// onSizeChanged() is called, the rest will be set
// correctly
mActualScale = scale;
mInvActualScale = 1 / scale;
mTextWrapScale = b.getFloat("textwrapScale", scale);
mInZoomOverview = b.getBoolean("overview");
invalidate();
return true;
}
} }
return false; try {
final FileInputStream in = new FileInputStream(src);
final Bundle copy = new Bundle(b);
new Thread(new Runnable() {
public void run() {
final Picture p = Picture.createFromStream(in);
if (p != null) {
// Post a runnable on the main thread to update the
// history picture fields.
mPrivateHandler.post(new Runnable() {
public void run() {
restoreHistoryPictureFields(p, copy);
}
});
}
try {
in.close();
} catch (Exception e) {
// Nothing we can do now.
}
}
}).start();
} catch (FileNotFoundException e){
e.printStackTrace();
}
return true;
} }
/** /**
@@ -3339,6 +3359,7 @@ public class WebView extends AbsoluteLayout
if (null == mWebViewCore) return; // CallbackProxy may trigger this if (null == mWebViewCore) return; // CallbackProxy may trigger this
if (mDrawHistory && mWebViewCore.pictureReady()) { if (mDrawHistory && mWebViewCore.pictureReady()) {
mDrawHistory = false; mDrawHistory = false;
mHistoryPicture = null;
invalidate(); invalidate();
int oldScrollX = mScrollX; int oldScrollX = mScrollX;
int oldScrollY = mScrollY; int oldScrollY = mScrollY;