New app version based on native Kotlin implementation

master
Fabio Manganiello 2 years ago
parent 691511f5ac
commit 78f5b73bf2
  1. 34
      .gitignore
  2. 10
      CHANGELOG.md
  3. 1
      app/.gitignore
  4. 46
      app/build.gradle
  5. 21
      app/proguard-rules.pro
  6. 26
      app/src/main/AndroidManifest.xml
  7. 292
      app/src/main/assets/web/css/style.css
  8. 0
      app/src/main/assets/web/icon/icon.png
  9. 0
      app/src/main/assets/web/icon/plus.svg
  10. 1
      app/src/main/assets/web/img/icon.png
  11. BIN
      app/src/main/assets/web/img/trash.png
  12. 101
      app/src/main/assets/web/index.html
  13. 167
      app/src/main/assets/web/js/main.js
  14. 0
      app/src/main/assets/web/js/vue.min.js
  15. BIN
      app/src/main/ic_launcher-playstore.png
  16. 54
      app/src/main/java/tech/platypush/platypush/JSON.kt
  17. 30
      app/src/main/java/tech/platypush/platypush/MainActivity.kt
  18. 249
      app/src/main/java/tech/platypush/platypush/Services.kt
  19. 67
      app/src/main/java/tech/platypush/platypush/WebAppInterface.kt
  20. 27
      app/src/main/java/tech/platypush/platypush/WebView.kt
  21. 26
      app/src/main/res/drawable/ic_launcher_foreground.xml
  22. 20
      app/src/main/res/layout/activity_main.xml
  23. 5
      app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml
  24. 5
      app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml
  25. BIN
      app/src/main/res/mipmap-hdpi/ic_launcher.png
  26. BIN
      app/src/main/res/mipmap-hdpi/ic_launcher_round.png
  27. BIN
      app/src/main/res/mipmap-mdpi/ic_launcher.png
  28. BIN
      app/src/main/res/mipmap-mdpi/ic_launcher_round.png
  29. BIN
      app/src/main/res/mipmap-xhdpi/ic_launcher.png
  30. BIN
      app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
  31. BIN
      app/src/main/res/mipmap-xxhdpi/ic_launcher.png
  32. BIN
      app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
  33. BIN
      app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
  34. BIN
      app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
  35. 4
      app/src/main/res/navigation/nav_graph.xml
  36. 16
      app/src/main/res/values-night/themes.xml
  37. 10
      app/src/main/res/values/colors.xml
  38. 3
      app/src/main/res/values/dimens.xml
  39. 4
      app/src/main/res/values/ic_launcher_background.xml
  40. 6
      app/src/main/res/values/strings.xml
  41. 25
      app/src/main/res/values/themes.xml
  42. 121
      assets/css/style.css
  43. BIN
      assets/icon/courses-icon-10.png
  44. BIN
      assets/icon/ic_launcher.png
  45. 306
      assets/js/androidjs.js
  46. 26
      build.gradle
  47. 3
      fastlane/metadata/android/en-US/changelogs/1000100.txt
  48. 21
      gradle.properties
  49. BIN
      gradle/wrapper/gradle-wrapper.jar
  50. 6
      gradle/wrapper/gradle-wrapper.properties
  51. 172
      gradlew
  52. 84
      gradlew.bat
  53. 21
      main.js
  54. 501
      package-lock.json
  55. 30
      package.json
  56. 2
      settings.gradle
  57. 96
      views/index.html

34
.gitignore vendored

@ -1,24 +1,12 @@
*.iml
.gradle
/local.properties
/.idea
.DS_Store
node_modules
/dist
/npm
# local env files
.env.local
.env.*.local
# Log files
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# Editor directories and files
.idea
.vscode
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
/build
/captures
.externalNativeBuild
.cxx
local.properties
/platypush/build
/app/release

@ -1,7 +1,15 @@
# Changelog
All notable changes to this project will be documented in this file.
## [Unreleased]
## [1.0.1] - 2021-04-28
### Added
- Support for saved/favourite hosts and services.
### Changed
- App migrated from AndroidJS to native Kotlin+webview (and APK size dropped to ~4MB).
### Fixed
- Improved speed and stability of services scan.
## [1.0.0] - 2021-02-26
### Added

