From 6f249835a4ff9e7e7e3ca0190b7ecf72e689656d Mon Sep 17 00:00:00 2001 From: Svetoslav Date: Tue, 2 Sep 2014 21:12:04 -0700 Subject: [PATCH] Fix some print UI issues. 1. Fixed a crash when orientation changes and the content is scrolled due to wrong size bitmap being requested. 2. Closed a file dscriptior that was being left open. 3. Clearing the bitmap before passing it to the renderer to ensure it is white for pixels not touched when rendering. 4. Removed debug logs. 5. Switched to the correct layout manager for RecyclerView. bug:16966145 Change-Id: I8ab9d22635c93cac5ff85c6f4b5d82e58cd8df5c --- .../model/PageContentRepository.java | 15 +++++--- .../model/RemotePrintDocument.java | 2 +- .../renderer/PdfRendererService.java | 3 ++ .../ui/PrintPreviewController.java | 5 +-- .../printspooler/widget/PageContentView.java | 35 ++++++++++++------- 5 files changed, 40 insertions(+), 20 deletions(-) diff --git a/packages/PrintSpooler/src/com/android/printspooler/model/PageContentRepository.java b/packages/PrintSpooler/src/com/android/printspooler/model/PageContentRepository.java index cd2ccbdff9450..4f37468d84248 100644 --- a/packages/PrintSpooler/src/com/android/printspooler/model/PageContentRepository.java +++ b/packages/PrintSpooler/src/com/android/printspooler/model/PageContentRepository.java @@ -24,13 +24,14 @@ import android.content.ServiceConnection; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; -import android.graphics.Rect; import android.graphics.drawable.BitmapDrawable; import android.os.AsyncTask; import android.os.IBinder; import android.os.ParcelFileDescriptor; import android.os.RemoteException; import android.print.PrintAttributes; +import android.print.PrintAttributes.MediaSize; +import android.print.PrintAttributes.Margins; import android.print.PrintDocumentInfo; import android.util.ArrayMap; import android.util.Log; @@ -41,7 +42,6 @@ import com.android.printspooler.renderer.PdfRendererService; import dalvik.system.CloseGuard; import libcore.io.IoUtils; -import java.io.FileDescriptor; import java.io.IOException; import java.util.Iterator; import java.util.LinkedHashMap; @@ -354,13 +354,14 @@ public final class PageContentRepository { public static final class RenderSpec { final int bitmapWidth; final int bitmapHeight; - final PrintAttributes printAttributes; + final PrintAttributes printAttributes = new PrintAttributes.Builder().build(); public RenderSpec(int bitmapWidth, int bitmapHeight, - PrintAttributes printAttributes) { + MediaSize mediaSize, Margins minMargins) { this.bitmapWidth = bitmapWidth; this.bitmapHeight = bitmapHeight; - this.printAttributes = printAttributes; + printAttributes.setMediaSize(mediaSize); + printAttributes.setMinMargins(minMargins); } @Override @@ -505,6 +506,10 @@ public final class PageContentRepository { } catch (RemoteException re) { Log.e(LOG_TAG, "Cannot open PDF document"); return MALFORMED_PDF_FILE_ERROR; + } finally { + // Close the fd as we passed it to another process + // which took ownership. + IoUtils.closeQuietly(source); } } } diff --git a/packages/PrintSpooler/src/com/android/printspooler/model/RemotePrintDocument.java b/packages/PrintSpooler/src/com/android/printspooler/model/RemotePrintDocument.java index d1aa33b7a7884..09e8b399b042c 100644 --- a/packages/PrintSpooler/src/com/android/printspooler/model/RemotePrintDocument.java +++ b/packages/PrintSpooler/src/com/android/printspooler/model/RemotePrintDocument.java @@ -55,7 +55,7 @@ import java.util.Arrays; public final class RemotePrintDocument { private static final String LOG_TAG = "RemotePrintDocument"; - private static final boolean DEBUG = true; + private static final boolean DEBUG = false; private static final int STATE_INITIAL = 0; private static final int STATE_STARTED = 1; diff --git a/packages/PrintSpooler/src/com/android/printspooler/renderer/PdfRendererService.java b/packages/PrintSpooler/src/com/android/printspooler/renderer/PdfRendererService.java index a4c693284e92f..5012cadcfb685 100644 --- a/packages/PrintSpooler/src/com/android/printspooler/renderer/PdfRendererService.java +++ b/packages/PrintSpooler/src/com/android/printspooler/renderer/PdfRendererService.java @@ -20,6 +20,7 @@ import android.app.Service; import android.content.Intent; import android.content.res.Configuration; import android.graphics.Bitmap; +import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Rect; import android.graphics.pdf.PdfRenderer; @@ -171,11 +172,13 @@ public final class PdfRendererService extends Service { private Bitmap getBitmapForSize(int width, int height) { if (mBitmap != null) { if (mBitmap.getWidth() == width && mBitmap.getHeight() == height) { + mBitmap.eraseColor(Color.WHITE); return mBitmap; } mBitmap.recycle(); } mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); + mBitmap.eraseColor(Color.WHITE); return mBitmap; } diff --git a/packages/PrintSpooler/src/com/android/printspooler/ui/PrintPreviewController.java b/packages/PrintSpooler/src/com/android/printspooler/ui/PrintPreviewController.java index ddf637eeb71fc..2b5b41bd53b99 100644 --- a/packages/PrintSpooler/src/com/android/printspooler/ui/PrintPreviewController.java +++ b/packages/PrintSpooler/src/com/android/printspooler/ui/PrintPreviewController.java @@ -24,6 +24,7 @@ import android.print.PageRange; import android.print.PrintAttributes.MediaSize; import android.print.PrintAttributes.Margins; import android.print.PrintDocumentInfo; +import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.OrientationHelper; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView.ViewHolder; @@ -51,7 +52,7 @@ class PrintPreviewController implements MutexFileProvider.OnReleaseRequestCallba private final MyHandler mHandler; private final PageAdapter mPageAdapter; - private final StaggeredGridLayoutManager mLayoutManger; + private final GridLayoutManager mLayoutManger; private final PrintOptionsLayout mPrintOptionsLayout; private final RecyclerView mRecyclerView; @@ -73,7 +74,7 @@ class PrintPreviewController implements MutexFileProvider.OnReleaseRequestCallba final int columnCount = mActivity.getResources().getInteger( R.integer.preview_page_per_row_count); - mLayoutManger = new StaggeredGridLayoutManager(columnCount, OrientationHelper.VERTICAL); + mLayoutManger = new GridLayoutManager(mActivity, columnCount); mRecyclerView = (RecyclerView) activity.findViewById(R.id.preview_content); mRecyclerView.setLayoutManager(mLayoutManger); diff --git a/packages/PrintSpooler/src/com/android/printspooler/widget/PageContentView.java b/packages/PrintSpooler/src/com/android/printspooler/widget/PageContentView.java index 76ff167293f42..d935f585dd287 100644 --- a/packages/PrintSpooler/src/com/android/printspooler/widget/PageContentView.java +++ b/packages/PrintSpooler/src/com/android/printspooler/widget/PageContentView.java @@ -20,7 +20,6 @@ import android.content.Context; import android.graphics.Canvas; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.ColorDrawable; -import android.print.PrintAttributes; import android.print.PrintAttributes.MediaSize; import android.print.PrintAttributes.Margins; import android.util.AttributeSet; @@ -39,14 +38,18 @@ import com.android.printspooler.model.PageContentRepository.RenderSpec; public class PageContentView extends View implements PageContentRepository.OnPageContentAvailableCallback { - private final PrintAttributes mAttributes = new PrintAttributes.Builder().build(); - private final ColorDrawable mEmptyState; private PageContentProvider mProvider; + private MediaSize mMediaSize; + + private Margins mMinMargins; + private boolean mContentRequested; + private boolean mNeedsLayout; + public PageContentView(Context context, AttributeSet attrs) { super(context, attrs); @@ -68,6 +71,7 @@ public class PageContentView extends View @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); + mNeedsLayout = false; requestPageContentIfNeeded(); } @@ -83,18 +87,23 @@ public class PageContentView extends View } public void init(PageContentProvider provider, MediaSize mediaSize, Margins minMargins) { - if (mProvider == null ? provider == null : mProvider.equals(provider) - && ((mAttributes.getMediaSize() == null) - ? mediaSize == null : mAttributes.getMediaSize().equals(mediaSize)) - && ((mAttributes.getMinMargins() == null) - ? minMargins == null : mAttributes.getMinMargins().equals(minMargins))) { + final boolean providerChanged = (mProvider == null) + ? provider != null : !mProvider.equals(provider); + final boolean mediaSizeChanged = (mMediaSize == null) + ? mediaSize != null : !mMediaSize.equals(mediaSize); + final boolean marginsChanged = (mMinMargins == null) + ? minMargins != null : !mMinMargins.equals(minMargins); + + if (!providerChanged && !mediaSizeChanged && !marginsChanged) { return; } mProvider = provider; - mAttributes.setMediaSize(mediaSize); - mAttributes.setMinMargins(minMargins); + mMediaSize = mediaSize; + mMinMargins = minMargins; + mContentRequested = false; + mNeedsLayout = mNeedsLayout || mediaSizeChanged || marginsChanged; // If there is no provider we want immediately to switch to // the empty state, so pages with no content appear blank. @@ -106,9 +115,11 @@ public class PageContentView extends View } private void requestPageContentIfNeeded() { - if (getWidth() > 0 && getHeight() > 0 && !mContentRequested && mProvider != null) { + if (getWidth() > 0 && getHeight() > 0 && !mContentRequested + && mProvider != null && !mNeedsLayout) { mContentRequested = true; - mProvider.getPageContent(new RenderSpec(getWidth(), getHeight(), mAttributes), this); + mProvider.getPageContent(new RenderSpec(getWidth(), getHeight(), + mMediaSize, mMinMargins), this); } } }