Skip to content

Commit

Permalink
Merge pull request #58 from umer0586/web-server-integration
Browse files Browse the repository at this point in the history
Web server integration
  • Loading branch information
umer0586 authored Jun 26, 2024
2 parents ccc81e4 + 5a70c3d commit 32e043a
Show file tree
Hide file tree
Showing 13 changed files with 664 additions and 6 deletions.
12 changes: 9 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
}

android {
Expand All @@ -25,12 +27,12 @@ android {
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}

kotlinOptions {
jvmTarget = "11"
jvmTarget = "17"
}

namespace 'github.umer0586.sensorserver'
Expand All @@ -40,6 +42,8 @@ android {
}
}

apply plugin: 'com.yanzhenjie.andserver'

dependencies {

def acraVersion = '5.9.7'
Expand All @@ -64,6 +68,8 @@ dependencies {
implementation 'androidx.core:core-ktx:1.10.1'
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.8.20"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4"
implementation 'com.yanzhenjie.andserver:api:2.1.12'
kapt 'com.yanzhenjie.andserver:processor:2.1.12'


}
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
android:enabled="true"
android:exported="false" />

<service
android:name=".service.HttpService"
android:foregroundServiceType="specialUse"
android:enabled="true"
android:exported="false" />

<activity
android:name=".activities.MainActivity"
android:exported="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ import android.os.Bundle
import android.os.IBinder
import android.util.Log
import android.view.MenuItem
import android.view.View
import android.widget.RelativeLayout
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SwitchCompat
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
Expand All @@ -17,11 +23,14 @@ import github.umer0586.sensorserver.databinding.ActivityMainBinding
import github.umer0586.sensorserver.fragments.AvailableSensorsFragment
import github.umer0586.sensorserver.fragments.ConnectionsFragment
import github.umer0586.sensorserver.fragments.ServerFragment
import github.umer0586.sensorserver.service.HttpServerStateListener
import github.umer0586.sensorserver.service.HttpService
import github.umer0586.sensorserver.service.WebsocketService
import github.umer0586.sensorserver.service.WebsocketService.LocalBinder
import github.umer0586.sensorserver.service.ServiceBindHelper
import github.umer0586.sensorserver.webserver.HttpServerInfo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.lang.Exception

class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListener
{
Expand All @@ -31,6 +40,9 @@ class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListen
private lateinit var serviceBindHelper: ServiceBindHelper
private var websocketService: WebsocketService? = null

private lateinit var httpServiceBinder: ServiceBindHelper
private var httpService: HttpService? = null

private lateinit var binding : ActivityMainBinding

companion object
Expand Down Expand Up @@ -60,6 +72,11 @@ class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListen
binding.dashboard.bottomNavView.selectedItemId = R.id.navigation_server
binding.dashboard.bottomNavView.setOnItemSelectedListener(this)






serviceBindHelper = ServiceBindHelper(
context = applicationContext,
service = WebsocketService::class.java,
Expand All @@ -68,7 +85,13 @@ class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListen

serviceBindHelper.onServiceConnected(this::onServiceConnected)

httpServiceBinder = ServiceBindHelper(
context = applicationContext,
service = HttpService::class.java,
componentLifecycle = lifecycle
)

httpServiceBinder.onServiceConnected(this::onHttpServiceConnected)

binding.dashboard.viewPager.isUserInputEnabled = false
binding.dashboard.viewPager.adapter = MyFragmentStateAdapter(this)
Expand Down Expand Up @@ -114,7 +137,7 @@ class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListen

fun onServiceConnected(binder: IBinder)
{
val localBinder = binder as LocalBinder
val localBinder = binder as WebsocketService.LocalBinder
websocketService = localBinder.service

websocketService?.let{ setConnectionCountBadge( it.getConnectionCount() ) }
Expand All @@ -128,6 +151,84 @@ class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListen

}

private fun onHttpServiceConnected(binder: IBinder){


val httpServerAddressParentView = (binding.drawerNavigationView.menu
.findItem(R.id.nav_drawer_http_server_address).actionView as RelativeLayout)
val httpServerAddress = httpServerAddressParentView.findViewById<TextView>(R.id.server_address)

val httpServerSwitch = (binding.drawerNavigationView.menu.findItem(R.id.nav_drawer_http_server_switch).actionView as RelativeLayout).getChildAt(0) as SwitchCompat


val showServerAddress : ((HttpServerInfo) -> Unit) = {info ->
httpServerAddressParentView.visibility = View.VISIBLE
httpServerAddress.apply {
visibility = View.VISIBLE
text = info.baseUrl
}

}

val hideServerAddress = {
httpServerAddressParentView.visibility = View.GONE
httpServerAddress.visibility = View.INVISIBLE
}

hideServerAddress()

val localBinder = binder as HttpService.LocalBinder
httpService = localBinder.service

httpService?.setServerStateListener(object : HttpServerStateListener{
override fun onStart(httpServerInfo: HttpServerInfo) {
lifecycleScope.launch(Dispatchers.Main){
showServerAddress(httpServerInfo)
Toast.makeText(this@MainActivity,"web server started",Toast.LENGTH_SHORT).show()
httpServerSwitch.isChecked = true
}
}

override fun onStop() {
lifecycleScope.launch(Dispatchers.Main){
hideServerAddress()
Toast.makeText(this@MainActivity,"web server stopped",Toast.LENGTH_SHORT).show()
httpServerSwitch.isChecked = false
}
}

override fun onError(exception: Exception) {
lifecycleScope.launch(Dispatchers.Main){
Toast.makeText(this@MainActivity,exception.message,Toast.LENGTH_SHORT).show()
httpServerSwitch.isChecked = false
Log.e(TAG,exception.message.toString())
}

}

override fun onRunning(httpServerInfo: HttpServerInfo) {
lifecycleScope.launch(Dispatchers.Main){
showServerAddress(httpServerInfo)
httpServerSwitch.isChecked = true
}
}

})

httpService?.checkState()

httpServerSwitch.setOnCheckedChangeListener { _, isChecked ->
val isServerRunning = httpService?.isServerRunning ?: false
if(isChecked && !isServerRunning){
val intent = Intent(applicationContext, HttpService::class.java)
ContextCompat.startForegroundService(applicationContext, intent)
}
else if (!isChecked && isServerRunning) {
this.sendBroadcast(Intent(HttpService.ACTION_STOP_SERVER))
}
}
}


override fun onPause()
{
Expand All @@ -136,6 +237,7 @@ class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListen

// To prevent memory leak
websocketService?.onConnectionsCountChange(callBack = null)
httpService?.setServerStateListener(null)
}


Expand Down
Loading

0 comments on commit 32e043a

Please sign in to comment.