Search in sources :

Example 6 with OptlyStorage

use of com.optimizely.ab.android.shared.OptlyStorage in project android-sdk by optimizely.

the class EventIntentService method onCreate.

/**
 * Create the event dispatcher {@link EventDispatcher}
 * @see IntentService#onCreate()
 */
@Override
public void onCreate() {
    super.onCreate();
    OptlyStorage optlyStorage = new OptlyStorage(this);
    EventClient eventClient = new EventClient(new Client(optlyStorage, LoggerFactory.getLogger(Client.class)), LoggerFactory.getLogger(EventClient.class));
    EventDAO eventDAO = EventDAO.getInstance(this, "1", LoggerFactory.getLogger(EventDAO.class));
    ServiceScheduler serviceScheduler = new ServiceScheduler(this, new ServiceScheduler.PendingIntentFactory(this), LoggerFactory.getLogger(ServiceScheduler.class));
    eventDispatcher = new EventDispatcher(this, optlyStorage, eventDAO, eventClient, serviceScheduler, LoggerFactory.getLogger(EventDispatcher.class));
}
Also used : OptlyStorage(com.optimizely.ab.android.shared.OptlyStorage) Client(com.optimizely.ab.android.shared.Client) ServiceScheduler(com.optimizely.ab.android.shared.ServiceScheduler)

Example 7 with OptlyStorage

use of com.optimizely.ab.android.shared.OptlyStorage in project android-sdk by optimizely.

the class DatafileLoader method getDatafile.

public void getDatafile(@NonNull String datafileUrl, @Nullable DatafileLoadedListener datafileLoadedListener) {
    if (datafileCache == null) {
        logger.warn("DatafileCache is not set.");
        return;
    }
    if (!allowDownload(datafileUrl, datafileLoadedListener)) {
        return;
    }
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    executorService.execute(new Runnable() {

        @Override
        public void run() {
            // if there is a problem with the cached datafile, set last modified to 1970
            if (!datafileCache.exists() || (datafileCache.exists() && datafileCache.load() == null)) {
                // create a wrapper for application context default storage.
                OptlyStorage storage = new OptlyStorage(context);
                // set the last modified for this url to 1 millisecond past Jan 1, 1970.
                storage.saveLong(datafileUrl, 1);
            }
            String dataFile = datafileClient.request(datafileUrl);
            if (dataFile != null && !dataFile.isEmpty()) {
                if (datafileCache.exists()) {
                    if (!datafileCache.delete()) {
                        logger.warn("Unable to delete old datafile");
                    }
                }
                if (!datafileCache.save(dataFile)) {
                    logger.warn("Unable to save new datafile");
                }
            } else {
                String cachedDatafile = getCachedDatafile();
                if (cachedDatafile != null) {
                    dataFile = cachedDatafile;
                }
            }
            // notify the listener asap and don't wait for the storage update of the time.
            notifyListener(datafileLoadedListener, dataFile);
            // set the download time and don't allow downloads to overlap less than a minute
            saveDownloadTime(datafileUrl);
            logger.info("Refreshing data file");
        }
    });
}
Also used : OptlyStorage(com.optimizely.ab.android.shared.OptlyStorage) ExecutorService(java.util.concurrent.ExecutorService)

Example 8 with OptlyStorage

use of com.optimizely.ab.android.shared.OptlyStorage in project android-sdk by optimizely.

the class DefaultDatafileHandler method downloadDatafile.

/**
 * Asynchronous download data file.
 * <p>
 * We create a DatafileService intent, create a DataService connection, and bind it to the application context.
 * After we receive the datafile, we unbind the service and cleanup the service connection.
 * This gets the project file from the Optimizely CDN.
 *
 * @param context   application context
 * @param datafileConfig DatafileConfig for the datafile to get
 * @param listener  listener to call when datafile download complete
 */
public void downloadDatafile(final Context context, DatafileConfig datafileConfig, final DatafileLoadedListener listener) {
    DatafileClient datafileClient = new DatafileClient(new Client(new OptlyStorage(context.getApplicationContext()), LoggerFactory.getLogger(OptlyStorage.class)), LoggerFactory.getLogger(DatafileClient.class));
    DatafileCache datafileCache = new DatafileCache(datafileConfig.getKey(), new Cache(context, LoggerFactory.getLogger(Cache.class)), LoggerFactory.getLogger(DatafileCache.class));
    String datafileUrl = datafileConfig.getUrl();
    DatafileLoader datafileLoader = new DatafileLoader(context, datafileClient, datafileCache, LoggerFactory.getLogger(DatafileLoader.class));
    datafileLoader.getDatafile(datafileUrl, new DatafileLoadedListener() {

        @Override
        public void onDatafileLoaded(@Nullable String dataFile) {
            if (listener != null) {
                listener.onDatafileLoaded(dataFile);
            }
        }
    });
}
Also used : OptlyStorage(com.optimizely.ab.android.shared.OptlyStorage) Client(com.optimizely.ab.android.shared.Client) Cache(com.optimizely.ab.android.shared.Cache)

Aggregations

OptlyStorage (com.optimizely.ab.android.shared.OptlyStorage)8 Client (com.optimizely.ab.android.shared.Client)5 Cache (com.optimizely.ab.android.shared.Cache)3 ServiceScheduler (com.optimizely.ab.android.shared.ServiceScheduler)2 AlarmManager (android.app.AlarmManager)1 JobScheduler (android.app.job.JobScheduler)1 Context (android.content.Context)1 RequiresApi (androidx.annotation.RequiresApi)1 DatafileConfig (com.optimizely.ab.android.shared.DatafileConfig)1 ExecutorService (java.util.concurrent.ExecutorService)1 Before (org.junit.Before)1 Logger (org.slf4j.Logger)1