package com.test.hfsimple;

import android.app.Activity;
import android.app.Application;


import com.pda.hf.HFReader;

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

/**
 * use to global value
 * Created by admin on 2017/1/16.
 */
public class MyApplication extends Application {


    private HFReader hfReader = null ;


    private  int port = 14 ;
    private int power = HFReader.POWER_PSAM ;


    public HFReader getHfReader() {
//        hfReader = new HFReader(13, 115200 , HFReader.POWER_RFID) ;
        if (hfReader == null) {
            hfReader = new HFReader(port, 115200, power) ;
        }
        return hfReader;
    }

    public void setHfReader(HFReader hfReader) {
        this.hfReader = hfReader;
    }

    private List<Activity> listAc  = new ArrayList<Activity>();

    @Override
    public void onCreate() {
        super.onCreate();


    }

    /**
     * add activity
     * @param activity
     */
    public void addActivity(Activity activity){
        if(!listAc.contains(activity)){
            listAc.add(activity) ;
        }

    }

    /**
     * kill all activity
     */
    public void exit(){
        if (hfReader != null) {
            hfReader.close(port);
            hfReader = null ;
        }
        try {
            for(Activity ac : listAc){
                if (ac != null)
                    ac.finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.exit(0);
        }

    }
}
