Skip to content

Commit

Permalink
add a sceduler for update displayed date
Browse files Browse the repository at this point in the history
  • Loading branch information
jbdamiano committed Feb 2, 2018
1 parent 28329ff commit de78b6f
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 75 deletions.
2 changes: 2 additions & 0 deletions mainActivity/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
buildscript {
ext.anko_version = '0.10.0'
repositories {
maven { url 'https://maven.fabric.io/public' }
}
Expand Down Expand Up @@ -57,6 +58,7 @@ dependencies {
}
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
compile "org.jetbrains.anko:anko-common:$anko_version"
}

crashlytics {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,13 @@ class MainActivity : FragmentActivity(), LoginListener, AuthenticationManager, O
}
}

override fun cancel() {
monitoringService?.cancel()
}
override fun start() {
monitoringService?.start()
}

override fun sendAlarmEvent() {
//if (boundToMonitoringService && monitoringService != null) {
monitoringService!!.sendAlarmEvent()
Expand Down Expand Up @@ -509,6 +516,8 @@ class MainActivity : FragmentActivity(), LoginListener, AuthenticationManager, O
disconnectFromService()
}



private fun restartMonitoringService() {
stopMonitoringService()
startMonitoringService(objectName!!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ interface MonitorServiceManager {
fun sendAlarmEvent()
fun setMonitoringServiceListener(listener: MonitorServiceListener)
fun isServiceRunning(): Boolean
fun cancel()
fun start()
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.sierrawireless.avphone.task.SyncWithAvParams
import com.sierrawireless.avphone.tools.Tools
import kotlinx.android.synthetic.main.fragment_run.*
import net.airvantage.utils.PreferenceUtils
import org.jetbrains.anko.runOnUiThread
import java.lang.Exception
import java.util.*
import kotlin.concurrent.schedule
Expand Down Expand Up @@ -275,12 +276,16 @@ open class RunFragment : AvPhoneFragment(), MonitorServiceListener, CustomLabels

this.setLinkToSystem(systemUid, systemName)
startTimer()
Log.d(TAG, "onResume called")
monitorServiceManager?.start()

}

override fun onPause() {
super.onPause()
stopTimer()
this.monitorServiceManager?.cancel()

}

private fun startMonitoringService() {
Expand Down Expand Up @@ -364,8 +369,8 @@ open class RunFragment : AvPhoneFragment(), MonitorServiceListener, CustomLabels
listObject.add(temp)
}
val adapter = RunListViewAdapter(activity, listObject)
objectLstView.adapter = adapter
objectLstView.invalidateViews()
objectLstView?.adapter = adapter
objectLstView?.invalidateViews()
}

override fun onServiceStarted(service: MonitoringService) {
Expand Down Expand Up @@ -427,18 +432,6 @@ open class RunFragment : AvPhoneFragment(), MonitorServiceListener, CustomLabels

}
objectsManager.saveOnPref()
MainActivity.instance.runOnUiThread {
try {
setCustomDataLabels()
}catch(e:Exception) {
// catch to avoid crash when the user choices another view
// and this thread is working access to object can be null
// but not critical for the application

Log.d(TAG,"Catch non displayed exception")
}
}
// MainActivity.instance.setCustomDataLabels(run)
startTimer()

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import org.eclipse.paho.client.mqttv3.MqttException
import org.eclipse.paho.client.mqttv3.MqttMessage
import java.nio.charset.Charset
import java.util.*
import kotlin.concurrent.fixedRateTimer
import kotlin.concurrent.schedule

class MonitoringService : Service() {

Expand Down Expand Up @@ -62,6 +64,8 @@ class MonitoringService : Service() {

private var dbm: Int? = null

var timer:Timer? = null


private val lastKnownLocation: Location?
@SuppressLint("MissingPermission")
Expand Down Expand Up @@ -168,13 +172,29 @@ class MonitoringService : Service() {

}


telephonyManager!!.listen(phoneStateListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS)


startedSince = System.currentTimeMillis()
start()

}

fun start() {
Log.d(TAG, "start")
//protect multiple start
if (timer == null) {
//periodic timer of 5 s do avoid
timer = fixedRateTimer("UItimer", false, 0, 2000L) {
setCustomDataForUi()
}
}
setUpLocationListeners()
}



fun startSendData() {
val notif = R.string.notif_title
// Create an intent to start the activity when clicking the notification
Expand Down Expand Up @@ -202,6 +222,73 @@ class MonitoringService : Service() {
stopForeground(true)
}

@SuppressLint("MissingPermission")
private fun setCustomDataForUi():NewData {
Log.d(TAG, "SetCustomDataForUi")
val location = lastKnownLocation

// retrieve data
val data = NewData()

if (dbm != null) {
data.rssi = dbm!!
}else {

val cellInfos = telephonyManager!!.allCellInfo
if (cellInfos != null && !cellInfos.isEmpty()) {
val cellInfo = cellInfos[0]
if (cellInfo is CellInfoGsm) {
data.rssi = cellInfo.cellSignalStrength.dbm
// } else if (cellInfo instanceof CellInfoWcdma) {
// RSSI ?
// data.setRssi(((CellInfoWcdma) cellInfo).getCellSignalStrength().getDbm());
} else if (cellInfo is CellInfoLte) {
data.rsrp = cellInfo.cellSignalStrength.dbm
}
}
}

if (telephonyManager!!.phoneType == TelephonyManager.PHONE_TYPE_GSM) {
@Suppress("DEPRECATION")
data.imei = telephonyManager!!.deviceId
}

data.operator = telephonyManager!!.networkOperatorName

when (telephonyManager!!.networkType) {
TelephonyManager.NETWORK_TYPE_GPRS -> data.networkType = "GPRS"
TelephonyManager.NETWORK_TYPE_EDGE -> data.networkType = "EDGE"
TelephonyManager.NETWORK_TYPE_UMTS -> data.networkType = "UMTS"
TelephonyManager.NETWORK_TYPE_HSDPA -> data.networkType = "HSDPA"
TelephonyManager.NETWORK_TYPE_HSPAP -> data.networkType = "HSPA+"
TelephonyManager.NETWORK_TYPE_HSPA -> data.networkType = "HSPA"
TelephonyManager.NETWORK_TYPE_HSUPA -> data.networkType = "HSUPA"
TelephonyManager.NETWORK_TYPE_LTE -> data.networkType = "LTE"
}// to be continued


// location
if (location != null) {
data.latitude = location.latitude
data.longitude = location.longitude
lastLocation = location.time
}

// bytes sent/received
data.bytesReceived = TrafficStats.getMobileRxBytes()
data.bytesSent = TrafficStats.getMobileTxBytes()

//execute action on current object datas
//objectsManager!!.execOnCurrent()
// Custom data
data.setCustom()


// dispatch new data event to update the activity UI
LocalBroadcastManager.getInstance(this).sendBroadcast(data)
return data
}

@SuppressLint("HardwareIds", "MissingPermission")
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {

Expand Down Expand Up @@ -237,74 +324,15 @@ class MonitoringService : Service() {


if (mustConnect) {
val location = lastKnownLocation

// retrieve data
val data = NewData()

if (dbm != null) {
data.rssi = dbm!!
}else {

@SuppressLint("MissingPermission") val cellInfos = telephonyManager!!.allCellInfo
if (cellInfos != null && !cellInfos.isEmpty()) {
val cellInfo = cellInfos[0]
if (cellInfo is CellInfoGsm) {
data.rssi = cellInfo.cellSignalStrength.dbm
// } else if (cellInfo instanceof CellInfoWcdma) {
// RSSI ?
// data.setRssi(((CellInfoWcdma) cellInfo).getCellSignalStrength().getDbm());
} else if (cellInfo is CellInfoLte) {
data.rsrp = cellInfo.cellSignalStrength.dbm
}
}
}

if (telephonyManager!!.phoneType == TelephonyManager.PHONE_TYPE_GSM) {
@Suppress("DEPRECATION")
data.imei = telephonyManager!!.deviceId
}

data.operator = telephonyManager!!.networkOperatorName

when (telephonyManager!!.networkType) {
TelephonyManager.NETWORK_TYPE_GPRS -> data.networkType = "GPRS"
TelephonyManager.NETWORK_TYPE_EDGE -> data.networkType = "EDGE"
TelephonyManager.NETWORK_TYPE_UMTS -> data.networkType = "UMTS"
TelephonyManager.NETWORK_TYPE_HSDPA -> data.networkType = "HSDPA"
TelephonyManager.NETWORK_TYPE_HSPAP -> data.networkType = "HSPA+"
TelephonyManager.NETWORK_TYPE_HSPA -> data.networkType = "HSPA"
TelephonyManager.NETWORK_TYPE_HSUPA -> data.networkType = "HSUPA"
TelephonyManager.NETWORK_TYPE_LTE -> data.networkType = "LTE"
}// to be continued


// location
if (location != null && location.time != lastLocation) {
data.latitude = location.latitude
data.longitude = location.longitude
lastLocation = location.time
}

// bytes sent/received
data.bytesReceived = TrafficStats.getMobileRxBytes()
data.bytesSent = TrafficStats.getMobileTxBytes()

//execute action on current object datas
//objectsManager!!.execOnCurrent()
// Custom data
data.setCustom()
val data = setCustomDataForUi()


//customDataSource.next(new Date());

// save new data values
if (data.extras != null) {
lastData.putExtras(data.extras!!)
}

// dispatch new data event to update the activity UI
LocalBroadcastManager.getInstance(this).sendBroadcast(data)

val sendDataTask = SendDataTask()
val params = SendDataParams()
Expand All @@ -318,7 +346,7 @@ class MonitoringService : Service() {

sendDataTask.addProgressListener { result -> lastLog = result.lastLog }

setUpLocationListeners()
// setUpLocationListeners()
}

} catch (e: Exception) {
Expand Down Expand Up @@ -349,6 +377,13 @@ class MonitoringService : Service() {
stopForeground(true)

stopLocationListeners()
timer?.cancel()
}

fun cancel(){
timer?.cancel()
stopLocationListeners()
timer = null
}

@SuppressLint("MissingPermission")
Expand Down

0 comments on commit de78b6f

Please sign in to comment.