content: /**
    * Copyright 2017 JessYan
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
    *
    *      http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    * See the License for the specific language governing permissions and
    * limitations under the License.
    */
  package <%= appPackage %>.integration.lifecycle;
  
  import android.app.Activity;
  import android.app.Application;
  import android.os.Bundle;
  import android.support.v4.app.FragmentActivity;
  import android.support.v4.app.FragmentManager;
  
  import com.trello.rxlifecycle2.RxLifecycle;
  import com.trello.rxlifecycle2.android.ActivityEvent;
  
  import javax.inject.Inject;
  import javax.inject.Singleton;
  
  import io.reactivex.subjects.Subject;
  
  /**
   * ================================================
   * 配合 {@link ActivityLifecycleable} 使用,使 {@link Activity} 具有 {@link RxLifecycle} 的特性
   *
   * Created by JessYan on 25/08/2017 18:56
   * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
   * <a href="https://github.com/JessYanCoding">Follow me</a>
   * ================================================
   */
  @Singleton
  public class ActivityLifecycleForRxLifecycle implements Application.ActivityLifecycleCallbacks {
      private FragmentManager.FragmentLifecycleCallbacks mFragmentLifecycle;
  
      @Inject
      public ActivityLifecycleForRxLifecycle() {
      }
  
      @Override
      public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
          if (activity instanceof ActivityLifecycleable) {
              obtainSubject(activity).onNext(ActivityEvent.CREATE);
              if (activity instanceof FragmentActivity){
                  if (mFragmentLifecycle == null) {
                      mFragmentLifecycle = new FragmentLifecycleForRxLifecycle();
                  }
                  ((FragmentActivity) activity).getSupportFragmentManager().registerFragmentLifecycleCallbacks(mFragmentLifecycle, true);
              }
          }
      }
  
      @Override
      public void onActivityStarted(Activity activity) {
          if (activity instanceof ActivityLifecycleable) {
              obtainSubject(activity).onNext(ActivityEvent.START);
          }
      }
  
      @Override
      public void onActivityResumed(Activity activity) {
          if (activity instanceof ActivityLifecycleable) {
              obtainSubject(activity).onNext(ActivityEvent.RESUME);
          }
      }
  
      @Override
      public void onActivityPaused(Activity activity) {
          if (activity instanceof ActivityLifecycleable) {
              obtainSubject(activity).onNext(ActivityEvent.PAUSE);
          }
      }
  
      @Override
      public void onActivityStopped(Activity activity) {
          if (activity instanceof ActivityLifecycleable) {
              obtainSubject(activity).onNext(ActivityEvent.STOP);
          }
      }
  
      @Override
      public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
  
      }
  
      @Override
      public void onActivityDestroyed(Activity activity) {
          if (activity instanceof ActivityLifecycleable) {
              obtainSubject(activity).onNext(ActivityEvent.DESTROY);
          }
      }
  
      private Subject<ActivityEvent> obtainSubject(Activity activity) {
          return ((ActivityLifecycleable) activity).provideLifecycleSubject();
      }
  }
  