diff --git a/lib_base/src/main/java/com/android/base/app/AndroidComponentLifecycleInjector.kt b/lib_base/src/main/java/com/android/base/app/AndroidComponentLifecycleInjector.kt new file mode 100644 index 0000000..74d5cf7 --- /dev/null +++ b/lib_base/src/main/java/com/android/base/app/AndroidComponentLifecycleInjector.kt @@ -0,0 +1,67 @@ +package com.android.base.app + +import android.app.Activity +import android.content.Context +import android.os.Bundle +import androidx.fragment.app.Fragment +import androidx.fragment.app.FragmentActivity +import androidx.fragment.app.FragmentManager +import com.android.base.app.activity.ActivityDelegateOwner +import com.android.base.app.dagger.Injectable +import com.android.base.app.fragment.delegates.FragmentDelegateOwner +import com.android.base.interfaces.ActivityLifecycleCallbacksAdapter +import dagger.android.AndroidInjection +import dagger.android.support.AndroidSupportInjection + +internal class AndroidComponentLifecycleInjector : ActivityLifecycleCallbacksAdapter { + + private var isAutoInjectEnable = false + + var delegateInjector: DelegateInjector? = null + + fun enableAutoInject() { + isAutoInjectEnable = true + } + + override fun onActivityCreated(activity: Activity?, savedInstanceState: Bundle?) { + handleAutoInjectActivity(activity) + handleAutoInstallActivityDelegate(activity) + if (activity is FragmentActivity) { + injectFragmentLifecycle(activity) + } + } + + private fun injectFragmentLifecycle(activity: FragmentActivity) { + activity.supportFragmentManager.registerFragmentLifecycleCallbacks(object : FragmentManager.FragmentLifecycleCallbacks() { + override fun onFragmentAttached(fm: FragmentManager, fragment: Fragment, context: Context) { + handleAutoInjectFragment(fragment) + handleAutoInstallFragmentDelegate(fragment) + } + }, true) + } + + private fun handleAutoInstallFragmentDelegate(fragment: Fragment) { + if (fragment is FragmentDelegateOwner) { + delegateInjector?.injectFragmentDelegate(fragment) + } + } + + private fun handleAutoInstallActivityDelegate(activity: Activity?) { + if (activity is ActivityDelegateOwner) { + delegateInjector?.injectActivityDelegate(activity) + } + } + + private fun handleAutoInjectActivity(activity: Activity?) { + if (isAutoInjectEnable && activity is Injectable) { + AndroidInjection.inject(activity) + } + } + + private fun handleAutoInjectFragment(fragment: Fragment) { + if (isAutoInjectEnable && fragment is Injectable) { + AndroidSupportInjection.inject(fragment) + } + } + +} \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/app/ApplicationDelegate.kt b/lib_base/src/main/java/com/android/base/app/ApplicationDelegate.kt index a701e45..87e3b42 100644 --- a/lib_base/src/main/java/com/android/base/app/ApplicationDelegate.kt +++ b/lib_base/src/main/java/com/android/base/app/ApplicationDelegate.kt @@ -19,9 +19,10 @@ import java.util.concurrent.atomic.AtomicBoolean * Email: ztiany3@gmail.com * Date : 2018-10-12 18:19 */ -class ApplicationDelegate internal constructor() { +internal class ApplicationDelegate internal constructor(private val androidComponentLifecycleInjector: AndroidComponentLifecycleInjector) { lateinit var application: Application + private lateinit var crashHandler: CrashHandler /** 获取可观察的 app 生命周期 */ @@ -45,6 +46,8 @@ class ApplicationDelegate internal constructor() { application.registerReceiver(NetStateReceiver(), IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)) //App前台后台 listenActivityLifecycleCallbacks() + //声明周期回调 + application.registerActivityLifecycleCallbacks(androidComponentLifecycleInjector) } fun onTerminate() {} diff --git a/lib_base/src/main/java/com/android/base/app/Sword.kt b/lib_base/src/main/java/com/android/base/app/Sword.kt index 3a39234..5d0da05 100644 --- a/lib_base/src/main/java/com/android/base/app/Sword.kt +++ b/lib_base/src/main/java/com/android/base/app/Sword.kt @@ -1,27 +1,19 @@ package com.android.base.app import android.app.Activity -import android.app.Application.ActivityLifecycleCallbacks import android.content.Context -import android.os.Bundle -import androidx.fragment.app.Fragment -import androidx.fragment.app.FragmentActivity -import androidx.fragment.app.FragmentManager -import androidx.fragment.app.FragmentManager.FragmentLifecycleCallbacks -import com.android.base.app.dagger.Injectable +import com.android.base.app.activity.ActivityDelegateOwner import com.android.base.app.fragment.animator.FragmentAnimator +import com.android.base.app.fragment.delegates.FragmentDelegateOwner import com.android.base.app.fragment.tools.FragmentConfig import com.android.base.app.ui.LoadingView import com.android.base.app.ui.PageNumber import com.android.base.app.ui.RefreshLoadViewFactory import com.android.base.app.ui.RefreshLoadViewFactory.Factory import com.android.base.app.ui.RefreshViewFactory -import com.android.base.interfaces.ActivityLifecycleCallbacksAdapter import com.android.base.receiver.NetworkState import com.blankj.utilcode.util.ActivityUtils import com.blankj.utilcode.util.AppUtils -import dagger.android.AndroidInjection -import dagger.android.support.AndroidSupportInjection import io.reactivex.Flowable import io.reactivex.plugins.RxJavaPlugins import timber.log.Timber @@ -35,8 +27,10 @@ import timber.log.Timber */ object Sword { + private val androidComponentLifecycleInjector = AndroidComponentLifecycleInjector() + /** Application lifecycle delegate */ - val applicationDelegate = ApplicationDelegate() + internal val applicationDelegate = ApplicationDelegate(androidComponentLifecycleInjector) /** 错误类型分类器 */ var errorClassifier: ErrorClassifier? = null @@ -88,31 +82,12 @@ object Sword { } fun enableAutoInject(): Sword { - val activityLifecycleCallbacks: ActivityLifecycleCallbacks = object : ActivityLifecycleCallbacksAdapter { - override fun onActivityCreated(activity: Activity?, savedInstanceState: Bundle?) { - if (activity is Injectable) { - if ((activity as Injectable).enableInject()) { - AndroidInjection.inject(activity) - if (activity is FragmentActivity) { - handedFragmentInject(activity as FragmentActivity) - } - } - } - } - - private fun handedFragmentInject(activity: FragmentActivity) { - activity.supportFragmentManager.registerFragmentLifecycleCallbacks(object : FragmentLifecycleCallbacks() { - override fun onFragmentAttached(fm: FragmentManager, f: Fragment, context: Context) { - if (f is Injectable) { - if ((f as Injectable).enableInject()) { - AndroidSupportInjection.inject(f) - } - } - } - }, true) - } - } - applicationDelegate.application.registerActivityLifecycleCallbacks(activityLifecycleCallbacks) + androidComponentLifecycleInjector.enableAutoInject() + return this + } + + fun setDelegateInjector(delegateInjector: DelegateInjector): Sword { + androidComponentLifecycleInjector.delegateInjector = delegateInjector return this } @@ -147,4 +122,12 @@ interface CrashProcessor { interface ErrorClassifier { fun isNetworkError(throwable: Throwable): Boolean fun isServerError(throwable: Throwable): Boolean -} \ No newline at end of file +} + +interface DelegateInjector { + + fun injectFragmentDelegate(fragment: FragmentDelegateOwner) + + fun injectActivityDelegate(activity: ActivityDelegateOwner) + +} diff --git a/lib_base/src/main/java/com/android/base/app/dagger/Injectable.java b/lib_base/src/main/java/com/android/base/app/dagger/Injectable.java index 146a7b7..8f0e9a5 100644 --- a/lib_base/src/main/java/com/android/base/app/dagger/Injectable.java +++ b/lib_base/src/main/java/com/android/base/app/dagger/Injectable.java @@ -23,9 +23,4 @@ package com.android.base.app.dagger; */ public interface Injectable { - default boolean enableInject() { - return true; - } - -} - +} \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/utils/android/XIntentUtils.java b/lib_base/src/main/java/com/android/base/utils/android/XIntentUtils.java index 27651ea..d10a05b 100644 --- a/lib_base/src/main/java/com/android/base/utils/android/XIntentUtils.java +++ b/lib_base/src/main/java/com/android/base/utils/android/XIntentUtils.java @@ -91,15 +91,22 @@ public class XIntentUtils { * @param subject 主题 * @param text 发送的内容 */ - public static void sendEmail(Context context, String[] email, String subject, String text) { + public static boolean sendEmail(Context context, String[] email, String subject, String text) { Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - emailIntent.setType("toastMessage/rfc822"); + emailIntent.setType("message/rfc822"); emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, email); emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject); emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, text); if (emailIntent.resolveActivity(context.getPackageManager()) != null) { - context.startActivity(emailIntent); + try { + context.startActivity(emailIntent); + return true; + } catch (Exception e) { + return false; + } + } else { + return false; } } diff --git a/lib_base/src/main/java/com/android/base/utils/common/Collections.kt b/lib_base/src/main/java/com/android/base/utils/common/Collections.kt index 9f17492..f09d45e 100644 --- a/lib_base/src/main/java/com/android/base/utils/common/Collections.kt +++ b/lib_base/src/main/java/com/android/base/utils/common/Collections.kt @@ -12,6 +12,12 @@ fun List?.toArrayList(copy: Boolean = false): ArrayList { } +fun List?.ifNotEmpty(action: List.() -> Unit) { + if (!this.isNullOrEmpty()) { + action.invoke(this) + } +} + fun MutableList.removeWhich(filter: (E) -> Boolean): Boolean { var removed = false val each = iterator()