package hh.dejsem.fm;

import android.os.Message;
import android.os.RemoteException;

import java.util.ArrayList;

import hh.dejsem.K;
import hh.dejsem.TitleBar;
import hh.lib.D;

/**
 /**----------------------------------------------------------
 * singleton udržující evidenci o probíhajících síťových přenosech
 * -----------------------------------------------------------
 */
public class TransferOverview {

	D d;
	BarNotification barNotif = null;
	public final ArrayList<NetTaskRef> statSet = new ArrayList<NetTaskRef>();
	public final int refreshInterval = 1000;

	public TransferOverview(D d) {
		this.d = d.klon(this);
		barNotif = new BarNotification(this.d, ProgressMonitor.class);
	}

	int transfCnt() { synchronized(statSet) { return statSet.size(); } }

	public void registerTask(NetTaskRef netTaskRef) {
		synchronized(statSet) {
			barNotif.pin();
			if(transfCnt() == 0) d.l(4, "bar notification established");
			TitleBar.setPending();
			try { K.title.msgr.send(Message.obtain(null, K.MSG_WATCH_PROGRESS)); } catch(RemoteException e) {}
			statSet.add(netTaskRef);
		}
		if(d.ll(4)) d.l("background task on port " + netTaskRef.getPort() + " registered");
	}

	public void unRegisterTask(NetTaskRef netTaskRef) {
		synchronized(statSet) {
			statSet.remove(netTaskRef);
			if(transfCnt() == 0) {
				barNotif.cut();
				TitleBar.unSetPending();
			}
		}
		if(d.ll(4)) d.l("background task on port " + netTaskRef.getPort() + " unregistered");
	}
}
