<commit_before>package com.qiscus.sdk.service;

import android.app.job.JobInfo;
import android.app.job.JobParameters;
import android.app.job.JobScheduler;
import android.app.job.JobService;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;

import com.qiscus.sdk.Qiscus;
import com.qiscus.sdk.data.model.QiscusAccount;
import com.qiscus.sdk.data.model.QiscusComment;
import com.qiscus.sdk.data.remote.QiscusApi;
import com.qiscus.sdk.data.remote.QiscusPusherApi;
import com.qiscus.sdk.event.QiscusCommentReceivedEvent;
import com.qiscus.sdk.event.QiscusSyncEvent;
import com.qiscus.sdk.event.QiscusUserEvent;
import com.qiscus.sdk.util.QiscusAndroidUtil;
import com.qiscus.sdk.util.QiscusErrorLogger;
import com.qiscus.sdk.util.QiscusLogger;
import com.qiscus.sdk.util.QiscusPushNotificationUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.Random;
import java.util.concurrent.TimeUnit;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by adicatur on 11/23/17.
 */

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class QiscusSyncJobService extends JobService {
    private static final String TAG = QiscusSyncJobService.class.getSimpleName();

    private ComponentName componentName;

    @Override
    public void onCreate() {
        super.onCreate();

        QiscusLogger.print(TAG, "Creating...");
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

        if (Qiscus.hasSetupUser()) {
            QiscusAndroidUtil.runOnUIThread(() -> QiscusPusherApi.getInstance().restartConnection());
            componentName = new ComponentName(this, QiscusSyncJobService.class);
            syncJob();
        }

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    private void scheduleSync() {

        if (Qiscus.isOnForeground()) {
            QiscusApi.getInstance().sync()
                    .doOnNext(qiscusComment -> {
                        if (!qiscusComment.isMyComment()) {
                            QiscusPusherApi.getInstance()
                                    .setUserDelivery(qiscusComment.getRoomId(), qiscusComment.getTopicId(),
                                            qiscusComment.getId(), qiscusComment.getUniqueId());
                        }
                        QiscusComment savedQiscusComment = Qiscus.getDataStore()
                                .getComment(qiscusComment.getId(), qiscusComment.getUniqueId());
                        if (savedQiscusComment != null && savedQiscusComment.getState() > qiscusComment.getState()) {
                            qiscusComment.setState(savedQiscusComment.getState());
                        }
                    })
                    .doOnSubscribe(() -> {
                        EventBus.getDefault().post((QiscusSyncEvent.STARTED));
                        QiscusLogger.print("Sync started...");
                    })
                    .doOnCompleted(() -> {
                        EventBus.getDefault().post((QiscusSyncEvent.COMPLETED));
                        QiscusLogger.print("Sync completed...");
                    })
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(qiscusComment -> {
                        QiscusPushNotificationUtil.handlePushNotification(Qiscus.getApps(), qiscusComment);
                        EventBus.getDefault().post(new QiscusCommentReceivedEvent(qiscusComment));
                    }, throwable -> {
                        QiscusErrorLogger.print(throwable);
                        EventBus.getDefault().post(QiscusSyncEvent.FAILED);
                        QiscusLogger.print("Sync failed...");
                    });
        }

    }

    private void stopSync() {
        JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
        if (jobScheduler != null) {
            jobScheduler.cancelAll();
        }
    }

    @Subscribe
    public void onUserEvent(QiscusUserEvent userEvent) {
        switch (userEvent) {
            case LOGIN:
                QiscusAndroidUtil.runOnUIThread(() -> QiscusPusherApi.getInstance().restartConnection());
                syncJob();
                break;
            case LOGOUT:
                stopSync();
                break;
        }
    }

    @Override
    public void onDestroy() {
        QiscusLogger.print(TAG, "Destroying...");
        EventBus.getDefault().unregister(this);
        stopSync();
        super.onDestroy();
    }

    @Override
    public boolean onStartJob(JobParameters params) {
        QiscusLogger.print(TAG, "Job started...");

        if (Qiscus.hasSetupUser()) {
            QiscusAndroidUtil.runOnUIThread(() -> QiscusPusherApi.getInstance().restartConnection());
            scheduleSync();
            jobFinished(params, true);
        }

        return true;
    }

    @Override
    public boolean onStopJob(JobParameters params) {

        return false;
    }

    private void syncJob() {
        QiscusAccount qiscusAccount = Qiscus.getQiscusAccount();

        Random rand = new Random();
        int randomValue = rand.nextInt(50);

        JobInfo jobInfo = new JobInfo.Builder(qiscusAccount.getId() + randomValue, componentName)
                .setPeriodic(TimeUnit.MINUTES.toMillis(15))
                .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
                .build();

        JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
        jobScheduler.schedule(jobInfo);

    }

}<commit_msg>Avoid NPE<commit_after>package com.qiscus.sdk.service;

import android.app.job.JobInfo;
import android.app.job.JobParameters;
import android.app.job.JobScheduler;
import android.app.job.JobService;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;

import com.qiscus.sdk.Qiscus;
import com.qiscus.sdk.data.model.QiscusAccount;
import com.qiscus.sdk.data.model.QiscusComment;
import com.qiscus.sdk.data.remote.QiscusApi;
import com.qiscus.sdk.data.remote.QiscusPusherApi;
import com.qiscus.sdk.event.QiscusCommentReceivedEvent;
import com.qiscus.sdk.event.QiscusSyncEvent;
import com.qiscus.sdk.event.QiscusUserEvent;
import com.qiscus.sdk.util.QiscusAndroidUtil;
import com.qiscus.sdk.util.QiscusErrorLogger;
import com.qiscus.sdk.util.QiscusLogger;
import com.qiscus.sdk.util.QiscusPushNotificationUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.Random;
import java.util.concurrent.TimeUnit;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by adicatur on 11/23/17.
 */

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class QiscusSyncJobService extends JobService {
    private static final String TAG = QiscusSyncJobService.class.getSimpleName();

    private ComponentName componentName;

    @Override
    public void onCreate() {
        super.onCreate();

        QiscusLogger.print(TAG, "Creating...");
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

        if (Qiscus.hasSetupUser()) {
            QiscusAndroidUtil.runOnUIThread(() -> QiscusPusherApi.getInstance().restartConnection());
            componentName = new ComponentName(this, QiscusSyncJobService.class);
            syncJob();
        }

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    private void scheduleSync() {

        if (Qiscus.isOnForeground()) {
            QiscusApi.getInstance().sync()
                    .doOnNext(qiscusComment -> {
                        if (!qiscusComment.isMyComment()) {
                            QiscusPusherApi.getInstance()
                                    .setUserDelivery(qiscusComment.getRoomId(), qiscusComment.getTopicId(),
                                            qiscusComment.getId(), qiscusComment.getUniqueId());
                        }
                        QiscusComment savedQiscusComment = Qiscus.getDataStore()
                                .getComment(qiscusComment.getId(), qiscusComment.getUniqueId());
                        if (savedQiscusComment != null && savedQiscusComment.getState() > qiscusComment.getState()) {
                            qiscusComment.setState(savedQiscusComment.getState());
                        }
                    })
                    .doOnSubscribe(() -> {
                        EventBus.getDefault().post((QiscusSyncEvent.STARTED));
                        QiscusLogger.print("Sync started...");
                    })
                    .doOnCompleted(() -> {
                        EventBus.getDefault().post((QiscusSyncEvent.COMPLETED));
                        QiscusLogger.print("Sync completed...");
                    })
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(qiscusComment -> {
                        QiscusPushNotificationUtil.handlePushNotification(Qiscus.getApps(), qiscusComment);
                        EventBus.getDefault().post(new QiscusCommentReceivedEvent(qiscusComment));
                    }, throwable -> {
                        QiscusErrorLogger.print(throwable);
                        EventBus.getDefault().post(QiscusSyncEvent.FAILED);
                        QiscusLogger.print("Sync failed...");
                    });
        }

    }

    private void stopSync() {
        JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
        if (jobScheduler != null) {
            jobScheduler.cancelAll();
        }
    }

    @Subscribe
    public void onUserEvent(QiscusUserEvent userEvent) {
        switch (userEvent) {
            case LOGIN:
                QiscusAndroidUtil.runOnUIThread(() -> QiscusPusherApi.getInstance().restartConnection());
                syncJob();
                break;
            case LOGOUT:
                stopSync();
                break;
        }
    }

    @Override
    public void onDestroy() {
        QiscusLogger.print(TAG, "Destroying...");
        EventBus.getDefault().unregister(this);
        stopSync();
        super.onDestroy();
    }

    @Override
    public boolean onStartJob(JobParameters params) {
        QiscusLogger.print(TAG, "Job started...");

        if (Qiscus.hasSetupUser()) {
            QiscusAndroidUtil.runOnUIThread(() -> QiscusPusherApi.getInstance().restartConnection());
            scheduleSync();
            jobFinished(params, true);
        }

        return true;
    }

    @Override
    public boolean onStopJob(JobParameters params) {

        return false;
    }

    private void syncJob() {
        QiscusAccount qiscusAccount = Qiscus.getQiscusAccount();

        Random rand = new Random();
        int randomValue = rand.nextInt(50);

        JobInfo jobInfo = new JobInfo.Builder(qiscusAccount.getId() + randomValue, componentName)
                .setPeriodic(TimeUnit.MINUTES.toMillis(15))
                .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
                .build();

        JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
        if (jobScheduler != null) {
            jobScheduler.schedule(jobInfo);
        }

    }

}
{'fraction_non_alphanumeric': 0.10510307536329841, 'fraction_numerical': 0.0016897600540723217, 'mean_word_length': 2.139787798408488, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4441814', 'n_tokens_mistral': 3384, 'n_tokens_neox': 3124, 'n_words': 484}