package com.fyx.yunpan.net;

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

import android.content.Context;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;

import com.fyx.yunpan.db.DBManager;
import com.fyx.yunpan.parse.entity.FileDataDBEntity;
import com.fyx.yunpan.parse.entity.FileDataEntity;

public class DownloadRun implements Runnable {
	private int id = 0;
	private int t = 0;
	private Context context;
	private Handler handler;
	private Message msg;
	private DownloadRun thread;
	private List<FileDataEntity> file;
	private DBManager utils;

	private Handler threadHandler = null;

	private DownloadRun(int t, int id, Context context, Handler handler,
			List<FileDataEntity> file) {

		this.id = id;
		this.file = file;
		this.t = t;
		this.thread = this;
		this.context = context;
		this.handler = handler;
		
	}

	


	public DownloadRun() {
		super();
		// TODO Auto-generated constructor stub
	}




	public static DownloadRun getDownLoadRun() {
		return new DownloadRun();
	}

	public Handler getThreadHandler() {
		return threadHandler;
	}

	
	public void addToDB(int t, int id, Context context, Handler handler,
			List<FileDataEntity> file) {
		this.id = id;
		this.file = file;
		this.t = t;
		this.thread = this;
		this.context = context;
		this.handler = handler;
		utils = new DBManager(context);
		utils.open();
		new Thread(this).start();
		

	}
	
	

	

	public void add() {
		//ÏÂÔØ ÁÐ±í
		String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+Environment.DIRECTORY_DCIM;
		for (int i = 0; i < file.size(); i++) {
			FileDataDBEntity dataDBEntity = file.get(i).getFileDataDBEntity();
			dataDBEntity.setPath(path);
			utils.addDownloadingFile(dataDBEntity);
		}
		msg = new Message();
		msg.what = 1;
		handler.sendMessage(msg);
	}

	@Override
	public void run() {
					add();
	}

}