1
app/.gitignore vendored

@ -0,0 +1 @@
/build

@ -0,0 +1,46 @@
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
}
android {
compileSdkVersion 29
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "tech.platypush.platypush"
minSdkVersion 23
targetSdkVersion 29
versionCode 1000100
versionName "1.0.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tech.platypush.platypush">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Platypush"
android:usesCleartextTraffic="true">
<activity
android:name="tech.platypush.platypush.MainActivity"
android:label="@string/app_name"
android:theme="@style/Theme.Platypush.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

@ -0,0 +1,292 @@
html, body {
width: 100%;
height: 100%;
margin: 0;
}
#app {
width: 100%;
height: 100%;
background: #e0eae8;
font-size: 20px;
display: flex;
justify-content: center;
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
color: #2c3e50;
}
.services {
width: 60%;
height: max-content;
min-width: 17em;
max-width: 22.5em;
background: white;
margin-top: 2em;
box-shadow: 1px 1px 2px 2px #bbb;
border-radius: 1.5em;
}
.services h3 {
width: 100%;
text-align: center;
}
.no-items {
text-align: center;
padding: 2em 1em;
}
.service {
width: 100%;
padding: 1em .5em;
border-bottom: 1px solid #ccc;
cursor: pointer;
position: relative;
}
.service:first-child {
border-radius: 1.5em 1.5em 0 0;
}
.service:last-child {
border-radius: 0 0 1.5em 1.5em;
}
.service:hover {
background: #bef6da;
}
.service .remove {
position: absolute;
right: 1.5em;
}
.service .remove img {
width: 1.2em;
height: 1.2em;
}
.name {
font-weight: bold;
}
.add-btn {
width: 2.5em;
height: 2.5em;
background: url('../icon/plus.svg');
position: fixed;
bottom: 1em;
right: 1em;
cursor: pointer;
}
.add-btn:hover {
opacity: .75;
}
/* Add host modal */
.add-modal-container {
width: 100%;
height: 100%;
position: fixed;
display: flex;
justify-content: center;
align-items: center;
z-index: 2;
}
.add-modal-background {
width: 100%;
height: 100%;
background: black;
position: absolute;
opacity: .87;
}
.add-modal {
width: 80%;
height: max-content;
max-width: 22.5em;
background: #f0f0f0;
display: flex;
flex-direction: column;
font-size: 1.2em;
padding: 1em;
border-radius: 1em;
z-index: 3;
}
.add-modal .header {
display: flex;
align-items: center;
justify-content: center;
padding-bottom: .5em;
}
.add-modal form,
.add-modal form label {
width: 100%;
}
.add-modal form input {
border-radius: .25em;
padding: .25em;
}
.add-modal form input[type=text],
.add-modal form input[type=number] {
width: 95%;
font-size: 1.05em;
margin-bottom: .5em;
}
.add-modal form input[type=submit] {
font-size: 1.05em;
}
.add-modal .buttons {
width: 100%;
display: flex;
align-items: center;
}
.add-modal .buttons .button {
width: 50%;
}
/* Splash screen */
.splash {
width: 100vw;
height: 100vh;
position: absolute;
background: white;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
font-size: 1.2em;
z-index: 1000;
}
.splash .icon {
width: 150px;
height: 150px;
display: flex;
margin-bottom: .5em;
}
.splash .icon img {
width: 100%;
height: 100%;
}
/* Fade in/out animations */
.fade-in {
animation-duration: 0.5s;
-webkit-animation-duration: 0.5s;
animation-fill-mode: both;
animation-name: fadeIn;
-webkit-animation-name: fadeIn;
}
.fade-out {
animation-duration: 0.5s;
-webkit-animation-duration: 0.5s;
animation-fill-mode: both;
animation-name: fadeOut;
-webkit-animation-name: fadeOut;
}
@keyframes fadeIn {
0% {opacity: 0;}
100% {opacity: 1;}
}
@keyframes fadeOut {
0% {opacity: 1;}
100% {
opacity: 0;
display: none;
}
}
/* Loading animation */
.loading {
display: flex;
align-items: center;
justify-content: center;
font-size: 3em;
position: absolute;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
background: #909090;
opacity: 0.5;
}
.icon {
display: inline-block;
position: relative;
width: 80px;
height: 80px;
}
.icon div {
position: absolute;
top: 33px;
width: 13px;
height: 13px;
border-radius: 50%;
background: #fff;
animation-timing-function: cubic-bezier(0, 1, 1, 0);
}
.icon div:nth-child(1) {
left: 8px;
animation: lds-ellipsis1 0.6s infinite;
}
.icon div:nth-child(2) {
left: 8px;
animation: lds-ellipsis2 0.6s infinite;
}
.icon div:nth-child(3) {
left: 32px;
animation: lds-ellipsis2 0.6s infinite;
}
.icon div:nth-child(4) {
left: 56px;
animation: lds-ellipsis3 0.6s infinite;
}
@keyframes lds-ellipsis1 {
0% {
transform: scale(0);
}
100% {
transform: scale(1);
}
}
@keyframes lds-ellipsis3 {
0% {
transform: scale(1);
}
100% {
transform: scale(0);
}
}
@keyframes lds-ellipsis2 {
0% {
transform: translate(0, 0);
}
100% {
transform: translate(24px, 0);
}
}

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

