Commit ad4bab2f authored by Leonardo Aramaki's avatar Leonardo Aramaki

Implement KeepAliveJob and initialized android-job

parent 3c2da86a
......@@ -5,6 +5,7 @@ import android.support.multidex.MultiDexApplication;
import android.support.v7.app.AppCompatDelegate;
import com.crashlytics.android.Crashlytics;
import com.evernote.android.job.JobManager;
import java.util.List;
......@@ -34,6 +35,7 @@ public class RocketChatApplication extends MultiDexApplication {
@Override
public void onCreate() {
super.onCreate();
JobManager.create(this).addJobCreator(new RocketChatJobCreator());
DDPClient.initialize(OkHttpHelper.INSTANCE.getClientForWebSocket());
Fabric.with(this, new Crashlytics());
......
package chat.rocket.android
import chat.rocket.android.service.KeepAliveJob
import com.evernote.android.job.Job
import com.evernote.android.job.JobCreator
class RocketChatJobCreator : JobCreator {
override fun create(tag: String): Job? {
when (tag) {
KeepAliveJob.TAG -> return KeepAliveJob()
else -> return null
}
}
}
\ No newline at end of file
package chat.rocket.android.service
import chat.rocket.android.RocketChatApplication
import chat.rocket.android.RocketChatCache
import com.evernote.android.job.Job
import com.evernote.android.job.JobManager
import com.evernote.android.job.JobRequest
import java.util.concurrent.TimeUnit
class KeepAliveJob : Job() {
private val connectivityManager: ConnectivityManagerApi
private val rocketChatCache: RocketChatCache
companion object {
val TAG = "chat.rocket.android.service.KeepAliveJob"
fun schedule() {
JobRequest.Builder(TAG)
.setExecutionWindow(TimeUnit.SECONDS.toMillis(3L), TimeUnit.SECONDS.toMillis(10L))
.setBackoffCriteria(10L, JobRequest.BackoffPolicy.EXPONENTIAL)
.setUpdateCurrent(true)
.setRequiredNetworkType(JobRequest.NetworkType.CONNECTED)
.setRequiresCharging(false)
.setRequirementsEnforced(true)
.build()
.schedule()
}
fun cancel() {
JobManager.instance().cancelAllForTag(TAG)
}
}
init {
val context = RocketChatApplication.getInstance()
connectivityManager = ConnectivityManager.getInstance(context)
rocketChatCache = RocketChatCache(context)
}
override fun onRunJob(params: Params): Result {
connectivityManager.keepAliveServer()
return Result.SUCCESS
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment