Search in sources :

Example 6 with Cache

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

the class DatafileService method onStartCommand.

/**
 * @hide
 * @see Service#onStartCommand(Intent, int, int)
 */
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent != null) {
        if (intent.hasExtra(EXTRA_DATAFILE_CONFIG)) {
            String extraDatafileConfig = intent.getStringExtra(EXTRA_DATAFILE_CONFIG);
            DatafileConfig datafileConfig = DatafileConfig.fromJSONString(extraDatafileConfig);
            DatafileClient datafileClient = new DatafileClient(new Client(new OptlyStorage(this.getApplicationContext()), LoggerFactory.getLogger(OptlyStorage.class)), LoggerFactory.getLogger(DatafileClient.class));
            DatafileCache datafileCache = new DatafileCache(datafileConfig.getKey(), new Cache(this.getApplicationContext(), LoggerFactory.getLogger(Cache.class)), LoggerFactory.getLogger(DatafileCache.class));
            String datafileUrl = datafileConfig.getUrl();
            DatafileLoader datafileLoader = new DatafileLoader(this, datafileClient, datafileCache, LoggerFactory.getLogger(DatafileLoader.class));
            datafileLoader.getDatafile(datafileUrl, null);
        } else {
            logger.warn("Data file service received an intent with no project id extra");
        }
    } else {
        logger.warn("Data file service received a null intent");
    }
    return super.onStartCommand(intent, flags, startId);
}
Also used : OptlyStorage(com.optimizely.ab.android.shared.OptlyStorage) DatafileConfig(com.optimizely.ab.android.shared.DatafileConfig) Client(com.optimizely.ab.android.shared.Client) Cache(com.optimizely.ab.android.shared.Cache)

Example 7 with Cache

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

the class DatafileServiceConnection method onServiceConnected.

/**
 * Get the bound {@link DatafileService} and set it up for download.
 *
 * @see ServiceConnection#onServiceConnected(ComponentName, IBinder)
 */
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
    if (!(service instanceof DatafileService.LocalBinder)) {
        return;
    }
    // We've bound to DatafileService, cast the IBinder and get DatafileService instance
    DatafileService.LocalBinder binder = (DatafileService.LocalBinder) service;
    final DatafileService datafileService = binder.getService();
    if (datafileService != null) {
        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.getApplicationContext(), LoggerFactory.getLogger(Cache.class)), LoggerFactory.getLogger(DatafileCache.class));
        DatafileLoader datafileLoader = new DatafileLoader(context, datafileClient, datafileCache, LoggerFactory.getLogger(DatafileLoader.class));
        datafileService.getDatafile(datafileConfig.getUrl(), datafileLoader, listener);
    }
    bound = true;
}
Also used : OptlyStorage(com.optimizely.ab.android.shared.OptlyStorage) Client(com.optimizely.ab.android.shared.Client) Cache(com.optimizely.ab.android.shared.Cache)

Example 8 with Cache

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

the class DatafileWorker method doWork.

@NonNull
@Override
public Result doWork() {
    DatafileConfig datafileConfig = getDataConfig(getInputData());
    String datafileUrl = datafileConfig.getUrl();
    DatafileCache datafileCache = new DatafileCache(datafileConfig.getKey(), new Cache(this.getApplicationContext(), LoggerFactory.getLogger(Cache.class)), LoggerFactory.getLogger(DatafileCache.class));
    datafileLoader.getDatafile(datafileUrl, datafileCache, null);
    return Result.success();
}
Also used : DatafileConfig(com.optimizely.ab.android.shared.DatafileConfig) Cache(com.optimizely.ab.android.shared.Cache) NonNull(androidx.annotation.NonNull)

Example 9 with Cache

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

the class DefaultDatafileHandler method enableUpdateConfigOnNewDatafile.

public synchronized void enableUpdateConfigOnNewDatafile(Context context, DatafileConfig datafileConfig, DatafileLoadedListener listener) {
    // do not restart observer if already set
    if (fileObserver != null) {
        return;
    }
    DatafileCache datafileCache = new DatafileCache(datafileConfig.getKey(), new Cache(context, LoggerFactory.getLogger(Cache.class)), LoggerFactory.getLogger(DatafileCache.class));
    File filesFolder = context.getFilesDir();
    fileObserver = new FileObserver(filesFolder.getPath()) {

        @Override
        public void onEvent(int event, @Nullable String path) {
            logger.debug("EVENT: " + String.valueOf(event) + " " + path + " (" + datafileCache.getFileName() + ")");
            if (event == MODIFY && path.equals(datafileCache.getFileName())) {
                JSONObject newConfig = datafileCache.load();
                if (newConfig == null) {
                    logger.error("Cached datafile is empty or corrupt");
                    return;
                }
                String config = newConfig.toString();
                setDatafile(config);
                if (listener != null) {
                    listener.onDatafileLoaded(config);
                }
            }
        }
    };
    fileObserver.startWatching();
}
Also used : JSONObject(org.json.JSONObject) File(java.io.File) Cache(com.optimizely.ab.android.shared.Cache) FileObserver(android.os.FileObserver)

Example 10 with Cache

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

the class DefaultDatafileHandler method saveDatafile.

/**
 * Save the datafile to cache.
 *
 * @param context   application context
 * @param datafileConfig DatafileConfig for the datafile
 * @param dataFile  the datafile to save
 */
public void saveDatafile(Context context, DatafileConfig datafileConfig, String dataFile) {
    DatafileCache datafileCache = new DatafileCache(datafileConfig.getKey(), new Cache(context, LoggerFactory.getLogger(Cache.class)), LoggerFactory.getLogger(DatafileCache.class));
    datafileCache.delete();
    datafileCache.save(dataFile);
}
Also used : Cache(com.optimizely.ab.android.shared.Cache)

Aggregations

Cache (com.optimizely.ab.android.shared.Cache)28 Test (org.junit.Test)10 Logger (org.slf4j.Logger)9 Before (org.junit.Before)8 DatafileConfig (com.optimizely.ab.android.shared.DatafileConfig)6 Client (com.optimizely.ab.android.shared.Client)5 ExecutorService (java.util.concurrent.ExecutorService)5 Context (android.content.Context)3 OptlyStorage (com.optimizely.ab.android.shared.OptlyStorage)3 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 JSONObject (org.json.JSONObject)3 SdkSuppress (androidx.test.filters.SdkSuppress)2 JSONException (org.json.JSONException)2 Intent (android.content.Intent)1 FileObserver (android.os.FileObserver)1 IBinder (android.os.IBinder)1 NonNull (androidx.annotation.NonNull)1 ListenableWorker (androidx.work.ListenableWorker)1 File (java.io.File)1