Before

Width:  |  Height:  |  Size: 1.0 KiB

After

Width:  |  Height:  |  Size: 1.0 KiB

@ -0,0 +1 @@
../../../res/mipmap-xxxhdpi/ic_launcher.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB

@ -0,0 +1,101 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Platypush</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript" src="js/vue.min.js"></script>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="app">
<div class="splash" v-if="splash">
<div class="icon">
<img src="img/icon.png" alt="">
</div>
<div class="app-name">
Platypush
</div>
</div>
<div class="loading" v-if="loading">
<div class="icon">
<div v-for="n in 4" :key="n"></div>
</div>
</div>
<div class="services">
<div class="saved" v-if="services.saved?.length">
<h3>Saved services</h3>
<div class="service" v-for="(service, i) in services.saved" :key="i"
@click="onServiceClick(service)">
<span class="name" v-text="service.name" v-if="service.name"></span>
<span v-if="service.name"> on </span>
<span class="address" v-text="service.host"></span>:<span class="port" v-text="service.port"></span>
<span class="remove" @click="removeService(i, $event)">
<img src="img/trash.png" alt="Remove">
</span>
</div>
</div>
<div class="scanned">
<h3>Scanned services</h3>
<div class="no-items" v-if="!services.scanned?.length">
<div class="empty">No Platypush web services found on the network</div>
</div>
<div class="service" v-for="(service, i) in services.scanned" :key="i"
@click="onServiceClick(service)">
<span class="name" v-text="service.name" v-if="service.name"></span>
<span v-if="service.name"> on </span>
<span class="address" v-text="service.host"></span>:<span class="port" v-text="service.port"></span>
</div>
</div>
</div>
<div class="add-modal-container" @click="addModal.visible = false" v-if="addModal.visible">
<div class="add-modal-background"></div>
<div class="add-modal" @click="$event.stopPropagation()">
<div class="header">
Connect to a Platypush web service
</div>
<div class="body">
<form @submit.prevent="onServiceConnect">
<label>
<input type="text" placeholder="IP or hostname" v-model="addModal.host">
</label>
<label>
<input type="number" placeholder="Port" v-model="addModal.port">
</label>
<label v-if="addModal.save">
<input type="text" placeholder="Name" v-model="addModal.name">
</label>
<div class="buttons">
<div class="button">
<input type="submit" value="Connect">
</div>
<div class="button">
<label>
Remember this host
<input type="checkbox" v-model="addModal.save">
</label>
</div>
</div>
</form>
</div>
</div>
</div>
<div class="add-btn" @click="addModal.visible = true"></div>
</div>
<script type="text/javascript" src="js/main.js"></script>
</body>
</html>

