Office Hours — Today, April 9

Thursday, April 5

Apr 9
8:50 AM
Mark M.
has entered the room
Mark M.
turned on guest access
8:55 AM
Anurag S.
has entered the room
Mark M.
hello, Anurag!
how can I help you today?
Anurag S.
View paste (3 more lines)

Hi Mark,

I am struggling in generating pdf from the application in offline mode.
Pdf will embed text and images. Image sizes will be depending upon the 
native camera resolution(e.g. 3.5 mb each). Total number of images could
vary in between from 100 to 120. So the generated pdf will contain pages
around 130(10 for the text in tabular format).

I thought of using https://itextpdf.com/itext-7-core but at build time an error is
generated as MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O (--min-api 26)
Message{kind=ERROR, text=MethodHandle.invoke and MethodHandle.invokeExact 
are only supported starting with Android O (--min-api 26), sources=[Unknown source file], tool name=Optional.of(D8)}.

which I believe I need to set minSdkVersion to O. If set it works fine.
...
Mark M.
I think that there are older iText implementations that work on Android and work with Java 7
my guess is that you will run out of memory, no matter what you use
bill
has entered the room
Anurag S.
Yes, correct.
Mark M.
(BTW, hello bill! I'll be with you shortly!)
bill
Hello Mark
Mark M.
personally, I would have the work be done on a server, if possible
Anurag S.
Yes, I thought so but the requirement is as such to work in offline.
9:00 AM
Mark M.
you could try PdfDocument in Android 5.0+, but that's really designed for Android's printing framework, not for creating arbitrary PDFs
with regards to iText, try iText 5, instead of iText 7
also, bear in mind that iText is licensed under the GPL
Anurag S.
So any other alternative approach?
Yes.
Mark M.
than those? none that I know of, though there are probably some commercial libraries floating around
Anurag S.
As pointed above iText 5 dosen't compiles
Mark M.
your original question was for iText 7
Anurag S.
It's not found on maven
Yes
Mark M.
then perhaps compile from source, or ask the iText people where it moved to
Anurag S.
Itried both with 5 and 7. It is not working.
Ok
Is it possible to generate html or render those images with text and image in an android view?
Mark M.
let me take a question from Bill, and I'll return to you in a bit
Bill: your turn! do you have a question?
Anurag S.
Sure
Thanks
bill
View paste
Hello, I want to make a classic question regarding to the 3 libraries: AlarmManager, JobScheduler and evernote / android-jobs. I develop a business application for a company. I want to schedule jobs every day for the time window between 21:00 to 07:00. The exact time does not matter as long as it is in the above time window. Sometimes they do maintenance on the companies server and therefore the android job may fail. Then the job should be rescheduled.
Which library from the before mentioned libraries should I use given that our minSdkVersion  API is 21
Mark M.
for a minSdkVersion of 21, I would just use JobScheduler
9:05 AM
Mark M.
well, actually, not with that window
take that back
you'd probably need to use AlarmManager and setWindow()
the problem with JobScheduler is that you have limited control over actual timing
with AlarmManager, you would need to implement rescheduling yourself
bill
I am afraid that if I use periodic timing there would be sliding outside the time window
With periodic timing I mean every 24 hours
Mark M.
schedule it for, say, 21:15 or 21:30, somewhere close to the start of the window
validate in the BroadcastReceiver that you got control in the desired window, rescheduling if not
bill
I understand.
Mark M.
alternatively, use none of these solutions, and have the server send an FCM high-priority message to ask the device to do its work
that's supposed to break through even Doze mode, though I haven't tried it
bill
Ok, good point
I want to be a possibility to wake the device
Mark M.
I'm assuming that the work to be done requires Internet access, given your reference to the server
9:10 AM
Mark M.
AlarmManager, JobScheduler, and FCM messages can wake the device
bill
One last question. evernote has something to offer me better than the JobScheduler
Mark M.
their library simply wraps around JobScheduler and AlarmManager
it's a nice library, particularly if you want JobScheduler semantics on Android 4.4 and older
if you like their API better, feel free to use it
bill
As I said our minSDKApi is 21.
Mark M.
however, it does not change the underlying limitations of AlarmManager and JobScheduler, particularly with respect to things like Doze mode
that's why I did not suggest using their library
IMHO, the value of that library falls a fair bit once you have a minSdkVersion of 21 or higher
but, again, if you like the library for other reasons, use it
let me switch back to Anurag for a while, and I'll return to you shortly
Anurag: your turn! do you have another question?
bill
Ok. I want time to study your answers
Anurag S.
Is it possible to generate html or render those images with text and image in an android view?
Mark M.
Anurag: you can certainly generate HTML
I have a few sample apps in the book that do that, using the jMustache template library
9:15 AM
Mark M.
you can certainly create a layout that shows text and images
however, if your end objective is a PDF, your primary options of getting a PDF from those things would be the Android printing framework
which is designed for printing
any PDF that comes out of that process *should* be general-purpose, but I get nervous when developers count on that
Anurag S.
Images will be placed on external storage. So, using the jMustache template library would it be possible to access those images and render the htm in a webview?
Mark M.
sure
call getAbsolutePath() on the File object pointing to the image, and use the resulting value in the HTML for the src attribute on an <img> tag
you'll need permission to work with external storage, but presumably you already have that
Anurag S.
Ok. So could I create an html file using jMustache template library on external storage and using Android printing framework save it as pdf?
Like chrome has an option.
Mark M.
you can print an HTML document with WebView
9:20 AM
Mark M.
the WebView API (createPrintDocumentAdapter()) is not designed to create a standalone PDF
Anurag S.
Ok
So what about generating htlm using jMustache template library and then using another third party library for converting html to pdf from the application itself?
Mark M.
I am not aware of one that works on Android
on a server, you could use Prince XML (commercial) or wkhtml2pdf (open source)
but I'm not aware of either of those being an on-device option
Anurag S.
Fine.
Mark M.
let me take another question from Bill, and I will return to you shortly
Bill: your turn! do you have another question?
Anurag S.
Itried using PdfDocument in Android 5.0 but it's slow while rendering. It uses canvas. Out of Memory is another issue with it. Morover I need to play in pixes to place text and calculate overflowing or wrapping of text. etc.
OK
9:25 AM
bill
For today I think I have not other question. Maybe tomorrow on the some issue. Thanks a lot.
Mark M.
OK, if you come up with another one while you are still here, let me know
meanwhile...
Anurag: back to you!
Anurag S.
I tried using PdfDocument in Android 5.0 but it's slow while rendering. It uses canvas. Out of Memory is another issue with it. Morover I need to play in pixes to place text and calculate overflowing or wrapping of text. etc.
bill
OK Mark. Thanks a lot again!!!
Mark M.
(Bill: you're welcome!)
Anurag: mostly, the Canvas-based approach seems to be designed to print a layout that you may already have set up on-screen
eventually, Android will need better PDF generation options
probably via a third-party library
though, for many use cases (albeit perhaps not yours), server-side PDF generation is fine and greatly increases your flexibility
Anurag S.
Yes, I completely agree with you. But the problem is end user captures n number of picture and need to see report immediately. Uploading all those images to server will take time.
Mark M.
what will be done with the report, after the user sees it?
in other words, does the report *have* to be a PDF?
9:30 AM
Mark M.
perhaps the on-device representation is in HTML, and you ship a ZIP with the HTML+images up to the server for PDF generation
Anurag S.
I think this would be much better..
Client needs to signature the document or pdf.
Mark M.
one commercial library in this space is PDFjet: http://pdfjet.com/java/index.html
I have not tried it
Anurag S.
So generating html will also not throw out of memory exception I guess because it will contain only path to images and some kbs of text.
Mark M.
correct
assuming that your text is not huge, of course
Anurag S.
This design approach seems to work.
Text is just some question and answers
So createing this zip of around 300mb. Will it be fine?
100 images of 3mb each
Andre R.
has entered the room
Mark M.
if you mean in terms of OutOfMemoryError, if you use ZipOutputStream, I think that you will be OK
Andre R.
Good Morning
Anurag S.
Yes, OK.
9:35 AM
Mark M.
let me take a question from Andre, and I'll return to you in a little bit
Andre: hi! do you have a question?
Anurag S.
Sure, Mark.
Andre R.
Can you take a look at this question ? https://stackoverflow.com/questions/49690010/ho...
Mark M.
yes, I saw that when you posted it
Andre R.
What do you think of the approach ?
Is there anything I should be aware of, regarding memory leaks ?
Mark M.
nothing in importaBkpObservable() seems like it would represent a leak, if that's what you mean
assuming that Room is happy after the backup operation, this seems OK
Andre R.
I would like to know if it is sufficient to assign null to the Room database instance ?
Mark M.
well, Room doesn't know anything about that
9:40 AM
Mark M.
if you are asking whether in your business logic that suffices, probably, though you should ensure there is no race condition with something else trying to use that INSTANCE
part of what I need to spend some time on is seeing how well Room deals with close() on a database, in terms of its background InvalidationTracker
unfortunately, other work has been getting in the way of my spending time on that subject... :-(
Andre R.
I am creating again my activities and calling another instance for them, I don't know what is happening to the former instance. Is it going to be garbage collected ?
Mark M.
if nothing else has a lingering reference to it, yes
in principle, once you call close(), Room is responsible for removing any of its references (e.g., terminate the InvalidationTracker thread)
if Room does that, and you do not have any references to it in your own code, it will (eventually) get garbage-collected
9:45 AM
Mark M.
so, if you are not seeing leaks, and Room is happy with the new instance, you should be in decent shape
Andre R.
Ok. So the risk is if Room doesn't make its job before I request another instance of it ?
Mark M.
I do not know what "if Room doesn't make its job" means, sorry
Andre R.
Like closing the database and terminate the invalidation tracker ?
Mark M.
if they are doing it correctly, it should be synchronous as part of close()
but, again, this is an area that I want to research further
Andre R.
Ok. I will keep my eyes open. Thank you.
Mark M.
also, it's open source, so you can do what I'll (eventually) do, which is to look at what close() does and see if there are any signs of potential synchronization issues
I'll also be bugging Google to better document the rules around close()
9:50 AM
Mark M.
as this is an area that really needs explicit guarantees written into the JavaDocs
at least, IMHO
OK, free-for-all for the last 10 minutes -- if anyone has a question, go ahead, and I'll try to keep up :-)
Anurag S.
View paste
I am thinking of creating zip and uploading it on the server in another process. And notify it on the UI thread when running in foreground. How to notify the UI thread.
Or what do you think about using this design approach?
Andre R.
has left the room
Mark M.
Anurag: if you are using a separate process for uploads, you will need to use a system broadcast (e.g., sendBroadcast() on Context), as event bus/RxJava options are all in-process
Anurag S.
Ok
What do you think about using this design approach?
Mark M.
personally, I try to avoid extra processes
a JobIntentService, in the main process, should be sufficient here
extra processes for *really* long-lived stuff (e.g., runs for hours) may be a good tradeoff for the user
Anurag S.
OK
9:55 AM
Anurag S.
What about using https://github.com/zeroturnaround/zt-zip instead of ZipOutputStream?
Mark M.
I have not used it
the key for your case is the stream
to avoid OutOfMemoryErrors
Anurag S.
Sure
Mark M.
you would need to look at the code of that library to see whether ZipUtil.pack() and stuff use streams or not
probably, they do
in which case, that library has a nice convenient API
Anurag S.
I will try to implement and share the update or ask question if stuck.
Thankyou, Mark you have been of great help! Thankyou, for your precious time! Take care Mark :-)!
Mark M.
you're welcome!
Anurag S.
How to bookmark this conversation?
Mark M.
I will be posting the transcript to https://commonsware.com/office-hours/ shortly after the chat ends
so, there will be a 2018-04-09 entry there in a bit
Anurag S.
Ok Thank you :-)
10:00 AM
Mark M.
that's a wrap for today's chat
the next chat is tomorrow at 4pm US Eastern
have a pleasant day!
Anurag S.
has left the room
bill
has left the room
Mark M.
turned off guest access

Thursday, April 5

 

Office Hours

People in this transcript

  • Andre Rocha
  • Anurag Singh
  • bill
  • Mark Murphy