fix XIntentUtils.sendEmail,add DelegateInjector.

androidx
Ztiany 5 years ago
parent 30a0e1ecc9
commit 460d2856ea
  1. 67
      lib_base/src/main/java/com/android/base/app/AndroidComponentLifecycleInjector.kt
  2. 5
      lib_base/src/main/java/com/android/base/app/ApplicationDelegate.kt
  3. 55
      lib_base/src/main/java/com/android/base/app/Sword.kt
  4. 5
      lib_base/src/main/java/com/android/base/app/dagger/Injectable.java
  5. 13
      lib_base/src/main/java/com/android/base/utils/android/XIntentUtils.java
  6. 6
      lib_base/src/main/java/com/android/base/utils/common/Collections.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)
}
}
}

@ -19,9 +19,10 @@ import java.util.concurrent.atomic.AtomicBoolean
* Email: ztiany3@gmail.com * Email: ztiany3@gmail.com
* Date : 2018-10-12 18:19 * Date : 2018-10-12 18:19
*/ */
class ApplicationDelegate internal constructor() { internal class ApplicationDelegate internal constructor(private val androidComponentLifecycleInjector: AndroidComponentLifecycleInjector) {
lateinit var application: Application lateinit var application: Application
private lateinit var crashHandler: CrashHandler private lateinit var crashHandler: CrashHandler
/** 获取可观察的 app 生命周期 */ /** 获取可观察的 app 生命周期 */
@ -45,6 +46,8 @@ class ApplicationDelegate internal constructor() {
application.registerReceiver(NetStateReceiver(), IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)) application.registerReceiver(NetStateReceiver(), IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION))
//App前台后台 //App前台后台
listenActivityLifecycleCallbacks() listenActivityLifecycleCallbacks()
//声明周期回调
application.registerActivityLifecycleCallbacks(androidComponentLifecycleInjector)
} }
fun onTerminate() {} fun onTerminate() {}

@ -1,27 +1,19 @@
package com.android.base.app package com.android.base.app
import android.app.Activity import android.app.Activity
import android.app.Application.ActivityLifecycleCallbacks
import android.content.Context import android.content.Context
import android.os.Bundle import com.android.base.app.activity.ActivityDelegateOwner
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.fragment.animator.FragmentAnimator 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.fragment.tools.FragmentConfig
import com.android.base.app.ui.LoadingView import com.android.base.app.ui.LoadingView
import com.android.base.app.ui.PageNumber import com.android.base.app.ui.PageNumber
import com.android.base.app.ui.RefreshLoadViewFactory import com.android.base.app.ui.RefreshLoadViewFactory
import com.android.base.app.ui.RefreshLoadViewFactory.Factory import com.android.base.app.ui.RefreshLoadViewFactory.Factory
import com.android.base.app.ui.RefreshViewFactory import com.android.base.app.ui.RefreshViewFactory
import com.android.base.interfaces.ActivityLifecycleCallbacksAdapter
import com.android.base.receiver.NetworkState import com.android.base.receiver.NetworkState
import com.blankj.utilcode.util.ActivityUtils import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.AppUtils import com.blankj.utilcode.util.AppUtils
import dagger.android.AndroidInjection
import dagger.android.support.AndroidSupportInjection
import io.reactivex.Flowable import io.reactivex.Flowable
import io.reactivex.plugins.RxJavaPlugins import io.reactivex.plugins.RxJavaPlugins
import timber.log.Timber import timber.log.Timber
@ -35,8 +27,10 @@ import timber.log.Timber
*/ */
object Sword { object Sword {
private val androidComponentLifecycleInjector = AndroidComponentLifecycleInjector()
/** Application lifecycle delegate */ /** Application lifecycle delegate */
val applicationDelegate = ApplicationDelegate() internal val applicationDelegate = ApplicationDelegate(androidComponentLifecycleInjector)
/** 错误类型分类器 */ /** 错误类型分类器 */
var errorClassifier: ErrorClassifier? = null var errorClassifier: ErrorClassifier? = null
@ -88,31 +82,12 @@ object Sword {
} }
fun enableAutoInject(): Sword { fun enableAutoInject(): Sword {
val activityLifecycleCallbacks: ActivityLifecycleCallbacks = object : ActivityLifecycleCallbacksAdapter { androidComponentLifecycleInjector.enableAutoInject()
override fun onActivityCreated(activity: Activity?, savedInstanceState: Bundle?) { return this
if (activity is Injectable) { }
if ((activity as Injectable).enableInject()) {
AndroidInjection.inject(activity) fun setDelegateInjector(delegateInjector: DelegateInjector): Sword {
if (activity is FragmentActivity) { androidComponentLifecycleInjector.delegateInjector = delegateInjector
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)
return this return this
} }
@ -148,3 +123,11 @@ interface ErrorClassifier {
fun isNetworkError(throwable: Throwable): Boolean fun isNetworkError(throwable: Throwable): Boolean
fun isServerError(throwable: Throwable): Boolean fun isServerError(throwable: Throwable): Boolean
} }
interface DelegateInjector {
fun injectFragmentDelegate(fragment: FragmentDelegateOwner)
fun injectActivityDelegate(activity: ActivityDelegateOwner)
}

@ -23,9 +23,4 @@ package com.android.base.app.dagger;
*/ */
public interface Injectable { public interface Injectable {
default boolean enableInject() {
return true;
}
} }

@ -91,15 +91,22 @@ public class XIntentUtils {
* @param subject 主题 * @param subject 主题
* @param text 发送的内容 * @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); Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 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_EMAIL, email);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject); emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, text); emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, text);
if (emailIntent.resolveActivity(context.getPackageManager()) != null) { if (emailIntent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(emailIntent); try {
context.startActivity(emailIntent);
return true;
} catch (Exception e) {
return false;
}
} else {
return false;
} }
} }

@ -12,6 +12,12 @@ fun <E> List<E>?.toArrayList(copy: Boolean = false): ArrayList<E> {
} }
fun <E> List<E>?.ifNotEmpty(action: List<E>.() -> Unit) {
if (!this.isNullOrEmpty()) {
action.invoke(this)
}
}
fun <E> MutableList<E>.removeWhich(filter: (E) -> Boolean): Boolean { fun <E> MutableList<E>.removeWhich(filter: (E) -> Boolean): Boolean {
var removed = false var removed = false
val each = iterator() val each = iterator()

Loading…
Cancel
Save