@ -0,0 +1,167 @@
new Vue({
el: '#app',
data: function() {
return {
loading: false,
splash: false,
services: {
scanned: [],
saved: [],
},
addModal: {
visible: false,
host: undefined,
port: 8008,
name: undefined,
save: false,
},
}
},
computed: {
servicesByName: function() {
return Object.values(this.services).reduce((obj, services) =>
services.reduce((obj2, srv) => {
obj2[srv.name] = srv
return obj2
}, obj), {})
},
servicesByHostAndPort: function() {
return Object.values(this.services).reduce((obj, services) =>
services.reduce((obj2, srv) => {
obj2[`${srv.host}:${srv.port}`] = srv
return obj2
}, obj), {})
},
},
methods: {
refresh: function() {
this.services.scanned = [
...this.services.scanned,
...JSON.parse(app.pollServices()).filter((srv) => !(srv.name in this.servicesByName)),
]
},
onServiceClick: function(service) {
this.connect(service.host, service.port)
},
onServiceConnect: function() {
if (!this.addModal.host?.length) {
app.alert('Please specify a host name or IP address')
return
}
if (this.addModal.save) {
if (!this.saveService(this.addModal.host, this.addModal.port, this.addModal.name || ''))
return
}
this.connect(this.addModal.host, this.addModal.port)
},
saveService: function(host, port, name) {
name = this.addModal.name.trim()
if (!name.length) {
app.alert('Please specify a name')
return false
}
let overwrite = false
if (name in this.servicesByName) {
if (!confirm(`A service named ${name} already exists. ` +
`Do you want to overwrite it?`))
return false
overwrite = true
} else if (`${host}:${port}` in this.servicesByHostAndPort) {
if (!confirm(`A service on ${host}:${port} already exists. ` +
`Do you want to overwrite it?`))
return false
overwrite = true
}
this.loading = true
try {
const rs = app.saveService(host, port, name, overwrite)
if (rs?.error)
throw rs.error
this.loadServices()
return true
} finally {
this.loading = false
}
},
removeService: function(savedIndex, event) {
event.stopPropagation()
const srv = this.services.saved[savedIndex]
if (!(srv && confirm('Are you sure that you want to remove this service?')))
return false
this.loading = true
try {
const rs = app.removeService(srv.host, srv.port, srv.name)
if (rs?.error)
throw rs.error
this.loadServices()
return true
} finally {
this.loading = false
}
},
connect: function(host, port) {
this.loading = true
app.stopServicesPoll()
window.location.href = `http://${host}:${port}/`
},
splashScreen: function(duration) {
const self = this
this.splash = true
setTimeout(() => {
self.splash = false
}, duration)
},
resetAddModal: function() {
this.addModal = {
host: undefined,
port: 8008,
name: undefined,
save: false,
}
},
loadServices: function() {
this.services = {
...this.services,
...JSON.parse(app.loadServices())
}
},
},
mounted: function() {
this.splashScreen(1500)
this.$watch(() => this.addModal.visible, (newValue) => {
if (!newValue)
this.resetAddModal()
})
this.$watch(() => this.addModal.save, (newValue) => {
if (newValue)
this.addModal.name = this.addModal.host
})
this.loadServices()
app.startServicesPoll()
setInterval(this.refresh, 500)
}
})

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

@ -0,0 +1,54 @@
package tech.platypush.platypush
import org.json.JSONArray
import org.json.JSONObject
import java.util.*
import kotlin.collections.HashMap
class JSON {
companion object {
private fun toMap(obj: JSONObject): Map<String, Any?> {
val ret = HashMap<String, Any?>()
val keys = obj.keys()
while (keys.hasNext()) {
val key = keys.next()
var value = obj[key]
if (value is JSONObject)
value = toMap(value)
else if (value is JSONArray)
value = toList(value)
ret[key] = value
}
return ret
}
private fun toList(arr: JSONArray): List<Any?> {
val ret = LinkedList<Any?>()
for (i in 0 until arr.length()) {
var value = arr[i]
if (value is JSONObject)
value = toMap(value)
else if (value is JSONArray)
value = toList(value)
ret.add(value)
}
return ret
}
fun load(str: String): Map<String, Any?> {
return toMap(JSONObject(str))
}
fun dump(obj: Map<String, Any?>): String {
return JSONObject(obj).toString()
}
}
}

@ -0,0 +1,30 @@
package tech.platypush.platypush
import android.annotation.SuppressLint
import android.os.Bundle
import android.webkit.WebChromeClient
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
@SuppressLint("SetJavaScriptEnabled")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
webview.webViewClient = WebView()
webview.webChromeClient = WebChromeClient()
webview.settings.javaScriptEnabled = true
webview.settings.javaScriptCanOpenWindowsAutomatically = true
webview.clearCache(true)
webview.addJavascriptInterface(WebAppInterface(this), "app")
webview.loadUrl("file:///android_asset/web/index.html")
}
override fun onBackPressed() {
if (webview != null && webview.canGoBack())
webview.goBack()
else
super.onBackPressed()
}
}

