修改一处android-cim-sdk可能出现ConcurrentModificationException的问题

This commit is contained in:
远方夕阳 2021-03-14 11:01:14 +08:00
parent 0e6f3ee400
commit 0f38754cda
6 changed files with 68 additions and 36 deletions

View File

@ -6,7 +6,7 @@
<groupId>com.farsunset</groupId> <groupId>com.farsunset</groupId>
<artifactId>cim-android-sdk</artifactId> <artifactId>cim-android-sdk</artifactId>
<version>3.8.1</version> <version>3.8.2</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<properties> <properties>

View File

@ -63,44 +63,55 @@ public class CIMListenerManager {
} }
public static void notifyOnNetworkChanged(NetworkInfo info) { public static void notifyOnNetworkChanged(NetworkInfo info) {
for (CIMEventListener listener : cimListeners) { Iterator<CIMEventListener> iterable = cimListeners.iterator();
listener.onNetworkChanged(info); while (iterable.hasNext()) {
iterable.next().onNetworkChanged(info);
} }
} }
public static void notifyOnConnectFinished(boolean hasAutoBind) { public static void notifyOnConnectFinished(boolean hasAutoBind) {
for (CIMEventListener listener : cimListeners) { Iterator<CIMEventListener> iterable = cimListeners.iterator();
listener.onConnectFinished(hasAutoBind); while (iterable.hasNext()) {
iterable.next().onConnectFinished(hasAutoBind);
} }
} }
public static void notifyOnMessageReceived(Message message) { public static void notifyOnMessageReceived(Message message) {
for (CIMEventListener listener : cimListeners) { Iterator<CIMEventListener> iterable = cimListeners.iterator();
listener.onMessageReceived(message); while (iterable.hasNext()) {
iterable.next().onMessageReceived(message);
} }
} }
public static void notifyOnConnectionClosed() { public static void notifyOnConnectionClosed() {
for (CIMEventListener listener : cimListeners) { Iterator<CIMEventListener> iterable = cimListeners.iterator();
listener.onConnectionClosed(); while (iterable.hasNext()) {
iterable.next().onConnectionClosed();
} }
} }
public static void notifyOnConnectFailed() { public static void notifyOnConnectFailed() {
for (CIMEventListener listener : cimListeners) {
listener.onConnectFailed(); Iterator<CIMEventListener> iterable = cimListeners.iterator();
while (iterable.hasNext()) {
iterable.next().onConnectFailed();
} }
} }
public static void notifyOnReplyReceived(ReplyBody body) { public static void notifyOnReplyReceived(ReplyBody body) {
for (CIMEventListener listener : cimListeners) {
listener.onReplyReceived(body); Iterator<CIMEventListener> iterable = cimListeners.iterator();
while (iterable.hasNext()) {
iterable.next().onReplyReceived(body);
} }
} }
public static void notifyOnSendFinished(SentBody body) { public static void notifyOnSendFinished(SentBody body) {
for (CIMEventListener listener : cimListeners) { Iterator<CIMEventListener> iterable = cimListeners.iterator();
listener.onSendFinished(body); while (iterable.hasNext()) {
iterable.next().onSendFinished(body);
} }
} }
@ -109,8 +120,9 @@ public class CIMListenerManager {
} }
public static void logListenersName() { public static void logListenersName() {
for (CIMEventListener listener : cimListeners) { Iterator<CIMEventListener> iterable = cimListeners.iterator();
Log.i(CIMEventListener.class.getSimpleName(), "#######" + listener.getClass().getName() + "#######"); while (iterable.hasNext()) {
Log.i(CIMEventListener.class.getSimpleName(), "#######" + iterable.next().getClass().getName() + "#######");
} }
} }

View File

