|
| 1 | +/**************************************************************************** |
| 2 | + * Copyright 2020, Optimizely, Inc. and contributors * |
| 3 | + * * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); * |
| 5 | + * you may not use this file except in compliance with the License. * |
| 6 | + * You may obtain a copy of the License at * |
| 7 | + * * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 * |
| 9 | + * * |
| 10 | + * Unless required by applicable law or agreed to in writing, software * |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, * |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * |
| 13 | + * See the License for the specific language governing permissions and * |
| 14 | + * limitations under the License. * |
| 15 | + ***************************************************************************/ |
| 16 | + |
| 17 | +package com.optimizely.ab.android.test_app; |
| 18 | + |
| 19 | +import android.content.Context; |
| 20 | +import android.util.Log; |
| 21 | + |
| 22 | +import com.optimizely.ab.android.sdk.OptimizelyClient; |
| 23 | +import com.optimizely.ab.android.sdk.OptimizelyManager; |
| 24 | +import com.optimizely.ab.android.sdk.OptimizelyStartListener; |
| 25 | +import com.optimizely.ab.config.Variation; |
| 26 | + |
| 27 | +import java.util.concurrent.TimeUnit; |
| 28 | + |
| 29 | +public class SamplesForAPI { |
| 30 | + |
| 31 | + static public void samplesForInitialization(Context context) { |
| 32 | + OptimizelyManager optimizelyManager; |
| 33 | + OptimizelyClient optimizelyClient; |
| 34 | + Variation variation; |
| 35 | + |
| 36 | + // These are sample codes for synchronous and asynchronous SDK initializations with multiple options |
| 37 | + |
| 38 | + // [Synchronous] |
| 39 | + |
| 40 | + // [S1] Synchronous initialization |
| 41 | + // 1. SDK is initialized instantly with a cached (or bundled) datafile |
| 42 | + // 2. A new datafile can be downloaded in background and cached after the SDK is initialized. |
| 43 | + // The cached datafile will be used only when the SDK re-starts in the next session. |
| 44 | + optimizelyManager = OptimizelyManager.builder() |
| 45 | + .withSDKKey("<Your_SDK_Key>") |
| 46 | + .build(context); |
| 47 | + optimizelyClient = optimizelyManager.initialize(context, R.raw.datafile); |
| 48 | + variation = optimizelyClient.activate("<Experiment_Key>", "<User_ID>"); |
| 49 | + |
| 50 | + // [S2] Synchronous initialization |
| 51 | + // 1. SDK is initialized instantly with a cached (or bundled) datafile |
| 52 | + // 2. A new datafile can be downloaded in background and cached after the SDK is initialized. |
| 53 | + // The cached datafile is used immediately to update the SDK project config. |
| 54 | + optimizelyManager = OptimizelyManager.builder() |
| 55 | + .withSDKKey("<Your_SDK_Key>") |
| 56 | + .build(context); |
| 57 | + optimizelyClient = optimizelyManager.initialize(context, R.raw.datafile, true, true); |
| 58 | + variation = optimizelyClient.activate("<Experiment_Key>", "<User_ID>"); |
| 59 | + |
| 60 | + // [S3] Synchronous initialization |
| 61 | + // 1. SDK is initialized instantly with a cached (or bundled) datafile |
| 62 | + // 2. A new datafile can be downloaded in background and cached after the SDK is initialized. |
| 63 | + // The cached datafile is used immediately to update the SDK project config. |
| 64 | + // 3. Polling datafile periodically. |
| 65 | + // The cached datafile is used immediately to update the SDK project config. |
| 66 | + optimizelyManager = OptimizelyManager.builder() |
| 67 | + .withSDKKey("<Your_SDK_Key>") |
| 68 | + .withDatafileDownloadInterval(TimeUnit.MINUTES.toSeconds(15)) |
| 69 | + .build(context); |
| 70 | + optimizelyClient = optimizelyManager.initialize(context, R.raw.datafile, true, true); |
| 71 | + variation = optimizelyClient.activate("<Experiment_Key>", "<User_ID>"); |
| 72 | + |
| 73 | + // [Asynchronous] |
| 74 | + |
| 75 | + // [A1] Asynchronous initialization |
| 76 | + // 1. A datafile is downloaded from the server and the SDK is initialized with the datafile |
| 77 | + optimizelyManager = OptimizelyManager.builder() |
| 78 | + .withSDKKey("<Your_SDK_Key>") |
| 79 | + .build(context); |
| 80 | + optimizelyManager.initialize(context, null, (OptimizelyClient client) -> { |
| 81 | + Variation variation2 = client.activate("<Experiment_Key>", "<User_ID>"); |
| 82 | + }); |
| 83 | + |
| 84 | + // [A2] Asynchronous initialization |
| 85 | + // 1. A datafile is downloaded from the server and the SDK is initialized with the datafile |
| 86 | + // 2. Polling datafile periodically. |
| 87 | + // The cached datafile is used immediately to update the SDK project config. |
| 88 | + optimizelyManager = OptimizelyManager.builder() |
| 89 | + .withSDKKey("<Your_SDK_Key>") |
| 90 | + .withDatafileDownloadInterval(TimeUnit.MINUTES.toSeconds(15)) |
| 91 | + .build(context); |
| 92 | + optimizelyManager.initialize(context, null, (OptimizelyClient client) -> { |
| 93 | + Variation variation2 = client.activate("<Experiment_Key>", "<User_ID>"); |
| 94 | + }); |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments