Skip to content

Commit 2c8f97d

Browse files
committed
Improve staying on background by checking network status.
1 parent 85ce2fb commit 2c8f97d

File tree

1 file changed

+69
-45
lines changed

1 file changed

+69
-45
lines changed

src/android/BackgroundModeExt.java

Lines changed: 69 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Licensed to the Apache Software Foundation (ASF) under one
3333
import android.content.IntentFilter;
3434
import android.content.pm.PackageManager;
3535
import android.net.Uri;
36+
import android.net.ConnectivityManager;
37+
import android.net.NetworkInfo;
3638
import android.os.Build;
3739
import android.os.PowerManager;
3840
import android.view.View;
@@ -74,9 +76,6 @@ Licensed to the Apache Software Foundation (ASF) under one
7476

7577
import static android.net.wifi.WifiManager.WIFI_MODE_FULL_HIGH_PERF;
7678

77-
78-
79-
8079
/**
8180
* Implements extended functions around the main purpose
8281
* of infinite execution in the background.
@@ -88,7 +87,7 @@ public class BackgroundModeExt extends CordovaPlugin {
8887
private AlarmManager alarmMgr;
8988
private PendingIntent alarmIntent;
9089
final String RECEIVER = ".AlarmReceiver";
91-
final int TIMEOUT = 180 * 1000; // 3 mins
90+
final int TIMEOUT = 120 * 1000; // 2 mins
9291
final int QUICK_TIMEOUT = 2 * 1000; // 2 secs
9392
final int WAKELIMIT = 2;
9493
private boolean isOnBg = false;
@@ -104,54 +103,79 @@ private class AlarmReceiver extends BroadcastReceiver {
104103

105104
@Override
106105
public void onReceive(Context context, Intent intent) {
106+
PowerManager pm = (PowerManager)context.getSystemService(POWER_SERVICE);
107+
wakeLock = pm.newWakeLock(
108+
PARTIAL_WAKE_LOCK, "backgroundmode:wakelock");
109+
wakeLock.acquire();
107110

108-
timeout = TIMEOUT;
109-
if(isOnBg()) {
110-
if(++wakeCounter == WAKELIMIT) {
111-
PowerManager pm = (PowerManager)context.getSystemService(POWER_SERVICE);
112-
WifiManager wm = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
113-
114-
if(null == wakeLock) {
115-
wakeLock = pm.newWakeLock(
116-
PARTIAL_WAKE_LOCK, "backgroundmode:wakelock");
117-
wakeLock.acquire();
111+
try {
112+
WifiManager wm = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
113+
114+
timeout = TIMEOUT;
115+
if(isOnBg()) {
116+
if(++wakeCounter == WAKELIMIT) {
117+
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
118+
getApp().runOnUiThread(() -> {
119+
View view = webView.getEngine().getView();
120+
view.dispatchWindowVisibilityChanged(View.VISIBLE);
121+
});
122+
123+
if(cm != null) {
124+
NetworkInfo netInfo = cm.getActiveNetworkInfo();
125+
//should check null because in airplane mode it will be null
126+
if(netInfo != null && netInfo.isConnected()) {
127+
wfl = wm.createWifiLock(WIFI_MODE_FULL_HIGH_PERF, "backgroundmode:sync_all_wifi");
128+
wfl.acquire();
129+
130+
try {
131+
webView.loadUrl("javascript:syncReconnect()");
132+
}
133+
finally {
134+
wfl.release();
135+
wfl = null;
136+
}
137+
}
138+
else {
139+
Log.d("MlesTalk", "No network!");
140+
}
141+
}
142+
else {
143+
Log.d("MlesTalk", "No CM!");
144+
}
145+
146+
wakeCounter = 0;
147+
timeout = QUICK_TIMEOUT;
118148
}
119-
if(null == wfl) {
120-
wfl = wm.createWifiLock(WIFI_MODE_FULL_HIGH_PERF, "backgroundmode:sync_all_wifi");
121-
wfl.acquire();
149+
else if(1 == wakeCounter) {
150+
getApp().runOnUiThread(() -> {
151+
View view = webView.getEngine().getView();
152+
view.dispatchWindowVisibilityChanged(View.GONE);
153+
});
122154
}
155+
}
123156

124-
getApp().runOnUiThread(() -> {
125-
View view = webView.getEngine().getView();
126-
view.dispatchWindowVisibilityChanged(View.VISIBLE);
127-
});
128-
129-
webView.loadUrl("javascript:syncReconnect()");
157+
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
130158

131-
wfl.release();
132-
wfl = null;
133-
wakeLock.release();
134-
wakeLock = null;
159+
Intent newIntent = new Intent(RECEIVER);
135160

136-
wakeCounter = 0;
137-
timeout = QUICK_TIMEOUT;
138-
}
139-
else if(1 == wakeCounter) {
140-
getApp().runOnUiThread(() -> {
141-
View view = webView.getEngine().getView();
142-
view.dispatchWindowVisibilityChanged(View.GONE);
143-
});
144-
}
161+
alarmIntent = PendingIntent.getBroadcast(context, 0, newIntent, 0);
162+
alarmMgr.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
163+
SystemClock.elapsedRealtime() + timeout, alarmIntent);
145164
}
146-
147-
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
165+
catch(Exception e) {
166+
Log.d("MlesTalk", "Got exception, no intent loaded, loading 60 s!");
167+
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
148168

149-
Intent newIntent = new Intent(RECEIVER);
150-
151-
alarmIntent = PendingIntent.getBroadcast(context, 0, newIntent, 0);
152-
alarmMgr.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
153-
SystemClock.elapsedRealtime() + timeout, alarmIntent);
154-
169+
Intent newIntent = new Intent(RECEIVER);
170+
171+
alarmIntent = PendingIntent.getBroadcast(context, 0, newIntent, 0);
172+
alarmMgr.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
173+
SystemClock.elapsedRealtime() + 60*1000, alarmIntent);
174+
}
175+
finally {
176+
wakeLock.release();
177+
wakeLock = null;
178+
}
155179
}
156180
}
157181
/**
@@ -272,7 +296,7 @@ private void disableWebViewOptimizations() {
272296
alarmMgr.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
273297
SystemClock.elapsedRealtime() + TIMEOUT, alarmIntent);
274298

275-
Log.d("MlesAlarm", "Starting alarm");
299+
Log.d("MlesTalk", "Starting alarm");
276300
}
277301

278302
private void enablePartialWake() {

0 commit comments

Comments
 (0)