diff --git a/README.md b/README.md index fc4d1e5..19dc3a9 100644 --- a/README.md +++ b/README.md @@ -65,5 +65,5 @@ CIM采用业内主流开源技术构建,易于扩展和使用,并完美支 android端sdk引用 ``` -implementation "com.farsunset:cim-android-sdk:4.2.13" +implementation "com.farsunset:cim-android-sdk:4.2.15" ``` \ No newline at end of file diff --git a/cim-client-sdk/cim-android-sdk/libs/android.jar b/cim-client-sdk/cim-android-sdk/libs/android.jar index 923bafc..2d6652e 100644 Binary files a/cim-client-sdk/cim-android-sdk/libs/android.jar and b/cim-client-sdk/cim-android-sdk/libs/android.jar differ diff --git a/cim-client-sdk/cim-android-sdk/pom.xml b/cim-client-sdk/cim-android-sdk/pom.xml index 7cf3f41..9e23ef5 100755 --- a/cim-client-sdk/cim-android-sdk/pom.xml +++ b/cim-client-sdk/cim-android-sdk/pom.xml @@ -6,7 +6,7 @@ com.farsunset cim-android-sdk - 4.2.13 + 4.2.15 jar ${project.groupId}:${project.artifactId} @@ -29,7 +29,7 @@ UTF-8 UTF-8 1.8 - 3.22.0 + 3.25.4 @@ -45,7 +45,7 @@ android android system - 10.0.0 + 15.0.0 ${project.basedir}/libs/android.jar diff --git a/cim-client-sdk/cim-android-sdk/src/main/java/com/farsunset/cim/sdk/android/CIMCacheManager.java b/cim-client-sdk/cim-android-sdk/src/main/java/com/farsunset/cim/sdk/android/CIMCacheManager.java index 110aad1..bec4c5f 100644 --- a/cim-client-sdk/cim-android-sdk/src/main/java/com/farsunset/cim/sdk/android/CIMCacheManager.java +++ b/cim-client-sdk/cim-android-sdk/src/main/java/com/farsunset/cim/sdk/android/CIMCacheManager.java @@ -43,13 +43,13 @@ class CIMCacheManager { public static final String KEY_CIM_CONNECTION_STATE = "KEY_CIM_CONNECTION_STATE"; + public static final String KEY_NTC_SWITCH = "KEY_NTC_SWITCH"; public static final String KEY_NTC_CHANNEL_NAME = "KEY_NTC_CHANNEL_NAME"; public static final String KEY_NTC_CHANNEL_MESSAGE = "KEY_NTC_CHANNEL_MESSAGE"; - public static final String KEY_NTC_CHANNEL_ICON = "KEY_NTC_CHANNEL_ICON"; - + public static final String KEY_NTC_CHANNEL_ICON = "KEY_NTC_ICON"; public static final String CONTENT_URI = "content://%s.cim.provider"; diff --git a/cim-client-sdk/cim-android-sdk/src/main/java/com/farsunset/cim/sdk/android/CIMEventBroadcastReceiver.java b/cim-client-sdk/cim-android-sdk/src/main/java/com/farsunset/cim/sdk/android/CIMEventBroadcastReceiver.java index 7905791..9c8d374 100644 --- a/cim-client-sdk/cim-android-sdk/src/main/java/com/farsunset/cim/sdk/android/CIMEventBroadcastReceiver.java +++ b/cim-client-sdk/cim-android-sdk/src/main/java/com/farsunset/cim/sdk/android/CIMEventBroadcastReceiver.java @@ -152,7 +152,7 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver { private void connect(long delay) { Intent serviceIntent = new Intent(context, CIMPushService.class); serviceIntent.putExtra(BundleKey.KEY_DELAYED_TIME, delay); - serviceIntent.setAction(ServiceAction.ACTION_CREATE_CIM_CONNECTION); + serviceIntent.setAction(ServiceAction.ACTION_CREATE_CONNECTION); CIMPushManager.startService(context, serviceIntent); } diff --git a/cim-client-sdk/cim-android-sdk/src/main/java/com/farsunset/cim/sdk/android/CIMPushManager.java b/cim-client-sdk/cim-android-sdk/src/main/java/com/farsunset/cim/sdk/android/CIMPushManager.java index 137066f..46c2db3 100644 --- a/cim-client-sdk/cim-android-sdk/src/main/java/com/farsunset/cim/sdk/android/CIMPushManager.java +++ b/cim-client-sdk/cim-android-sdk/src/main/java/com/farsunset/cim/sdk/android/CIMPushManager.java @@ -67,7 +67,7 @@ public class CIMPushManager { Intent serviceIntent = new Intent(context, CIMPushService.class); - serviceIntent.setAction(ServiceAction.ACTION_CREATE_CIM_CONNECTION); + serviceIntent.setAction(ServiceAction.ACTION_CREATE_CONNECTION); startService(context, serviceIntent); } @@ -172,7 +172,7 @@ public class CIMPushManager { } Intent serviceIntent = new Intent(context, CIMPushService.class); - serviceIntent.setAction(ServiceAction.ACTION_CIM_CONNECTION_PONG); + serviceIntent.setAction(ServiceAction.ACTION_CREATE_CONNECTION); startService(context, serviceIntent); } @@ -239,7 +239,7 @@ public class CIMPushManager { CIMCacheManager.putBoolean(context, CIMCacheManager.KEY_MANUAL_STOP, true); Intent serviceIntent = new Intent(context, CIMPushService.class); - serviceIntent.setAction(ServiceAction.ACTION_CLOSE_CIM_CONNECTION); + serviceIntent.setAction(ServiceAction.ACTION_CREATE_CONNECTION); startService(context, serviceIntent); } @@ -253,7 +253,7 @@ public class CIMPushManager { CIMCacheManager.remove(context, CIMCacheManager.KEY_UID); Intent serviceIntent = new Intent(context, CIMPushService.class); - serviceIntent.setAction(ServiceAction.ACTION_DESTROY_CIM_SERVICE); + serviceIntent.setAction(ServiceAction.ACTION_CREATE_CONNECTION); startService(context, serviceIntent); } diff --git a/cim-client-sdk/cim-android-sdk/src/main/java/com/farsunset/cim/sdk/android/CIMPushService.java b/cim-client-sdk/cim-android-sdk/src/main/java/com/farsunset/cim/sdk/android/CIMPushService.java index e7b7a91..7a6ed72 100644 --- a/cim-client-sdk/cim-android-sdk/src/main/java/com/farsunset/cim/sdk/android/CIMPushService.java +++ b/cim-client-sdk/cim-android-sdk/src/main/java/com/farsunset/cim/sdk/android/CIMPushService.java @@ -41,6 +41,7 @@ import com.farsunset.cim.sdk.android.model.Pong; import com.farsunset.cim.sdk.android.model.SentBody; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.logging.Logger; /** * 与服务端连接服务 @@ -49,9 +50,8 @@ import java.util.concurrent.atomic.AtomicBoolean; */ public class CIMPushService extends Service { - private static final String TRANSIENT_NTC_CHANNEL_ID = "CIM_PUSH_TRANSIENT_NTC_ID"; - private static final String PERSIST_NTC_CHANNEL_ID = "CIM_PUSH_PERSIST_NTC_ID"; - + private static final String TRANSIENT_NTC_CHANNEL_ID = "PUSH_TRANSIENT_NTC_ID"; + private static final String PERSIST_NTC_CHANNEL_ID = "PUSH_PERSIST_NTC_ID"; private static final int NOTIFICATION_ID = Integer.MAX_VALUE; private CIMConnectManager connectManager; @@ -59,7 +59,6 @@ public class CIMPushService extends Service { private KeepAliveBroadcastReceiver keepAliveReceiver; private ConnectivityManager connectivityManager; private NotificationManager notificationManager; - private final AtomicBoolean persistHolder = new AtomicBoolean(false); @Override public void onCreate() { @@ -68,30 +67,26 @@ public class CIMPushService extends Service { notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - keepAliveReceiver = new KeepAliveBroadcastReceiver(); - registerReceiver(keepAliveReceiver, keepAliveReceiver.getIntentFilter()); - } + keepAliveReceiver = new KeepAliveBroadcastReceiver(); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + this.registerReceiver(keepAliveReceiver, keepAliveReceiver.getIntentFilter()); - connectivityManager = getSystemService(ConnectivityManager.class); + connectivityManager = getSystemService(ConnectivityManager.class); - connectivityManager.registerDefaultNetworkCallback(networkCallback); + connectivityManager.registerDefaultNetworkCallback(networkCallback); - } } private final ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() { @Override public void onAvailable(Network network) { - sendBroadcast(new Intent(IntentAction.ACTION_NETWORK_CHANGED)); + sendBroadcast(new Intent(IntentAction.ACTION_NETWORK_CHANGED).setPackage(getPackageName())); handleKeepAlive(); } @Override public void onLost(Network network) { - sendBroadcast(new Intent(IntentAction.ACTION_NETWORK_CHANGED)); + sendBroadcast(new Intent(IntentAction.ACTION_NETWORK_CHANGED).setPackage(getPackageName())); } }; @@ -105,16 +100,15 @@ public class CIMPushService extends Service { private final Handler notificationHandler = new Handler() { @Override public void handleMessage(android.os.Message message) { - if (persistHolder.get()){ - return; + if (!CIMCacheManager.getBoolean(CIMPushService.this,CIMCacheManager.KEY_NTC_SWITCH)){ + stopForeground(true); } - stopForeground(true); } }; @Override public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { return super.registerReceiver(keepAliveReceiver, keepAliveReceiver.getIntentFilter(),Context.RECEIVER_EXPORTED); }else { return super.registerReceiver(keepAliveReceiver, keepAliveReceiver.getIntentFilter()); @@ -136,9 +130,9 @@ public class CIMPushService extends Service { String action = newIntent.getAction(); - createTransientNotification(newIntent); + createTransientNotification(); - if (ServiceAction.ACTION_CREATE_CIM_CONNECTION.equals(action)) { + if (ServiceAction.ACTION_CREATE_CONNECTION.equals(action)) { this.prepareConnect(newIntent.getLongExtra(BundleKey.KEY_DELAYED_TIME, 0)); } @@ -146,7 +140,7 @@ public class CIMPushService extends Service { connectManager.send((SentBody) newIntent.getSerializableExtra(BundleKey.KEY_SEND_BODY)); } - if (ServiceAction.ACTION_CLOSE_CIM_CONNECTION.equals(action)) { + if (ServiceAction.ACTION_CLOSE_CONNECTION.equals(action)) { connectManager.close(); } @@ -154,12 +148,12 @@ public class CIMPushService extends Service { handleKeepAlive(); } - if (ServiceAction.ACTION_DESTROY_CIM_SERVICE.equals(action)) { + if (ServiceAction.ACTION_DESTROY_SERVICE.equals(action)) { connectManager.close(); this.stopSelf(); } - if (ServiceAction.ACTION_CIM_CONNECTION_PONG.equals(action)) { + if (ServiceAction.ACTION_CONNECTION_PONG.equals(action)) { connectManager.send(Pong.getInstance()); } @@ -168,21 +162,20 @@ public class CIMPushService extends Service { CIMLogger.getLogger().debugMode(enable); } + if (ServiceAction.ACTION_HIDE_PERSIST_NOTIFICATION.equals(action)) { + this.stopForeground(true); + CIMCacheManager.putBoolean(this, CIMCacheManager.KEY_NTC_SWITCH,false); + } + if (ServiceAction.ACTION_SHOW_PERSIST_NOTIFICATION.equals(action)) { createPersistNotification(newIntent.getStringExtra(BundleKey.KEY_NOTIFICATION_CHANNEL), - newIntent.getStringExtra(BundleKey.KEY_NOTIFICATION_MESSAGE), - newIntent.getIntExtra(BundleKey.KEY_NOTIFICATION_ICON,0)); - persistHolder.set(true); + newIntent.getStringExtra(BundleKey.KEY_NOTIFICATION_MESSAGE), + newIntent.getIntExtra(BundleKey.KEY_NOTIFICATION_ICON,0)); + + return super.onStartCommand(intent,flags,startId); } - if (ServiceAction.ACTION_HIDE_PERSIST_NOTIFICATION.equals(action)) { - stopForeground(true); - persistHolder.set(false); - } - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - notificationHandler.sendEmptyMessageDelayed(0, 200); - } + notificationHandler.sendEmptyMessageDelayed(0, 200); return super.onStartCommand(intent,flags,startId); } @@ -208,7 +201,7 @@ public class CIMPushService extends Service { String host = CIMCacheManager.getString(this, CIMCacheManager.KEY_CIM_SERVER_HOST); int port = CIMCacheManager.getInt(this, CIMCacheManager.KEY_CIM_SERVER_PORT); - if (host == null || host.trim().length() == 0 || port <= 0) { + if (host == null || host.trim().isEmpty() || port <= 0) { Log.e(this.getClass().getSimpleName(), "Invalid hostname or port. host:" + host + " port:" + port); return; } @@ -242,39 +235,18 @@ public class CIMPushService extends Service { stopForeground(true); - persistHolder.set(false); - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - unregisterReceiver(keepAliveReceiver); - } - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - connectivityManager.unregisterNetworkCallback(networkCallback); - } - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && notificationManager.getNotificationChannel(PERSIST_NTC_CHANNEL_ID) != null) { - notificationManager.deleteNotificationChannel(TRANSIENT_NTC_CHANNEL_ID); - } + unregisterReceiver(keepAliveReceiver); + connectivityManager.unregisterNetworkCallback(networkCallback); } - private void createTransientNotification(Intent intent) { - - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O || persistHolder.get()) { - return; - } - - - if (ServiceAction.ACTION_SHOW_PERSIST_NOTIFICATION.equals(intent.getAction()) - || ServiceAction.ACTION_HIDE_PERSIST_NOTIFICATION.equals(intent.getAction())) { - return; - } + private void createTransientNotification() { if (notificationManager.getNotificationChannel(PERSIST_NTC_CHANNEL_ID) != null) { - int icon = CIMCacheManager.getInt(this,CIMCacheManager.KEY_NTC_CHANNEL_ICON); - String title = CIMCacheManager.getString(this,CIMCacheManager.KEY_NTC_CHANNEL_NAME); - String message = CIMCacheManager.getString(this,CIMCacheManager.KEY_NTC_CHANNEL_MESSAGE); + int icon = CIMCacheManager.getInt(this, CIMCacheManager.KEY_NTC_CHANNEL_ICON); + String title = CIMCacheManager.getString(this, CIMCacheManager.KEY_NTC_CHANNEL_NAME); + String message = CIMCacheManager.getString(this, CIMCacheManager.KEY_NTC_CHANNEL_MESSAGE); Notification notification = makeNotification(PERSIST_NTC_CHANNEL_ID,icon,title,message); @@ -291,7 +263,7 @@ public class CIMPushService extends Service { notificationManager.createNotificationChannel(channel); } - Notification notification = makeNotification(TRANSIENT_NTC_CHANNEL_ID,0,CIMPushService.class.getSimpleName(),null); + Notification notification = makeNotification(TRANSIENT_NTC_CHANNEL_ID,0, CIMPushService.class.getSimpleName(),null); startForegroundNotification(NOTIFICATION_ID, notification); @@ -300,11 +272,12 @@ public class CIMPushService extends Service { private void createPersistNotification(String channelName ,String message,int icon) { - CIMCacheManager.putString(this,CIMCacheManager.KEY_NTC_CHANNEL_NAME,channelName); - CIMCacheManager.putString(this,CIMCacheManager.KEY_NTC_CHANNEL_MESSAGE,message); - CIMCacheManager.putInt(this,CIMCacheManager.KEY_NTC_CHANNEL_ICON,icon); + CIMCacheManager.putString(this, CIMCacheManager.KEY_NTC_CHANNEL_NAME,channelName); + CIMCacheManager.putString(this, CIMCacheManager.KEY_NTC_CHANNEL_MESSAGE,message); + CIMCacheManager.putInt(this, CIMCacheManager.KEY_NTC_CHANNEL_ICON,icon); + CIMCacheManager.putBoolean(this, CIMCacheManager.KEY_NTC_SWITCH,true); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && notificationManager.getNotificationChannel(PERSIST_NTC_CHANNEL_ID) == null) { + if (notificationManager.getNotificationChannel(PERSIST_NTC_CHANNEL_ID) == null) { NotificationChannel channel = new NotificationChannel(PERSIST_NTC_CHANNEL_ID,channelName, NotificationManager.IMPORTANCE_DEFAULT); channel.enableLights(false); channel.setShowBadge(false); @@ -322,18 +295,14 @@ public class CIMPushService extends Service { private Notification makeNotification(String channel,int icon,String title,String message){ Notification.Builder builder; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ - builder = new Notification.Builder(this,channel); - }else { - builder = new Notification.Builder(this); - } + builder = new Notification.Builder(this, channel); builder.setAutoCancel(false) - .setOngoing(false) - .setWhen(System.currentTimeMillis()) - .setContentIntent(getPendingIntent()) - .setContentTitle(title) - .setContentText(message); + .setOngoing(false) + .setWhen(System.currentTimeMillis()) + .setContentIntent(getPendingIntent()) + .setContentTitle(title) + .setContentText(message); if (icon > 0){ builder.setSmallIcon(icon); diff --git a/cim-client-sdk/cim-android-sdk/src/main/java/com/farsunset/cim/sdk/android/constant/ServiceAction.java b/cim-client-sdk/cim-android-sdk/src/main/java/com/farsunset/cim/sdk/android/constant/ServiceAction.java index 73a5503..62c77da 100644 --- a/cim-client-sdk/cim-android-sdk/src/main/java/com/farsunset/cim/sdk/android/constant/ServiceAction.java +++ b/cim-client-sdk/cim-android-sdk/src/main/java/com/farsunset/cim/sdk/android/constant/ServiceAction.java @@ -23,15 +23,15 @@ package com.farsunset.cim.sdk.android.constant; public interface ServiceAction { - String ACTION_CREATE_CIM_CONNECTION = "ACTION_CREATE_CIM_CONNECTION"; + String ACTION_CREATE_CONNECTION = "ACTION_CREATE_CONNECTION"; - String ACTION_DESTROY_CIM_SERVICE = "ACTION_DESTROY_CIM_SERVICE"; + String ACTION_DESTROY_SERVICE = "ACTION_DESTROY_SERVICE"; String ACTION_ACTIVATE_PUSH_SERVICE = "ACTION_ACTIVATE_PUSH_SERVICE"; String ACTION_SEND_REQUEST_BODY = "ACTION_SEND_REQUEST_BODY"; - String ACTION_CLOSE_CIM_CONNECTION = "ACTION_CLOSE_CIM_CONNECTION"; + String ACTION_CLOSE_CONNECTION = "ACTION_CLOSE_CONNECTION"; String ACTION_SET_LOGGER_EATABLE = "ACTION_SET_LOGGER_EATABLE"; @@ -39,6 +39,5 @@ public interface ServiceAction { String ACTION_HIDE_PERSIST_NOTIFICATION = "ACTION_HIDE_PERSIST_NOTIFICATION"; - String ACTION_CIM_CONNECTION_PONG = "ACTION_CIM_CONNECTION_PONG"; - + String ACTION_CONNECTION_PONG = "ACTION_CONNECTION_PONG"; } diff --git a/cim-use-examples/cim-client-android/app/build.gradle b/cim-use-examples/cim-client-android/app/build.gradle index b12351e..cf2d92c 100644 --- a/cim-use-examples/cim-client-android/app/build.gradle +++ b/cim-use-examples/cim-client-android/app/build.gradle @@ -1,22 +1,26 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 33 - buildToolsVersion '33.0.0' + namespace 'com.farsunset.cim' defaultConfig { applicationId "com.farsunset.cim" minSdkVersion 23 - targetSdkVersion 33 + targetSdkVersion 35 + compileSdk 35 versionCode 1 versionName "1.0" /* CIM服务器,IP修改为自己的服务器IP */ - buildConfigField("String", "CIM_API_URL", '"http://192.168.31.175:8080"') - buildConfigField("String", "CIM_SERVER_HOST", '"192.168.31.175"') - buildConfigField("Integer", "CIM_SERVER_PORT", '23456') + buildConfigField("String", "CIM_API_URL", '"http://114.215.199.17:8080"') + buildConfigField("String", "CIM_SERVER_HOST", '"114.215.199.17"') + buildConfigField("Integer", "CIM_SERVER_PORT", '34567') + } + + buildFeatures { + buildConfig = true } buildTypes { @@ -32,12 +36,13 @@ android { } dependencies { - implementation "com.farsunset:cim-android-sdk:4.2.13" - implementation 'androidx.appcompat:appcompat:1.6.0' - implementation 'com.google.android.material:material:1.8.0' - implementation 'androidx.annotation:annotation:1.3.0' - implementation 'androidx.constraintlayout:constraintlayout:2.1.4' - implementation 'com.google.protobuf:protobuf-javalite:3.22.0' - implementation 'com.squareup.retrofit2:retrofit:2.9.0' - implementation 'com.squareup.retrofit2:converter-gson:2.9.0' + implementation 'androidx.appcompat:appcompat:1.7.1' + implementation 'com.google.android.material:material:1.12.0' + implementation 'androidx.annotation:annotation:1.9.1' + implementation 'androidx.constraintlayout:constraintlayout:2.2.1' + implementation 'com.google.protobuf:protobuf-javalite:3.25.4' + implementation 'com.squareup.retrofit2:retrofit:3.0.0' + implementation 'com.squareup.retrofit2:converter-gson:3.0.0' + implementation "com.farsunset:cim-android-sdk:4.2.15" + } \ No newline at end of file diff --git a/cim-use-examples/cim-client-android/app/src/main/res/values/themes.xml b/cim-use-examples/cim-client-android/app/src/main/res/values/themes.xml index 3953aed..b4ceb86 100644 --- a/cim-use-examples/cim-client-android/app/src/main/res/values/themes.xml +++ b/cim-use-examples/cim-client-android/app/src/main/res/values/themes.xml @@ -2,6 +2,7 @@