package com.secspace.lib.common.base;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

/**
 * Created by cyw_m on 2018/3/31.
 */

public class BaseApplication extends Application {
    protected final String TAG = "SecSpaceApplication";

    private static BaseApplication mBase;
    private String mPreInstalCachePath;

    @Override
    public void onCreate() {
        super.onCreate();
        mBase = this;
        mPreInstalCachePath = getCacheDir().getAbsolutePath();
        Log.i(TAG, "onCreate: ");
    }

    public static BaseApplication getBase() {
        return mBase;
    }
    public Handler getMainHandler(){
        return new Handler(Looper.getMainLooper());
    }

    public String getPreInstallCachePath() {
        return mPreInstalCachePath;
    }

    //这个函数是模拟一个过程环境，在真机中永远也不会被调用。
    @Override
    public void onTerminate() {
        super.onTerminate();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        Log.w(TAG, "onLowMemory: ");
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        switch (level) {
            case TRIM_MEMORY_COMPLETE:
                Log.w(TAG, "onTrimMemory TRIM_MEMORY_COMPLETE");
                break;
            case TRIM_MEMORY_MODERATE:
                Log.w(TAG, "onTrimMemory TRIM_MEMORY_MODERATE");
                break;
            case TRIM_MEMORY_BACKGROUND:
                Log.w(TAG, "onTrimMemory TRIM_MEMORY_BACKGROUND");
                break;
            case TRIM_MEMORY_RUNNING_CRITICAL:
                Log.w(TAG, "onTrimMemory TRIM_MEMORY_RUNNING_CRITICAL");
                break;
            case TRIM_MEMORY_RUNNING_LOW:
                Log.w(TAG, "onTrimMemory TRIM_MEMORY_RUNNING_LOW");
                break;
            case TRIM_MEMORY_RUNNING_MODERATE:
                Log.w(TAG, "onTrimMemory TRIM_MEMORY_RUNNING_MODERATE");
                break;
            case TRIM_MEMORY_UI_HIDDEN:
                Log.w(TAG, "onTrimMemory TRIM_MEMORY_UI_HIDDEN");
                break;
        }
    }


    /**
     * 获取当前进程名称
     */
    private String getCurrentProcessName() {
        String processName = "";
        ActivityManager manager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo process : manager.getRunningAppProcesses()) {
            if (process.pid == android.os.Process.myPid()) {
                processName = process.processName;
            }
        }
        return processName;
    }

    /**
     * 判断是不是主进程
     *
     * @param
     * @return
     */
    public boolean isMainProcess() {
        return getApplicationContext().getPackageName().equals(getCurrentProcessName());
    }


}