@ -0,0 +1,249 @@
package tech.platypush.platypush
import android.content.ContentValues.TAG
import android.content.Context
import android.net.nsd.NsdManager
import android.net.nsd.NsdServiceInfo
import android.util.Log
import java.io.File
import java.io.FileWriter
import java.lang.RuntimeException
import java.util.*
import java.util.concurrent.locks.ReentrantLock
import kotlin.concurrent.withLock
/**
* Service data class
*/
data class Service(val host: String, val port: Int, val name: String?) {
fun toMap(): Map<String, Any?> {
return mapOf(
"host" to host,
"port" to port,
"name" to name
)
}
override fun toString(): String {
return JSON.dump(toMap())
}
companion object {
fun load(obj: Map<String, Any?>): Service {
return Service(
host = obj["host"] as String,
port = obj["port"] as Int,
name = obj["name"] as String?
)
}
}
}
/**
* ZeroConf/Bonjour/mDNS event listener
*/
class Listener(private val scanner: Scanner): NsdManager.DiscoveryListener {
val serviceType = "_platypush-http._tcp."
override fun onDiscoveryStarted(regType: String) {
Log.d(TAG, "Service discovery started")
}
override fun onServiceFound(service: NsdServiceInfo) {
Log.d(TAG, "Service discovery succeeded: $service")
if (service.serviceType != serviceType) {
Log.d(TAG, "Unknown service type: ${service.serviceType}")
return
}
scanner.resolveService(service)
}
override fun onServiceLost(service: NsdServiceInfo) {
Log.w(TAG, "Service lost: $service")
}
override fun onDiscoveryStopped(serviceType: String) {
Log.i(TAG, "Discovery stopped: $serviceType")
}
override fun onStartDiscoveryFailed(serviceType: String, errorCode: Int) {
Log.e(TAG, "Discovery start failed: Error code: $errorCode")
scanner.stopScan()
}
override fun onStopDiscoveryFailed(serviceType: String, errorCode: Int) {
Log.e(TAG, "Discovery stop failed: Error code: $errorCode")
scanner.stopScan()
}
}
/**
* ZeroConf/Bonjour/mDNS service scanner and resolver
*/
class Scanner(context: Context) {
private val nsdManager = context.getSystemService(Context.NSD_SERVICE) as NsdManager
private val listener = Listener(this)
private val services = HashMap<Pair<String, Int>, Service>()
private val resolverLock = ReentrantLock()
fun startScan() {
nsdManager.discoverServices(listener.serviceType, NsdManager.PROTOCOL_DNS_SD, listener)
}
fun stopScan() {
nsdManager.stopServiceDiscovery(listener)
}
fun getServices(): Collection<Service> {
return services.values
}
fun resolveService(service: NsdServiceInfo) {
// Service resolution is a critical section
resolverLock.lock()
val scanner = this
nsdManager.resolveService(service, object : NsdManager.ResolveListener {
override fun onResolveFailed(serviceInfo: NsdServiceInfo, errorCode: Int) {
resolverLock.unlock()
val msg = "Resolve of service ${serviceInfo.serviceName} failed"
// Retry logic
when (errorCode) {
NsdManager.FAILURE_ALREADY_ACTIVE -> {
Thread.sleep(100)
scanner.resolveService(serviceInfo)
Log.w(TAG, "$msg: Resolver already active")
}
NsdManager.FAILURE_MAX_LIMIT -> {
Thread.sleep(5000)
scanner.resolveService(serviceInfo)
Log.e(TAG, "$msg: Maximum number of resolve requests reached")
}
NsdManager.FAILURE_INTERNAL_ERROR -> {
Log.e(TAG, "$msg: Internal error")
}
}
}
override fun onServiceResolved(serviceInfo: NsdServiceInfo) {
services[Pair(serviceInfo.host.hostAddress, serviceInfo.port)] = Service(
serviceInfo.host.hostAddress, serviceInfo.port, serviceInfo.serviceName)
resolverLock.unlock()
Log.i(TAG, "Resolve succeeded: $serviceInfo")
}
})
}
}
/**
* Services manager class
*/
class Manager(context: Context) {
private val servicesFile = File(context.filesDir, "services.json")
private val servicesFileLock = ReentrantLock()
private val emptyServices = mapOf<String, List<Service>>(
"saved" to emptyList(),
"lastConnected" to emptyList()
)
private fun initServicesFile(reset: Boolean = false) {
if (reset || !servicesFile.exists())
saveServices(emptyServices)
}
fun loadServices(): Map<String, List<Service>> {
initServicesFile()
var services: Map<String, List<Service>> = emptyServices
try {
services = deserializeServices(servicesFile.readText())
} catch (e: Exception) {
Log.e(TAG, "Error while parsing $servicesFile: resetting it", e)
initServicesFile(reset = true)
}
return services
}
private fun saveServices(services: Map<String, List<Service>>) {
servicesFileLock.withLock {
FileWriter(servicesFile).use {
it.write(serializeServices(services))
}
}
}
fun saveService(host: String, port: Int, name: String, overwrite: Boolean = false) {
val service = Service(host, port, name)
val services = this.loadServices().toMutableMap()
val savedServices = services["saved"]?.toMutableList() ?: ArrayList()
val dupIndex = getSavedServiceIndex(service, savedServices)
if (dupIndex != null && !overwrite)
throw DuplicateServiceException(service)
savedServices.add(service)
services["saved"] = savedServices
saveServices(services)
}
fun removeService(host: String? = null, port: Int? = null, name: String? = null) {
val service = Service(host=host ?: "", port=port ?: 0, name=name)
val services = this.loadServices().toMutableMap()
val savedServices = ArrayList<Service>(services["saved"] ?: emptyList<Service>())
val srvIndex = getSavedServiceIndex(service, savedServices)
?: throw NoSuchServiceException(host, port, name)
savedServices.removeAt(srvIndex)
services["saved"] = savedServices
saveServices(services)
}
private fun getSavedServiceIndex(service: Service, services: List<Service>): Int? {
val matchingSrvIndexes = services.indices.filter {
(services[it].host == service.host && services[it].port == service.port) ||
services[it].name == service.name
}
return if (matchingSrvIndexes.isNotEmpty()) matchingSrvIndexes[0] else null
}
fun serializeServices(services: Map<String, List<Service>>): String {
val parsedServices = HashMap<String, List<Map<String, Any?>>>()
for (srvType in listOf("saved", "lastConnected")) {
val outputList = LinkedList<Map<String, Any?>>()
for (srv in services[srvType] ?: emptyList())
outputList.add(srv.toMap())
parsedServices[srvType] = outputList.toList()
}
return JSON.dump(parsedServices)
}
private fun deserializeServices(servicesJson: String): Map<String, List<Service>> {
@Suppress("UNCHECKED_CAST")
val services = JSON.load(servicesJson) as Map<String, List<Map<String, Any?>>>
val parsedServices = HashMap<String, List<Service>>()
for (srvType in listOf("saved", "lastConnected")) {
val outputList = LinkedList<Service>()
for (srv in services[srvType] ?: emptyList())
outputList.add(Service.load(srv))
parsedServices[srvType] = outputList
}
return parsedServices
}
}
class DuplicateServiceException(service: Service) : RuntimeException("Duplicate service: $service")
class NoSuchServiceException(host: String? = null, port: Int? = null, name: String? = null):
RuntimeException("No such host: host=$host, port=$port, name=$name")

