Search in sources :

Example 1 with DatabasePersistenceAsyncCallback

use of com.microsoft.azure.mobile.persistence.DatabasePersistenceAsync.DatabasePersistenceAsyncCallback in project mobile-center-sdk-android by Microsoft.

the class DefaultChannel method enqueue.

/**
     * Actual implementation of enqueue logic. Will increase counters, triggers of batching logic.
     *
     * @param log       the Log to be enqueued
     * @param groupName the queue to use
     */
@Override
public synchronized void enqueue(@NonNull Log log, @NonNull final String groupName) {
    /* Check group name is registered. */
    final GroupState groupState = mGroupStates.get(groupName);
    if (groupState == null) {
        MobileCenterLog.error(LOG_TAG, "Invalid group name:" + groupName);
        return;
    }
    /* Check if disabled with discarding logs. */
    if (mDiscardLogs) {
        MobileCenterLog.warn(LOG_TAG, "Channel is disabled, log are discarded.");
        if (groupState.mListener != null) {
            groupState.mListener.onBeforeSending(log);
            groupState.mListener.onFailure(log, new CancellationException());
        }
        return;
    }
    /* Call listeners so that they can decorate the log. */
    for (Listener listener : mListeners) listener.onEnqueuingLog(log, groupName);
    /* Attach device properties to every log if its not already attached by a service. */
    if (log.getDevice() == null) {
        /* Generate device properties only once per process life time. */
        if (mDevice == null) {
            try {
                mDevice = DeviceInfoHelper.getDeviceInfo(mContext);
            } catch (DeviceInfoHelper.DeviceInfoException e) {
                MobileCenterLog.error(LOG_TAG, "Device log cannot be generated", e);
                return;
            }
        }
        /* Attach device properties. */
        log.setDevice(mDevice);
    }
    /* Set an absolute timestamp, we'll convert to relative just before sending. Don't do it if the service already set a timestamp.*/
    if (log.getToffset() == 0L)
        log.setToffset(System.currentTimeMillis());
    /* Persist log. */
    final int stateSnapshot = mCurrentState;
    mPersistence.putLog(groupName, log, new DatabasePersistenceAsyncCallback() {

        @Override
        public void onSuccess(Object result) {
            checkLogsAfterPut(groupState, stateSnapshot);
        }

        @Override
        public void onFailure(Exception e) {
            MobileCenterLog.error(LOG_TAG, "Error persisting log with exception: " + e.toString());
        }
    });
}
Also used : DatabasePersistenceAsyncCallback(com.microsoft.azure.mobile.persistence.DatabasePersistenceAsync.DatabasePersistenceAsyncCallback) AbstractDatabasePersistenceAsyncCallback(com.microsoft.azure.mobile.persistence.DatabasePersistenceAsync.AbstractDatabasePersistenceAsyncCallback) CancellationException(com.microsoft.azure.mobile.CancellationException) DeviceInfoHelper(com.microsoft.azure.mobile.utils.DeviceInfoHelper) IOException(java.io.IOException) CancellationException(com.microsoft.azure.mobile.CancellationException)

Aggregations

CancellationException (com.microsoft.azure.mobile.CancellationException)1 AbstractDatabasePersistenceAsyncCallback (com.microsoft.azure.mobile.persistence.DatabasePersistenceAsync.AbstractDatabasePersistenceAsyncCallback)1 DatabasePersistenceAsyncCallback (com.microsoft.azure.mobile.persistence.DatabasePersistenceAsync.DatabasePersistenceAsyncCallback)1 DeviceInfoHelper (com.microsoft.azure.mobile.utils.DeviceInfoHelper)1 IOException (java.io.IOException)1