package cn.net.caoyao.cpzshandset;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.facebook.stetho.Stetho;

import cn.net.caoyao.cpzshandset.common.config.Config;
import cn.net.caoyao.cpzshandset.common.util.CrashHandler;

public class MyHandsetApp extends Application {
    private List<Activity> activities;
    private static Context applicationContext;

    @Override
    public void onCreate() {
        super.onCreate();
        activities = new ArrayList<Activity>();
        applicationContext = getApplicationContext();
        if ((!Config.isDebuger) || Config.testForJC) {
            CrashHandler catchHandler = CrashHandler.getInstance();
            catchHandler.init(getApplicationContext());
        }
//        SeuicScanSetting.init(this);
        Stetho.initializeWithDefaults(this);
    }

    public static int getProt(){
    	SharedPreferences preferences=getAppContext().getSharedPreferences("currentProtTypeConfig", Context.MODE_PRIVATE);
    	int prot = preferences.getInt("currentProtType", 1);
    	return prot;
    }
    public static int saveProt(){
    	SharedPreferences preferences=getAppContext().getSharedPreferences("currentProtTypeConfig", Context.MODE_PRIVATE);
    	int prot = preferences.getInt("currentProtType", 1);
    	Editor editor=preferences.edit();
    	int modifyProt = -1 * prot;
//    	Log.i(TConfig.Echo, "修改为currentProtType:" + modifyProt);
    	editor.putInt("currentProtType", modifyProt);
    	editor.commit();
    	return prot;
    }
    public static Context getAppContext() {

        return applicationContext;
    }

    /**
     * 将activity 添加到list中
     *
     * @param activity
     */
    public void addActivity(Activity activity) {
        activities.add(activity);
    }

    /**
     * 从集合中删除当前activity
     *
     * @param activity
     */
    public void removeActivity(Activity activity) {
        activities.remove(activity);
    }

    /**
     * 退出应用
     */
    public void exitApplication() {
        if (activities != null) {
            for (Activity activity : activities) {
                activity.finish();
            }
            System.exit(0);
        }
    }
}
