-
Notifications
You must be signed in to change notification settings - Fork 0
Lab 15
Let's implement a service to upload a Note
to a public GitHub
repo
-
Add an "Upload" button to
NoteDetailFragment
- When clicked, this button will eventually start our Service
-
Create a new package
upload
-
Within
upload
, create aNoteUploadService
that extendsService
and register it inAndroidManfiest.xml
-
Create a companion object in
NoteUploadService
-
Create a
getNoteUploadIntent(context: Context, note: Note): Intent
method- This should return an
Intent
for startingNoteUploadService
- This should add an extra for Base64 encoded "content" coming from the passed
Note
- This should add an extra for the filename to store the note as
- This should return an
-
Using
getNoteUploadIntent()
, start theService
fromNoteDetailFragment
when the upload button is clicked -
In
NoteUploadService.onStartCommand()
parse the filename, and contentString
s from theIntent
-
Build a
JsonObjectRequest
with Volley to create a new file, in GitHub, containing the contents of yourNote
- Should use a
PUT
- Url should be a url to some public repository that you own and include the desired filename as the final PATH param
- The
jsonRequest
body must include"message"
and"content"
values - Must include an
Authorization
header to authenticate the request - Add a new
BuildConfig
field namedGITHUB_ANDROID_WORKSHOP_TOKEN
that useas a PAT from GitHub to authenticate the request
- Should use a
-
Move request processing off the main thread
- Setup a
HandlerThread
inNoteUploadService.onCreate()
to manage requests off the mainThread
- Save a
Looper
reference - Create a
ServiceHandler
class to handleLooper
Messages
and upload file data - Create an instance of
ServiceHandler
within yourHandlerThread
on onCreate() - In
onStartCommand()
process incomingMessages
and send them to yourServiceHandler
to trigger upload
- Setup a
Add some form of user feedback to indicate that the upload was successful.
This could be as simple as a Toast
or Snackbar
.
- Add a unique id to each filename so that you can upload new copies of a
Note
- Use the GitHub api to acquire the SHA for an existing file so you can upload new versions of a file using the same filename
- Convert service to a Foreground
Service
- Display a notification while upload is in process
https://api.github.com/repos/<your username or org name>/<repo name>/contents/<name of file to upload>"
val requestQueue = Volley.newRequestQueue(this)
val body = JSONObject()
body.put("message", "<your desired commit message>") // commit message of file upload to GitHub
body.put("content", noteContent)
val request = object : JsonObjectRequest(
Method.PUT,
"<your upload url>",
body,
{
// handle success
},
{
// handle error
}
) {
override fun getHeaders(): MutableMap<String, String> {
return mutableMapOf(
"Authorization" to "token <your token>",
"Accept" to "application/vnd.github.v3+json"
)
}
}
requestQueue.add(request)
Android has some utility methods for Base64 encoding. We can leverage those.
val noteContent = note.title + "\n" + note.category + "\n" + note.content
return Base64.encodeToString(noteContent.toByteArray(), android.util.Base64.DEFAULT)
You might need to double check that you are authenticating your request.
Ensure that you're adding a valid GitHub access token as a header to your request.
To do that with Volley, override the getHeaders()
method of your JsonObjectRequest
override fun getHeaders(): MutableMap<String, String> {
return mutableMapOf(
"Authorization" to "token <your token here>"
)
}