Skip to content

Commit

Permalink
Update sample app & recode some Orb method
Browse files Browse the repository at this point in the history
  • Loading branch information
Ezra Lazuardy committed May 6, 2020
1 parent def07e1 commit 6f8b31a
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 66 deletions.
2 changes: 2 additions & 0 deletions .idea/dictionaries/ezra.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 16 additions & 21 deletions app/src/main/java/com/ezralazuardy/orb/demo/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Created by Ezra Lazuardy on 5/4/20 2:42 AM
* Copyright (c) 2020 . All rights reserved.
* Last modified 5/4/20 2:42 AM
* Created by Ezra Lazuardy on 5/6/20 8:23 AM
* Copyright (c) 2020. All rights reserved.
* Last modified 5/6/20 7:55 AM
*/

package com.ezralazuardy.orb.demo
Expand All @@ -10,34 +10,31 @@ import android.annotation.SuppressLint
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.ezralazuardy.orb.Orb
import com.ezralazuardy.orb.OrbHelper.orbObserver
import com.ezralazuardy.orb.OrbListener
import com.ezralazuardy.orb.OrbResponse
import kotlinx.android.synthetic.main.activity_main.*

@SuppressLint("SetTextI18n")
class MainActivity : AppCompatActivity(), OrbListener {

private lateinit var orb: Orb
private lateinit var observer: (OrbResponse) -> Unit

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
orb = Orb.with(this)
observeOrb()
observer = orbObserver {
state.text = it.state.toString()
type.text = it.type.toString()
errorMessage.text = it.errorMessage.toString()
}
orb = Orb.with(this).setListener(this).observe(observer)
buttonToggleOrb.apply { isChecked = true }.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) observeOrb() else orb.stop()
if (isChecked) orb.observe(observer) else orb.stop()
}
}

private fun observeOrb() = orb.setListener(this).observe {
state.text = it.state.toString()
type.text = it.type.toString()
errorMessage.text = it.errorMessage.toString()
}

override fun onOrbCreate() {
log.text = "Creating Orb..."
}

override fun onOrbObserve() {
log.text = "Start observing Orb..."
}
Expand All @@ -50,10 +47,8 @@ class MainActivity : AppCompatActivity(), OrbListener {
log.text = "Orb is inactive"
}

override fun onOrbStop(stopped: Boolean) {
if (stopped) {
buttonToggleOrb.isChecked = false
log.text = "Orb has been stopped"
} else log.text = "Error happened when stopping Orb"
override fun onOrbStop() {
buttonToggleOrb.isChecked = false
log.text = "Orb has been stopped"
}
}
24 changes: 10 additions & 14 deletions orb/src/main/java/com/ezralazuardy/orb/Orb.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Created by Ezra Lazuardy on 5/4/20 4:18 AM
* Created by Ezra Lazuardy on 5/6/20 8:23 AM
* Copyright (c) 2020. All rights reserved.
* Last modified 5/4/20 4:17 AM
* Last modified 5/6/20 7:55 AM
*/

