Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Stefan Zabka
Companion App Android
Commits
eec5e7ba
Commit
eec5e7ba
authored
Aug 18, 2019
by
Anon
Browse files
Download app archive to temporary file.
parent
12d8ad1b
Changes
3
Hide whitespace changes
Inline
Side-by-side
app/src/main/java/de/ccc/events/badge/card10/hatchery/AppDetailFragment.kt
View file @
eec5e7ba
...
...
@@ -22,6 +22,7 @@
package
de.ccc.events.badge.card10.hatchery
import
android.os.AsyncTask
import
android.os.Bundle
import
android.view.LayoutInflater
import
android.view.View
...
...
@@ -29,7 +30,7 @@ import android.view.ViewGroup
import
androidx.fragment.app.Fragment
import
de.ccc.events.badge.card10.R
import
kotlinx.android.synthetic.main.app_detail_fragment.*
import
java.
lang.IllegalStateException
import
java.
io.File
class
AppDetailFragment
:
Fragment
()
{
...
...
@@ -51,5 +52,30 @@ class AppDetailFragment : Fragment() {
label_content_size
.
text
=
getString
(
R
.
string
.
app_detail_content_size
,
app
.
size_of_content
)
label_description
.
text
=
app
.
description
button_download
.
setOnClickListener
{
ReleaseDownload
(
app
).
execute
()
}
}
private
class
ReleaseDownload
(
private
val
app
:
App
)
:
AsyncTask
<
Void
,
Void
,
String
?>()
{
override
fun
doInBackground
(
vararg
p0
:
Void
?):
String
?
{
return
try
{
val
inputStream
=
HatcheryClient
().
openDownloadStream
(
app
)
val
file
=
File
.
createTempFile
(
app
.
slug
,
".tar.gz"
)
val
outputStream
=
file
.
outputStream
()
inputStream
.
copyTo
(
outputStream
)
file
.
absolutePath
}
catch
(
e
:
Exception
)
{
null
}
}
override
fun
onPostExecute
(
result
:
String
?)
{
// TODO: Handle result
}
}
}
app/src/main/java/de/ccc/events/badge/card10/hatchery/HatcheryClient.kt
View file @
eec5e7ba
...
...
@@ -29,6 +29,8 @@ import okhttp3.Request
import
okhttp3.Response
import
org.json.JSONArray
import
org.json.JSONException
import
org.json.JSONObject
import
java.io.InputStream
private
const
val
TAG
=
"HatcheryClient"
...
...
@@ -81,4 +83,57 @@ class HatcheryClient {
return
resultList
}
fun
getReleaseUrl
(
app
:
App
):
String
{
val
client
=
OkHttpClient
()
val
request
=
Request
.
Builder
()
.
url
(
"$HATCHERY_BASE_URL/eggs/get/${app.slug}/json"
)
.
build
()
val
response
:
Response
try
{
response
=
client
.
newCall
(
request
).
execute
()
}
catch
(
e
:
Exception
)
{
throw
HatcheryClientException
(
0
)
}
if
(
response
.
code
!=
200
)
{
throw
HatcheryClientException
(
response
.
code
)
}
val
body
=
response
.
body
?.
string
()
?:
""
try
{
val
responseJson
=
JSONObject
(
body
)
val
version
=
responseJson
.
getJSONObject
(
"info"
).
getString
(
"version"
)
return
responseJson
.
getJSONObject
(
"releases"
)
.
getJSONArray
(
version
).
getJSONObject
(
0
).
getString
(
"url"
)
}
catch
(
e
:
JSONException
)
{
Log
.
e
(
TAG
,
"Error parsing JSON: ${e.message}"
)
throw
HatcheryClientException
(
0
)
}
}
fun
openDownloadStream
(
app
:
App
):
InputStream
{
val
releaseUrl
=
getReleaseUrl
(
app
)
val
client
=
OkHttpClient
()
val
request
=
Request
.
Builder
()
.
url
(
releaseUrl
)
.
build
()
val
response
:
Response
try
{
response
=
client
.
newCall
(
request
).
execute
()
}
catch
(
e
:
Exception
)
{
throw
HatcheryClientException
(
0
)
}
if
(
response
.
code
!=
200
)
{
throw
HatcheryClientException
(
response
.
code
)
}
return
response
.
body
?.
byteStream
()
?:
throw
HatcheryClientException
(
0
)
}
}
app/src/main/res/layout/app_detail_fragment.xml
View file @
eec5e7ba
...
...
@@ -35,8 +35,7 @@
app:layout_constraintRight_toRightOf=
"parent"
app:layout_constraintTop_toTopOf=
"parent"
android:id=
"@+id/button_download"
android:text=
"@string/app_detail_button_download"
android:enabled=
"false"
/>
android:text=
"@string/app_detail_button_download"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment