jp.co.ipublishing.esnavi.impl.alert.AlertStore.java Source code

Java tutorial

Introduction

Here is the source code for jp.co.ipublishing.esnavi.impl.alert.AlertStore.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.esnavi.impl.alert;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;

import net.iharder.Base64;

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

import java.io.IOException;

import jp.co.ipublishing.aeskit.alert.models.Alert;
import rx.Observable;
import rx.Subscriber;

/**
 * ?
 * 1???????
 */
public class AlertStore implements jp.co.ipublishing.aeskit.alert.AlertStore {
    private static final String TAG = "AlertStore";

    /**
     * 
     */
    @NonNull
    private final Context mContext;

    /**
     * ?
     */
    @Nullable
    private Alert mLatestAlert;

    /**
     * 
     *
     * @param context 
     */
    public AlertStore(@NonNull Context context) {
        mContext = context;
    }

    @NonNull
    @Override
    public Observable<Alert> fetchAlert() {
        return Observable.create(new Observable.OnSubscribe<Alert>() {
            @Override
            public void call(Subscriber<? super Alert> subscriber) {
                if (mLatestAlert == null) {
                    mLatestAlert = restoreLatestAlert();
                    if (mLatestAlert != null) {
                        storeLatestAlert(mLatestAlert);
                    }
                }

                if (mLatestAlert != null) {
                    subscriber.onNext(mLatestAlert);
                }

                subscriber.onCompleted();
            }
        });
    }

    @NonNull
    @Override
    public Observable<Alert> storeAlert(@NonNull final Alert alert) {
        return Observable.create(new Observable.OnSubscribe<Alert>() {
            @Override
            public void call(Subscriber<? super Alert> subscriber) {
                // 1?????????
                if (mLatestAlert != null && mLatestAlert.getTime().getTime() == alert.getTime().getTime()) {
                    subscriber.onNext(null);
                } else {
                    mLatestAlert = alert;
                    storeLatestAlert(mLatestAlert);
                    subscriber.onNext(mLatestAlert);
                }

                subscriber.onCompleted();
            }
        });
    }

    /**
     * ?????
     */
    private static final String KEY_LATEST_ALERT = "latest_alert";

    /**
     * ??
     *
     * @return 
     */
    @Nullable
    private Alert restoreLatestAlert() {
        final String string = getPreferences(mContext).getString(KEY_LATEST_ALERT, null);
        if (string != null) {
            try {
                return (Alert) Base64.decodeToObject(string);
            } catch (IOException | ClassCastException | ClassNotFoundException e) {
                Log.e(TAG, ExceptionUtils.getStackTrace(e));
                return null;
            }
        } else {
            return null;
        }
    }

    /**
     * ??
     *
     * @param alert 
     */
    private void storeLatestAlert(@NonNull Alert alert) {
        try {
            final String string = Base64.encodeObject(alert);
            getPreferences(mContext).edit().putString(KEY_LATEST_ALERT, string).commit();
        } catch (IOException e) {
            Log.e(TAG, ExceptionUtils.getStackTrace(e));
        }
    }

    /**
     * ????????SharedPreferences??
     * @param context 
     * @return SharedPreferences
     */
    @NonNull
    private static SharedPreferences getPreferences(@NonNull Context context) {
        return context.getSharedPreferences(AlertStore.class.getSimpleName(), Context.MODE_PRIVATE);
    }
}