Skip to content

Commit 75f0d49

Browse files
authored
Add customizable interval in RequestSenderTimer (#428)
1 parent 1b41243 commit 75f0d49

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2020, Leanplum, Inc. All rights reserved.
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
package com.leanplum;
23+
24+
/**
25+
* The enumeration represents time interval to periodically upload events to server.
26+
* Possible values are 5, 10, or 15 minutes.
27+
*/
28+
public enum EventsUploadInterval {
29+
/**
30+
* 5 minutes interval
31+
*/
32+
AT_MOST_5_MINUTES(5),
33+
34+
/**
35+
* 10 minutes interval
36+
*/
37+
AT_MOST_10_MINUTES(10),
38+
39+
/**
40+
* 15 minutes interval
41+
*/
42+
AT_MOST_15_MINUTES(15);
43+
44+
private final int minutes;
45+
46+
EventsUploadInterval(int minutes) {
47+
this.minutes = minutes;
48+
}
49+
50+
public int getMinutes() {
51+
return minutes;
52+
}
53+
}

AndroidSDKCore/src/main/java/com/leanplum/Leanplum.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2275,4 +2275,16 @@ public static CountAggregator countAggregator() {
22752275
public static FeatureFlagManager featureFlagManager() {
22762276
return featureFlagManager;
22772277
}
2278+
2279+
/**
2280+
* Sets the time interval to periodically upload events to server.
2281+
* Default is {@link EventsUploadInterval#AT_MOST_15_MINUTES}.
2282+
*
2283+
* @param uploadInterval The time between uploads.
2284+
*/
2285+
public static void setEventsUploadInterval(EventsUploadInterval uploadInterval) {
2286+
if (uploadInterval != null) {
2287+
RequestSenderTimer.get().setTimerInterval(uploadInterval);
2288+
}
2289+
}
22782290
}

AndroidSDKCore/src/main/java/com/leanplum/internal/RequestSenderTimer.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,24 @@
2020
*/
2121
package com.leanplum.internal;
2222

23+
import androidx.annotation.NonNull;
24+
import com.leanplum.EventsUploadInterval;
2325
import com.leanplum.internal.Request.RequestType;
2426

2527
public class RequestSenderTimer {
2628
private static final RequestSenderTimer INSTANCE = new RequestSenderTimer();
27-
private static final long TIMER_MILLIS = 15 * 60 * 1000; // 15min
29+
private EventsUploadInterval timerInterval = EventsUploadInterval.AT_MOST_15_MINUTES;
2830

2931
private Runnable timerOperation;
3032

3133
public static RequestSenderTimer get() {
3234
return INSTANCE;
3335
}
3436

37+
private long getIntervalMillis() {
38+
return timerInterval.getMinutes() * 60 * 1000;
39+
}
40+
3541
private void sendAllRequestsWithHeartbeat() {
3642
Request request = RequestBuilder
3743
.withHeartbeatAction()
@@ -45,7 +51,7 @@ private Runnable createTimerOperation() {
4551
@Override
4652
public void run() {
4753
sendAllRequestsWithHeartbeat();
48-
OperationQueue.sharedInstance().addOperationAfterDelay(timerOperation, TIMER_MILLIS);
54+
OperationQueue.sharedInstance().addOperationAfterDelay(timerOperation, getIntervalMillis());
4955
}
5056
};
5157
}
@@ -55,7 +61,7 @@ public synchronized void start() {
5561
return;
5662

5763
timerOperation = createTimerOperation();
58-
OperationQueue.sharedInstance().addOperationAfterDelay(timerOperation, TIMER_MILLIS);
64+
OperationQueue.sharedInstance().addOperationAfterDelay(timerOperation, getIntervalMillis());
5965
}
6066

6167
public synchronized void stop() {
@@ -65,4 +71,8 @@ public synchronized void stop() {
6571
OperationQueue.sharedInstance().removeOperation(timerOperation);
6672
timerOperation = null;
6773
}
74+
75+
public void setTimerInterval(@NonNull EventsUploadInterval timerInterval) {
76+
this.timerInterval = timerInterval;
77+
}
6878
}

0 commit comments

Comments
 (0)