Skip to content

Commit

Permalink
Update pin request page
Browse files Browse the repository at this point in the history
  • Loading branch information
dkrivoruchko committed Aug 26, 2017
1 parent 7366e52 commit 7af954d
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 7 deletions.
Binary file added app/src/main/assets/logo_big.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 3 additions & 6 deletions app/src/main/assets/pinrequest.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
<head>
<meta charset=utf-8>
<title>Screen Stream</title>
<style>
body,html{height:80%}html{display:table;margin:auto;font-family:sans-serif}body{display:table-cell;vertical-align:middle;background:#EEE;text-align:center}
</style>
<style>body,html{height:80%}html{display:table;margin:auto;color:#FFF;font-family:sans-serif;font-size:larger}body{display:table-cell;vertical-align:middle;background:#455a64;text-align:center}</style>
</head>
<body><p>STREAM_REQUIRE_PIN</p>
<body><img src="logo_big.png" height="256" width="256"/><p>STREAM_REQUIRE_PIN</p>
<form action=/ autocomplete=off>ENTER_PIN
<input autofocus maxlength=4 name=pin pattern=[0-9]{4} required size=4 title="FOUR_DIGITS"
type=password>
<input autofocus maxlength=4 name=pin pattern=[0-9]{4} required size=4 title="FOUR_DIGITS" type=password>
<button type=submit>SUBMIT_TEXT</button>
</form>
<p style=color:red;font-weight:700>WRONG_PIN_MESSAGE</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface HttpServer {
const val DEFAULT_STREAM_ADDRESS = "/stream.mjpeg"
const val DEFAULT_ICON_ADDRESS = "/favicon.ico"
const val DEFAULT_PIN_ADDRESS = "/?pin="
const val DEFAULT_LOGO_ADDRESS = "/logo_big.png"
}

// Clients. Open for HttpServerImpl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import kotlin.collections.HashMap

class HttpServerImpl constructor(serverAddress: InetSocketAddress,
favicon: ByteArray,
logo: ByteArray,
baseIndexHtml: String,
backgroundColor: Int,
disableMJpegCheck: Boolean,
Expand Down Expand Up @@ -139,6 +140,7 @@ class HttpServerImpl constructor(serverAddress: InetSocketAddress,

httpServerRxHandler = HttpServerRxHandler(
favicon,
logo,
indexHtmlPage,
pinEnabled,
pinAddress,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import java.net.InetSocketAddress
import java.util.concurrent.Executors

internal class HttpServerRxHandler(private val favicon: ByteArray,
private val logo: ByteArray,
private val indexHtmlPage: String,
private val pinEnabled: Boolean,
private val pinAddress: String,
Expand Down Expand Up @@ -70,6 +71,7 @@ internal class HttpServerRxHandler(private val favicon: ByteArray,
if (BuildConfig.DEBUG_MODE) println(TAG + ": Thread [${Thread.currentThread().name}] Priority: ${Thread.currentThread().priority} HttpServerRxHandler: Handle: $uri")
when {
uri == HttpServer.DEFAULT_ICON_ADDRESS -> return sendFavicon(response)
uri == HttpServer.DEFAULT_LOGO_ADDRESS -> return sendLogo(response)
uri == HttpServer.DEFAULT_HTML_ADDRESS -> return if (pinEnabled) sendHtml(response, pinRequestHtmlPage) else sendHtml(response, indexHtmlPage)
uri == pinAddress && pinEnabled -> return sendHtml(response, indexHtmlPage)
uri.startsWith(HttpServer.DEFAULT_PIN_ADDRESS) && pinEnabled -> return sendHtml(response, pinRequestErrorHtmlPage)
Expand All @@ -95,6 +97,15 @@ internal class HttpServerRxHandler(private val favicon: ByteArray,
return response.writeBytesAndFlushOnEach(Observable.just(favicon))
}

private fun sendLogo(response: HttpServerResponse<ByteBuf>): Observable<Void> {
response.status = HttpResponseStatus.OK
response.addHeader(HttpHeaderNames.CONTENT_TYPE, "image/png")
response.setHeader(HttpHeaderNames.CACHE_CONTROL, "no-cache,no-store,max-age=0,must-revalidate")
response.setHeader(HttpHeaderNames.CONTENT_LENGTH, Integer.toString(logo.size))
response.setHeader(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE)
return response.writeBytesAndFlushOnEach(Observable.just(logo))
}

private fun sendHtml(response: HttpServerResponse<ByteBuf>, html: String): Observable<Void> {
response.status = HttpResponseStatus.OK
response.addHeader(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=UTF-8")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ class ForegroundServicePresenter @Inject internal constructor(private val settin
}

is ForegroundServiceView.FromEvent.StartHttpServer -> {
val (serverAddress, favicon, baseIndexHtml, basePinRequestHtml, pinRequestErrorMsg, jpegByteStream) = fromEvent
val (serverAddress, favicon, logo, baseIndexHtml, basePinRequestHtml, pinRequestErrorMsg, jpegByteStream) = fromEvent
globalStatus.error.set(null)
httpServer = HttpServerImpl(serverAddress,
favicon,
logo,
baseIndexHtml,
settings.htmlBackColor,
settings.disableMJPEGCheck,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class ForegroundService : Service(), ForegroundServiceView {

// Base values
private lateinit var baseFavicon: ByteArray
private lateinit var baseLogo: ByteArray
private lateinit var baseIndexHtml: String
private lateinit var basePinRequestHtml: String
private lateinit var pinRequestErrorMsg: String
Expand Down Expand Up @@ -134,6 +135,7 @@ class ForegroundService : Service(), ForegroundServiceView {
when (event) {
is ForegroundService.LocalEvent.StartService -> {
baseFavicon = getFavicon(applicationContext)
baseLogo = getLogo(applicationContext)
baseIndexHtml = getBaseIndexHtml(applicationContext)
basePinRequestHtml = getBasePinRequestHtml(applicationContext)
pinRequestErrorMsg = applicationContext.getString(R.string.html_wrong_pin)
Expand Down Expand Up @@ -162,6 +164,7 @@ class ForegroundService : Service(), ForegroundServiceView {
fromEvents.onNext(ForegroundServiceView.FromEvent.StartHttpServer(
serverAddress,
baseFavicon,
baseLogo,
baseIndexHtml,
basePinRequestHtml,
pinRequestErrorMsg,
Expand Down Expand Up @@ -417,6 +420,12 @@ class ForegroundService : Service(), ForegroundServiceView {
return iconBytes
}

private fun getLogo(context: Context): ByteArray {
val logoBytes = getFileFromAssets(context, "logo_big.png")
if (logoBytes.isEmpty()) throw IllegalStateException("logo_big.png is empty")
return logoBytes
}

private fun getBaseIndexHtml(context: Context): String {
val htmlBytes = getFileFromAssets(context, "index.html")
if (htmlBytes.isEmpty()) throw IllegalStateException("index.html is empty")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface ForegroundServiceView {
@Keep class Init : FromEvent()
@Keep data class StartHttpServer(val serverAddress: InetSocketAddress,
val favicon: ByteArray,
val logo: ByteArray,
val baseIndexHtml: String,
val basePinRequestHtml: String,
val pinRequestErrorMsg: String,
Expand Down

0 comments on commit 7af954d

Please sign in to comment.