Smack/smack-android-extensions/src/main/java/org/jivesoftware/smackx/ping/android/ServerPingWithAlarmManager....

179 lines
7.7 KiB
Java
Raw Normal View History

/**
*
* Copyright © 2014-2021 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.ping.android;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.logging.Logger;
import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPConnectionRegistry;
Call pingServerIfNecessary in new thread in order to prevent the BroadcastReceiver from timing out. To prevent: 10-24 10:59:35.096 W/BroadcastQueue( 1044): Timeout of broadcast BroadcastRecord{43400df0 u0 org.igniterealtime.smackx.ping.ACTION} - receiver=android.os.BinderProxy@42d37cc0, started 60000ms ago 10-24 10:59:35.096 W/BroadcastQueue( 1044): Receiver during timeout: BroadcastFilter{42cee7b0 u0 ReceiverList{42cfd9f0 16003 org.projectmaxs.transport.xmpp/10149/u0 remote:42d37cc0}} 10-24 10:59:38.201 E/ActivityManager( 1044): ANR in org.projectmaxs.transport.xmpp 10-24 10:59:38.201 E/ActivityManager( 1044): PID: 16003 10-24 10:59:38.201 E/ActivityManager( 1044): Reason: Broadcast of Intent { act=org.igniterealtime.smackx.ping.ACTION flg=0x14 (has extras) } 10-24 10:59:38.201 E/ActivityManager( 1044): Load: 18.97 / 31.7 / 45.71 10-24 10:59:38.201 E/ActivityManager( 1044): CPU usage from 20588ms to 0ms ago: 10-24 10:59:38.201 E/ActivityManager( 1044): 0.4% 1044/system_server: 0.2% user + 0.1% kernel / faults: 188 minor 1 major 10-24 10:59:38.201 E/ActivityManager( 1044): 0.4% 9089/kworker/0:3: 0% user + 0.4% kernel 10-24 10:59:38.201 E/ActivityManager( 1044): 0.1% 1567/mpdecision: 0% user + 0.1% kernel 10-24 10:59:38.201 E/ActivityManager( 1044): 0.1% 28507/kworker/u:5: 0% user + 0.1% kernel 10-24 10:59:38.201 E/ActivityManager( 1044): 0% 7/kworker/u:0H: 0% user + 0% kernel 10-24 10:59:38.201 E/ActivityManager( 1044): 0% 148/mmcqd/0: 0% user + 0% kernel 10-24 10:59:38.201 E/ActivityManager( 1044): 0% 14687/com.teslacoilsw.launcher: 0% user + 0% kernel / faults: 6 minor 10-24 10:59:38.201 E/ActivityManager( 1044): 0% 3/ksoftirqd/0: 0% user + 0% kernel 10-24 10:59:38.201 E/ActivityManager( 1044): 0% 1111/MC_Thread: 0% user + 0% kernel 10-24 10:59:38.201 E/ActivityManager( 1044): 0% 1135/wpa_supplicant: 0% user + 0% kernel 10-24 10:59:38.201 E/ActivityManager( 1044): 0% 1283/com.android.phone: 0% user + 0% kernel 10-24 10:59:38.201 E/ActivityManager( 1044): 0% 11469/com.getpebble.android: 0% user + 0% kernel / faults: 1 minor 10-24 10:59:38.201 E/ActivityManager( 1044): 0% 14971/com.google.android.apps.unveil: 0% user + 0% kernel / faults: 6 minor 10-24 10:59:38.201 E/ActivityManager( 1044): 0% 28677/watch_server: 0% user + 0% kernel 10-24 10:59:38.201 E/ActivityManager( 1044): 0% 31539/kworker/0:2: 0% user + 0% kernel 10-24 10:59:38.201 E/ActivityManager( 1044): 1.2% TOTAL: 0.4% user + 0.5% kernel + 0.2% iowait 10-24 10:59:38.201 E/ActivityManager( 1044): CPU usage from 2536ms to 3060ms later: 10-24 10:59:38.201 E/ActivityManager( 1044): 9.2% 1044/system_server: 1.8% user + 7.4% kernel / faults: 56 minor 10-24 10:59:38.201 E/ActivityManager( 1044): 5.5% 1058/ActivityManager: 0% user + 5.5% kernel 10-24 10:59:38.201 E/ActivityManager( 1044): 1.3% 23415/kworker/0:1H: 0% user + 1.3% kernel 10-24 10:59:38.201 E/ActivityManager( 1044): 0% TOTAL: 0% user + 0% kernel + 0% iowait 10-24 10:59:38.208 I/ActivityManager( 1044): Killing 16003:org.projectmaxs.transport.xmpp/u0a149 (adj 0): background ANR
2014-10-24 11:39:08 +02:00
import org.jivesoftware.smack.util.Async;
import org.jivesoftware.smackx.ping.PingManager;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.SystemClock;
/**
* Send automatic server pings with the help of {@link AlarmManager}.
* <p>
* Smack's {@link PingManager} uses a <code>ScheduledThreadPoolExecutor</code> to schedule the
* automatic server pings, but on Android, those scheduled pings are not reliable. This is because
* the Android device may go into deep sleep where the system will not continue to run this causes
* <ul>
* <li>the system time to not move forward, which means that the time spent in deep sleep is not
* counted towards the scheduled delay time</li>
* <li>the scheduled Runnable is not run while the system is in deep sleep.</li>
* </ul>
* <p>
* That is the reason Android comes with an API to schedule those tasks: AlarmManager. Which this
* class uses to determine every 30 minutes if a server ping is necessary. The interval of 30
* minutes is the ideal trade-off between reliability and low resource (battery) consumption.
* </p>
* <p>
* In order to use this class you need to call {@link #onCreate(Context)} <b>once</b>, for example
* in the <code>onCreate()</code> method of your Service holding the XMPPConnection. And to avoid
* leaking any resources, you should call {@link #onDestroy()} when you no longer need any of its
* functionality.
* </p>
*/
public final class ServerPingWithAlarmManager extends Manager {
private static final Logger LOGGER = Logger.getLogger(ServerPingWithAlarmManager.class
.getName());
private static final String PING_ALARM_ACTION = "org.igniterealtime.smackx.ping.ACTION";
private static final Map<XMPPConnection, ServerPingWithAlarmManager> INSTANCES = new WeakHashMap<XMPPConnection, ServerPingWithAlarmManager>();
static {
XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
@Override
public void connectionCreated(XMPPConnection connection) {
getInstanceFor(connection);
}
});
}
public static synchronized ServerPingWithAlarmManager getInstanceFor(XMPPConnection connection) {
ServerPingWithAlarmManager serverPingWithAlarmManager = INSTANCES.get(connection);
if (serverPingWithAlarmManager == null) {
serverPingWithAlarmManager = new ServerPingWithAlarmManager(connection);
INSTANCES.put(connection, serverPingWithAlarmManager);
}
return serverPingWithAlarmManager;
}
private boolean mEnabled = true;
private ServerPingWithAlarmManager(XMPPConnection connection) {
super(connection);
}
/**
* If enabled, ServerPingWithAlarmManager will call {@link PingManager#pingServerIfNecessary()}
* for the connection of this instance every half hour.
*
* @param enabled whether or not this manager is should be enabled or not.
*/
public void setEnabled(boolean enabled) {
mEnabled = enabled;
}
public boolean isEnabled() {
return mEnabled;
}
private static final BroadcastReceiver ALARM_BROADCAST_RECEIVER = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
LOGGER.fine("Ping Alarm broadcast received");
Set<Map.Entry<XMPPConnection, ServerPingWithAlarmManager>> managers;
synchronized (ServerPingWithAlarmManager.class) {
// Make a copy to avoid ConcurrentModificationException when
// iterating directly over INSTANCES and the Set is modified
// concurrently by creating a new ServerPingWithAlarmManager.
managers = new HashSet<>(INSTANCES.entrySet());
}
for (Map.Entry<XMPPConnection, ServerPingWithAlarmManager> entry : managers) {
XMPPConnection connection = entry.getKey();
if (entry.getValue().isEnabled()) {
LOGGER.fine("Calling pingServerIfNecessary for connection "
+ connection);
final PingManager pingManager = PingManager.getInstanceFor(connection);
// Android BroadcastReceivers have a timeout of 60 seconds.
// The connections reply timeout may be higher, which causes
// timeouts of the broadcast receiver and a subsequent ANR
// of the App of the broadcast receiver. We therefore need
// to call pingServerIfNecessary() in a new thread to avoid
// this. It could happen that the device gets back to sleep
// until the Thread runs, but that's a risk we are willing
// to take into account as it's unlikely.
Async.go(new Runnable() {
@Override
public void run() {
pingManager.pingServerIfNecessary();
}
}, "PingServerIfNecessary (" + connection.getConnectionCounter() + ')');
} else {
LOGGER.fine("NOT calling pingServerIfNecessary (disabled) on connection "
+ connection.getConnectionCounter());
}
}
}
};
private static Context sContext;
private static PendingIntent sPendingIntent;
private static AlarmManager sAlarmManager;
/**
* Register a pending intent with the AlarmManager to be broadcasted every half hour and
* register the alarm broadcast receiver to receive this intent. The receiver will check all
* known questions if a ping is Necessary when invoked by the alarm intent.
*
* @param context an Android context.
*/
public static void onCreate(Context context) {
sContext = context;
context.registerReceiver(ALARM_BROADCAST_RECEIVER, new IntentFilter(PING_ALARM_ACTION));
sAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
sPendingIntent = PendingIntent.getBroadcast(context, 0, new Intent(PING_ALARM_ACTION), 0);
sAlarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_HALF_HOUR,
AlarmManager.INTERVAL_HALF_HOUR, sPendingIntent);
}
/**
* Unregister the alarm broadcast receiver and cancel the alarm.
*/
public static void onDestroy() {
sContext.unregisterReceiver(ALARM_BROADCAST_RECEIVER);
sAlarmManager.cancel(sPendingIntent);
}
}