|
16 | 16 | package com.optimizely.ab.android.test_app; |
17 | 17 |
|
18 | 18 | import android.content.Intent; |
| 19 | +import android.content.IntentFilter; |
| 20 | +import android.net.wifi.WifiManager; |
19 | 21 | import android.support.annotation.Nullable; |
20 | 22 | import android.support.v7.app.AppCompatActivity; |
21 | 23 | import android.os.Bundle; |
22 | 24 |
|
| 25 | +import com.optimizely.ab.android.event_handler.EventRescheduler; |
23 | 26 | import com.optimizely.ab.android.sdk.OptimizelyClient; |
24 | 27 | import com.optimizely.ab.android.sdk.OptimizelyManager; |
25 | 28 | import com.optimizely.ab.android.sdk.OptimizelyStartListener; |
@@ -49,57 +52,57 @@ protected void onCreate(Bundle savedInstanceState) { |
49 | 52 | protected void onStart() { |
50 | 53 | super.onStart(); |
51 | 54 |
|
52 | | -// if (optimizelyManager.isDatafileCached(this) == true) { |
53 | | -// optimizelyManager.initialize(this); |
54 | | -// String userId = myApplication.getAnonUserId(); |
55 | | -// Variation backgroundVariation = optimizelyManager.getOptimizely().activate("background_experiment", userId); |
56 | | -// Intent intent = null; |
57 | | -// // variation is nullable so we should check for null values |
58 | | -// if (backgroundVariation != null) { |
59 | | -// // Show activity based on the variation the user got bucketed into |
60 | | -// if (backgroundVariation.getKey().equals("variation_a")) { |
61 | | -// intent = new Intent(myApplication.getBaseContext(), VariationAActivity.class); |
62 | | -// } else if (backgroundVariation.getKey().equals("variation_b")) { |
63 | | -// intent = new Intent(myApplication.getBaseContext(), VariationBActivity.class); |
64 | | -// } |
65 | | -// } |
66 | | -// |
67 | | -// startActivity(intent); |
68 | | -// |
69 | | -// return; |
70 | | -// } |
71 | | - |
72 | | - // Initialize Optimizely asynchronously |
73 | | - optimizelyManager.initialize(this, new OptimizelyStartListener() { |
74 | | - |
75 | | - @Override |
76 | | - public void onStart(OptimizelyClient optimizely) { |
77 | | - // this is the control variation, it will show if we are not able to determine which variation to bucket the user into |
78 | | - Intent intent = new Intent(myApplication.getBaseContext(), ActivationErrorActivity.class); |
79 | | - |
80 | | - // Activate user and start activity based on the variation we get. |
81 | | - // You can pass in any string for the user ID. In this example we just use a convenience method to generate a random one. |
82 | | - String userId = myApplication.getAnonUserId(); |
83 | | - Variation backgroundVariation = optimizelyManager.getOptimizely().activate("background_experiment", userId); |
84 | | - |
85 | | - // Utility method for verifying event dispatches in our automated tests |
86 | | - CountingIdlingResourceManager.increment(); // increment for impression event |
87 | | - |
88 | | - // variation is nullable so we should check for null values |
89 | | - if (backgroundVariation != null) { |
90 | | - // Show activity based on the variation the user got bucketed into |
91 | | - if (backgroundVariation.getKey().equals("variation_a")) { |
92 | | - intent = new Intent(myApplication.getBaseContext(), VariationAActivity.class); |
93 | | - } else if (backgroundVariation.getKey().equals("variation_b")) { |
94 | | - intent = new Intent(myApplication.getBaseContext(), VariationBActivity.class); |
| 55 | + boolean INITIALIZE_ASYNCHRONOUSLY = true; |
| 56 | + |
| 57 | + // with the new Android O differences, you need to register the service for the intent filter you desire in code instead of |
| 58 | + // in the manifest. |
| 59 | + EventRescheduler eventRescheduler = new EventRescheduler(); |
| 60 | + |
| 61 | + getApplicationContext().registerReceiver(eventRescheduler, new IntentFilter(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)); |
| 62 | + |
| 63 | + // Example of initialize from raw file and use cached file after that. |
| 64 | + if (!INITIALIZE_ASYNCHRONOUSLY) { |
| 65 | + |
| 66 | + if (optimizelyManager.isDatafileCached(myApplication)) { |
| 67 | + optimizelyManager.initialize(myApplication); |
| 68 | + } else { |
| 69 | + optimizelyManager.initialize(myApplication, R.raw.datafile); |
| 70 | + } |
| 71 | + } else { |
| 72 | + // Initialize Optimizely asynchronously |
| 73 | + optimizelyManager.initialize(this, new OptimizelyStartListener() { |
| 74 | + |
| 75 | + @Override |
| 76 | + public void onStart(OptimizelyClient optimizely) { |
| 77 | + // this is the control variation, it will show if we are not able to determine which variation to bucket the user into |
| 78 | + Intent intent = new Intent(myApplication.getBaseContext(), ActivationErrorActivity.class); |
| 79 | + |
| 80 | + // Activate user and start activity based on the variation we get. |
| 81 | + // You can pass in any string for the user ID. In this example we just use a convenience method to generate a random one. |
| 82 | + String userId = myApplication.getAnonUserId(); |
| 83 | + Variation backgroundVariation = optimizelyManager.getOptimizely().activate("background_experiment", userId); |
| 84 | + |
| 85 | + // Utility method for verifying event dispatches in our automated tests |
| 86 | + CountingIdlingResourceManager.increment(); // increment for impression event |
| 87 | + |
| 88 | + // variation is nullable so we should check for null values |
| 89 | + if (backgroundVariation != null) { |
| 90 | + // Show activity based on the variation the user got bucketed into |
| 91 | + if (backgroundVariation.getKey().equals("variation_a")) { |
| 92 | + intent = new Intent(myApplication.getBaseContext(), VariationAActivity.class); |
| 93 | + } else if (backgroundVariation.getKey().equals("variation_b")) { |
| 94 | + intent = new Intent(myApplication.getBaseContext(), VariationBActivity.class); |
| 95 | + } |
95 | 96 | } |
| 97 | + |
| 98 | + startActivity(intent); |
| 99 | + |
| 100 | + //call this method if you set an interval but want to now stop doing bakcground updates. |
| 101 | + //optimizelyManager.getDatafileHandler().stopBackgroundUpdates(myApplication.getApplicationContext(), optimizelyManager.getProjectId()); |
96 | 102 | } |
| 103 | + }); |
| 104 | + } |
97 | 105 |
|
98 | | - startActivity(intent); |
99 | 106 |
|
100 | | - //call this method if you set an interval but want to now stop doing bakcground updates. |
101 | | - //optimizelyManager.getDatafileHandler().stopBackgroundUpdates(myApplication.getApplicationContext(), optimizelyManager.getProjectId()); |
102 | | - } |
103 | | - }); |
104 | 107 | } |
105 | 108 | } |
0 commit comments