Skip to content

Instantly share code, notes, and snippets.

@luthfihariz
Created May 5, 2019 02:28
Show Gist options
  • Save luthfihariz/701f43ba8668bb014eb76c44d2628a9c to your computer and use it in GitHub Desktop.
Save luthfihariz/701f43ba8668bb014eb76c44d2628a9c to your computer and use it in GitHub Desktop.
if (isIpAddressDifferentWithLastRecorded(latestIpAddress)) {
Log.d(
WorkManagerHelper.TAG,
"Different IP Address ($latestIpAddress}."
)
insertNewRecordToDb(latestIpAddress)
} else {
if (connManager.getNetworkClass() == ConnectionManager.NETWORK_WIFI) {
// check if its on wifi more than one hour
if (isRecordedMoreThanOneHourAgo()) {
Log.d(WorkManagerHelper.TAG, "It's on wifi for more than one hour ago. So turn off wifi")
connManager.toggleWifi()
Log.d(WorkManagerHelper.TAG, "Wait for $WAITING_INTERVAL seconds after turning wifi off.")
Thread.sleep(WAITING_INTERVAL)
latestIpAddress = connManager.getPublicIpAddress().blockingGet()
if (isIpAddressDifferentWithLastRecorded(latestIpAddress)) {
Log.d(WorkManagerHelper.TAG, "Different IP Address ($latestIpAddress}.")
insertNewRecordToDb(latestIpAddress)
}
Log.d(WorkManagerHelper.TAG, "Wait for $WAITING_INTERVAL seconds before turning wifi on.")
Thread.sleep(WAITING_INTERVAL)
Log.d(WorkManagerHelper.TAG, "Turn wifi on.")
connManager.toggleWifi()
Log.d(WorkManagerHelper.TAG, "Wait for $WAITING_INTERVAL seconds after turning wifi on.")
Thread.sleep(WAITING_INTERVAL)
latestIpAddress = connManager.getPublicIpAddress().blockingGet()
if (isIpAddressDifferentWithLastRecorded(latestIpAddress)) {
Log.d(WorkManagerHelper.TAG, "Different IP Address ($latestIpAddress}.")
insertNewRecordToDb(latestIpAddress)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment