package com.zjw.mobilesafe.service;

import java.util.List;

import com.zjw.mobilesafe.InputPwdActivity;
import com.zjw.mobilesafe.db.dao.AppLockDao;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;

public class WatchdogService extends Service {
	private ActivityManager am;
	private boolean flag;
	private AppLockDao dao;
	private TmpStopReceiver tmpStopReceiver;
	private ScreenOffReceiver screenOffReceiver;
	private AppLockListChangedReceiver appLockListChangedReceiver;
	private String protected_packname;
	private List<String> protected_list;
	private Intent intent;
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		tmpStopReceiver = new TmpStopReceiver();
		registerReceiver(tmpStopReceiver, new IntentFilter("com.zjw.mobilesafe.tmpstop"));
		screenOffReceiver = new ScreenOffReceiver();
		registerReceiver(screenOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
		appLockListChangedReceiver = new AppLockListChangedReceiver();
		registerReceiver(appLockListChangedReceiver, new IntentFilter("com.zjw.mobilesafe.appLockListChanged"));
		
		am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
		dao = new AppLockDao(this);
		flag = true;
		protected_list = dao.findAll();//Õâ¸ölistÊÇ»áÓÐ±ä»¯µÄ£¬Òò´ËÐèÒªÓÐÒ»¸ö¹ã²¥À´¸üÐÂ¸Ãlist
		
		intent = new Intent(getApplicationContext(), InputPwdActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		
		//»ñÈ¡×îÐÂ½ø³Ì£¬´Ë·½·¨×ÜÊÇ°Ñ×îÐÂ½ø³Ì·ÅÔÚµÚÒ»Î»
		//¼´£ºÈçºÎ»ñÈ¡ÓÃ»§µ±Ç°ÕýÔÚÊ¹ÓÃµÄactivity
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				while(flag){
					List<RunningTaskInfo> runningTasks = am.getRunningTasks(1);//ÓÅ»¯1£¬½«»ñÈ¡Êý´Ó100¸ÄÎª1
					String packageName = runningTasks.get(0).topActivity.getPackageName();
					System.out.println("packagename:"+packageName);//ÓÅ»¯2£¬×¢ÊÍµô´ËÐÐ´úÂë

//					if (dao.find(packageName)) {//ÓÅ»¯3£º²éÑ¯Êý¾Ý¿â
					if(protected_list.contains(packageName)){
						if (packageName.equals(protected_packname)) {
							//´ËÊ±²»ÐèÒª±£»¤£¬Ê²Ã´ÊÂÒ²²»×ö
						}else {
							//ÓÅ»¯4£º½«¿ªÆôÃÜÂëactivityµÄÒâÍ¼¶¨Òåµ½ÍâÃæÈ¥
							intent.putExtra("packname", packageName);
							startActivity(intent);
						}
					}
					try {
						//ÕâÊÇÒ»¶¨ÒªÐ´µÄ£¬²»È»Ì«¿ì
						Thread.sleep(20);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}).start();
		
		super.onCreate();
	}
	
	/**
	 * ÄÚ²¿Àà£º¹ã²¥½ÓÊÜÕß£¬½ÓÊÕÔÝÊ±Í£Ö¹¿´ÃÅ¹·¼à¿ØÖ¸¶¨°üÃûµÄ¹ã²¥
	 */
	private class TmpStopReceiver extends BroadcastReceiver{

		@Override
		public void onReceive(Context context, Intent intent) {
			protected_packname = intent.getStringExtra("packname");
		}
	}
	/**
	 * ¹ã²¥½ÓÊÕÕß£¬½ÓÊÜËøÆÁ¹ã²¥
	 */
	private class ScreenOffReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			System.out.println("ËøÆÁÁË");
			protected_packname = null;
		}
	}
	/**
	 * ¹ã²¥½ÓÊÕÕß£¬½ÓÊÕÊý¾Ý¿â·¢Éú±ä»¯µÄ¹ã²¥
	 */
	private class AppLockListChangedReceiver extends BroadcastReceiver{

		@Override
		public void onReceive(Context context, Intent intent) {
			protected_list = dao.findAll();
		}
	}
	
	@Override
	public void onDestroy() {
		flag = false;
		unregisterReceiver(tmpStopReceiver);
		tmpStopReceiver = null;
		unregisterReceiver(screenOffReceiver);
		screenOffReceiver = null;
		unregisterReceiver(appLockListChangedReceiver);
		appLockListChangedReceiver = null;
		super.onDestroy();
	}
	
}
