Created
November 27, 2017 11:08
-
-
Save tralamazza/c5c29799577ebfd50f0b02f81a4bff8d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.matchx.lpwancontroller.api | |
import android.app.IntentService | |
import android.content.Intent | |
import android.support.v4.content.LocalBroadcastManager | |
import java.io.DataInputStream | |
import java.io.File | |
import java.io.FileOutputStream | |
import java.net.URL | |
/** | |
* Expects URL: | |
* @param URL_PARAM | |
* Uses LocalBroadcastManager to communicate download progress in bytes. | |
* @see PROGRESS_INTENT_ACTION | |
* @see PROGRESS_BYTES_PARAM | |
* Uses LocalBroadcastManager to communicate results. | |
* @see RESULT_INTENT_ACTION | |
* @see RESULT_FILENAME_PARAM all good, temporary filename | |
* @see RESULT_ERROR_PARAM something went wrong, exception object | |
* */ | |
class DownloadFileService : IntentService("DownloadFileService") { | |
val URL_PARAM = "url" | |
val PROGRESS_INTENT_ACTION = "DownloadFileService.progress" | |
val PROGRESS_BYTES_PARAM = "progress.bytes" | |
val RESULT_INTENT_ACTION = "DownloadFileService.result" | |
val RESULT_FILENAME_PARAM = "result.filename" | |
val RESULT_ERROR_PARAM = "result.error" | |
private val BUFFER_SIZE = 1024 | |
override fun onHandleIntent(intent: Intent?) { | |
if (intent == null) | |
return postException(Exception("Missing intent")) | |
if (!intent.hasExtra(URL_PARAM)) | |
return postException(Exception("Missing URL parameter")) | |
try { | |
downloadFile(intent.getStringExtra(URL_PARAM)) | |
} catch (e: Exception) { | |
postException(e) | |
} | |
} | |
private fun downloadFile(url: String) { | |
val tmpfile = File.createTempFile("download-", null) | |
FileOutputStream(tmpfile).use { ostream -> | |
DataInputStream(URL(url).openStream()).use { istream -> | |
val buffer = ByteArray(BUFFER_SIZE) | |
var bytesRead: Int | |
bytesRead = istream.read(buffer) | |
while (bytesRead != -1) { | |
ostream.write(buffer, 0, bytesRead) | |
postProgress(bytesRead) | |
bytesRead = istream.read(buffer) | |
} | |
postDone(tmpfile.canonicalPath) | |
} | |
} | |
} | |
private fun postException(e: Exception) { | |
val intent = Intent(RESULT_INTENT_ACTION) | |
intent.putExtra(RESULT_ERROR_PARAM, e) | |
LocalBroadcastManager.getInstance(this).sendBroadcast(intent) | |
} | |
private fun postProgress(bytes: Int) { | |
val intent = Intent(PROGRESS_INTENT_ACTION) | |
intent.putExtra(PROGRESS_BYTES_PARAM, bytes) | |
LocalBroadcastManager.getInstance(this).sendBroadcast(intent) | |
} | |
private fun postDone(filename: String) { | |
val intent = Intent(RESULT_INTENT_ACTION) | |
intent.putExtra(RESULT_FILENAME_PARAM, filename) | |
LocalBroadcastManager.getInstance(this).sendBroadcast(intent) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment