Skip to content

Commit 1d88b41

Browse files
update for android o after device tests. updated the change log and … (#160)
* update for android o after device tests. updated the change log * fix raw datafile name * cleanup javadoc and testapp examples * move the register of the event rescheduler
1 parent acf4522 commit 1d88b41

File tree

5 files changed

+66
-54
lines changed

5 files changed

+66
-54
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Optimizely Android X SDK Changelog
22
### 1.5.0
3-
September 26, 2017
3+
October 30, 2017
44

55
- Release 1.5.0
66

@@ -24,6 +24,8 @@ September 26, 2017
2424

2525
- Same as 1.4.0 see below.
2626
- Need to add permissions to both receivers in your manifest if you plan on using the EventRescheduler or the DatafileRescheduler (see test_app manifest for example) https://github.com/optimizely/android-sdk/blob/master/test-app/src/main/AndroidManifest.xml
27+
- Updated build tools and target to API 26
28+
- Also for Android O, you must register for the SUPPLICANT_CONNECTION_CHANGE_ACTION intent filter in code (see the test-app for an example).
2729

2830
### 1.4.0
2931
August 9, 2017

android-sdk/src/main/java/com/optimizely/ab/android/sdk/OptimizelyManager.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,15 @@ private void notifyStartListener() {
120120
}
121121

122122
/**
123-
* Initialize Optimizely Synchronously
123+
* Initialize Optimizely Synchronously using the datafile passed in while downloading the latest datafile in the background from the CDN to cache.
124+
* It should be noted that even though it initiates a download of the datafile to cache, this method does not use that cached datafile.
125+
* You can always test if a datafile exists in cache with {@link #isDatafileCached(Context)}.
124126
* <p>
125-
* Instantiates and returns an {@link OptimizelyClient} instance. Will also cache the instance
127+
* Instantiates and returns an {@link OptimizelyClient} instance. It will also cache the instance
126128
* for future lookups via getClient
127129
*
128130
* @param context any {@link Context} instance
129-
* @param datafile the datafile
131+
* @param datafile the datafile used to initialize the OptimizelyClient.
130132
* @return an {@link OptimizelyClient} instance
131133
*/
132134
public OptimizelyClient initialize(@NonNull Context context, @NonNull String datafile) {
@@ -150,7 +152,10 @@ public OptimizelyClient initialize(@NonNull Context context, @NonNull String dat
150152
}
151153

152154
/**
153-
* Initialize Optimizely Synchronously
155+
* Initialize Optimizely Synchronously by loading the resource, use it to initialize Optimizely,
156+
* and downloading the latest datafile from the CDN in the background to cache.
157+
* It should be noted that even though it initiates a download of the datafile to cache, this method does not use that cached datafile.
158+
* You can always test if a datafile exists in cache with {@link #isDatafileCached(Context)}.
154159
* <p>
155160
* Instantiates and returns an {@link OptimizelyClient} instance. Will also cache the instance
156161
* for future lookups via getClient. The datafile should be stored in res/raw.
@@ -173,7 +178,8 @@ public OptimizelyClient initialize(@NonNull Context context, @RawRes int datafil
173178
}
174179

175180
/**
176-
* Initialize Optimizely Synchronously
181+
* Initialize Optimizely Synchronously. This one uses the cached datafile and expects it to exist.
182+
* You can use {@link #isDatafileCached(Context)} to see if the datafile exists in cache prior to this call.
177183
* <p>
178184
* Instantiates and returns an {@link OptimizelyClient} instance using the datafile cached on disk
179185
* if not available then it will return a dummy instance.

test-app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
2424
<uses-permission android:name="android.permission.WAKE_LOCK" />
2525
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
26+
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
2627

2728
<application
2829
android:name=".MyApplication"

test-app/src/main/java/com/optimizely/ab/android/test_app/SplashScreenActivity.java

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616
package com.optimizely.ab.android.test_app;
1717

1818
import android.content.Intent;
19+
import android.content.IntentFilter;
20+
import android.net.wifi.WifiManager;
1921
import android.support.annotation.Nullable;
2022
import android.support.v7.app.AppCompatActivity;
2123
import android.os.Bundle;
2224

25+
import com.optimizely.ab.android.event_handler.EventRescheduler;
2326
import com.optimizely.ab.android.sdk.OptimizelyClient;
2427
import com.optimizely.ab.android.sdk.OptimizelyManager;
2528
import com.optimizely.ab.android.sdk.OptimizelyStartListener;
@@ -49,57 +52,57 @@ protected void onCreate(Bundle savedInstanceState) {
4952
protected void onStart() {
5053
super.onStart();
5154

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+
}
9596
}
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());
96102
}
103+
});
104+
}
97105

98-
startActivity(intent);
99106

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-
});
104107
}
105108
}

0 commit comments

Comments
 (0)