package com.usai.redant.photo;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import com.usai.util.Network;
import com.usai.util.dbUtil;
import com.usai.util.dbgUtil;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity
{
	// static final int ACTIVITY_CAMERA = 0;
	// static final int ACTIVITY_VIEW = 1;
	// static final String serverurl = "http://192.168.23.1/xampp/";
	static final String	TAG						= "MainActivity";
	static final int	REQUEST_TAKE_PHOTO		= 1;
	static final int	REQUEST_PREVIEW			= 4;
	static final int	REQUEST_SCAN_BARCODE	= 2;
	static final int	REQUEST_LOCKER			= 3;
	// HashMap<String, String> hashMap = new HashMap<String, String>();

	// ArrayList<String> pic_file = new ArrayList<String>();
	// ArrayList<bitmap>
	// ArrayList<>
	// private String thumbpath = null;
	private int			iwidth					= 640;
	private int			iheight					= 480;
	private File		photoFile				= null;
	Bitmap				thumbBitmap				= null;
	// private int iPhotoCount = 0;
	public String		LastFileName			= "";
	private boolean		m_blocked				= false;
	TextView			m_tvalert;
	// private String user;
	// private String password;
	// String m_sName;
	// String m_sPassword;
	ArrayList<String>	photoList				= new ArrayList<String>();

	// @Override
	// protected void onResume()
	// {
	// //sendBroadcast(new Intent("REDANT.POP.REQUEST_LOCATION_CHANGED"));
	// super.onResume();
	// }

	private void StartCamera()
	{
		Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		// Ensure that there's a camera activity to handle the intent

		if (takePictureIntent.resolveActivity(getPackageManager()) != null)
		{
			// Create the File where the photo should go
			// File photoFile = null;
			if (photoFile != null)
				photoFile = null;
			try
			{
				photoFile = createImageFile();
			}
			catch (IOException ex)
			{
				// Error occurred while creating the File
				// ...
			}
			// Continue only if the File was successfully created
			Log.i(TAG, photoFile.toString());
			if (photoFile != null)
			{
				Log.i(TAG, "start system camera====================>");
				takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
						Uri.fromFile(photoFile));
				startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
			}
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			dbgUtil.fileLog("back button click , clear&exit");
			clear();

		}
		return super.onKeyDown(keyCode, event);

	}

	@Override
	protected void onResume()
	{
		sendBroadcast(new Intent("REDANT.POP.GPS_ON"));
		super.onResume();
	}

	@Override
	protected void onPause()
	{
		sendBroadcast(new Intent("REDANT.POP.GPS_OFF"));
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		Log.d(TAG, "=======================>MainActivity Created!");
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_main1);

		setTitle(getTitle() + "(" + RedAntApplication.station_name + ")");
		ImageView thumbsImageView = (ImageView) findViewById(R.id.ivThumbs);
		thumbsImageView.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				if (photoList.size() < 1)
					return;

				Intent intent = new Intent(MainActivity.this,
						PhotoPreviewActivity.class);
				intent.putExtra("pic_list", (Serializable) photoList);
				startActivityForResult(intent, REQUEST_PREVIEW);

			}
		});
		// m_sName = intent.getStringExtra("user");
		// m_sPassword = intent.getStringExtra("password");

		Log.d(TAG, "user=" + RedAntApplication.user + ",password="
				+ RedAntApplication.password);
		IntentFilter msgFilter = new IntentFilter();
		Intent intent = getIntent();
		msgFilter.addAction("android.intent.action.SCREEN_OFF");
		msgFilter.addAction("REDANT.POP.STATION_CHANGE");
		msgFilter.addAction("REDANT.POP.STATION_NOT_CHANGE");
		msgFilter.addAction("REDANT.POP.FINISH_UPLOAD_QUEUE");
		msgFilter.addAction("REDANT.POP.FINISH_UPLOAD_QUEUE_WITH_ERROR");
		msgFilter.addAction("REDANT.POP.CLEAR_UPLOAD_QUEUE");
		msgFilter.setPriority(2147483647);
		registerReceiver(MainActivityReceiver, msgFilter);
		Log.d(TAG, "=======================>msgFilter registed!");
		if (savedInstanceState != null)
		{
			thumbBitmap = getBitmap(savedInstanceState
					.getByteArray("thumbBitmap"));
			photoList.clear();
			// ArrayList list = (ArrayList)
			// Arrays.asList(savedInstanceState.getStringArray("arrayLabel"));
			photoList = savedInstanceState.getStringArrayList("photoList");
			
			TextView pidval = (TextView) findViewById(R.id.pidval);
			pidval.setText(savedInstanceState.getString("PIID"));
			// photoList = A
			// savedInstanceState.getStringArrayList("photoList");
			photoFile = (File) savedInstanceState.getSerializable("photoFile");

			if (thumbBitmap != null)
			{
				thumbsImageView.setImageBitmap(thumbBitmap);
				// Log.d(TAG, "load thumbpath==" + thumbpath);
			}
			// photoFile.delete();
			TextView countTextView = (TextView) findViewById(R.id.tvPhotoCount);
			countTextView.setText(photoList.size() + " Photos");

		}

		ImageButton ibtnscan = (ImageButton) findViewById(R.id.ibtnScan);
		ibtnscan.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				Log.d(TAG, "==============>Click Scan button");
				Intent intent = new Intent();

				intent.setClass(MainActivity.this, CaptureActivity.class);
				startActivityForResult(intent, REQUEST_SCAN_BARCODE);
			}
		});

		ImageButton captureButton = (ImageButton) findViewById(R.id.ibtnCapture);
		captureButton.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View v)
			{

				StartCamera();
			}
		});
		ImageButton uploadButton = (ImageButton) findViewById(R.id.ibtnOk);
		uploadButton.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View v)
			{

				sendBroadcast(new Intent("REDANT.POP.REQUEST_LOCATION"));
				return;

			}
		});

		m_tvalert = (TextView) findViewById(R.id.tvalert);
		m_tvalert.setSelected(true);

		// Intent intent = ;
		sendBroadcast(new Intent("REDANT.POP.MODIFY_QUEUE"));
		updateUploadButton();
	}

	@Override
	protected void onDestroy()
	{
	//	sendBroadcast(new Intent("REDANT.POP.GPS_OFF"));
		Log.d(TAG, "=======================>MainActivity Destroyed!");
		unregisterReceiver(MainActivityReceiver);
		Log.d(TAG, "=======================>msgFilter unregisted!");
		super.onDestroy();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private File createImageFile() throws IOException
	{
		// Create an image file name
		String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
				.format(new Date());
		String imageFileName = "JPEG_" + timeStamp + "_";
		File storageDir = new File(Environment.getExternalStorageDirectory()
				.getPath() + "/redant/pop/temp/");

		File dir1 = new File(Environment.getExternalStorageDirectory()
				.getPath() + "/redant");
		File dir2 = new File(Environment.getExternalStorageDirectory()
				.getPath() + "/redant/pop");
		File dir3 = new File(Environment.getExternalStorageDirectory()
				.getPath() + "/redant/pop/temp");

		
		if (!dir1.exists())
		{
			boolean b = dir1.mkdir();
		}
		if (!dir2.exists())
		{
			boolean b = dir2.mkdir();
		}
		if (!dir3.exists())
		{
			boolean b = dir3.mkdir();
		}
		// Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
		// Log.d(TAG, "DIR:" + storageDir);
		LastFileName = storageDir + imageFileName + ".jpg";
		Log.d(TAG, "File:" + LastFileName);
		File image = File.createTempFile(imageFileName, /* prefix */
				".jpg", /* suffix */
				storageDir /* directory */
		);

		// Save a file: path for use with ACTION_VIEW intents
		// mCurrentPhotoPath = "file:" + image.getAbsolutePath();
		return image;
	}

	boolean createDir(String path)
	{
		Log.i(TAG, "readFile:createDir=" + path);
		boolean ret = false;
		try
		{
			File file = new File(path);
			if (!file.exists())
				file.mkdirs();
			if (!file.isDirectory())
			{
				ret = file.mkdirs();
			}
		}
		catch (Exception e)
		{
			Log.d(TAG, e.toString());
		}
		return ret;
	}

	void updateUploadButton()
	{
		TextView pidval = (TextView) findViewById(R.id.pidval);
		ImageButton ibtnUpload = (ImageButton) findViewById(R.id.ibtnOk);
		if (photoList.size() > 0 && pidval.getText().length() > 0)
		{
			ibtnUpload.setVisibility(View.VISIBLE);

		}
		else
		{
			ibtnUpload.setVisibility(View.INVISIBLE);

		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onActivityResult(int, int,
	 * android.content.Intent)
	 */
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data)
	{

		if (requestCode == REQUEST_TAKE_PHOTO)
			// ������������յ�ͼƬ
			if (resultCode == Activity.RESULT_OK) // ��������

			{

				Log.d(TAG, photoFile.toString());
				photoList.add(photoFile.toString());

				StartCamera();
				// iPhotoCount++;
				super.onActivityResult(requestCode, resultCode, data);

			}
			else
			{

				ImageView thumbsImageView = (ImageView) findViewById(R.id.ivThumbs);

				if (photoList.size() > 0)
				{
					thumbBitmap = ThumbnailUtils.extractThumbnail(BitmapFactory
							.decodeFile(photoList.get(photoList.size() - 1)),
							iwidth, iheight);
					if (thumbBitmap != null)
						thumbsImageView.setImageBitmap(thumbBitmap);
				}

				photoFile.delete();
				TextView countTextView = (TextView) findViewById(R.id.tvPhotoCount);
				countTextView.setText(photoList.size() + " Photos");
				// ImageButton ibtnUpload = (ImageButton)
				// findViewById(R.id.ibtnOk);
				updateUploadButton();

			}

		else if (requestCode == REQUEST_LOCKER)
		{
			if (resultCode == Activity.RESULT_OK)
				m_blocked = false;
		}
		else if (requestCode == REQUEST_PREVIEW)
		{
			for (int i = 0; i < photoList.size(); i++)
			{
				File file = new File(photoList.get(i));
				if (!file.exists())
				{
					photoList.remove(i);
					i--;
				}
			}
			TextView countTextView = (TextView) findViewById(R.id.tvPhotoCount);
			countTextView.setText(photoList.size() + " Photos");
			if (photoList.size() == 0)
			{
				ImageView thumbsImageView = (ImageView) findViewById(R.id.ivThumbs);
				thumbsImageView.setImageResource(R.drawable.no_pic);
				thumbBitmap = null;
			}
		}
		else if (requestCode == REQUEST_SCAN_BARCODE)
		{
			if (resultCode == Activity.RESULT_OK)
			{
				Bundle bundle = data.getExtras();
				String pid = bundle.getString("pid");
				Log.d(TAG,
						"REQUEST_SCAN_BARCODE==========>pid="
								+ bundle.getString("pid"));
				TextView pidval = (TextView) findViewById(R.id.pidval);
				pidval.setText(pid);
				updateUploadButton();
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	public void saveMyBitmap(String bitName, Bitmap mBitmap)
	{
		File f = new File("/sdcard/" + bitName + ".png");
		try
		{
			f.createNewFile();
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			// DebugMessage.put("�ڱ���ͼƬʱ����" + e.toString());
		}
		FileOutputStream fOut = null;
		try
		{
			fOut = new FileOutputStream(f);
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
		try
		{
			fOut.flush();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		try
		{
			fOut.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	// boolean confirm_upload()
	// {
	// boolean bret = false;
	// final Boolean b;
	//
	// return bret;
	// }
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		// TODO Auto-generated method stub
		switch (item.getItemId())
		{
			case R.id.action_clear:
				dbgUtil.fileLog("menu item clear click , clear...");
				clear();
				break;
			case R.id.action_type_pid:
			{
				final View edit = new EditText(this);
				new AlertDialog.Builder(this)
						.setIconAttribute(android.R.attr.alertDialogIcon)
						.setTitle(R.string.title_input_pid)
						.setView(edit)
						.setPositiveButton(android.R.string.ok,
								new DialogInterface.OnClickListener()
								{
									public void onClick(DialogInterface dialog,
											int whichButton)
									{
										String pid = ((EditText) edit)
												.getText().toString();
										TextView pidval = (TextView) findViewById(R.id.pidval);
										pidval.setText(pid);
										updateUploadButton();

										/* User clicked OK so do some stuff */
									}
								})
						.setNegativeButton(android.R.string.cancel,
								new DialogInterface.OnClickListener()
								{
									public void onClick(DialogInterface dialog,
											int whichButton)
									{
										updateUploadButton();
										/* User clicked cancel so do some stuff */
									}
								}).create().show();
			}
				break;
			case R.id.action_server_settings:
			{
				Intent intent = new Intent();
				intent.setClass(MainActivity.this, ServerSettingActivity.class);
				startActivity(intent);

			}
				break;
			case R.id.action_view_queue:
			{
				Intent intent = new Intent();
				intent.setClass(MainActivity.this, UploadQueueActivity.class);
				startActivity(intent);
				break;
			}
			default:
				break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState)
	{
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
		if (photoFile != null)
			outState.putSerializable("photoFile", photoFile);

		if (thumbBitmap != null)
			outState.putByteArray("thumbBitmap", getBytes(thumbBitmap));// ("thumbBitmap",
																		// (Object)thumbBitmap);
																		// outState.putString("thumbpath",
																		// thumbpath);
		// outState.putString("m_sName", m_sName);
		// outState.putString("m_sPassword", m_sPassword);

		outState.putStringArrayList("photoList", photoList);
		// Log.d(TAG, "save thumbpath==" + thumbpath);
		//outState.putInt("iPhotoCount", photoList.size());
		TextView pidval = (TextView) findViewById(R.id.pidval);
		outState.putString("PIID", pidval.getText().toString());
	}

	public static byte[] getBytes(Bitmap bitmap)
	{
		// ʵ�����ֽ����������
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bitmap.compress(Bitmap.CompressFormat.PNG, 0, baos);// ѹ��λͼ
		return baos.toByteArray();// ���������ֽ�����
	}

	public static Bitmap getBitmap(byte[] data)
	{
		if (data == null)
			return null;
		return BitmapFactory.decodeByteArray(data, 0, data.length);// ���ֽ��������λͼ
	}

	private void clear()
	{
		for (int i = 0; i < photoList.size(); i++)
		{

			
			
			
			
			String timeStamp = new SimpleDateFormat("yyyy_MM_dd")
			.format(new Date());
			
			File storageDir = new File(Environment.getExternalStorageDirectory()
					.getPath() + "/redant/pop/clear/"+timeStamp);
			
			if (!storageDir.exists())
				storageDir.mkdirs();

//			File dir1 = new File(Environment.getExternalStorageDirectory()
//					.getPath() + "/redant");
//			File dir2 = new File(Environment.getExternalStorageDirectory()
//					.getPath() + "/redant/pop");
//			File dir3 = new File(Environment.getExternalStorageDirectory()
//					.getPath() + "/redant/pop/clear");
//
//			
//			if (!dir1.exists())
//			{
//				boolean b = dir1.mkdir();
//			}
//			if (!dir2.exists())
//			{
//				boolean b = dir2.mkdir();
//			}
//			if (!dir3.exists())
//			{
//				boolean b = dir3.mkdir();
//			}
//			
//			if (!storageDir.exists())
//			{
//				boolean b = storageDir.mkdir();
//			}
			
			File srcFile = new File(photoList.get(i));
		//	file.delete();
			dbgUtil.fileLog("move file "+srcFile.getName()+" to "+Environment.getExternalStorageDirectory()
					.getPath() + "/redant/pop/clear/"+timeStamp + File.separator);
			srcFile.renameTo(new File(Environment.getExternalStorageDirectory()
					.getPath() + "/redant/pop/clear/"+timeStamp + File.separator + srcFile.getName()));  
			
			

//			// Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
//			// Log.d(TAG, "DIR:" + storageDir);
//			LastFileName = storageDir + imageFileName + ".jpg";
//			Log.d(TAG, "File:" + LastFileName);
//			File image = File.createTempFile(imageFileName, /* prefix */
//					".jpg", /* suffix */
//					storageDir /* directory */
//			);
			
			
		}
		photoList.clear();
		TextView pidval = (TextView) findViewById(R.id.pidval);
		pidval.setText("");
		updateUploadButton();
		ImageView thumbsImageView = (ImageView) findViewById(R.id.ivThumbs);
		thumbsImageView.setImageResource(R.drawable.no_pic);
		thumbBitmap = null;
		TextView countTextView = (TextView) findViewById(R.id.tvPhotoCount);
		countTextView.setText("no photo");

	}

	private void clearfornew()
	{

		photoList.clear();
		TextView pidval = (TextView) findViewById(R.id.pidval);
		pidval.setText("");
		updateUploadButton();
		ImageView thumbsImageView = (ImageView) findViewById(R.id.ivThumbs);
		thumbsImageView.setImageResource(R.drawable.no_pic);
		thumbBitmap = null;
		TextView countTextView = (TextView) findViewById(R.id.tvPhotoCount);
		countTextView.setText("no photo");

	}

	private final BroadcastReceiver	MainActivityReceiver	= new BroadcastReceiver()
															{

																public void onReceive(
																		Context context,
																		Intent intent)
																{
																	String action = intent
																			.getAction();
																	// 如果捕捉到的action是ACTION_BATTERY_CHANGED

																	if ("REDANT.POP.USER_CHECK_FAILED"
																			.equals(action))
																	{
																		m_tvalert
																				.setTextColor(getResources()
																						.getColor(
																								R.color.message_fail));
																		m_tvalert
																				.setText("Upload failed:user verify failure.");

																	}
																	else if ("REDANT.POP.STATION_NOT_CHANGE"
																			.equals(action))
																	{
																		new AlertDialog.Builder(
																				MainActivity.this)
																				.setIconAttribute(
																						android.R.attr.alertDialogIcon)
																				.setTitle(
																						R.string.confirm_to_upload)
																				.setMessage(
																						"Upload to station "
																								+ RedAntApplication.station_name)
																				.setPositiveButton(
																						android.R.string.ok,
																						new DialogInterface.OnClickListener()
																						{
																							public void onClick(
																									DialogInterface dialog,
																									int whichButton)
																							{

																								SQLiteDatabase db = dbUtil
																										.OpenDB(MainActivity.this,
																												null,
																												true);
																								TextView pidval = (TextView) findViewById(R.id.pidval);
																								String pid = pidval
																										.getText()
																										.toString();
																								for (int i = 0; i < photoList
																										.size(); i++)
																								{

																									ContentValues cv = new ContentValues();
																									cv.put("picker",
																											RedAntApplication.user);
																									cv.put("server",
																											RedAntApplication.active_address);
																									cv.put("err_code",
																											Network.STATUS_WAIT);
																									cv.put("pid",
																											pid);
																									cv.put("local_path",
																											photoList
																													.get(i));
																									db.insert(
																											"pics",
																											null,
																											cv);

																								}
																								dbUtil.CloseDB(db);
																								// new
																								// UploadTask().execute((Void)
																								// null);
																								clearfornew();
																								sendBroadcast(new Intent(
																										"REDANT.POP.RESET_LOCATION"));
																								Intent intent = new Intent(
																										"REDANT.POP.MODIFY_QUEUE");
																								sendBroadcast(intent);


																							}
																						})
																				.setNegativeButton(
																						android.R.string.cancel,
																						new DialogInterface.OnClickListener()
																						{
																							public void onClick(
																									DialogInterface dialog,
																									int whichButton)
																							{
																								// debug
																								// update_location();
																							}
																						})
																				.create()
																				.show();

																	}
																	else if ("REDANT.POP.STATION_CHANGE"
																			.equals(action))
																	{
																		
																		sendBroadcast(new Intent(
																				"REDANT.POP.RESET_LOCATION"));
																		new AlertDialog.Builder(
																				MainActivity.this)
																				.setIconAttribute(
																						android.R.attr.alertDialogIcon)
																				.setTitle(
																						R.string.location_changed)
																				.setMessage(
																						"We have detected that your position has changed,do you want to modify the server setting? ")
																				.setPositiveButton(
																						android.R.string.ok,
																						new DialogInterface.OnClickListener()
																						{
																							public void onClick(
																									DialogInterface dialog,
																									int whichButton)
																							{

																								Intent intent = new Intent();
																								intent.setClass(
																										MainActivity.this,
																										ServerSettingActivity.class);
																								startActivity(intent);
																							}
																						})
																				.setNegativeButton(
																						android.R.string.cancel,
																						new DialogInterface.OnClickListener()
																						{
																							public void onClick(
																									DialogInterface dialog,
																									int whichButton)
																							{

																								// debug
																								// update_location();
																							}
																						})
																				.create()
																				.show();

																	}
																	else if ("REDANT.POP.FINISH_UPLOAD_QUEUE"
																			.equals(action))
																	{
																		m_tvalert
																				.setTextColor(getResources()
																						.getColor(
																								R.color.message_success));
																		m_tvalert
																				.setText("Upload successes!");

																	}
																	else if ("REDANT.POP.FINISH_UPLOAD_QUEUE_WITH_ERROR"
																			.equals(action))
																	{
																		m_tvalert
																				.setTextColor(getResources()
																						.getColor(
																								R.color.message_fail));
																		m_tvalert
																				.setText("Upload finish with some error!");

																	}
																	else if ("REDANT.POP.CLEAR_UPLOAD_QUEUE"
																			.equals(action))
																	{
																		m_tvalert
																				.setTextColor(getResources()
																						.getColor(
																								R.color.message_success));
																		m_tvalert
																				.setText("Error Cleared.");

																	}
																	else if (action
																			.equals(Intent.ACTION_SCREEN_OFF))
																	{
																		Log.d("hg",
																				"�յ��㲥!!!=======>m_blocked="
																						+ m_blocked);
																		if (m_blocked == true)
																			return;

																		SharedPreferences pref = RedAntApplication
																				.getInstance()
																				.getSharedPreferences(
																						"POP",
																						0);
																		boolean autologin = pref
																				.getBoolean(
																						"kepppass",
																						false);
																		if (autologin)
																			return;

																		Intent activityintent = new Intent();
																		activityintent
																				.setClass(
																						MainActivity.this,
																						LockerActivity.class);
																		startActivityForResult(
																				activityintent,
																				REQUEST_LOCKER);
																		Log.d(TAG,
																				"===================>start locker");
																		m_blocked = true;
																	}
																}
															};

}