@ -40,23 +40,24 @@ import java.util.UUID;
*/ */
public class CIMPushManager { public class CIMPushManager {
protected static final String ACTION_ACTIVATE_PUSH_SERVICE = "ACTION_ACTIVATE_PUSH_SERVICE";
protected static final String ACTION_CREATE_CIM_CONNECTION = "ACTION_CREATE_CIM_CONNECTION"; protected static final String ACTION_CREATE_CIM_CONNECTION = "ACTION_CREATE_CIM_CONNECTION";
protected static final String ACTION_DESTROY_CIM_SERVICE = "ACTION_DESTROY_CIM_SERVICE";
protected static final String ACTION_ACTIVATE_PUSH_SERVICE = "ACTION_ACTIVATE_PUSH_SERVICE";
protected static final String ACTION_SEND_REQUEST_BODY = "ACTION_SEND_REQUEST_BODY"; protected static final String ACTION_SEND_REQUEST_BODY = "ACTION_SEND_REQUEST_BODY";
protected static final String ACTION_CLOSE_CIM_CONNECTION = "ACTION_CLOSE_CIM_CONNECTION"; protected static final String ACTION_CLOSE_CIM_CONNECTION = "ACTION_CLOSE_CIM_CONNECTION";
protected static final String ACTION_DESTROY_CIM_SERVICE = "ACTION_DESTROY_CIM_SERVICE";
protected static final String ACTION_SET_LOGGER_EATABLE = "ACTION_SET_LOGGER_EATABLE"; protected static final String ACTION_SET_LOGGER_EATABLE = "ACTION_SET_LOGGER_EATABLE";
protected static final String ACTION_SHOW_PERSIST_NOTIFICATION = "ACTION_SHOW_PERSIST_NOTIFICATION"; protected static final String ACTION_SHOW_PERSIST_NOTIFICATION = "ACTION_SHOW_PERSIST_NOTIFICATION";
protected static final String ACTION_HIDE_PERSIST_NOTIFICATION = "ACTION_HIDE_PERSIST_NOTIFICATION"; protected static final String ACTION_HIDE_PERSIST_NOTIFICATION = "ACTION_HIDE_PERSIST_NOTIFICATION";
protected static final String ACTION_SEND_PONG = "ACTION_SEND_PONG"; protected static final String ACTION_CIM_CONNECTION_PONG = "ACTION_CIM_CONNECTION_PONG";
/** /**
* 初始化,连接服务端在程序启动页或者 在Application里调用 * 初始化,连接服务端在程序启动页或者 在Application里调用
@ -120,9 +121,10 @@ public class CIMPushManager {
} }
public static void pong(Context context) { public static void pong(Context context) {
Intent serviceIntent = new Intent(context, CIMPushService.class); if (isDestroyed(context) || isStopped(context)) {
serviceIntent.setAction(ACTION_SEND_PONG); return;
startService(context, serviceIntent); }
context.sendBroadcast(new Intent(ACTION_CIM_CONNECTION_PONG));
} }
private static void sendBindRequest(Context context, String account) { private static void sendBindRequest(Context context, String account) {

View File

@ -53,15 +53,16 @@ public class CIMPushService extends Service {
static final String KEY_NOTIFICATION_CHANNEL = "KEY_NOTIFICATION_CHANNEL"; static final String KEY_NOTIFICATION_CHANNEL = "KEY_NOTIFICATION_CHANNEL";
static final String KEY_NOTIFICATION_ICON = "KEY_NOTIFICATION_ICON"; static final String KEY_NOTIFICATION_ICON = "KEY_NOTIFICATION_ICON";
private final static String TRANSIENT_NTC_CHANNEL_ID = "CIM_PUSH_TRANSIENT_NTC_ID"; private static final String TRANSIENT_NTC_CHANNEL_ID = "CIM_PUSH_TRANSIENT_NTC_ID";
private final static String PERSIST_NTC_CHANNEL_ID = "CIM_PUSH_PERSIST_NTC_ID"; private static final String PERSIST_NTC_CHANNEL_ID = "CIM_PUSH_PERSIST_NTC_ID";
private final static int NOTIFICATION_ID = Integer.MAX_VALUE; private static final int NOTIFICATION_ID = Integer.MAX_VALUE;
private final static int PERSIST_NOTIFICATION_ID = Integer.MIN_VALUE; private static final int PERSIST_NOTIFICATION_ID = Integer.MIN_VALUE;
private CIMConnectorManager connectorManager; private CIMConnectorManager connectorManager;
private KeepAliveBroadcastReceiver keepAliveReceiver; private KeepAliveBroadcastReceiver keepAliveReceiver;
private InnerEventBroadcastReceiver innerEventReceiver;
private ConnectivityManager connectivityManager; private ConnectivityManager connectivityManager;
private NotificationManager notificationManager; private NotificationManager notificationManager;
private final AtomicBoolean persistHolder = new AtomicBoolean(false); private final AtomicBoolean persistHolder = new AtomicBoolean(false);
@ -71,8 +72,11 @@ public class CIMPushService extends Service {
public void onCreate() { public void onCreate() {
connectorManager = CIMConnectorManager.getManager(this.getApplicationContext()); connectorManager = CIMConnectorManager.getManager(this.getApplicationContext());
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
innerEventReceiver = new InnerEventBroadcastReceiver();
registerReceiver(innerEventReceiver, innerEventReceiver.getIntentFilter());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
keepAliveReceiver = new KeepAliveBroadcastReceiver(); keepAliveReceiver = new KeepAliveBroadcastReceiver();
registerReceiver(keepAliveReceiver, keepAliveReceiver.getIntentFilter()); registerReceiver(keepAliveReceiver, keepAliveReceiver.getIntentFilter());
} }
@ -147,8 +151,9 @@ public class CIMPushService extends Service {
handleKeepAlive(); handleKeepAlive();
} }
if (CIMPushManager.ACTION_SEND_PONG.equals(action)) { if (CIMPushManager.ACTION_DESTROY_CIM_SERVICE.equals(action)) {
connectorManager.send(Pong.getInstance()); connectorManager.close();
this.stopSelf();
} }
if (CIMPushManager.ACTION_SET_LOGGER_EATABLE.equals(action)) { if (CIMPushManager.ACTION_SET_LOGGER_EATABLE.equals(action)) {
@ -156,11 +161,6 @@ public class CIMPushService extends Service {
CIMLogger.getLogger().debugMode(enable); CIMLogger.getLogger().debugMode(enable);
} }
if (CIMPushManager.ACTION_DESTROY_CIM_SERVICE.equals(action)) {
connectorManager.close();
this.stopSelf();
}
if (CIMPushManager.ACTION_SHOW_PERSIST_NOTIFICATION.equals(action)) { if (CIMPushManager.ACTION_SHOW_PERSIST_NOTIFICATION.equals(action)) {
createPersistNotification(intent.getStringExtra(KEY_NOTIFICATION_CHANNEL), createPersistNotification(intent.getStringExtra(KEY_NOTIFICATION_CHANNEL),
intent.getStringExtra(KEY_NOTIFICATION_MESSAGE), intent.getStringExtra(KEY_NOTIFICATION_MESSAGE),
@ -239,6 +239,9 @@ public class CIMPushService extends Service {
persistHolder.set(false); persistHolder.set(false);
unregisterReceiver(innerEventReceiver);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
unregisterReceiver(keepAliveReceiver); unregisterReceiver(keepAliveReceiver);
} }
@ -320,4 +323,19 @@ public class CIMPushService extends Service {
} }
private class InnerEventBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
connectorManager.send(Pong.getInstance());
}
public IntentFilter getIntentFilter() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(CIMPushManager.ACTION_CIM_CONNECTION_PONG);
return intentFilter;
}
}
} }