Exemplo n.º 1
0
                protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] native_parms)
                {
                    // Go over all the pages and write only the requested ones.
                    // Create an adapter with the stats and an inflater
                    // to load resources for the printer density.
                    var adapter = new MotoGpStatAdapter(items,
                                                        (LayoutInflater)self.mPrintContext.GetSystemService(
                                                            Context.LayoutInflaterService));

                    int  currentPage       = -1;
                    int  pageContentHeight = 0;
                    int  viewType          = -1;
                    View view = null;

                    Android.Graphics.Pdf.PdfDocument.Page page = null;
                    var dummyParent = new LinearLayout(self.mPrintContext);

                    dummyParent.Orientation = Android.Widget.Orientation.Vertical;

                    // The content is laid out and rendered in screen pixels with
                    // the width and height of the paper size times the print
                    // density but the PDF canvas size is in points which are 1/72",
                    // so we will scale down the content.
                    float scale = System.Math.Min(
                        (float)mPdfDocument.PageContentRect.Width() / self.mRenderPageWidth,
                        (float)mPdfDocument.PageContentRect.Height() / self.mRenderPageHeight);

                    int itemCount = adapter.Count;

                    for (int i = 0; i < itemCount; i++)
                    {
                        // Be nice and respond to cancellation.
                        if (IsCancelled)
                        {
                            return(null);
                        }

                        // Get the next view.
                        int nextViewType = adapter.GetItemViewType(i);
                        if (viewType == nextViewType)
                        {
                            view = adapter.GetView(i, view, dummyParent);
                        }
                        else
                        {
                            view = adapter.GetView(i, null, dummyParent);
                        }
                        viewType = nextViewType;

                        // Measure the next view
                        self.MeasureView(view);

                        // Add the height but if the view crosses the page
                        // boundary we will put it to the next one.
                        pageContentHeight += view.MeasuredHeight;
                        if (currentPage < 0 || pageContentHeight > self.mRenderPageHeight)
                        {
                            pageContentHeight = view.MeasuredHeight;
                            currentPage++;
                            // Done with the current page - finish it.
                            if (page != null)
                            {
                                mPdfDocument.FinishPage(page);
                            }
                            // If the page is requested, render it.
                            if (self.ContainsPage(pages, currentPage))
                            {
                                page = mPdfDocument.StartPage(currentPage);
                                page.Canvas.Scale(scale, scale);
                                // Keep track which pages are written.
                                mWrittenPages.Append(mWrittenPages.Size(), currentPage);
                            }
                            else
                            {
                                page = null;
                            }
                        }

                        // If the current view is on a requested page, render it.
                        if (page != null)
                        {
                            // Layout an render the content.
                            view.Layout(0, 0, view.MeasuredWidth, view.MeasuredHeight);
                            view.Draw(page.Canvas);
                            // Move the canvas for the next view.
                            page.Canvas.Translate(0, view.Height);
                        }
                    }

                    // Done with the last page.
                    if (page != null)
                    {
                        mPdfDocument.FinishPage(page);
                    }

                    // Write the data and return success or failure.
                    try {
                        var fos = new Java.IO.FileOutputStream(destination.FileDescriptor);
                        mPdfDocument.WriteTo(new OutputStreamInvoker(fos));
                        // Compute which page ranges were written based on
                        // the bookkeeping we maintained.
                        var pageRanges = self.ComputeWrittenPageRanges(mWrittenPages);
                        callback.OnWriteFinished(pageRanges);
                    } catch (IOException) {
                        callback.OnWriteFailed((string)null);
                    } finally {
                        mPdfDocument.Close();
                    }

                    return(null);
                }
Exemplo n.º 2
0
 protected override void OnCreate(Bundle bundle)
 {
     base.OnCreate(bundle);
     ListAdapter = new MotoGpStatAdapter(LoadMotoGpStats(), LayoutInflater);
 }
Exemplo n.º 3
0
                protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] native_parms)
                {
                    try {
                        // Create an adapter with the stats and an inflater
                        // to load resources for the printer density.
                        var adapter = new MotoGpStatAdapter(items,
                                                            (LayoutInflater)self.mPrintContext.GetSystemService(
                                                                Context.LayoutInflaterService));

                        int  currentPage       = 0;
                        int  pageContentHeight = 0;
                        int  viewType          = -1;
                        View view        = null;
                        var  dummyParent = new LinearLayout(self.mPrintContext);
                        dummyParent.Orientation = Android.Widget.Orientation.Vertical;

                        int itemCount = adapter.Count;
                        for (int i = 0; i < itemCount; i++)
                        {
                            // Be nice and respond to cancellation.
                            if (IsCancelled)
                            {
                                return(null);
                            }

                            // Get the next view.
                            int nextViewType = adapter.GetItemViewType(i);
                            if (viewType == nextViewType)
                            {
                                view = adapter.GetView(i, view, dummyParent);
                            }
                            else
                            {
                                view = adapter.GetView(i, null, dummyParent);
                            }
                            viewType = nextViewType;

                            // Measure the next view
                            self.MeasureView(view);

                            // Add the height but if the view crosses the page
                            // boundary we will put it to the next page.
                            pageContentHeight += view.MeasuredHeight;
                            if (pageContentHeight > self.mRenderPageHeight)
                            {
                                pageContentHeight = view.MeasuredHeight;
                                currentPage++;
                            }
                        }

                        // Create a document info describing the result.
                        PrintDocumentInfo info = new PrintDocumentInfo
                                                 .Builder("MotoGP_stats.pdf")
                                                 .SetContentType(PrintContentType.Document)
                                                 .SetPageCount(currentPage + 1)
                                                 .Build();

                        // We completed the layout as a result of print attributes
                        // change. Hence, if we are here the content changed for
                        // sure which is why we pass true as the second argument.
                        callback.OnLayoutFinished(info, true);
                        return(info);
                    } catch (Java.Lang.Exception e) {
                        // An unexpected error, report that we failed and
                        // one may pass in a human readable localized text
                        // for what the error is if known.
                        callback.OnLayoutFailed((string)null);
                        throw new RuntimeException(e);
                    }
                }