diff --git a/Android-components/android-app-components/src/main/java/com/heaven7/android/component/AbstractLifeCycleComponentOwner.java b/Android-components/android-app-components/src/main/java/com/heaven7/android/component/AbstractLifeCycleComponentOwner.java new file mode 100644 index 0000000..8d84907 --- /dev/null +++ b/Android-components/android-app-components/src/main/java/com/heaven7/android/component/AbstractLifeCycleComponentOwner.java @@ -0,0 +1,137 @@ +package com.heaven7.android.component; + +import android.arch.lifecycle.Lifecycle; +import android.arch.lifecycle.LifecycleObserver; +import android.arch.lifecycle.LifecycleOwner; +import android.arch.lifecycle.OnLifecycleEvent; +import android.support.annotation.CallSuper; + +import java.util.ArrayList; +import java.util.Iterator; + +import static com.heaven7.android.component.lifecycle.LifeCycleComponent.ON_CREATE; +import static com.heaven7.android.component.lifecycle.LifeCycleComponent.ON_DESTROY; +import static com.heaven7.android.component.lifecycle.LifeCycleComponent.ON_PAUSE; +import static com.heaven7.android.component.lifecycle.LifeCycleComponent.ON_RESUME; +import static com.heaven7.android.component.lifecycle.LifeCycleComponent.ON_START; +import static com.heaven7.android.component.lifecycle.LifeCycleComponent.ON_STOP; + +/** + * the abstract life cycle owner. + * @param the life cycle component. see {@linkplain com.heaven7.android.component.lifecycle.LifeCycleComponent} and + * {@linkplain com.heaven7.android.component.lifecycle.LifeCycleComponent2} + * @since 1.1.3 + */ +public abstract class AbstractLifeCycleComponentOwner implements LifecycleObserver { + + protected final ArrayList> mWeakLives = new ArrayList<>(); + private final LifecycleOwner mOwner; + + public AbstractLifeCycleComponentOwner(LifecycleOwner owner) { + mOwner = owner; + owner.getLifecycle().addObserver(this); + } + + public LifecycleOwner getLifecycleOwner() { + return mOwner; + } + + /** + * register the life cycle component + * @param component the life cycle component + * @since 1.0.6 + */ + public final void registerLifeCycleComponent(T component) { + mWeakLives.add(new SmartReference0(component)); + } + + /** + * unregister the life cycle component + * @param component the life cycle component + * @since 1.0.6 + */ + public final void unregisterLifeCycleComponent(T component) { + findLifeCycleComponent(component, true); + } + + /** + * register the life cycle component as weakly.. + * @param component the life cycle component which will be weak reference + * @since 1.0.7 + */ + public final void registerLifeCycleComponentWeakly(T component) { + final SmartReference0 srf = new SmartReference0(component); + srf.tryWeakReference(); + mWeakLives.add(srf); + } + + /** + * indicate has the life cycle component or not. + * @param component the life cycle component + * @return true if has the target life cycle component + * @since 1.0.6 + */ + public final boolean hasLifeCycleComponent(T component){ + return findLifeCycleComponent(component, false) != null; + } + + @OnLifecycleEvent(Lifecycle.Event.ON_CREATE) + @CallSuper + public void onCreate() { + performLifeCycle(ON_CREATE); + } + + @OnLifecycleEvent(Lifecycle.Event.ON_START) + @CallSuper + public void onStart() { + performLifeCycle(ON_START); + } + + @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) + @CallSuper + public void onResume() { + performLifeCycle(ON_RESUME); + } + + @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) + @CallSuper + public void onPause() { + performLifeCycle(ON_PAUSE); + } + + @OnLifecycleEvent(Lifecycle.Event.ON_STOP) + @CallSuper + public void onStop() { + performLifeCycle(ON_STOP); + } + + @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) + @CallSuper + public void onDestroy() { + performLifeCycle(ON_DESTROY); + } + + private T findLifeCycleComponent(T context, boolean remove) { + T result = null; + final Iterator> it = mWeakLives.iterator(); + for (; it.hasNext(); ) { + final SmartReference0 srf = it.next(); + if (srf.isAlive()) { + final T tem = srf.get(); + if (tem == context || tem.equals(context)) { + result = tem; + if(remove) { + it.remove(); + } + break; + } + } else { + it.remove(); + } + } + return result; + } + + protected abstract void performLifeCycle(int liftCycle); + +} diff --git a/Android-components/android-app-components/src/main/java/com/heaven7/android/component/AppComponentOwner.java b/Android-components/android-app-components/src/main/java/com/heaven7/android/component/AppComponentOwner.java index 7532133..44a6c04 100644 --- a/Android-components/android-app-components/src/main/java/com/heaven7/android/component/AppComponentOwner.java +++ b/Android-components/android-app-components/src/main/java/com/heaven7/android/component/AppComponentOwner.java @@ -1,25 +1,18 @@ package com.heaven7.android.component; import android.app.Activity; -import android.app.Dialog; -import android.app.Fragment; import android.arch.lifecycle.Lifecycle; import android.arch.lifecycle.LifecycleObserver; import android.arch.lifecycle.OnLifecycleEvent; -import android.content.Context; -import android.os.Build; import android.support.annotation.CallSuper; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.FragmentActivity; -import android.support.v4.app.SupportActivity; -import android.support.v7.app.AppCompatActivity; -import android.util.Log; -import android.view.View; import com.heaven7.android.component.guide.AppGuideComponent; import com.heaven7.android.component.image.AppImageComponent; import com.heaven7.android.component.lifecycle.LifeCycleComponent; +import com.heaven7.android.component.lifecycle.LifeCycleComponent2; import com.heaven7.android.component.loading.AppLoadingComponent; import com.heaven7.android.component.toast.AppToastComponent; import com.heaven7.java.base.util.SmartReference; @@ -44,11 +37,8 @@ * * @since 1.0.5 */ -public class AppComponentOwner implements LifecycleObserver { - private static final String TAG = "AppComponentOwner"; - private final ArrayList mWeakLives; +public class AppComponentOwner extends AbstractLifeCycleComponentOwner{ private final AppComponentFactory mFactory; - private final FragmentActivity mContext; private AppImageComponent mImageCpt; private AppLoadingComponent mLoadingCpt; @@ -62,11 +52,8 @@ public class AppComponentOwner implements LifecycleObserver { * @param factory the component factory */ public AppComponentOwner(@NonNull FragmentActivity activity, @NonNull AppComponentFactory factory) { - this.mContext = activity; + super(activity); this.mFactory = factory; - this.mWeakLives = new ArrayList<>(8); - activity.getLifecycle().addObserver(this); - // ReportFragment.injectIfNeededIn(activity); } /** @@ -76,47 +63,9 @@ public AppComponentOwner(@NonNull FragmentActivity activity, @NonNull AppCompone */ @SuppressWarnings("unchecked") public final T getActivity() { - return (T) mContext; + return (T) getLifecycleOwner(); } - /** - * register the life cycle component - * @param component the life cycle component - * @since 1.0.6 - */ - public final void registerLifeCycleComponent(LifeCycleComponent component) { - mWeakLives.add(new SmartReference0(component)); - } - - /** - * unregister the life cycle component - * @param component the life cycle component - * @since 1.0.6 - */ - public final void unregisterLifeCycleComponent(LifeCycleComponent component) { - findLifeCycleComponent(component, true); - } - - /** - * register the life cycle component as weakly.. - * @param component the life cycle component which will be weak reference - * @since 1.0.7 - */ - public final void registerLifeCycleComponentWeakly(LifeCycleComponent component) { - final SmartReference0 srf = new SmartReference0(component); - srf.tryWeakReference(); - mWeakLives.add(srf); - } - - /** - * indicate has the life cycle component or not. - * @param component the life cycle component - * @return true if has the target life cycle component - * @since 1.0.6 - */ - public final boolean hasLifeCycleComponent(LifeCycleComponent component){ - return findLifeCycleComponent(component, false) != null; - } public @Nullable AppImageComponent getAppImageComponent() { if (mImageCpt == null) { @@ -153,73 +102,16 @@ AppToastComponent getAppToastComponent() { return mToastCpt; } - @OnLifecycleEvent(Lifecycle.Event.ON_CREATE) - @CallSuper - public void onCreate() { - performLifeCycle(ON_CREATE); - } - - @OnLifecycleEvent(Lifecycle.Event.ON_START) - @CallSuper - public void onStart() { - performLifeCycle(ON_START); - } - - @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) - @CallSuper - public void onResume() { - performLifeCycle(ON_RESUME); - } - - @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) - @CallSuper - public void onPause() { - performLifeCycle(ON_PAUSE); - } - - @OnLifecycleEvent(Lifecycle.Event.ON_STOP) - @CallSuper - public void onStop() { - performLifeCycle(ON_STOP); - } - - @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) - @CallSuper - public void onDestroy() { - performLifeCycle(ON_DESTROY); - } - - private LifeCycleComponent findLifeCycleComponent(LifeCycleComponent context, boolean remove) { - LifeCycleComponent result = null; - final Iterator it = mWeakLives.iterator(); - for (; it.hasNext(); ) { - final SmartReference0 srf = it.next(); - if (srf.isAlive()) { - final LifeCycleComponent tem = srf.get(); - if (tem == context || tem.equals(context)) { - result = tem; - if(remove) { - it.remove(); - } - break; - } - } else { - it.remove(); - } - } - return result; - } - private void registerLifeCycleContextIfNeed(Object component) { if (component != null && component instanceof LifeCycleComponent) { - mWeakLives.add(new SmartReference0((LifeCycleComponent) component)); + mWeakLives.add(new SmartReference0<>((LifeCycleComponent) component)); } } - private void performLifeCycle(int liftCycle) { + protected void performLifeCycle(int liftCycle) { final Activity activity = getActivity(); //currently flag only use as single, future may be multi - final Iterator it = mWeakLives.iterator(); + final Iterator> it = mWeakLives.iterator(); for (; it.hasNext(); ) { SmartReference item = it.next(); if (item.isAlive()) { @@ -230,62 +122,4 @@ private void performLifeCycle(int liftCycle) { } } - /** - * @since 1.0.6 - */ - private static class SmartReference0 extends SmartReference { - - /** - * create the smart reference for target object. - * - * @param t the object to reference. - */ - SmartReference0(LifeCycleComponent t) { - super(t); - } - - @Override - protected boolean shouldWeakReference(LifeCycleComponent t) { - return t instanceof AppComponentContext - || t instanceof Context - || t instanceof Fragment - || t instanceof android.support.v4.app.Fragment - || t instanceof View - || t instanceof Dialog; - } - - @Override - protected boolean shouldDestroyReference(LifeCycleComponent t) { - final String name = t.getClass().getName(); - if(t instanceof Activity){ - final Activity ac = (Activity) t; - if(ac.isFinishing()){ - Log.w(TAG,"shouldDestroyReference >>> the activity(" + name + ") is finishing."); - return true; - } - if (Build.VERSION.SDK_INT >= 17 && ac.isDestroyed()) { - Log.w(TAG, "shouldDestroyReference>>> memory leaked ? activity = " - + name); - return true; - } - } - if(t instanceof android.support.v4.app.Fragment){ - final android.support.v4.app.Fragment frag = (android.support.v4.app.Fragment) t; - if(frag.isDetached() || frag.isRemoving()){ - Log.w(TAG,"shouldDestroyReference>>> fragment is detached or removing. fragment = " - + name); - return true; - } - } - if( t instanceof Fragment){ - final Fragment frag = (Fragment) t; - if(frag.isDetached() || frag.isRemoving()){ - Log.w(TAG,"shouldDestroyReference>>> fragment is detached or removing. fragment = " - + name); - return true; - } - } - return false; - } - } } diff --git a/Android-components/android-app-components/src/main/java/com/heaven7/android/component/AppComponentOwner2.java b/Android-components/android-app-components/src/main/java/com/heaven7/android/component/AppComponentOwner2.java new file mode 100644 index 0000000..39d9db2 --- /dev/null +++ b/Android-components/android-app-components/src/main/java/com/heaven7/android/component/AppComponentOwner2.java @@ -0,0 +1,44 @@ +package com.heaven7.android.component; + +import android.arch.lifecycle.LifecycleObserver; +import android.arch.lifecycle.LifecycleOwner; +import android.support.annotation.NonNull; + +import com.heaven7.android.component.lifecycle.LifeCycleComponent2; +import com.heaven7.java.base.util.SmartReference; + +import java.util.Iterator; + +/** + * app fragment component owner + * @author heaven7 + */ +public class AppComponentOwner2 extends AbstractLifeCycleComponentOwner implements LifecycleObserver { + + private static final String TAG = "AppComponentOwner"; + + /** + * create app-component context. this should be called before 'super.onCreate(saveInstanceState)' + * + * @param owner the owner + */ + public AppComponentOwner2(@NonNull LifecycleOwner owner) { + super(owner); + // ReportFragment.injectIfNeededIn(activity); + } + + protected void performLifeCycle(int liftCycle) { + LifecycleOwner owner = getLifecycleOwner(); + //currently flag only use as single, future may be multi + final Iterator> it = mWeakLives.iterator(); + for (; it.hasNext(); ) { + SmartReference item = it.next(); + if (item.isAlive()) { + item.get().onLifeCycle(owner, liftCycle); + } else { + it.remove(); + } + } + } + +} \ No newline at end of file diff --git a/Android-components/android-app-components/src/main/java/com/heaven7/android/component/SmartReference0.java b/Android-components/android-app-components/src/main/java/com/heaven7/android/component/SmartReference0.java new file mode 100644 index 0000000..4259e57 --- /dev/null +++ b/Android-components/android-app-components/src/main/java/com/heaven7/android/component/SmartReference0.java @@ -0,0 +1,72 @@ +package com.heaven7.android.component; + +import android.app.Activity; +import android.app.Dialog; +import android.content.Context; +import android.os.Build; +import android.support.v4.app.Fragment; +import android.util.Log; +import android.view.View; + +import com.heaven7.java.base.util.SmartReference; + +/** + * @since 1.0.6 + */ +class SmartReference0 extends SmartReference { + + private static final String TAG = "SmartReference0"; + + /** + * create the smart reference for target object. + * + * @param t the object to reference. + */ + SmartReference0(T t) { + super(t); + } + + @Override + protected boolean shouldWeakReference(T t) { + return t instanceof AppComponentContext + || t instanceof Context + || t instanceof Fragment + || t instanceof android.support.v4.app.Fragment + || t instanceof View + || t instanceof Dialog; + } + + @Override + protected boolean shouldDestroyReference(T t) { + final String name = t.getClass().getName(); + if (t instanceof Activity) { + final Activity ac = (Activity) t; + if (ac.isFinishing()) { + Log.w(TAG, "shouldDestroyReference >>> the activity(" + name + ") is finishing."); + return true; + } + if (Build.VERSION.SDK_INT >= 17 && ac.isDestroyed()) { + Log.w(TAG, "shouldDestroyReference>>> memory leaked ? activity = " + + name); + return true; + } + } + if (t instanceof android.support.v4.app.Fragment) { + final android.support.v4.app.Fragment frag = (android.support.v4.app.Fragment) t; + if (frag.isDetached() || frag.isRemoving()) { + Log.w(TAG, "shouldDestroyReference>>> fragment is detached or removing. fragment = " + + name); + return true; + } + } + if (t instanceof Fragment) { + final Fragment frag = (Fragment) t; + if (frag.isDetached() || frag.isRemoving()) { + Log.w(TAG, "shouldDestroyReference>>> fragment is detached or removing. fragment = " + + name); + return true; + } + } + return false; + } +} \ No newline at end of file diff --git a/Android-components/android-app-components/src/main/java/com/heaven7/android/component/lifecycle/LifeCycleComponent2.java b/Android-components/android-app-components/src/main/java/com/heaven7/android/component/lifecycle/LifeCycleComponent2.java new file mode 100644 index 0000000..e16be27 --- /dev/null +++ b/Android-components/android-app-components/src/main/java/com/heaven7/android/component/lifecycle/LifeCycleComponent2.java @@ -0,0 +1,18 @@ +package com.heaven7.android.component.lifecycle; + +import android.arch.lifecycle.LifecycleOwner; + +/** + * the lifecycle context + * Created by heaven7 on 2017/12/20. + * @since 1.1.3 + */ +public interface LifeCycleComponent2 { + + /** + * callback on lifecycle changed + * @param context the context + * @param lifeCycle the lifecycle flag .see @{@linkplain LifeCycleComponent#ON_CREATE} and etc. + */ + void onLifeCycle(LifecycleOwner context, int lifeCycle); +}