
package com.tcl.factory.view;
//import com.mediatek.tv.common.ConfigType;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.os.SystemProperties;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.RelativeLayout;

import com.tcl.customerapi.R;
import com.tcl.factory.bsp.TV;
import com.tcl.factory.bsp.TVDefine;
import com.tcl.factory.def.DEFS;
import com.tcl.factory.thirdparty.ThirdParty;
import com.tcl.factory.utils.Debug;
import com.tcl.os.system.WindowManager;

public class ShopActivity extends Activity {
    private final Object mActionDoneSync = new Object();
    private boolean mActionDone;
    private static final int MAX_BROADCAST_TIME = 3*1000;

	private String TAG = this.getClass().getName();
	MyThread myHandler = new MyThread();
	Thread thread=null;
	RelativeLayout backGroundColor=null;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.fac_warmup);		
		backGroundColor=(RelativeLayout)findViewById(R.id.backgroud_layout);
		thread=	new Thread(myHandler);
		thread.start();
	}
	@Override
	 public void onAttachedToWindow(){
		//TV.APPKeyGuard(DEFS.Enable);
		super.onAttachedToWindow();
	 } 
	
	@Override
	protected void onStart() {
		Debug.v(TAG, "onStart");
		super.onStart();
		
		if(TV.readS(TVDefine.SerialDebug) == DEFS.Enable) 
		{
			//ִ�� SHOP ʱ���Զ����������Ŀ��
			//MAC Addr �Ƿ�д
			//Device ID �Ƿ�д
			//HDCP key �Ƿ�д
			String str="";
			short[] macIdstatus = ThirdParty.pollingDeviceIDAndMacStatus();
			short[] hdcpstatus = ThirdParty.pollingHdcpStatus();
			switch (macIdstatus[0]) {
			case 0:
				str = "Please SET MAC  DEVICEDID  ";
				break;
			case 1:
				str = "Please SET DEVICEDID";
				break;
			case 2:
				str = "Please SET MAC ";
				break;
			}
			if(!str.equalsIgnoreCase(""))
				DisplayError(str);
		}
	}
	@Override
	protected void onDestroy() {
		Debug.v(TAG, "onDestroy");
		backGroundColor.setBackgroundResource(R.color.transparent);
		super.onDestroy();
	}
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		return true;
	}
	private void DisplayError(String str) {
		AlertDialog.Builder b = new AlertDialog.Builder(this);
		b.setTitle("Error");
		b.setMessage(str);
		//		b.setPositiveButton("OK", new OnClickListener() {
		//			public void onClick(DialogInterface dialog, int which) {
		//				ShopActivity.this.finish();
		//			}
		//		});
		b.show();
	}
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch(msg.what) {
			case 1:
				backGroundColor.setBackgroundResource(R.color.green_color);
				break;
			case 2:
				backGroundColor.setBackgroundResource(R.color.red_color);
				break;
			}
			super.handleMessage(msg);
		}
	};
    void actionDone() {
    	synchronized (mActionDoneSync) {
    		mActionDone = true;
    		mActionDoneSync.notifyAll();
           	}
        }

	public class MyThread implements Runnable{
		@Override
		public void run() {
            BroadcastReceiver br = new BroadcastReceiver() {
            @Override public void onReceive(Context context, Intent intent) {
		        // We don't allow apps to cancel this, so ignore the result.
		        Log.d(TAG, "gaoyunpei,enter receiver.");
		        actionDone();
		        Log.d(TAG, "gaoyunpei,out receiver.");
		        }
            };

			Message message=Message.obtain();
			message.what=1;
			handler.sendMessage(message);
			
			Log.d(TAG,"dongle_init..");
			WindowManager manager = new WindowManager(ShopActivity.this); 
			manager.activeKeyByKeycode(-1, true);
			ThirdParty.setAdbdStatus(false); // ���Ҫ�󣺽���adb
			ThirdParty.hotel_reset_to_default();
			//SystemProperties.set("persist.sys.bootfromui","false");
			//SystemProperties.set("persist.sys.bootactivity","TV");		

			//RootSeeker.exec("su");
			//RootSeeker.exec("tclsu");
			//RootSeeker.exec("mount -o remount,rw /system");
			//RootSeeker.exec("system/xbin/dongle_init");
			SystemProperties.set("persist.P_MODE", "0");
			SystemProperties.set("persist.sys.logd.level", "S");
			ThirdParty.setRootSeek("rm -Rf /data/property/persist.sys.logd.level");
			ThirdParty.setRootSeek("rm /data/property/persist.sys.boot*");
			ThirdParty.setRootSeek("rm -Rf /data/misc/bluedroid/");
			//RootSeeker.exec("rm /data/property/persist.sys.locale");
            Log.d(TAG, "gaoyunpei,send broadcast.");
            mActionDone = false;
		    Intent intent = new Intent();
            intent.setAction("tcl.intent.ACTION_FACTORY_SHOP");
            intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
            sendOrderedBroadcast(intent, null, br,null, 0, null, null);
		    Log.d(TAG, "gaoyunpei,receiver work begin.");
            final long endTime = SystemClock.elapsedRealtime() + MAX_BROADCAST_TIME;
            synchronized (mActionDoneSync) {
          	  	while (!mActionDone) {
          	  		long delay = endTime - SystemClock.elapsedRealtime();
          	  		if (delay <= 0) {
          	  			Log.d(TAG, "gaoyunpei,Shop broadcast timed out");
          	  			break;
          	  		}
          	  		try {
          	  			mActionDoneSync.wait(delay);
          	  		} catch (InterruptedException e) {
          	  		}
          	  	}
            }

            Log.d(TAG, "gaoyunpei,receiver work finish.");
            ThirdParty.setRootSeek("rm -Rf /data/property/persist.P_MODE");
            ThirdParty.setRootSeek("rm -Rf /data/data/com.android.providers.settings/");
            ThirdParty.setRootSeek("rm -Rf /data/system/users/0/settings_secure.xml");
            ThirdParty.setRootSeek("rm -Rf /data/system/users/0/package-restrictions.xml");
			ThirdParty.mFactoryAdapter.shellExecute("shopInit");
			/*
			if(ThirdParty.IsEUArea())
			{
				finish();
			}
			*/
		}
	}
}
