package cn.ycoder.android.library;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;

import cn.ycoder.android.library.store.AppTagStore;
import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.launcher.ARouter;

import java.util.List;

import cn.ycoder.android.library.route.UriReplaceService;
import cn.ycoder.android.library.tool.ErrorHandlers;
import cn.ycoder.android.library.tool.LogUtils;
import cn.ycoder.android.library.tool.Utils;
import io.reactivex.plugins.RxJavaPlugins;

/**
 * @author 启研
 * @created at 2017/4/5 13:57
 */

public abstract class BaseApplication extends Application {

  public static boolean debug = true;

  private static BaseApplication app;

  /**
   * 获取Application单例
   */
  public static BaseApplication getInstance() {
    if (app == null) {
      new NullPointerException("MyApplication is null!");
    }
    return app;
  }

  private AppTagStore mAppTag;

  @Override
  public void onCreate() {
    super.onCreate();
    if (isMainProcess(this)) {
      app = this;
      //工具使用App
      Utils.init(app, app.getPackageName());
      RxJavaPlugins.setErrorHandler(ErrorHandlers.displayErrorConsumer(app));
      mAppTag = initAppTag(this);
      onlyInit();
      //开启日志
      LogUtils.setAllEnable(debug);
      //开启路由
      if (debug) {
        ARouter.openLog();
        ARouter.openDebug();
      }
      ARouter.init(app);
    }
  }


  /**
   * 仅初始化一次
   */
  public abstract void onlyInit();

  public abstract AppTagStore initAppTag(Context context);

  private boolean isMainProcess(Context context) {
    ActivityManager am = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE));
    List<ActivityManager.RunningAppProcessInfo> processInfoList = am.getRunningAppProcesses();
    String mainProcessName = context.getPackageName();
    int myPid = android.os.Process.myPid();
    for (ActivityManager.RunningAppProcessInfo info : processInfoList) {
      if (info.pid == myPid && mainProcessName.equals(info.processName)) {
        return true;
      }
    }
    return false;
  }

  /**
   * 获得需转换的路径服务
   */
  public abstract UriReplaceService[] getUriReplaceService();

  /**
   * 得到tag数据
   */
  public Object getTag(String tag) {
    if (mAppTag != null) {
      return mAppTag.getTag(tag);
    }
    return null;
  }

  /**
   * 设置tag数据
   */
  public void setTag(String tag, Object value) {
    if (mAppTag != null) {
      mAppTag.setTag(tag, value);
    }
  }
}

@@ -133,191 +133,8 @@
 xt;%0A
-%0Aimport cn.ycoder.android.library.store.AppTagStore;%0Aimport com.alibaba.android.arouter.facade.Postcard;%0Aimport com.alibaba.android.arouter.launcher.ARouter;%0A%0Aimport java.util.List;%0A%0A
 impo
@@ -187,16 +187,68 @@
 ervice;%0A
+import cn.ycoder.android.library.store.AppTagStore;%0A
 import c
@@ -381,24 +381,77 @@
 tool.Utils;%0A
+import com.alibaba.android.arouter.launcher.ARouter;%0A
 import io.re
@@ -480,16 +480,39 @@
 Plugins;
+%0Aimport java.util.List;
 %0A%0A/**%0A *

修正：删除不必要的方法