Skip to content

Commit d414366

Browse files
fixup docs and cleanup
1 parent c4b2b8d commit d414366

File tree

14 files changed

+188
-41
lines changed

14 files changed

+188
-41
lines changed

datafile-handler/src/main/AndroidManifest.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
</service>
3030
<!--
3131
Add these lines to your manifest if you want the services to schedule themselves again after a boot
32-
or package replace.
32+
or package replace. Without these lines in your application manifest, the DatafileRescheduler will not be used.
33+
See {link test_app} AndroidManifest.xml as an example.
3334
3435
<receiver
3536
android:name="DatafileRescheduler"

datafile-handler/src/main/java/com/optimizely/ab/android/datafile_handler/BackgroundWatchersCache.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.optimizely.ab.android.datafile_handler;
1818

19+
import android.content.Context;
1920
import android.support.annotation.NonNull;
2021
import android.support.annotation.Nullable;
2122

@@ -31,17 +32,33 @@
3132

3233
/**
3334
* Caches a json dict that saves state about which project IDs have background watching enabled.
35+
* This is used by the rescheduler to determine if backgrounding was on for a project id. If backgrounding is on,
36+
* then when the device is restarted or the app is reinstalled, the rescheduler will kick in and reschedule the datafile background
37+
* download. In order to use this the rescheduler needs to be included in the application manifest.
38+
* Calling {@link DatafileHandler#stopBackgroundUpdates(Context, String)} sets this background cache to false.
3439
*/
3540
class BackgroundWatchersCache {
3641
static final String BACKGROUND_WATCHERS_FILE_NAME = "optly-background-watchers.json";
3742
@NonNull private final Cache cache;
3843
@NonNull private final Logger logger;
3944

45+
/**
46+
* Create BackgroundWatchersCache Object.
47+
*
48+
* @param cache object for caching project id and whether watched or not.
49+
* @param logger the logger to log errors and warnings.
50+
*/
4051
BackgroundWatchersCache(@NonNull Cache cache, @NonNull Logger logger) {
4152
this.cache = cache;
4253
this.logger = logger;
4354
}
4455

56+
/**
57+
* Set the watching flag for the proejct id.
58+
* @param projectId project id to set watching.
59+
* @param watching flag to signify if the project is running in the background.
60+
* @return boolean indicating whether the set succeed or not
61+
*/
4562
boolean setIsWatching(@NonNull String projectId, boolean watching) {
4663
if (projectId.isEmpty()) {
4764
logger.error("Passed in an empty string for projectId");
@@ -61,6 +78,11 @@ boolean setIsWatching(@NonNull String projectId, boolean watching) {
6178
return false;
6279
}
6380

81+
/**
82+
* Return if the project is set to be watched in the background or not.
83+
* @param projectId project id to test
84+
* @return true if it has backgrounding, false if not.
85+
*/
6486
boolean isWatching(@NonNull String projectId) {
6587
if (projectId.isEmpty()) {
6688
logger.error("Passed in an empty string for projectId");
@@ -81,6 +103,10 @@ boolean isWatching(@NonNull String projectId) {
81103
return false;
82104
}
83105

106+
/**
107+
* Get a list of all project ids that are being watched for backgrounding.
108+
* @return a list of project ids
109+
*/
84110
List<String> getWatchingProjectIds() {
85111
List<String> projectIds = new ArrayList<>();
86112
try {
@@ -101,6 +127,11 @@ List<String> getWatchingProjectIds() {
101127
return projectIds;
102128
}
103129

130+
/**
131+
* Load the JSONObject from cache
132+
* @return JSONObject if successful. JSONObject can be empty
133+
* @throws JSONException if there was a problem parsing the JSON
134+
*/
104135
@Nullable
105136
private JSONObject load() throws JSONException {
106137
String backGroundWatchersFile = cache.load(BACKGROUND_WATCHERS_FILE_NAME);
@@ -112,10 +143,19 @@ private JSONObject load() throws JSONException {
112143
return new JSONObject(backGroundWatchersFile);
113144
}
114145

146+
/**
147+
* Delete the background watchers cache file.
148+
* @return true if successful and false if it failed.
149+
*/
115150
protected boolean delete() {
116151
return cache.delete(BACKGROUND_WATCHERS_FILE_NAME);
117152
}
118153

154+
/**
155+
* Save the JSON string to the background cache file.
156+
* @param backgroundWatchersJson JSON string containing projectid and whether watched or not.
157+
* @return true if successful.
158+
*/
119159
private boolean save(String backgroundWatchersJson) {
120160
logger.info("Saving background watchers file {}.", BACKGROUND_WATCHERS_FILE_NAME);
121161
boolean saved = cache.save(BACKGROUND_WATCHERS_FILE_NAME, backgroundWatchersJson);

datafile-handler/src/main/java/com/optimizely/ab/android/datafile_handler/DatafileCache.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.slf4j.Logger;
2828

2929
/**
30-
* Abstracts the actual data "file" {@link java.io.File}.
30+
* Abstracts the actual data "file" {@link java.io.File}. to a cached file
3131
*/
3232
public class DatafileCache {
3333

@@ -37,25 +37,47 @@ public class DatafileCache {
3737
@NonNull private final String filename;
3838
@NonNull private final Logger logger;
3939

40+
/**
41+
* Create a DatafileCache Object
42+
* @param projectId project id for cache
43+
* @param cache shared generic file based {link Cache}
44+
* @param logger logger to use
45+
*/
4046
public DatafileCache(@NonNull String projectId, @NonNull Cache cache, @NonNull Logger logger) {
4147
this.cache = cache;
4248
this.filename = String.format(FILENAME, projectId);
4349
this.logger = logger;
4450
}
4551

52+
/**
53+
* Delete the datafile cache
54+
* @return true if successful
55+
*/
4656
public boolean delete() {
4757
return cache.delete(filename);
4858
}
4959

60+
/**
61+
* Check to see if the datafile cache exists
62+
* @return true if it exists
63+
*/
5064
public boolean exists() {
5165
return cache.exists(filename);
5266
}
5367

68+
/**
69+
* Return the filename to the datafile cache
70+
* @return filename for datafile cache
71+
*/
5472
@VisibleForTesting
5573
public String getFileName() {
5674
return filename;
5775
}
5876

77+
/**
78+
* Loads the datafile from cache into a JSONObject
79+
* @return JSONObject if exists or nulll if it doesn't or there was a problem
80+
*/
5981
@Nullable
6082
public JSONObject load() {
6183
String datafile = cache.load(filename);
@@ -71,6 +93,11 @@ public JSONObject load() {
7193
}
7294
}
7395

96+
/**
97+
* Save a datafile to cache.
98+
* @param dataFile to write to cache
99+
* @return true if successful.
100+
*/
74101
public boolean save(String dataFile) {
75102
return cache.save(filename, dataFile);
76103
}

datafile-handler/src/main/java/com/optimizely/ab/android/datafile_handler/DatafileClient.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public class DatafileClient {
3535
@NonNull private final Client client;
3636
@NonNull private final Logger logger;
3737

38+
/**
39+
* Create a DatafileClient Object.
40+
* @param client Shared {@link Client} object
41+
* @param logger logger for logging.
42+
*/
3843
public DatafileClient(@NonNull Client client, @NonNull Logger logger) {
3944
this.client = client;
4045
this.logger = logger;

datafile-handler/src/main/java/com/optimizely/ab/android/datafile_handler/DatafileRescheduler.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,22 @@
3131
/**
3232
* Broadcast Receiver that handles app upgrade and phone restart broadcasts in order
3333
* to reschedule {@link DatafileService}
34+
* In order to use this class you must include the declaration in your AndroidManifest.xml.
35+
* <pre>
36+
* {@code
37+
* <receiver
38+
* android:name="DatafileRescheduler"
39+
* android:enabled="true"
40+
* android:exported="false">
41+
* <intent-filter>
42+
* <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
43+
* <action android:name="android.intent.action.BOOT_COMPLETED" />
44+
* </intent-filter>
45+
* </receiver>
46+
* }
47+
* </pre>
3448
*
35-
* @hide
49+
* as well as set the download interval for datafile download in the Optimizely builder.
3650
*/
3751
public class DatafileRescheduler extends BroadcastReceiver {
3852
Logger logger = LoggerFactory.getLogger(DatafileRescheduler.class);

datafile-handler/src/main/java/com/optimizely/ab/android/datafile_handler/DatafileService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535

3636
/**
3737
* Service that handles loading the datafile from cache or downloads it from the CDN
38-
*
39-
* @hide
38+
* These services will only be used if you are using our {@link DefaultDatafileHandler}.
39+
* You can chose to implement your own handler and use all or part of this package.
4040
*/
4141
public class DatafileService extends Service {
4242
/**

datafile-handler/src/main/java/com/optimizely/ab/android/datafile_handler/DatafileServiceConnection.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232

3333
import java.util.concurrent.Executors;
3434

35+
/**
36+
* The DatafileServiceConnection is used to bind to a DatafileService. The DatafileService does that actual download.
37+
* The Service Connection kicks off the service after being connected. The connection is unbound after a successful download.
38+
*/
3539
public class DatafileServiceConnection implements ServiceConnection {
3640

3741
@NonNull private final Context context;
@@ -40,14 +44,21 @@ public class DatafileServiceConnection implements ServiceConnection {
4044

4145
private boolean bound = false;
4246

47+
/**
48+
* Create a datafile service connection object.
49+
* @param projectId project id for service
50+
* @param context current application context.
51+
* @param listener listener to call after service download has completed.
52+
*/
4353
public DatafileServiceConnection(@NonNull String projectId, @NonNull Context context, @NonNull DatafileLoadedListener listener) {
4454
this.projectId = projectId;
4555
this.context = context;
4656
this.listener = listener;
4757
}
4858

4959
/**
50-
* @hide
60+
* Get the bound {@link DatafileService} and set it up for download.
61+
*
5162
* @see ServiceConnection#onServiceConnected(ComponentName, IBinder)
5263
*/
5364
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
@@ -84,7 +95,7 @@ public void onServiceConnected(ComponentName className,
8495
}
8596

8697
/**
87-
* @hide
98+
* Call stop on the listener after the service has been disconnected.
8899
* @see ServiceConnection#onServiceDisconnected(ComponentName)
89100
*/
90101
@Override
@@ -93,10 +104,18 @@ public void onServiceDisconnected(ComponentName arg0) {
93104
listener.onStop(context);
94105
}
95106

107+
/**
108+
* Is the service bound?
109+
* @return true if it is bound.
110+
*/
96111
public boolean isBound() {
97112
return bound;
98113
}
99114

115+
/**
116+
* Set whether the service is bound or not.
117+
* @param bound boolean flag.
118+
*/
100119
public void setBound(Boolean bound) {
101120
this.bound = bound;
102121
}

event-handler/src/main/java/com/optimizely/ab/android/event_handler/DefaultEventHandler.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
/**
3030
* Reference implementation of {@link EventHandler} for Android.
3131
* <p>
32-
* This is the main entry point to the Android Module
32+
* This is the main entry point to the Android Module. This event handler creates a service intent and starts it in the passed
33+
* in context. The intent service will attempt to send any and all queued events.
3334
*/
3435
public class DefaultEventHandler implements EventHandler {
3536

@@ -38,6 +39,10 @@ public class DefaultEventHandler implements EventHandler {
3839
Logger logger = LoggerFactory.getLogger(DefaultEventHandler.class);
3940
private long dispatchInterval = -1;
4041

42+
/**
43+
* Private constructor
44+
* @param context current context for service.
45+
*/
4146
private DefaultEventHandler(@NonNull Context context) {
4247
this.context = context;
4348
}

event-handler/src/main/java/com/optimizely/ab/android/event_handler/EventClient.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ class EventClient {
3939
this.logger = logger;
4040
}
4141

42+
/**
43+
* Attempt to send the event to the event url.
44+
* @param event to send
45+
* @return true if successful
46+
*/
4247
boolean sendEvent(final Event event) {
4348
Client.Request<Boolean> request = new Client.Request<Boolean>() {
4449
@Override

event-handler/src/main/java/com/optimizely/ab/android/event_handler/EventDAO.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,43 @@
3434

3535
/**
3636
* Handles interactions with the SQLiteDatabase that store {@link Event} instances.
37+
* This is the event queue for Android.
3738
*/
3839
class EventDAO {
3940

4041
@NonNull
4142
final Logger logger;
4243
@NonNull private final EventSQLiteOpenHelper dbHelper;
4344

45+
/**
46+
* Private constructor
47+
*
48+
* @param dbHelper helper for SQLite calls.
49+
* @param logger where to log errors and warnings.
50+
*/
4451
private EventDAO(@NonNull EventSQLiteOpenHelper dbHelper, @NonNull Logger logger) {
4552
this.dbHelper = dbHelper;
4653
this.logger = logger;
4754
}
4855

56+
/**
57+
* Static initializer for EventDAO.
58+
* @param context current context
59+
* @param projectId current project id
60+
* @param logger where to log errors and warnings.
61+
* @return a new instance of EventDAO.
62+
*/
4963
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
5064
static EventDAO getInstance(@NonNull Context context, @NonNull String projectId, @NonNull Logger logger) {
5165
EventSQLiteOpenHelper sqLiteOpenHelper = new EventSQLiteOpenHelper(context, projectId, null, EventSQLiteOpenHelper.VERSION, LoggerFactory.getLogger(EventSQLiteOpenHelper.class));
5266
return new EventDAO(sqLiteOpenHelper, logger);
5367
}
5468

69+
/**
70+
* Store an event in SQLite
71+
* @param event to store
72+
* @return true if successful
73+
*/
5574
boolean storeEvent(@NonNull Event event) {
5675
logger.info("Inserting {} into db", event);
5776
ContentValues values = new ContentValues();
@@ -74,6 +93,10 @@ boolean storeEvent(@NonNull Event event) {
7493
return false;
7594
}
7695

96+
/**
97+
* Get all the events in the SQLite queue
98+
* @return a list of events.
99+
*/
77100
List<Pair<Long, Event>> getEvents() {
78101
List<Pair<Long, Event>> events = new LinkedList<>();
79102

@@ -139,6 +162,11 @@ List<Pair<Long, Event>> getEvents() {
139162
return events;
140163
}
141164

165+
/**
166+
* Remove an event from SQLite db.
167+
* @param eventId id of the event to remove
168+
* @return true on success.
169+
*/
142170
boolean removeEvent(long eventId) {
143171
// Define 'where' part of query.
144172
String selection = EventTable._ID + " = ?";
@@ -164,6 +192,9 @@ boolean removeEvent(long eventId) {
164192
return false;
165193
}
166194

195+
/**
196+
* Close the SQLite DB.
197+
*/
167198
void closeDb() {
168199
try {
169200
dbHelper.close();

0 commit comments

Comments
 (0)