jp.co.ipublishing.aeskit.alert.AlertService.java Source code

Java tutorial

Introduction

Here is the source code for jp.co.ipublishing.aeskit.alert.AlertService.java

Source

/*
 * Copyright 2015 iPublishing Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jp.co.ipublishing.aeskit.alert;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import org.apache.commons.lang3.exception.ExceptionUtils;

import javax.inject.Inject;

import de.greenrobot.event.EventBus;
import jp.co.ipublishing.aeskit.alert.events.AlertFailedDownloadAlertEvent;
import jp.co.ipublishing.aeskit.alert.events.AlertFailedStoreAlertEvent;
import jp.co.ipublishing.aeskit.alert.events.AlertRequestAlertEvent;
import jp.co.ipublishing.aeskit.alert.events.AlertUpdatedAlertEvent;
import jp.co.ipublishing.aeskit.alert.models.Alert;
import jp.co.ipublishing.aeskit.helpers.dagger.ObjectGraph;
import rx.Subscriber;
import rx.schedulers.Schedulers;

/**
 * ?
 */
@SuppressLint("Registered")
public class AlertService extends Service {
    private static final String TAG = "AlertService";

    /**
     * ??
     */
    @Inject
    AlertManager mAlertManager;

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

        ObjectGraph.inject(this);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

        Log.i(TAG, "AlertService#onStartCommand");

        fetchAlert();

        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);

        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    /**
     * ??
     *
     * @param event 
     */
    public void onEvent(AlertRequestAlertEvent event) {
        Log.i(TAG, "AlertService#onEvent");

        fetchAlert();
    }

    /**
     * ???
     */
    private void fetchAlert() {
        mAlertManager.fetchAlert().subscribeOn(Schedulers.newThread()).subscribe(new Subscriber<Alert>() {
            @Override
            public void onCompleted() {
                // FIXME: Alert??????????
                downloadAlert();
            }

            @Override
            public void onError(Throwable e) {
                Log.e(TAG, ExceptionUtils.getStackTrace(e));

                // ????????
                // downloadLatestAlert??
                downloadAlert();
            }

            @Override
            public void onNext(Alert alert) {
                // post?
                EventBus.getDefault().post(new AlertUpdatedAlertEvent(alert));
            }
        });
    }

    /**
     * ???
     * ?????????AlertUpdateEvent????
     * ?????????
     */
    private void downloadAlert() {
        mAlertManager.downloadAlert().subscribe(new Subscriber<Alert>() {
            @Override
            public void onCompleted() {
                // Nothing to do
            }

            @Override
            public void onError(Throwable e) {
                Log.e(TAG, ExceptionUtils.getStackTrace(e));

                EventBus.getDefault().post(new AlertFailedDownloadAlertEvent(e));
            }

            @Override
            public void onNext(Alert alert) {
                // addAlert??
                // ????AlertUpdateLatestEvent??
                mAlertManager.storeAlert(alert).subscribe(new Subscriber<Alert>() {
                    @Override
                    public void onCompleted() {
                        // Nothing to do
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, ExceptionUtils.getStackTrace(e));
                        EventBus.getDefault().post(new AlertFailedStoreAlertEvent(e));
                    }

                    @Override
                    public void onNext(Alert alert) {
                        if (alert != null) {
                            EventBus.getDefault().post(new AlertUpdatedAlertEvent(alert));
                        }
                    }
                });
            }
        });
    }
}