Office Hours — Today, March 25

Yesterday, March 24

Mar 25
7:20 PM
Mark M.
has entered the room
Mark M.
turned on guest access
7:25 PM
EGHDK
has entered the room
7:30 PM
Mark M.
hello, EGHDK!
how can I help you today?
EGHDK
View paste
Hey Mark,
So i read the chapter you told me to about gradle and libs. Still had a couple of questions, I was hoping you would clarify.
Mark M.
go right ahead!
EGHDK
first, you mention closure a lot. Is that groovy specific for a block?
Mark M.
well, the term "closure" isn't unique to Groovy
and it's a trifle more involved than just a block
EGHDK
Okay. Just making sure. I just wasn't sure reading the chapter and you were mentioning this closure or that closure, and wasn't 100% on what you meant.
Mark M.
yeah, well, I probably should try to explain that term more in the book
though it's really a Groovy syntax thing
EGHDK
I guess in the context of that chapter, it was basically refering to it as a code block i guess right?
Mark M.
more or less
it's the stuff in { }
EGHDK
Okay, I just wasn't sure, because i hadn't heard it before really. I have heard of clojure and wasn't sure if it was the same or something.
Mark M.
I suspect that Clojure has closures, though I haven't really looked at it
7:35 PM
EGHDK
heh. Okay. cool. Next question.
in the root build.gradle we include com.android.tools:gradle:verision#. Is that the gradle dependency number or the Android plugin for gradle version?
Mark M.
the latter
so most of my samples use 1.0.0
EGHDK
Because gradle has a version number and so does the android plugin for gradle correct?
Mark M.
correct
EGHDK
Where is gradle version set?
Mark M.
that depends
for most projects, it is in gradle/wrapper/gradle-wrapper.properties, as part of the distributionUrl property
Raghu
has entered the room
Mark M.
hello, Raghu!
Raghu
hi mark
Mark M.
EGHDK: let me take a question from Raghu, and I will be back with you shortly
Raghu: your turn! do you have a question?
Raghu
yes
EGHDK
No problem. I'll start typing my other questions out.
7:40 PM
Raghu
as i told u yesturday i am using a intent service to start download and upload in a queue
maximum limit for a queue is 6
i have maintin queue of size 6
upload is happening one by one
like in google playstore
when we download updates for apps in google play store
they happen in a queue
when we want cancel a particular item we can cancel the download
i wanted to implement similar to google play stores way of downloading updates
for each app
Mark M.
OK... but what is your question?
Raghu
wat is the best way to download in queue,maintain a queue and cancel or remove items from a queue
7:45 PM
Mark M.
I would start by not using IntentService
Raghu
ok
Mark M.
use a regular Service
Raghu
ok
Mark M.
use a LinkedBlockingQueue or PriorityQueue, depending on whether you need priority-based sorting or something
Raghu
ok
its easy to remove items in linkedblocking queue,,?
Mark M.
onStartCommand() would look at the Intent to see if it is a request to add a job or cancel an existing job
Raghu
ohh ok
Mark M.
LinkedBlockingQueue has a remove() method
Raghu
linked blocking queue should be used in service
Mark M.
for your purposes, yes
onStartCommand() would then either put a job in the queue or find a job (based on some criteria, like an ID) and remove it
Raghu
ok
how do i update the ui
Mark M.
then, use an event bus (e.g., greenrobot's EventBus) to post events related to the jobs, such as completion status
Raghu
ok
how do i send the request from the queue
iterate through the queue
service will start a thread..?
Mark M.
you use the LinkedBlockingQueue with a ThreadPoolExecutor, with just one thread (since you want only one download at a time)
Raghu
yes exactly
Mark M.
if you need this queue to survive process termination, either use Square's Tape library or come up with your own persistence model
Raghu
ok
Mark M.
and with that, let me take a question from EGHDK, and I will be back with you in a bit
EGHDK: your turn! do you have a question?
Raghu
sure
EGHDK
just to piggyback here. How do you know of all the best collections for a specific task. just reading docs? or any good collections book?, second what if you didn't want to use an event bus?
7:50 PM
Mark M.
I'm sure that I don't know all the best collections for a specific task
I have my go-to set of collection classes, ones that usually fit the way my brain works from a design standpoint
and you're either going to use an event bus or you are going to write your own event bus equivalent from scratch, which seems like a waste
EGHDK
would that revolve around broadcastrecivers?
Mark M.
there aren't great options for updating a UI from a service's thread without eventually getting to an event bus of some form
LocalBroadcastManager is one event bus implementation
it's not the one I'd recommend in general, but it does qualify
EGHDK
Oh. LocalBroadcastManager IS an event bus
Mark M.
yes
EGHDK
didn't know that.
Mark M.
we used to use system broadcasts for that role, which was bad bad bad bad bad
and so they gave us LocalBroadcastManager as a way to easily migrate from the bad stuff to a better approach
but, as event bus implementations go, it's not the most feature-rich and it's a bit clunky to code to
EGHDK
Interesting that when they built the platform that they didn't build LocalBroadcastManager then... huh?
or that they didn't put in a good way to update UI
Mark M.
there are lots of things that weren't in Android 1.0
EGHDK
True. That's why its a 1.0
Mark M.
oh, there are lower-level things, like the Handler/Messenger combination
EGHDK
Anyway. Good question Raghu
Okay back to my actual questions. Will I ever have to update the gradle version...? or only if the android gradle plugin updates and uses something that my current gradle version doesn't know about.
Mark M.
"Will I ever have to update the gradle version...?" -- someday, probably
"or only if the android gradle plugin updates and uses something that my current gradle version doesn't know about" -- yes
EGHDK
You can go back to raghu after that. I know I kinda took up my time with event bus/collections. I'll keep jotting down my other questions.
Mark M.
that has Google's plans for the relationship between Gradle version and Gradle for Android version
OK
Raghu: back to you -- do you have another question?
Raghu
sorry i have more queries on first question
7:55 PM
Raghu
is that fine mark
Mark M.
sure
Raghu
service processes one request(using a thread pool) and we use event bus
to update
how will i start the next download
1,2,3,4,5
1 completes
how do i move to 2
2 to 3
Mark M.
you put 1, 2, 3, 4, 5 in the queue
Raghu
ok
Mark M.
everything else, we already discussed
you use ScheduledExecutorService with the LinkedBlockingQueue
it gives you control to do the download
Raghu
ok
Mark M.
if you put 1, 2, 3, 4, 5 in the queue, it will give you control five times, to do 1, 2, 3, 4, 5
Raghu
yes
activity will have to trigger in the response or scheduleexecutor knows that there are more items
to process
right
Mark M.
activity will call startService() to send the request to do the download to the service
service gets control in onStartCommand(), puts an item in the work queue
when the item comes to the head of the queue, the thread from the ScheduledExecutorService gives you control to do the work
and part of that work can be posting events on an event bus, or using a Messenger, or whatever, to update your UI
8:00 PM
Raghu
this thread is responsible for sending and handling the response
and also updating the Ui
Mark M.
this thread is responsible for doing whatever background work you wanted to have done via the queue
Raghu
yes
Mark M.
and this thread is responsible for notifying the UI (event bus, Messenger, whatever) about anything the UI needs to know about
Raghu
thanks mark
i am clear
Mark M.
let me take a question from EGHDK, and I will be back with you in a bit
EGHDK: back to you!
EGHDK
Okay, tell me if I'm correct. Maven Central is a remote repository that holds thousands of artifacts. Apache Maven is a build system. Maven is a type of way to set up an artifact.
Mark M.
the first two are solid
I'd phrase the third one more as "Maven Central implements what is known as a Maven-style repository for artifacts, with specific rules for metadata formats, directory structures, etc."
there are other artifact repository formats in the Java world (e.g., Apache Ivy), though Maven has largely won out in the marketplace of implementations
EGHDK
So can I replace "Maven Central" in that statement with JCenter?
Mark M.
yes
EGHDK
I'm just confused as of why https://bintray.com/bintray/jcenter shows a "Maven" logo right on that page.
8:05 PM
Mark M.
JCenter is a Maven-style repository
"JCenter is the place to find and share popular Apache Maven packages for use by Maven, Gradle, Ivy, SBT, etc. "
an "Apache Maven package" is the combination of the actual artifact itself (e.g., a JAR) and some metadata about the artifact (a POM file)
EGHDK
okay, so maven central is crystal clear now... but is apache maven a build system that can build apache maven packages?
Mark M.
Apache Maven is a build system in general
same basic roles as Apache Ant, Gradle, etc.
they popularized the notion of artifacts and repositories in the Java space
their build system itself is reasonably popular, and much more powerful than is Ant
EGHDK
gotcha. okay. that helps a bit. thanks
Mark M.
that's one of the problems in this area: "Maven" is used for lots of related but disparate things
let me take another question from Raghu, and I will be back with you shortly
Raghu: your turn! do you have another question?
Raghu
no mark thanks for the solution
Mark M.
OK
if you come up with another question before the chat ends, let me know
Raghu
sure
Mark M.
EGHDK: over to you!
EGHDK
In your book you mention if you want to use maven central you can add it in your app module build.gradle. And your example does just that. But why wouldn't you just put that right under where jcenter() is entered by default?
Mark M.
you usually don't need both jcenter() and mavenCentral()
8:10 PM
Mark M.
you need one or the other
in a new Android Studio project, it will be set up with jcenter()
that didn't use to be the case
earlier versions of Android Studio used mavenCentral()
they switched around the time of Android Studio 1.0, or perhaps a bit before
so you'll see bits of both in the book samples, but more mavenCentral(), due to the timing of when I did the work
EGHDK
gotcha. But lets say one dependency I want is on maven central and not bin tray... I could put that in just the root build.gradle then right?
Mark M.
yes
you might consider replacing jcenter() with mavenCentral(), though, rather than having both
EGHDK
cool.
Back to something that got my interest. event bus.
So I don't use services that often, so I don't know how I would communicate back to an activity, thats why Im asking these next questions.
I thought services could be bound or unbound (i was taking that as "bound" or "unbound" to the gui)
So you said localBroadCast is something you can use right now without any adiitional library right?
8:15 PM
Mark M.
well, LocalBroadcastManager is in the Android Support package
so you would need support-v4 or support-v13
EGHDK
You said that before that you would use System Broadcasts?
Mark M.
back in the 2009-2011 timeframe, that was one approach we erroneously used
EGHDK
How did you use a system broadcast? aren't system broadcasts for like low battery and phone call and boot complete and stuff?
Why erroneous?
Mark M.
when I use the term "system broadcast", I mean methods like sendBroadcast() on Context, where the receivers could be in any app
in contrast to a "local broadcast", where sendBroadcast() is on LocalBroadcastManager
system broadcasts are a cross-process event bus
however, from a security and performance standpoint, never use a cross-process X when you can achieve the same results using an in-process X
and I, and others, weren't thinking of those implications back then
we were young(er) and stupid
EGHDK
Gotcha. So security and performance.
Mark M.
right
EGHDK
Makes sense. Don't want any app to catch your broadcast and do something you didn't want.
Mark M.
now, if you need a cross-process event bus, system broadcasts are your best option, but you just have to work out the security ramifications
right
Raghu
can i ask a last question on linkedblockqueue mapping with the Ui(activity).
Mark M.
Raghu: go ahead
8:20 PM
Raghu
I wanted to cancel nth item(click on cancel from UI),how do I map the clicked item with the linked blocking queue in service so that i can remove it from the queue
Mark M.
you need some sort of identifier
Raghu
yes
Mark M.
could be the URL of the download
or a generated ID
Raghu
this url is part of queue
in a obect
object
Mark M.
I was under the impression that your background work involved downloads, which is why I suggested a URL
but it would be part of the work that you would put into the queue
Raghu
yes exactly
Mark M.
ideally, those objects would be from some class (call it Job for now) that has a proper implementation of equals()
Raghu
we can find the item from the identifier
Mark M.
that does the comparison by ID
then, you can use remove() on a Job with the same ID to remove the real actual Job instance that is in the queue
Raghu
ok
job has equals and hashcode
Mark M.
remove(), contains(), etc. on LinkedBlockingQueue use equals(), rather than object identity, for finding things in the queue
right
so, the activity calls startService() with a "cancel this job" Intent, with the identifier
onStartCommand() creates the Job with that identifier and calls remove() on the LinkedBlockingQueue, thereby removing any Job for that identifier
8:25 PM
Mark M.
EGHDK: if you have other questions, go ahead, as we are running out of chat time
EGHDK
You mentioned a lower level event bus? Message and Handler?
Mark M.
it's not really an "event bus", per se, but it is a way for disparate components to communicate
I know you have worked with Handler, based on prior chats
you can create a Messenger tied to a Handler
Messenger can go in things like an Intent extra
so a service's client (e.g., activity) can have a Handler, create a Messenger, and pass that Messenger to the service
the service can hold onto that Messenger and send messages back to the client
doing this right requires some amount of garbage-collection bookkeeping
EGHDK
Okay, so it is pretty low level. Is that what these other "good" event buses are built off of?
Mark M.
in the end, they all wind up using a Handler
EGHDK
Even localBroadcastReceiver?
Mark M.
actually, that one might not
no, no, it should
Otto is the one that might not
it comes back to threading
LocalBroadcastManager always delivers events on the main application thread, and so it is using a Handler tied to that thread
EGHDK
Okay, I'll look into it. I have to start using services. At this point, I don't see where I would use an event bus except service to activity interaction
Mark M.
BroadcastReceiver to UI interaction too
particularly for OS-initiated broadcasts that you have a manifest-registered receiver for
8:30 PM
EGHDK
Main application thread has a single handler... or is it an Activity that has its own handler?
Mark M.
Handlers are tied to threads
EGHDK
or both... I guess?
Mark M.
there can be N Handler instances associated with the main application thread
an activity usually does not need more than one
and that's a wrap for today's chat
EGHDK
Gotcha. Okay, cool I will read about them. Super interesting stuff here.
Mark M.
the transcript will go up on http://commonsware.com/office-hours/ shortly
EGHDK
appreciate it mark. Till next chat! Thanks
Mark M.
the next chat is Tuesday, also at 7:30pm
have a pleasant day!
EGHDK
has left the room
Raghu
has left the room
Mark M.
turned off guest access

Yesterday, March 24

 

Office Hours

People in this transcript

  • EGHDK
  • Mark Murphy
  • Raghu