mirror of
https://gitee.com/farsunset/cim.git
synced 2025-07-23 16:26:42 +08:00
重构android SDK代码
This commit is contained in:
parent
edd5670b45
commit
2978e13c3c
@ -69,7 +69,6 @@ class CIMCacheManager {
|
||||
Cursor cursor = resolver.query(Uri.parse(String.format(CONTENT_URI,context.getPackageName())), new String[] { key }, null, null, null);
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
value = cursor.getString(0);
|
||||
cursor.close();
|
||||
}
|
||||
closeQuietly(cursor);
|
||||
return value;
|
||||
|
@ -111,7 +111,7 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver {
|
||||
* 重新连接,如果断开的话
|
||||
*/
|
||||
if (intent.getAction().equals(CIMConstant.IntentAction.ACTION_CONNECTION_RECOVERY)) {
|
||||
CIMPushManager.connect(context, 0);
|
||||
connect(0);
|
||||
}
|
||||
}
|
||||
|
||||
@ -132,7 +132,7 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver {
|
||||
CIMCacheManager.putBoolean(context, CIMCacheManager.KEY_CIM_CONNECTION_STATE, false);
|
||||
|
||||
if (CIMPushManager.isNetworkConnected(context)) {
|
||||
CIMPushManager.connect(context, 0);
|
||||
connect(0);
|
||||
}
|
||||
|
||||
onConnectionClosed();
|
||||
@ -141,9 +141,10 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver {
|
||||
private void onConnectionFailed(long reinterval) {
|
||||
|
||||
if (CIMPushManager.isNetworkConnected(context)) {
|
||||
|
||||
onConnectionFailed();
|
||||
|
||||
CIMPushManager.connect(context, reinterval);
|
||||
|
||||
connect(reinterval);
|
||||
}
|
||||
}
|
||||
|
||||
@ -157,11 +158,18 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver {
|
||||
private void onDevicesNetworkChanged() {
|
||||
|
||||
if (CIMPushManager.isNetworkConnected(context)) {
|
||||
CIMPushManager.connect(context, 0);
|
||||
connect(0);
|
||||
}
|
||||
|
||||
onNetworkChanged();
|
||||
}
|
||||
|
||||
private void connect(long delay) {
|
||||
Intent serviceIntent = new Intent(context, CIMPushService.class);
|
||||
serviceIntent.putExtra(CIMPushService.KEY_DELAYED_TIME, delay);
|
||||
serviceIntent.setAction(CIMPushManager.ACTION_CREATE_CIM_CONNECTION);
|
||||
CIMPushManager.startService(context,serviceIntent);
|
||||
}
|
||||
|
||||
private void onInnerMessageReceived(com.farsunset.cim.sdk.android.model.Message message, Intent intent) {
|
||||
if (isForceOfflineMessage(message.getAction())) {
|
||||
|
@ -41,19 +41,19 @@ import com.farsunset.cim.sdk.android.model.SentBody;
|
||||
*/
|
||||
public class CIMPushManager {
|
||||
|
||||
static String ACTION_ACTIVATE_PUSH_SERVICE = "ACTION_ACTIVATE_PUSH_SERVICE";
|
||||
protected static String ACTION_ACTIVATE_PUSH_SERVICE = "ACTION_ACTIVATE_PUSH_SERVICE";
|
||||
|
||||
static String ACTION_CREATE_CIM_CONNECTION = "ACTION_CREATE_CIM_CONNECTION";
|
||||
protected static String ACTION_CREATE_CIM_CONNECTION = "ACTION_CREATE_CIM_CONNECTION";
|
||||
|
||||
static String ACTION_SEND_REQUEST_BODY = "ACTION_SEND_REQUEST_BODY";
|
||||
protected static String ACTION_SEND_REQUEST_BODY = "ACTION_SEND_REQUEST_BODY";
|
||||
|
||||
static String ACTION_CLOSE_CIM_CONNECTION = "ACTION_CLOSE_CIM_CONNECTION";
|
||||
protected static String ACTION_CLOSE_CIM_CONNECTION = "ACTION_CLOSE_CIM_CONNECTION";
|
||||
|
||||
static String ACTION_SET_LOGGER_EANABLE = "ACTION_SET_LOGGER_EANABLE";
|
||||
protected static String ACTION_SET_LOGGER_EANABLE = "ACTION_SET_LOGGER_EANABLE";
|
||||
|
||||
static String KEY_SEND_BODY = "KEY_SEND_BODY";
|
||||
protected static String KEY_SEND_BODY = "KEY_SEND_BODY";
|
||||
|
||||
static String KEY_CIM_CONNECTION_STATUS = "KEY_CIM_CONNECTION_STATUS";
|
||||
protected static String KEY_CIM_CONNECTION_STATUS = "KEY_CIM_CONNECTION_STATUS";
|
||||
|
||||
/**
|
||||
* 初始化,连接服务端,在程序启动页或者 在Application里调用
|
||||
@ -64,57 +64,34 @@ public class CIMPushManager {
|
||||
*/
|
||||
public static void connect(Context context, String host, int port) {
|
||||
|
||||
connect(context, host, port, false, 0);
|
||||
|
||||
}
|
||||
|
||||
private static void connect(Context context, String host, int port, boolean autoBind, long delayedTime) {
|
||||
|
||||
if(TextUtils.isEmpty(host) || port == 0) {
|
||||
CIMLogger.getLogger().invalidHostPort(host, port);
|
||||
return;
|
||||
}
|
||||
|
||||
CIMCacheManager.putBoolean(context, CIMCacheManager.KEY_CIM_DESTROYED, false);
|
||||
CIMCacheManager.putBoolean(context, CIMCacheManager.KEY_MANUAL_STOP, false);
|
||||
|
||||
CIMCacheManager.putString(context, CIMCacheManager.KEY_CIM_SERVIER_HOST, host);
|
||||
CIMCacheManager.putInt(context, CIMCacheManager.KEY_CIM_SERVIER_PORT, port);
|
||||
|
||||
if (!autoBind) {
|
||||
CIMCacheManager.remove(context, CIMCacheManager.KEY_ACCOUNT);
|
||||
}
|
||||
|
||||
CIMCacheManager.putBoolean(context, CIMCacheManager.KEY_CIM_DESTROYED, false);
|
||||
CIMCacheManager.putBoolean(context, CIMCacheManager.KEY_MANUAL_STOP, false);
|
||||
|
||||
CIMCacheManager.remove(context, CIMCacheManager.KEY_ACCOUNT);
|
||||
|
||||
|
||||
Intent serviceIntent = new Intent(context, CIMPushService.class);
|
||||
serviceIntent.putExtra(CIMCacheManager.KEY_CIM_SERVIER_HOST, host);
|
||||
serviceIntent.putExtra(CIMCacheManager.KEY_CIM_SERVIER_PORT, port);
|
||||
serviceIntent.putExtra(CIMPushService.KEY_DELAYED_TIME, delayedTime);
|
||||
serviceIntent.setAction(ACTION_CREATE_CIM_CONNECTION);
|
||||
startServiceCompat(context,serviceIntent);
|
||||
startService(context,serviceIntent);
|
||||
|
||||
}
|
||||
|
||||
public static void setLoggerEnable(Context context,boolean enable) {
|
||||
Intent serviceIntent = new Intent(context, CIMPushService.class);
|
||||
serviceIntent.putExtra(CIMPushService.KEY_LOGGER_ENABLE, enable);
|
||||
serviceIntent.setAction(ACTION_SET_LOGGER_EANABLE);
|
||||
startServiceCompat(context,serviceIntent);
|
||||
startService(context,serviceIntent);
|
||||
}
|
||||
|
||||
protected static void connect(Context context, long delayedTime) {
|
||||
|
||||
boolean isManualStop = CIMCacheManager.getBoolean(context, CIMCacheManager.KEY_MANUAL_STOP);
|
||||
boolean isManualDestory = CIMCacheManager.getBoolean(context, CIMCacheManager.KEY_CIM_DESTROYED);
|
||||
|
||||
if (isManualStop || isManualDestory) {
|
||||
return;
|
||||
}
|
||||
|
||||
String host = CIMCacheManager.getString(context, CIMCacheManager.KEY_CIM_SERVIER_HOST);
|
||||
int port = CIMCacheManager.getInt(context, CIMCacheManager.KEY_CIM_SERVIER_PORT);
|
||||
|
||||
connect(context, host, port, true, delayedTime);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置一个账号登录到服务端
|
||||
@ -124,8 +101,7 @@ public class CIMPushManager {
|
||||
*/
|
||||
public static void bindAccount(Context context, String account) {
|
||||
|
||||
boolean isManualDestory = CIMCacheManager.getBoolean(context, CIMCacheManager.KEY_CIM_DESTROYED);
|
||||
if (isManualDestory || account == null || account.trim().length() == 0) {
|
||||
if (isDestoryed(context) || account == null || account.trim().length() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -159,8 +135,7 @@ public class CIMPushManager {
|
||||
protected static boolean autoBindAccount(Context context) {
|
||||
|
||||
String account = CIMCacheManager.getString(context, CIMCacheManager.KEY_ACCOUNT);
|
||||
boolean isManualDestory = CIMCacheManager.getBoolean(context, CIMCacheManager.KEY_CIM_DESTROYED);
|
||||
if (account == null || account.trim().length() == 0 || isManualDestory) {
|
||||
if (account == null || account.trim().length() == 0 || isDestoryed(context)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -177,17 +152,14 @@ public class CIMPushManager {
|
||||
*/
|
||||
public static void sendRequest(Context context, SentBody body) {
|
||||
|
||||
boolean isManualStop = CIMCacheManager.getBoolean(context, CIMCacheManager.KEY_MANUAL_STOP);
|
||||
boolean isManualDestory = CIMCacheManager.getBoolean(context, CIMCacheManager.KEY_CIM_DESTROYED);
|
||||
|
||||
if (isManualStop || isManualDestory) {
|
||||
if (isDestoryed(context) || isStoped(context)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Intent serviceIntent = new Intent(context, CIMPushService.class);
|
||||
serviceIntent.putExtra(KEY_SEND_BODY, body);
|
||||
serviceIntent.setAction(ACTION_SEND_REQUEST_BODY);
|
||||
startServiceCompat(context,serviceIntent);
|
||||
startService(context,serviceIntent);
|
||||
|
||||
}
|
||||
|
||||
@ -198,8 +170,7 @@ public class CIMPushManager {
|
||||
*/
|
||||
public static void stop(Context context) {
|
||||
|
||||
boolean isManualDestory = CIMCacheManager.getBoolean(context, CIMCacheManager.KEY_CIM_DESTROYED);
|
||||
if (isManualDestory) {
|
||||
if (isDestoryed(context)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -207,7 +178,7 @@ public class CIMPushManager {
|
||||
|
||||
Intent serviceIntent = new Intent(context, CIMPushService.class);
|
||||
serviceIntent.setAction(ACTION_CLOSE_CIM_CONNECTION);
|
||||
startServiceCompat(context,serviceIntent);
|
||||
startService(context,serviceIntent);
|
||||
|
||||
}
|
||||
|
||||
@ -232,14 +203,21 @@ public class CIMPushManager {
|
||||
*/
|
||||
public static void resume(Context context) {
|
||||
|
||||
boolean isManualDestory = CIMCacheManager.getBoolean(context, CIMCacheManager.KEY_CIM_DESTROYED);
|
||||
if (isManualDestory) {
|
||||
if (isDestoryed(context)) {
|
||||
return;
|
||||
}
|
||||
|
||||
autoBindAccount(context);
|
||||
}
|
||||
|
||||
public static boolean isDestoryed(Context context) {
|
||||
return CIMCacheManager.getBoolean(context, CIMCacheManager.KEY_CIM_DESTROYED);
|
||||
}
|
||||
|
||||
public static boolean isStoped(Context context) {
|
||||
return CIMCacheManager.getBoolean(context, CIMCacheManager.KEY_MANUAL_STOP);
|
||||
}
|
||||
|
||||
public static boolean isConnected(Context context) {
|
||||
return CIMCacheManager.getBoolean(context, CIMCacheManager.KEY_CIM_CONNECTION_STATE);
|
||||
}
|
||||
@ -253,6 +231,17 @@ public class CIMPushManager {
|
||||
return ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
|
||||
}
|
||||
|
||||
|
||||
public static void startService(Context context,Intent intent) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
context.startForegroundService(intent);
|
||||
} else {
|
||||
context.startService(intent);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static String getVersionName(Context context) {
|
||||
String versionName = null;
|
||||
try {
|
||||
@ -263,14 +252,6 @@ public class CIMPushManager {
|
||||
return versionName;
|
||||
}
|
||||
|
||||
private static void startServiceCompat(Context context,Intent intent) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
context.startForegroundService(intent);
|
||||
} else {
|
||||
context.startService(intent);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -34,7 +34,6 @@ import android.net.Network;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.os.IBinder;
|
||||
import android.os.Message;
|
||||
|
||||
import com.farsunset.cim.sdk.android.coder.CIMLogger;
|
||||
import com.farsunset.cim.sdk.android.constant.CIMConstant;
|
||||
@ -50,6 +49,8 @@ public class CIMPushService extends Service {
|
||||
public final static String KEY_DELAYED_TIME = "KEY_DELAYED_TIME";
|
||||
public final static String KEY_LOGGER_ENABLE = "KEY_LOGGER_ENABLE";
|
||||
|
||||
private final static int NOTIFICATION_ID = Integer.MAX_VALUE;
|
||||
|
||||
private CIMConnectorManager manager;
|
||||
private KeepAliveBroadcastReceiver keepAliveReceiver;
|
||||
private ConnectivityManager connectivityManager;
|
||||
@ -90,15 +91,19 @@ public class CIMPushService extends Service {
|
||||
|
||||
};
|
||||
|
||||
Handler connectionHandler = new Handler() {
|
||||
Handler connectHandler = new Handler() {
|
||||
@Override
|
||||
public void handleMessage(android.os.Message message) {
|
||||
String host = message.getData().getString(CIMCacheManager.KEY_CIM_SERVIER_HOST);
|
||||
int port = message.getData().getInt(CIMCacheManager.KEY_CIM_SERVIER_PORT, 0);
|
||||
handleConnection(host, port);
|
||||
connect();
|
||||
}
|
||||
};
|
||||
|
||||
Handler notificationHandler = new Handler() {
|
||||
@Override
|
||||
public void handleMessage(android.os.Message message) {
|
||||
stopForeground(true);
|
||||
}
|
||||
};
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
|
||||
@ -107,9 +112,13 @@ public class CIMPushService extends Service {
|
||||
NotificationChannel channel = new NotificationChannel(getClass().getSimpleName(),getClass().getSimpleName(), NotificationManager.IMPORTANCE_LOW);
|
||||
channel.enableLights(false);
|
||||
channel.enableVibration(false);
|
||||
channel.setSound(null, null);
|
||||
notificationManager.createNotificationChannel(channel);
|
||||
Notification notification = new Notification.Builder(this, channel.getId()).build();
|
||||
startForeground(this.hashCode(),notification);
|
||||
Notification notification = new Notification.Builder(this, channel.getId())
|
||||
.setContentTitle("Push service")
|
||||
.setContentText("Push service is running")
|
||||
.build();
|
||||
startForeground(NOTIFICATION_ID,notification);
|
||||
}
|
||||
|
||||
intent = (intent == null ? new Intent(CIMPushManager.ACTION_ACTIVATE_PUSH_SERVICE) : intent);
|
||||
@ -117,7 +126,7 @@ public class CIMPushService extends Service {
|
||||
String action = intent.getAction();
|
||||
|
||||
if (CIMPushManager.ACTION_CREATE_CIM_CONNECTION.equals(action)) {
|
||||
handleDelayConnection(intent);
|
||||
connect(intent.getLongExtra(KEY_DELAYED_TIME, 0));
|
||||
}
|
||||
|
||||
if (CIMPushManager.ACTION_SEND_REQUEST_BODY.equals(action)) {
|
||||
@ -138,42 +147,36 @@ public class CIMPushService extends Service {
|
||||
}
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
stopForeground(true);
|
||||
notificationHandler.sendEmptyMessageDelayed(0, 1000);
|
||||
}
|
||||
|
||||
return super.onStartCommand(intent, flags, startId);
|
||||
}
|
||||
|
||||
private void handleDelayConnection(Intent intent) {
|
||||
private void connect(long delayMillis) {
|
||||
|
||||
long delayMillis = intent.getLongExtra(KEY_DELAYED_TIME, 0);
|
||||
|
||||
if (delayMillis <= 0) {
|
||||
String host = intent.getStringExtra(CIMCacheManager.KEY_CIM_SERVIER_HOST);
|
||||
int port = intent.getIntExtra(CIMCacheManager.KEY_CIM_SERVIER_PORT, 0);
|
||||
handleConnection(host, port);
|
||||
if(delayMillis <= 0) {
|
||||
connect();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Message msg = connectionHandler.obtainMessage();
|
||||
msg.what = 0;
|
||||
msg.setData(intent.getExtras());
|
||||
connectionHandler.removeMessages(0);
|
||||
connectionHandler.sendMessageDelayed(msg, delayMillis);
|
||||
connectHandler.sendEmptyMessageDelayed(0, delayMillis);
|
||||
|
||||
}
|
||||
|
||||
private void handleConnection(String host,int port) {
|
||||
private void connect() {
|
||||
|
||||
boolean isManualStop = CIMCacheManager.getBoolean(getApplicationContext(), CIMCacheManager.KEY_MANUAL_STOP);
|
||||
boolean isDestroyed = CIMCacheManager.getBoolean(getApplicationContext(), CIMCacheManager.KEY_CIM_DESTROYED);
|
||||
if(isManualStop || isDestroyed) {
|
||||
if(CIMPushManager.isDestoryed(this) || CIMPushManager.isStoped(this)) {
|
||||
return;
|
||||
}
|
||||
manager.connect(host, port);
|
||||
}
|
||||
|
||||
String host = CIMCacheManager.getString(this, CIMCacheManager.KEY_CIM_SERVIER_HOST);
|
||||
int port = CIMCacheManager.getInt(this, CIMCacheManager.KEY_CIM_SERVIER_PORT);
|
||||
|
||||
manager.connect(host, port);
|
||||
|
||||
}
|
||||
|
||||
private void handleKeepAlive() {
|
||||
|
||||
if (manager.isConnected()) {
|
||||
@ -181,10 +184,8 @@ public class CIMPushService extends Service {
|
||||
return;
|
||||
}
|
||||
|
||||
String host = CIMCacheManager.getString(this, CIMCacheManager.KEY_CIM_SERVIER_HOST);
|
||||
int port = CIMCacheManager.getInt(this, CIMCacheManager.KEY_CIM_SERVIER_PORT);
|
||||
|
||||
handleConnection(host,port);
|
||||
connect();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -196,9 +197,12 @@ public class CIMPushService extends Service {
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
manager.destroy();
|
||||
connectionHandler.removeMessages(0);
|
||||
connectHandler.removeMessages(0);
|
||||
notificationHandler.removeMessages(0);
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
unregisterReceiver(keepAliveReceiver);
|
||||
|
||||
}
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
|
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user