Skip to content

Commit c4b2b8d

Browse files
changed reschedulers to wake on wifi state change : connecting
1 parent 47bf717 commit c4b2b8d

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import android.content.BroadcastReceiver;
2121
import android.content.Context;
2222
import android.content.Intent;
23+
import android.net.NetworkInfo;
2324
import android.net.wifi.WifiManager;
2425
import android.support.annotation.NonNull;
2526

@@ -68,17 +69,19 @@ void reschedule(@NonNull Context context, @NonNull Intent broadcastIntent, @NonN
6869
broadcastIntent.getAction().equals(Intent.ACTION_MY_PACKAGE_REPLACED)) {
6970
context.startService(eventServiceIntent);
7071
logger.info("Rescheduling event flushing if necessary");
71-
} else if (broadcastIntent.getAction().equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)
72-
&& broadcastIntent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)) {
72+
} else if (broadcastIntent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
73+
NetworkInfo networkInfo = broadcastIntent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
74+
if (networkInfo.getState() == NetworkInfo.State.CONNECTING) {
7375

74-
if (serviceScheduler.isScheduled(eventServiceIntent)) {
75-
// If we get wifi and the event flushing service is scheduled preemptively
76-
// flush events before the next interval occurs. If sending fails even
77-
// with wifi the service will be rescheduled on the interval.
78-
// Wifi connection state changes all the time and starting services is expensive
79-
// so it's important to only do this if we have stored events.
80-
context.startService(eventServiceIntent);
81-
logger.info("Preemptively flushing events since wifi became available");
76+
if (serviceScheduler.isScheduled(eventServiceIntent)) {
77+
// If we get wifi and the event flushing service is scheduled preemptively
78+
// flush events before the next interval occurs. If sending fails even
79+
// with wifi the service will be rescheduled on the interval.
80+
// Wifi connection state changes all the time and starting services is expensive
81+
// so it's important to only do this if we have stored events.
82+
context.startService(eventServiceIntent);
83+
logger.info("Preemptively flushing events since wifi became available");
84+
}
8285
}
8386
} else {
8487
logger.warn("Received unsupported broadcast action to event rescheduler");

test-app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
<intent-filter>
6060
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
6161
<action android:name="android.intent.action.BOOT_COMPLETED" />
62-
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
62+
<action android:name="android.net.wifi.STATE_CHANGE" />
6363
</intent-filter>
6464
</receiver>
6565
<!--
@@ -73,7 +73,7 @@
7373
<intent-filter>
7474
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
7575
<action android:name="android.intent.action.BOOT_COMPLETED" />
76-
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
76+
<action android:name="android.net.wifi.STATE_CHANGE" />
7777
</intent-filter>
7878
</receiver>
7979

0 commit comments

Comments
 (0)