package com.yixia.camera.demo;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;

import com.yixia.camera.VCamera;
import com.yixia.camera.demo.service.AssertService;
import com.yixia.camera.util.DeviceUtils;

public class VCameraDemoApplication extends Application {

	private static VCameraDemoApplication application;

	@Override
	public void onCreate() {
		super.onCreate();
		application = this;

		// 璁剧疆鎷嶆憚瑙嗛缂撳瓨璺緞
		File dcim = Environment
				.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
		if (DeviceUtils.isZte()) {
			if (dcim.exists()) {
				VCamera.setVideoCachePath(dcim + "/WeChatJuns/");
			} else {
				VCamera.setVideoCachePath(dcim.getPath().replace("/sdcard/",
						"/sdcard-ext/")
						+ "/WeChatJuns/");
			}
		} else {
			VCamera.setVideoCachePath(dcim + "/WeChatJuns/");
		}
		// 寮€鍚痩og杈撳嚭,ffmpeg杈撳嚭鍒發ogcat
		VCamera.setDebugMode(true);
		// 鍒濆鍖栨媿鎽凷DK锛屽繀椤?		VCamera.initialize(this);

		// 瑙e帇assert閲岄潰鐨勬枃浠?		startService(new Intent(this, AssertService.class));
	}

	public static Context getContext() {
		return application;
	}

	// 杩愮敤list鏉ヤ繚瀛樹滑姣忎竴涓猘ctivity鏄叧閿?	private List<Activity> mList = new LinkedList<Activity>();
	private static VCameraDemoApplication instance;

	// 鏋勯€犳柟娉?	// 瀹炰緥鍖栦竴娆?	public synchronized static VCameraDemoApplication getInstance() {
		if (null == instance) {
			instance = new VCameraDemoApplication();
		}
		return instance;
	}

	// add Activity
	public void addActivity(Activity activity) {
		mList.add(activity);
	}

	// 鍏抽棴姣忎竴涓猯ist鍐呯殑activity
	public void exit() {
		try {
			for (Activity activity : mList) {
				if (activity != null)
					activity.finish();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			System.exit(0);
		}
	}

}