@ -0,0 +1,67 @@
package tech.platypush.platypush
import android.content.ContentValues.TAG
import android.content.Context
import android.util.Log
import android.webkit.JavascriptInterface
import org.json.JSONArray
import java.util.*
class WebAppInterface(context: Context) {
private val serviceScanner = Scanner(context)
private val serviceManager = Manager(context)
@Suppress("unused")
@JavascriptInterface
fun startServicesPoll() {
serviceScanner.startScan()
}
@Suppress("unused")
@JavascriptInterface
fun stopServicesPoll() {
serviceScanner.stopScan()
}
@Suppress("unused")
@JavascriptInterface
fun pollServices(): String {
val services = LinkedList<Map<String, Any?>>()
for (srv in serviceScanner.getServices())
services.add(srv.toMap())
return JSONArray(services).toString()
}
@Suppress("unused")
@JavascriptInterface
fun loadServices(): String {
return serviceManager.serializeServices(serviceManager.loadServices())
}
@Suppress("unused")
@JavascriptInterface
fun saveService(host: String, port: Int, name: String, overwrite: Boolean = false): String? {
try {
serviceManager.saveService(host, port, name, overwrite=overwrite)
} catch (e: Exception) {
Log.e(TAG, e.toString())
return JSON.dump(mapOf("error" to e.toString()))
}
return null
}
@Suppress("unused")
@JavascriptInterface
fun removeService(host: String, port: Int, name: String): String? {
try {
serviceManager.removeService(host, port, name)
} catch (e: Exception) {
Log.e(TAG, e.toString())
return JSON.dump(mapOf("error" to e.toString()))
}
return null
}
}