package com.ezralazuardy.orb
Expand All @@ -25,9 +25,9 @@ import com.ezralazuardy.orb.OrbHelper.orbOptions
*/
class Orb private constructor() {

private lateinit var observer: Observer<OrbResponse>
private lateinit var context: Context
private lateinit var orbEngine: OrbEngine
private lateinit var observer: Observer<OrbResponse>
private var orbListener: OrbListener? = null
set(value) {
if (this::orbEngine.isInitialized) this.orbEngine.orbListener = value
Expand All @@ -40,14 +40,11 @@ class Orb private constructor() {
* This method will construct the parameter that used in OrbEngine. Make sure to call
* this method first before calling the other method as it is used to initializing the Orb.
*
* OrbListener.onOrbCreate() will be called in this method.
*
* @param context
* @param orbOptions
*/
@MainThread
fun with(context: Context, options: OrbOptions = OrbOptions()): Orb = Orb().apply {
this.orbListener?.onOrbCreate()
if (!this::context.isInitialized) {
this.context = context
this.orbEngine = OrbEngine(this.context, options)
Expand Down Expand Up @@ -109,18 +106,17 @@ class Orb private constructor() {
*/
@MainThread
fun stop(): Boolean {
this.orbListener?.onOrbStop()
try {
if (this::orbEngine.isInitialized) {
if (this::observer.isInitialized) {
orbEngine.removeObserver(this.observer)
this.orbListener?.onOrbStop(true)
return true
} else throw OrbError(OrbError.ORB_NOT_INITIALIZED)
} else throw OrbError(OrbError.ORB_NOT_INITIALIZED)
} catch (e: OrbError) {
e.printStackTrace()
}
this.orbListener?.onOrbStop(false)
return false
}

Expand Down Expand Up @@ -176,7 +172,7 @@ class Orb private constructor() {
*/
@TargetApi(Build.VERSION_CODES.M)
fun allowLowPan(state: Boolean = true): Orb {
this.orbEngine.setOption(OrbType.LOW_PAN, state)
this.orbEngine.setOption(OrbType.LOWPAN, state)
return this
}

Expand Down Expand Up @@ -232,7 +228,7 @@ class Orb private constructor() {
mapOf(
OrbType.CELLULAR to false,
OrbType.ETHERNET to false,
OrbType.LOW_PAN to false,
OrbType.LOWPAN to false,
OrbType.VPN to false,
OrbType.WIFI to false,
OrbType.WIFI_AWARE to false
Expand All @@ -250,7 +246,7 @@ class Orb private constructor() {
mapOf(
OrbType.BLUETOOTH to false,
OrbType.ETHERNET to false,
OrbType.LOW_PAN to false,
OrbType.LOWPAN to false,
OrbType.VPN to false,
OrbType.WIFI to false,
OrbType.WIFI_AWARE to false
Expand All @@ -268,7 +264,7 @@ class Orb private constructor() {
mapOf(
OrbType.BLUETOOTH to false,
OrbType.CELLULAR to false,
OrbType.LOW_PAN to false,
OrbType.LOWPAN to false,
OrbType.VPN to false,
OrbType.WIFI to false,
OrbType.WIFI_AWARE to false
Expand Down Expand Up @@ -310,7 +306,7 @@ class Orb private constructor() {
OrbType.CELLULAR to false,
OrbType.ETHERNET to false,
OrbType.VPN to false,
OrbType.LOW_PAN to false,
OrbType.LOWPAN to false,
OrbType.WIFI_AWARE to false
)
}
Expand All @@ -332,7 +328,7 @@ class Orb private constructor() {
OrbType.CELLULAR to false,
OrbType.ETHERNET to false,
OrbType.VPN to false,
OrbType.LOW_PAN to false,
OrbType.LOWPAN to false,
OrbType.WIFI to false
)
}
Expand Down
8 changes: 4 additions & 4 deletions orb/src/main/java/com/ezralazuardy/orb/OrbEngine.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Created by Ezra Lazuardy on 5/4/20 4:18 AM
* Created by Ezra Lazuardy on 5/6/20 8:23 AM
* Copyright (c) 2020. All rights reserved.
* Last modified 5/4/20 4:17 AM
* Last modified 5/6/20 8:05 AM
*/

package com.ezralazuardy.orb
Expand Down Expand Up @@ -112,7 +112,7 @@ class OrbEngine(
addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
if (OrbType.ETHERNET.isEnabledInOrbOptions(orbOptions))
addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET)
if (OrbType.LOW_PAN.isEnabledInOrbOptions(orbOptions) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1)
if (OrbType.LOWPAN.isEnabledInOrbOptions(orbOptions) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1)
addTransportType(NetworkCapabilities.TRANSPORT_LOWPAN)
if (OrbType.VPN.isEnabledInOrbOptions(orbOptions))
addTransportType(NetworkCapabilities.TRANSPORT_VPN)
Expand Down Expand Up @@ -164,7 +164,7 @@ class OrbEngine(
OrbType.BLUETOOTH -> orbOptions.bluetooth = state
OrbType.CELLULAR -> orbOptions.cellular = state
OrbType.ETHERNET -> orbOptions.ethernet = state
OrbType.LOW_PAN -> orbOptions.lowPan = state
OrbType.LOWPAN -> orbOptions.loWPAN = state
OrbType.VPN -> orbOptions.vpn = state
OrbType.WIFI -> orbOptions.wifi = state
OrbType.WIFI_AWARE -> orbOptions.wifiAware = state
Expand Down
10 changes: 5 additions & 5 deletions orb/src/main/java/com/ezralazuardy/orb/OrbHelper.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Created by Ezra Lazuardy on 5/4/20 4:18 AM
* Created by Ezra Lazuardy on 5/6/20 8:23 AM
* Copyright (c) 2020. All rights reserved.
* Last modified 5/4/20 4:07 AM
* Last modified 5/6/20 8:05 AM
*/

package com.ezralazuardy.orb
Expand Down Expand Up @@ -43,7 +43,7 @@ object OrbHelper {
OrbType.BLUETOOTH -> orbOptions.bluetooth
OrbType.CELLULAR -> orbOptions.cellular
OrbType.ETHERNET -> orbOptions.ethernet
OrbType.LOW_PAN -> orbOptions.lowPan
OrbType.LOWPAN -> orbOptions.loWPAN
OrbType.VPN -> orbOptions.vpn
OrbType.WIFI -> orbOptions.wifi
OrbType.WIFI_AWARE -> orbOptions.wifiAware
Expand All @@ -65,7 +65,7 @@ object OrbHelper {
hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH) -> OrbType.BLUETOOTH
hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> OrbType.CELLULAR
hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> OrbType.ETHERNET
hasTransport(NetworkCapabilities.TRANSPORT_LOWPAN) -> OrbType.LOW_PAN
hasTransport(NetworkCapabilities.TRANSPORT_LOWPAN) -> OrbType.LOWPAN
hasTransport(NetworkCapabilities.TRANSPORT_VPN) -> OrbType.VPN
hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> OrbType.WIFI
hasTransport(NetworkCapabilities.TRANSPORT_WIFI_AWARE) -> OrbType.WIFI_AWARE
Expand Down Expand Up @@ -108,7 +108,7 @@ object OrbHelper {
bluetooth = mapOptions.getOrElse(OrbType.BLUETOOTH, { true })
cellular = mapOptions.getOrElse(OrbType.CELLULAR, { true })
ethernet = mapOptions.getOrElse(OrbType.ETHERNET, { true })
lowPan = mapOptions.getOrElse(OrbType.LOW_PAN, { true })
loWPAN = mapOptions.getOrElse(OrbType.LOWPAN, { true })
vpn = mapOptions.getOrElse(OrbType.VPN, { true })
wifi = mapOptions.getOrElse(OrbType.WIFI, { true })
wifiAware = mapOptions.getOrElse(OrbType.WIFI_AWARE, { true })
Expand Down
14 changes: 4 additions & 10 deletions orb/src/main/java/com/ezralazuardy/orb/OrbListener.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Created by Ezra Lazuardy on 5/4/20 2:42 AM
* Copyright (c) 2020 . All rights reserved.
* Last modified 5/4/20 2:41 AM
* Created by Ezra Lazuardy on 5/6/20 8:23 AM
* Copyright (c) 2020. All rights reserved.
* Last modified 5/6/20 7:45 AM
*/

package com.ezralazuardy.orb
Expand All @@ -12,12 +12,6 @@ package com.ezralazuardy.orb
*/
interface OrbListener {

/**
* onOrbCreate() will be invoked on creation of Orb instance. In short word, this method will
* called after client code calls .with().
*/
fun onOrbCreate()

/**
* onOrbObserve() will be invoked on observation of an observer that specified in .observe().
* This method will be called after the client code calls .observe().
Expand All @@ -40,5 +34,5 @@ interface OrbListener {
* onOrbStop() will be invoked when Orb is being stopped. This method will be called after
* client code calls .stop().
*/
fun onOrbStop(stopped: Boolean)
fun onOrbStop()
}
8 changes: 4 additions & 4 deletions orb/src/main/java/com/ezralazuardy/orb/OrbOptions.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Created by Ezra Lazuardy on 5/4/20 2:42 AM
* Copyright (c) 2020 . All rights reserved.
* Last modified 5/4/20 2:41 AM
* Created by Ezra Lazuardy on 5/6/20 8:23 AM
* Copyright (c) 2020. All rights reserved.
* Last modified 5/6/20 8:03 AM
*/

package com.ezralazuardy.orb
Expand All @@ -21,7 +21,7 @@ data class OrbOptions(
var bluetooth: Boolean = true,
var cellular: Boolean = true,
var ethernet: Boolean = true,
var lowPan: Boolean = true,
var loWPAN: Boolean = true,
var vpn: Boolean = true,
var wifi: Boolean = true,
var wifiAware: Boolean = true
Expand Down
8 changes: 4 additions & 4 deletions orb/src/main/java/com/ezralazuardy/orb/OrbState.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Created by Ezra Lazuardy on 5/4/20 2:42 AM
* Copyright (c) 2020 . All rights reserved.
* Last modified 5/4/20 2:41 AM
* Created by Ezra Lazuardy on 5/6/20 8:23 AM
* Copyright (c) 2020. All rights reserved.
* Last modified 5/6/20 8:05 AM
*/

package com.ezralazuardy.orb
Expand All @@ -10,5 +10,5 @@ package com.ezralazuardy.orb
* OrbState is an enum class that hold some Orb network state properties
*/
enum class OrbState {
CONNECTED, DISCONNECTED, UNKNOWN
UNKNOWN, CONNECTED, DISCONNECTED
}
8 changes: 4 additions & 4 deletions orb/src/main/java/com/ezralazuardy/orb/OrbType.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Created by Ezra Lazuardy on 5/4/20 2:42 AM
* Copyright (c) 2020 . All rights reserved.
* Last modified 5/4/20 2:41 AM
* Created by Ezra Lazuardy on 5/6/20 8:23 AM
* Copyright (c) 2020. All rights reserved.
* Last modified 5/6/20 7:55 AM
*/

package com.ezralazuardy.orb
Expand All @@ -10,5 +10,5 @@ package com.ezralazuardy.orb
* OrbType is an enum class that hold some Orb network type properties
*/
enum class OrbType {
UNKNOWN, BLUETOOTH, CELLULAR, ETHERNET, LOW_PAN, VPN, WIFI, WIFI_AWARE
UNKNOWN, BLUETOOTH, CELLULAR, ETHERNET, LOWPAN, VPN, WIFI, WIFI_AWARE
}

0 comments on commit 6f8b31a

Please sign in to comment.