Android Open Source - mobile-connector-sdk-android U B F Augmentation Service Impl






From Project

Back to project page mobile-connector-sdk-android.

License

The source code is released under:

Apache License

If you think the Android project mobile-connector-sdk-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.silverpop.engage.augmentation;
//  w  w w.java2 s.  c om
import android.content.Context;
import android.util.Log;

import com.silverpop.engage.augmentation.plugin.UBFAugmentationPlugin;
import com.silverpop.engage.config.EngageConfigManager;
import com.silverpop.engage.domain.EngageEvent;
import com.silverpop.engage.domain.UBF;
import com.silverpop.engage.network.UBFClient;
import com.silverpop.engage.store.EngageLocalEventStore;
import com.silverpop.engage.util.TimedAsyncTask;

import java.util.ArrayList;

/**
 * Created by jeremydyer on 6/3/14.
 */
public class UBFAugmentationServiceImpl
    implements UBFAugmentationService {

    private static final String TAG = UBFAugmentationServiceImpl.class.getName();

    private static UBFAugmentationServiceImpl augmentationService = null;
    private Context mAppContext = null;
    private EngageLocalEventStore engageLocalEventStore = null;
    private int maxCacheSize = 3;
    private UBFClient ubfClient = null;

    private final ArrayList<UBFAugmentationPlugin> plugins = new ArrayList<UBFAugmentationPlugin>();

    public static UBFAugmentationServiceImpl get(Context context ){
        if (augmentationService == null) {
            augmentationService = new UBFAugmentationServiceImpl(context);
        }
        return augmentationService;
    }

    /**
     * {@inheritDoc}
     */
    private UBFAugmentationServiceImpl(Context context) {
        mAppContext = context;
        engageLocalEventStore = EngageLocalEventStore.get(context);
        maxCacheSize = EngageConfigManager.get(context).ubfEventCacheSize();
        ubfClient = UBFClient.get(context);

        //Adds the Augmentation plugins
        String[] augmentationPlugins = EngageConfigManager.get(context).augmentationPluginClasses();
        for (String augmentationClass : augmentationPlugins) {
            try {
                Class augClazz = Class.forName(augmentationClass);
                UBFAugmentationPlugin augmentationPlugin = (UBFAugmentationPlugin) augClazz.newInstance();
                augmentationPlugin.setContext(context);
                plugins.add(augmentationPlugin);
            } catch (Exception ex) {
                Log.w(TAG, "Unable to initialize Pluggable Augmentation class '"
                        + augmentationClass + "' : " + ex.getMessage());
            }
        }
    }

    /**
     * {@inheritDoc}
     */
    public int augmentorsCount() {
        if (plugins != null) {
            return plugins.size();
        } else {
            return 0;
        }
    }

    /**
     * {@inheritDoc}
     */
    public void augmentUBFEvent(final UBF ubfEvent, final EngageEvent engageEvent, final long expirationSeconds) {

        if (ubfEvent != null && engageEvent != null) {
            final TimedAsyncTask<Void, Void, Void> task = new TimedAsyncTask<Void, Void, Void>() {
                @Override
                protected Void doInBackground(Void... objects) {

                    ArrayList<UBFAugmentationPlugin> notProcessedPlugins = new ArrayList<UBFAugmentationPlugin>();
                    for (UBFAugmentationPlugin plugin : plugins) {
                        notProcessedPlugins.add(plugin);
                    }

                    int index = 0;
                    UBF mutEvent = ubfEvent;
                    while (!isExpired() && notProcessedPlugins.size() > 0) {
                        if (index >= notProcessedPlugins.size()) {
                            index = 0;
                        }

                        UBFAugmentationPlugin plugin = notProcessedPlugins.get(index);
                        if (plugin.isSupplementalDataReady()) {
                            mutEvent = plugin.process(mutEvent);
                            notProcessedPlugins.remove(plugin);
                            //Index does not need to be updated since the list size has decreased by one.
                        } else if (!plugin.processSyncronously()) {
                            index++;
                        }
                        //Else we don't wait to update the index because we must wait until complete or timeout.
                    }

                    engageEvent.setEventJson(mutEvent.toJSONString());

                    //Save the event with the appropriate status.
                    if (notProcessedPlugins.size() == 0) {
                        //Save the event with a ready to post state.
                        engageEvent.setEventStatus(EngageEvent.READY_TO_POST);
                    } else {
                        //Save the event with an expired state.
                        engageEvent.setEventStatus(EngageEvent.EXPIRED);
                    }

                    engageLocalEventStore.saveUBFEvent(engageEvent);

                    if (engageLocalEventStore.countEventsReadyToPost() >= maxCacheSize) {
                        Log.d(TAG, "Local UBF cache limit has been reached. Posting events to Silverpop");
                        ubfClient.postUBFEngageEvents(null, null);
                    }

                    return null;
                }
            };
            task.setExpiresInSeconds(expirationSeconds);
            task.execute();
        }
    }

}