@ -0,0 +1,27 @@
package tech.platypush.platypush
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
class WebView : WebViewClient() {
private fun shouldOverrideUrlLoadingInner(view: WebView, url: String): Boolean {
view.loadUrl(url)
return true
}
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
if (view == null || request == null)
return super.shouldOverrideUrlLoading(view, request)
return this.shouldOverrideUrlLoadingInner(view, request.url.toString())
}
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
@Suppress("DEPRECATION")
if (view == null || url == null)
return super.shouldOverrideUrlLoading(view, url)
return this.shouldOverrideUrlLoadingInner(view, url)
}
}

@ -0,0 +1,26 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="108dp"
android:height="108dp"
android:viewportWidth="108"
android:viewportHeight="108">
<group android:scaleX="0.5016"
android:scaleY="0.5016"
android:translateX="28.92"
android:translateY="28.92">
<path
android:pathData="M0,50l0,-50 50,0 50,0 0,50 0,50 -50,0 -50,0 0,-50zM73.2,86.2c0.9,-1 0.6,-1.1 -1.6,-0.2 -4.8,1.8 -10.2,1.2 -13.3,-1.4 -2.6,-2.2 -2.8,-3 -2.8,-9.6 0,-9.2 1.8,-11 10.5,-11 7.8,0 10,2.5 10,11.4 0,3.4 -0.5,6.7 -1.2,7.4 -0.9,0.9 -0.4,0.9 2.3,0.2 1.8,-0.6 3.5,-1 3.7,-1 0.1,0 -0.5,-1.1 -1.4,-2.5 -1.2,-1.9 -1.5,-4.1 -1.2,-9l0.4,-6.5 -2.8,0c-3.4,0 -7.8,-2.6 -7.8,-4.6 0,-1.9 -1,-1.8 -2.5,0.2 -0.7,1 -3.2,1.9 -6.2,2.1l-5.1,0.5 -0.4,6.5c-0.3,4.8 -0.9,6.8 -2.1,7.5 -2.1,1.1 -2.2,2.8 -0.3,2.8 2.1,0 5.6,5.4 5.6,8.7 0,2.8 0.1,2.8 6.7,2.9 4.7,0.1 6.9,0.6 7.2,1.5 0.3,0.8 0.7,0.1 0.9,-1.6 0.2,-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 22.6,15.1 0,0.9 -13.8,10.6 -15.9,11.3 -0.3,0.1 -1.6,-0.2 -2.8,-0.7z"
android:fillColor="#60b64f"/>
</group>
</vector>

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_launcher_background"/>
<foreground android:drawable="@drawable/ic_launcher_foreground"/>
</adaptive-icon>

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_launcher_background"/>
<foreground android:drawable="@drawable/ic_launcher_foreground"/>
</adaptive-icon>

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/nav_graph">
</navigation>

@ -0,0 +1,16 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Platypush" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_200</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/black</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_200</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
</resources>

@ -0,0 +1,3 @@
<resources>
<dimen name="fab_margin">16dp</dimen>
</resources>

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="ic_launcher_background">#FFFFFF</color>
</resources>

@ -0,0 +1,6 @@
<resources>
<string name="app_name">Platypush</string>
<string name="action_settings">Settings</string>
<string name="next">Next</string>
<string name="previous">Previous</string>
</resources>

@ -0,0 +1,25 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Platypush" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color