|
|
@@ -5,6 +5,10 @@ import android.content.Context
|
|
|
import android.content.Intent
|
|
|
import android.os.Handler
|
|
|
import android.os.Looper
|
|
|
+import com.alibaba.android.arouter.facade.annotation.Route
|
|
|
+import com.alibaba.android.arouter.facade.template.IProvider
|
|
|
+import com.narutohuo.xindazhou.core.log.ILog
|
|
|
+import com.narutohuo.xindazhou.core.share.IShareService
|
|
|
import com.narutohuo.xindazhou.share.api.ShareService
|
|
|
import com.narutohuo.xindazhou.share.model.ShareConfig
|
|
|
import com.narutohuo.xindazhou.share.model.ShareContent
|
|
|
@@ -19,7 +23,6 @@ import com.umeng.socialize.bean.SHARE_MEDIA
|
|
|
import com.umeng.socialize.media.UMImage
|
|
|
import com.umeng.socialize.media.UMWeb
|
|
|
import com.umeng.socialize.media.UMediaObject
|
|
|
-import com.narutohuo.xindazhou.core.log.ILog
|
|
|
|
|
|
/**
|
|
|
* 分享服务实现类(单例模式)
|
|
|
@@ -27,8 +30,11 @@ import com.narutohuo.xindazhou.core.log.ILog
|
|
|
* 使用单例方便管理,多个界面可以共享同一个分享服务
|
|
|
*
|
|
|
* 封装友盟分享 SDK,提供统一的分享服务接口
|
|
|
+ *
|
|
|
+ * 通过 ARouter 注册,实现 base-core 与 share 模块的解耦
|
|
|
*/
|
|
|
-class ShareServiceImpl private constructor() : ShareService {
|
|
|
+@Route(path = "/share/service", name = "分享服务")
|
|
|
+class ShareServiceImpl private constructor() : IProvider, IShareService, ShareService {
|
|
|
|
|
|
companion object {
|
|
|
@Volatile
|
|
|
@@ -61,6 +67,346 @@ class ShareServiceImpl private constructor() : ShareService {
|
|
|
private var pendingShareContent: ShareContent? = null
|
|
|
private var pendingShareCallback: ((ShareResponse) -> Unit)? = null
|
|
|
|
|
|
+ /**
|
|
|
+ * ARouter IProvider 接口的初始化方法
|
|
|
+ *
|
|
|
+ * 注意:此方法在 ARouter 初始化 provider 时调用,不应该抛出异常
|
|
|
+ */
|
|
|
+ override fun init(context: Context) {
|
|
|
+ try {
|
|
|
+ ILog.d(tag, "ShareServiceImpl 初始化(ARouter)")
|
|
|
+ // 这里不进行实际的初始化,实际的初始化在 ShareServiceFactory.init() 中完成
|
|
|
+ // 这样可以避免在 ARouter 初始化时出现问题
|
|
|
+ } catch (e: Exception) {
|
|
|
+ // 确保不会抛出异常,避免影响 ARouter 初始化
|
|
|
+ ILog.e(tag, "ShareServiceImpl ARouter 初始化异常(不影响使用)", e)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // ========== IShareService 接口实现(适配层) ==========
|
|
|
+
|
|
|
+ override fun initialize(
|
|
|
+ context: Context,
|
|
|
+ umengAppKey: String?,
|
|
|
+ umengChannel: String,
|
|
|
+ weChatAppId: String?,
|
|
|
+ weChatAppSecret: String?,
|
|
|
+ qqAppId: String?,
|
|
|
+ qqAppKey: String?,
|
|
|
+ weiboAppKey: String?,
|
|
|
+ weiboAppSecret: String?,
|
|
|
+ weiboRedirectUrl: String?
|
|
|
+ ) {
|
|
|
+ // 适配 IShareService 接口,将参数转换为 ShareConfig
|
|
|
+ val config = ShareConfig(
|
|
|
+ umengAppKey = umengAppKey,
|
|
|
+ umengChannel = umengChannel,
|
|
|
+ weChatConfig = if (weChatAppId != null && weChatAppSecret != null) {
|
|
|
+ com.narutohuo.xindazhou.share.model.WeChatConfig(
|
|
|
+ appId = weChatAppId,
|
|
|
+ appSecret = weChatAppSecret
|
|
|
+ )
|
|
|
+ } else null,
|
|
|
+ qqConfig = if (qqAppId != null && qqAppKey != null) {
|
|
|
+ com.narutohuo.xindazhou.share.model.QQConfig(
|
|
|
+ appId = qqAppId,
|
|
|
+ appKey = qqAppKey
|
|
|
+ )
|
|
|
+ } else null,
|
|
|
+ weiboConfig = if (weiboAppKey != null && weiboAppSecret != null && weiboRedirectUrl != null) {
|
|
|
+ com.narutohuo.xindazhou.share.model.WeiboConfig(
|
|
|
+ appKey = weiboAppKey,
|
|
|
+ appSecret = weiboAppSecret,
|
|
|
+ redirectUrl = weiboRedirectUrl
|
|
|
+ )
|
|
|
+ } else null
|
|
|
+ )
|
|
|
+ initialize(context, config)
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun share(
|
|
|
+ activity: Activity,
|
|
|
+ title: String,
|
|
|
+ description: String,
|
|
|
+ url: String?,
|
|
|
+ imageUrl: String?,
|
|
|
+ thumbImageUrl: String?,
|
|
|
+ platform: String?,
|
|
|
+ callback: (Boolean, String?, String?) -> Unit
|
|
|
+ ) {
|
|
|
+ // 适配 IShareService 接口,将基本类型参数转换为 ShareContent
|
|
|
+ val platformEnum = platform?.let {
|
|
|
+ try {
|
|
|
+ SharePlatform.valueOf(it.uppercase())
|
|
|
+ } catch (e: Exception) {
|
|
|
+ null
|
|
|
+ }
|
|
|
+ }
|
|
|
+ val content = ShareContent(
|
|
|
+ title = title,
|
|
|
+ description = description,
|
|
|
+ platform = platformEnum,
|
|
|
+ url = url,
|
|
|
+ imageUrl = imageUrl,
|
|
|
+ thumbImageUrl = thumbImageUrl
|
|
|
+ )
|
|
|
+ share(activity, content) { response ->
|
|
|
+ callback(response.success, response.data?.name, response.errorMessage)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun shareManual(
|
|
|
+ activity: Activity,
|
|
|
+ title: String,
|
|
|
+ description: String,
|
|
|
+ url: String?,
|
|
|
+ imageUrl: String?,
|
|
|
+ thumbImageUrl: String?,
|
|
|
+ platform: String?,
|
|
|
+ callback: (Boolean, String?, String?) -> Unit
|
|
|
+ ) {
|
|
|
+ val platformEnum = platform?.let {
|
|
|
+ try {
|
|
|
+ SharePlatform.valueOf(it.uppercase())
|
|
|
+ } catch (e: Exception) {
|
|
|
+ null
|
|
|
+ }
|
|
|
+ }
|
|
|
+ val content = ShareContent(
|
|
|
+ title = title,
|
|
|
+ description = description,
|
|
|
+ platform = platformEnum,
|
|
|
+ url = url,
|
|
|
+ imageUrl = imageUrl,
|
|
|
+ thumbImageUrl = thumbImageUrl
|
|
|
+ )
|
|
|
+ shareManual(activity, content) { response ->
|
|
|
+ callback(response.success, response.data?.name, response.errorMessage)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun showShareDialog(
|
|
|
+ activity: Activity,
|
|
|
+ title: String,
|
|
|
+ description: String,
|
|
|
+ url: String?,
|
|
|
+ imageUrl: String?,
|
|
|
+ thumbImageUrl: String?,
|
|
|
+ callback: (Boolean, String?, String?) -> Unit
|
|
|
+ ) {
|
|
|
+ val content = ShareContent(
|
|
|
+ title = title,
|
|
|
+ description = description,
|
|
|
+ platform = null,
|
|
|
+ url = url,
|
|
|
+ imageUrl = imageUrl,
|
|
|
+ thumbImageUrl = thumbImageUrl
|
|
|
+ )
|
|
|
+ showShareDialog(activity, content) { response ->
|
|
|
+ callback(response.success, response.data?.name, response.errorMessage)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun shareToWeChat(
|
|
|
+ activity: Activity,
|
|
|
+ title: String,
|
|
|
+ description: String,
|
|
|
+ url: String?,
|
|
|
+ imageUrl: String?,
|
|
|
+ thumbImageUrl: String?,
|
|
|
+ callback: (Boolean, String?, String?) -> Unit
|
|
|
+ ) {
|
|
|
+ val content = ShareContent(
|
|
|
+ title = title,
|
|
|
+ description = description,
|
|
|
+ platform = SharePlatform.WECHAT,
|
|
|
+ url = url,
|
|
|
+ imageUrl = imageUrl,
|
|
|
+ thumbImageUrl = thumbImageUrl
|
|
|
+ )
|
|
|
+ shareToWeChat(activity, content) { response ->
|
|
|
+ callback(response.success, response.data?.name, response.errorMessage)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun shareToWeChatMoments(
|
|
|
+ activity: Activity,
|
|
|
+ title: String,
|
|
|
+ description: String,
|
|
|
+ url: String?,
|
|
|
+ imageUrl: String?,
|
|
|
+ thumbImageUrl: String?,
|
|
|
+ callback: (Boolean, String?, String?) -> Unit
|
|
|
+ ) {
|
|
|
+ val content = ShareContent(
|
|
|
+ title = title,
|
|
|
+ description = description,
|
|
|
+ platform = SharePlatform.WECHAT_MOMENTS,
|
|
|
+ url = url,
|
|
|
+ imageUrl = imageUrl,
|
|
|
+ thumbImageUrl = thumbImageUrl
|
|
|
+ )
|
|
|
+ shareToWeChatMoments(activity, content) { response ->
|
|
|
+ callback(response.success, response.data?.name, response.errorMessage)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun shareToQQ(
|
|
|
+ activity: Activity,
|
|
|
+ title: String,
|
|
|
+ description: String,
|
|
|
+ url: String?,
|
|
|
+ imageUrl: String?,
|
|
|
+ thumbImageUrl: String?,
|
|
|
+ callback: (Boolean, String?, String?) -> Unit
|
|
|
+ ) {
|
|
|
+ val content = ShareContent(
|
|
|
+ title = title,
|
|
|
+ description = description,
|
|
|
+ platform = SharePlatform.QQ,
|
|
|
+ url = url,
|
|
|
+ imageUrl = imageUrl,
|
|
|
+ thumbImageUrl = thumbImageUrl
|
|
|
+ )
|
|
|
+ shareToQQ(activity, content) { response ->
|
|
|
+ callback(response.success, response.data?.name, response.errorMessage)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun shareToQZone(
|
|
|
+ activity: Activity,
|
|
|
+ title: String,
|
|
|
+ description: String,
|
|
|
+ url: String?,
|
|
|
+ imageUrl: String?,
|
|
|
+ thumbImageUrl: String?,
|
|
|
+ callback: (Boolean, String?, String?) -> Unit
|
|
|
+ ) {
|
|
|
+ val content = ShareContent(
|
|
|
+ title = title,
|
|
|
+ description = description,
|
|
|
+ platform = SharePlatform.QZONE,
|
|
|
+ url = url,
|
|
|
+ imageUrl = imageUrl,
|
|
|
+ thumbImageUrl = thumbImageUrl
|
|
|
+ )
|
|
|
+ shareToQZone(activity, content) { response ->
|
|
|
+ callback(response.success, response.data?.name, response.errorMessage)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun shareToWeibo(
|
|
|
+ activity: Activity,
|
|
|
+ title: String,
|
|
|
+ description: String,
|
|
|
+ url: String?,
|
|
|
+ imageUrl: String?,
|
|
|
+ thumbImageUrl: String?,
|
|
|
+ callback: (Boolean, String?, String?) -> Unit
|
|
|
+ ) {
|
|
|
+ val content = ShareContent(
|
|
|
+ title = title,
|
|
|
+ description = description,
|
|
|
+ platform = SharePlatform.WEIBO,
|
|
|
+ url = url,
|
|
|
+ imageUrl = imageUrl,
|
|
|
+ thumbImageUrl = thumbImageUrl
|
|
|
+ )
|
|
|
+ shareToWeibo(activity, content) { response ->
|
|
|
+ callback(response.success, response.data?.name, response.errorMessage)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun shareToDouyin(
|
|
|
+ activity: Activity,
|
|
|
+ title: String,
|
|
|
+ description: String,
|
|
|
+ url: String?,
|
|
|
+ imageUrl: String?,
|
|
|
+ thumbImageUrl: String?,
|
|
|
+ callback: (Boolean, String?, String?) -> Unit
|
|
|
+ ) {
|
|
|
+ val content = ShareContent(
|
|
|
+ title = title,
|
|
|
+ description = description,
|
|
|
+ platform = SharePlatform.DOUYIN,
|
|
|
+ url = url,
|
|
|
+ imageUrl = imageUrl,
|
|
|
+ thumbImageUrl = thumbImageUrl
|
|
|
+ )
|
|
|
+ shareToDouyin(activity, content) { response ->
|
|
|
+ callback(response.success, response.data?.name, response.errorMessage)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun shareToXiaohongshu(
|
|
|
+ activity: Activity,
|
|
|
+ title: String,
|
|
|
+ description: String,
|
|
|
+ url: String?,
|
|
|
+ imageUrl: String?,
|
|
|
+ thumbImageUrl: String?,
|
|
|
+ callback: (Boolean, String?, String?) -> Unit
|
|
|
+ ) {
|
|
|
+ val content = ShareContent(
|
|
|
+ title = title,
|
|
|
+ description = description,
|
|
|
+ platform = SharePlatform.XIAOHONGSHU,
|
|
|
+ url = url,
|
|
|
+ imageUrl = imageUrl,
|
|
|
+ thumbImageUrl = thumbImageUrl
|
|
|
+ )
|
|
|
+ shareToXiaohongshu(activity, content) { response ->
|
|
|
+ callback(response.success, response.data?.name, response.errorMessage)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun shareToKuaishou(
|
|
|
+ activity: Activity,
|
|
|
+ title: String,
|
|
|
+ description: String,
|
|
|
+ url: String?,
|
|
|
+ imageUrl: String?,
|
|
|
+ thumbImageUrl: String?,
|
|
|
+ callback: (Boolean, String?, String?) -> Unit
|
|
|
+ ) {
|
|
|
+ val content = ShareContent(
|
|
|
+ title = title,
|
|
|
+ description = description,
|
|
|
+ platform = SharePlatform.KUAISHOU,
|
|
|
+ url = url,
|
|
|
+ imageUrl = imageUrl,
|
|
|
+ thumbImageUrl = thumbImageUrl
|
|
|
+ )
|
|
|
+ shareToKuaishou(activity, content) { response ->
|
|
|
+ callback(response.success, response.data?.name, response.errorMessage)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun shareToSystem(
|
|
|
+ activity: Activity,
|
|
|
+ title: String,
|
|
|
+ description: String,
|
|
|
+ url: String?,
|
|
|
+ imageUrl: String?,
|
|
|
+ thumbImageUrl: String?,
|
|
|
+ callback: (Boolean, String?, String?) -> Unit
|
|
|
+ ) {
|
|
|
+ val content = ShareContent(
|
|
|
+ title = title,
|
|
|
+ description = description,
|
|
|
+ platform = SharePlatform.SYSTEM,
|
|
|
+ url = url,
|
|
|
+ imageUrl = imageUrl,
|
|
|
+ thumbImageUrl = thumbImageUrl
|
|
|
+ )
|
|
|
+ shareToSystem(activity, content) { response ->
|
|
|
+ callback(response.success, response.data?.name, response.errorMessage)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // ========== ShareService 接口实现(原有接口) ==========
|
|
|
+
|
|
|
override fun initialize(context: Context, config: ShareConfig) {
|
|
|
if (isInitialized) {
|
|
|
ILog.w(tag, "分享服务已经初始化,跳过重复初始化")
|
|
|
@@ -150,9 +496,9 @@ class ShareServiceImpl private constructor() : ShareService {
|
|
|
}
|
|
|
|
|
|
override fun share(activity: Activity, content: ShareContent, callback: (ShareResponse) -> Unit) {
|
|
|
- // 直接调用 shareManual,业务层需要在 Activity 的 onActivityResult 中调用 shareService.onActivityResult()
|
|
|
- // 注意:如果使用 ShareProxyActivity,可以调用 ShareProxyActivity.startShare() 代替此方法
|
|
|
- shareManual(activity, content, callback)
|
|
|
+ // 自动启动 ShareProxyActivity,统一处理所有平台的分享回调
|
|
|
+ // 业务层不需要在 Activity 的 onActivityResult 中处理回调,完全封装在模块内部
|
|
|
+ com.narutohuo.xindazhou.share.ui.ShareProxyActivity.startShare(activity, content, callback)
|
|
|
}
|
|
|
|
|
|
/**
|