Java Source Code List

com.silverpop.engage.EngageApplication.java
com.silverpop.engage.UBFManager.java
com.silverpop.engage.XMLAPIManager.java
com.silverpop.engage.augmentation.UBFAugmentationServiceImpl.java
com.silverpop.engage.augmentation.UBFAugmentationService.java
com.silverpop.engage.augmentation.plugin.UBFAddressAugmentationPlugin.java
com.silverpop.engage.augmentation.plugin.UBFAugmentationPlugin.java
com.silverpop.engage.augmentation.plugin.UBFCoordinatesAugmentationPlugin.java
com.silverpop.engage.augmentation.plugin.UBFLocationNameAugmentationPlugin.java
com.silverpop.engage.config.EngageConfigManagerTests.java
com.silverpop.engage.config.EngageConfigManager.java
com.silverpop.engage.config.EngageConfigTest.java
com.silverpop.engage.config.EngageConfig.java
com.silverpop.engage.deeplinking.EngageDeepLinkManager.java
com.silverpop.engage.demo.engagetest.Application.java
com.silverpop.engage.demo.engagetest.EngageNotificationReceiver.java
com.silverpop.engage.demo.engagetest.MainActivity.java
com.silverpop.engage.demo.engagetest.PushReceiver.java
com.silverpop.engage.demo.engagetest.fragment.EngageConfigFragment.java
com.silverpop.engage.demo.engagetest.fragment.UBFAPIFragment.java
com.silverpop.engage.demo.engagetest.fragment.XMLAPIFragment.java
com.silverpop.engage.domain.EngageEvent.java
com.silverpop.engage.domain.JSONable.java
com.silverpop.engage.domain.UBFTests.java
com.silverpop.engage.domain.UBF.java
com.silverpop.engage.domain.XMLAPIEnum.java
com.silverpop.engage.domain.XMLAPITest.java
com.silverpop.engage.domain.XMLAPI.java
com.silverpop.engage.exception.XMLResponseParseException.java
com.silverpop.engage.location.manager.EngageLocationManager.java
com.silverpop.engage.location.manager.plugin.EngageLocationManagerDefault.java
com.silverpop.engage.location.receiver.EngageLocationReceiver.java
com.silverpop.engage.location.receiver.plugin.EngageLocationReceiverBase.java
com.silverpop.engage.location.receiver.plugin.EngageLocationReceiverGeocode.java
com.silverpop.engage.location.receiver.plugin.EngageLocationReceiverHardcodeTest.java
com.silverpop.engage.network.Credential.java
com.silverpop.engage.network.EngageClient.java
com.silverpop.engage.network.RequestCacheWrapper.java
com.silverpop.engage.network.UBFClient.java
com.silverpop.engage.network.XMLAPIClient.java
com.silverpop.engage.response.EngageResponseXMLTests.java
com.silverpop.engage.response.EngageResponseXML.java
com.silverpop.engage.response.XMLAPIResponseNode.java
com.silverpop.engage.store.EngageLocalEventStoreTest.java
com.silverpop.engage.store.EngageLocalEventStore.java
com.silverpop.engage.store.EngageSQLiteHelper.java
com.silverpop.engage.util.EngageExpirationParserTests.java
com.silverpop.engage.util.EngageExpirationParser.java
com.silverpop.engage.util.TimedAsyncTask.java