Ray Zhang %!s(int64=11) %!d(string=hai) anos
pai
achega
03f7633b66

+ 7 - 2
RedAnt Photo/src/com/usai/redant/photo/LoginActivity.java

@@ -1,8 +1,11 @@
 package com.usai.redant.photo;
 
+import java.io.File;
+
 import com.usai.util.Crypto;
 import com.usai.util.Network;
 import com.usai.util.dbUtil;
+import com.usai.util.dbgUtil;
 //import com.example.com.apex.usa.test1.R.id;
 //import com.example.com.apex.usa.test1.R.layout;
 //import com.example.com.apex.usa.test1.R.menu;
@@ -19,6 +22,7 @@ import android.database.sqlite.SQLiteDatabase;
 import android.os.AsyncTask;
 import android.os.Build;
 import android.os.Bundle;
+import android.os.Environment;
 import android.text.TextUtils;
 import android.util.Log;
 import android.view.Gravity;
@@ -164,7 +168,7 @@ public class LoginActivity extends Activity
 				android.R.layout.simple_list_item_1, dbUtil.get_userlist());
 		m_etName.setAdapter(adapter);
 		
-		if(TextUtils.isEmpty(RedAntApplication.active_address))
+		if(TextUtils.isEmpty(RedAntApplication.station_name))
 		{
 			Intent intent = new Intent();
 			intent.setClass(LoginActivity.this,ServerSettingActivity.class);
@@ -413,6 +417,7 @@ public class LoginActivity extends Activity
 				Intent intent = new Intent();
 				intent.setClass(LoginActivity.this, MainActivity.class);
 
+				dbgUtil.fileLog("user "+m_sName+" login @server "+RedAntApplication.active_address);
 				startActivity(intent);
 				finish();
 			}
@@ -461,7 +466,7 @@ public class LoginActivity extends Activity
 								Intent intent = new Intent();
 								intent.setClass(LoginActivity.this,
 										MainActivity.class);
-
+								dbgUtil.fileLog("user "+m_sName+" offline access @server "+RedAntApplication.active_address);
 								startActivity(intent);
 								finish();
 							}

+ 253 - 35
RedAnt Photo/src/com/usai/redant/photo/MainActivity.java

@@ -11,9 +11,13 @@ 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;
@@ -71,6 +75,13 @@ public class MainActivity extends Activity
 	// 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);
@@ -108,6 +119,7 @@ public class MainActivity extends Activity
 	{
 		if (keyCode == KeyEvent.KEYCODE_BACK)
 		{
+			dbgUtil.fileLog("back button click , clear&exit");
 			clear();
 
 		}
@@ -115,13 +127,30 @@ public class MainActivity extends Activity
 
 	}
 
+	@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()
 		{
@@ -147,7 +176,8 @@ public class MainActivity extends Activity
 		IntentFilter msgFilter = new IntentFilter();
 		Intent intent = getIntent();
 		msgFilter.addAction("android.intent.action.SCREEN_OFF");
-		// msgFilter.addAction("REDANT.POP.USER_CHECK_FAILED");
+		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.setPriority(2147483647);
@@ -161,6 +191,9 @@ public class MainActivity extends Activity
 			// 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");
@@ -209,27 +242,10 @@ public class MainActivity extends Activity
 			@Override
 			public void onClick(View v)
 			{
-				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);
+				sendBroadcast(new Intent("REDANT.POP.REQUEST_LOCATION"));
+				return;
 
-				}
-				dbUtil.CloseDB(db);
-				// new UploadTask().execute((Void) null);
-				clearfornew();
-				Intent intent = new Intent("REDANT.POP.MODIFY_QUEUE");
-				sendBroadcast(intent);
 			}
 		});
 
@@ -238,13 +254,13 @@ public class MainActivity extends Activity
 
 		// Intent intent = ;
 		sendBroadcast(new Intent("REDANT.POP.MODIFY_QUEUE"));
-
+		updateUploadButton();
 	}
 
 	@Override
 	protected void onDestroy()
 	{
-		// TODO Auto-generated method stub
+	//	sendBroadcast(new Intent("REDANT.POP.GPS_OFF"));
 		Log.d(TAG, "=======================>MainActivity Destroyed!");
 		unregisterReceiver(MainActivityReceiver);
 		Log.d(TAG, "=======================>msgFilter unregisted!");
@@ -275,6 +291,7 @@ public class MainActivity extends Activity
 		File dir3 = new File(Environment.getExternalStorageDirectory()
 				.getPath() + "/redant/pop/temp");
 
+		
 		if (!dir1.exists())
 		{
 			boolean b = dir1.mkdir();
@@ -422,7 +439,7 @@ public class MainActivity extends Activity
 								+ bundle.getString("pid"));
 				TextView pidval = (TextView) findViewById(R.id.pidval);
 				pidval.setText(pid);
-				 updateUploadButton();
+				updateUploadButton();
 			}
 		}
 		super.onActivityResult(requestCode, resultCode, data);
@@ -468,6 +485,13 @@ public class MainActivity extends Activity
 		}
 	}
 
+	// boolean confirm_upload()
+	// {
+	// boolean bret = false;
+	// final Boolean b;
+	//
+	// return bret;
+	// }
 	@Override
 	public boolean onOptionsItemSelected(MenuItem item)
 	{
@@ -475,6 +499,7 @@ public class MainActivity extends Activity
 		switch (item.getItemId())
 		{
 			case R.id.action_clear:
+				dbgUtil.fileLog("menu item clear click , clear...");
 				clear();
 				break;
 			case R.id.action_type_pid:
@@ -514,11 +539,11 @@ public class MainActivity extends Activity
 			case R.id.action_server_settings:
 			{
 				Intent intent = new Intent();
-				intent.setClass(MainActivity.this,ServerSettingActivity.class);
+				intent.setClass(MainActivity.this, ServerSettingActivity.class);
 				startActivity(intent);
-				
+
 			}
-			break;
+				break;
 			case R.id.action_view_queue:
 			{
 				Intent intent = new Intent();
@@ -550,7 +575,9 @@ public class MainActivity extends Activity
 
 		outState.putStringArrayList("photoList", photoList);
 		// Log.d(TAG, "save thumbpath==" + thumbpath);
-		outState.putInt("iPhotoCount", photoList.size());
+		//outState.putInt("iPhotoCount", photoList.size());
+		TextView pidval = (TextView) findViewById(R.id.pidval);
+		outState.putString("PIID", pidval.getText().toString());
 	}
 
 	public static byte[] getBytes(Bitmap bitmap)
@@ -572,8 +599,65 @@ public class MainActivity extends Activity
 	{
 		for (int i = 0; i < photoList.size(); i++)
 		{
-			File file = new File(photoList.get(i));
-			file.delete();
+
+			
+			
+			
+			
+			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);
@@ -623,6 +707,134 @@ public class MainActivity extends Activity
 																		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))
@@ -654,13 +866,19 @@ public class MainActivity extends Activity
 																						+ m_blocked);
 																		if (m_blocked == true)
 																			return;
-																		
-																		SharedPreferences pref = RedAntApplication.getInstance()
-																				.getSharedPreferences("POP", 0);
-																		boolean autologin = pref.getBoolean("kepppass", false);
-																		if(autologin)
+
+																		SharedPreferences pref = RedAntApplication
+																				.getInstance()
+																				.getSharedPreferences(
+																						"POP",
+																						0);
+																		boolean autologin = pref
+																				.getBoolean(
+																						"kepppass",
+																						false);
+																		if (autologin)
 																			return;
-																		
+
 																		Intent activityintent = new Intent();
 																		activityintent
 																				.setClass(

+ 47 - 1
RedAnt Photo/src/com/usai/redant/photo/RedAntApplication.java

@@ -4,23 +4,66 @@ import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReadWriteLock;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 
+import android.app.AlarmManager;
 import android.app.Application;
+import android.app.PendingIntent;
+import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
+import android.os.SystemClock;
 import android.text.TextUtils;
+import android.util.Log;
 
 public class RedAntApplication extends Application
 {
     private static RedAntApplication instance;  
     public static ReadWriteLock lock = new ReentrantReadWriteLock(true);  
+    public static ReadWriteLock loglock = new ReentrantReadWriteLock(true);  
+    public static Lock logwritelock   = loglock.writeLock();   
     public static Lock writeLock   = lock.writeLock();   
     public static String user;
     public static String password;
     public static String active_address;
+    public static String station_name;
     public static RedAntApplication getInstance() {  
         return instance;  
     }  
-  
+    
+	public static void startalarm()
+	{
+
+		Intent iAlarm = new Intent("REDANT.POP.RETRY_UPLOAD");
+		//iAlarm.putExtra("caller", caller);
+//		iAlarm.setAction("com.usai.apex.push");
+		PendingIntent sender = PendingIntent.getBroadcast(getInstance(), 0,
+				iAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
+
+		long firstime = SystemClock.elapsedRealtime();
+//		UpdateLastAlermTime();
+		AlarmManager am = (AlarmManager) getInstance().getSystemService(
+				Context.ALARM_SERVICE);
+
+		// 5分钟一个周期,不停的发送广播
+		am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstime,
+				300 * 1000, sender);
+		Log.d("redant pop","start alarm");
+	}
+
+	public static void cancelalarm()
+	{
+
+		// 启动完成
+		Intent iAlarm = new Intent("REDANT.POP.RETRY_UPLOAD");
+//		iAlarm.setAction("com.usai.apex.push");
+		PendingIntent sender = PendingIntent.getBroadcast(getInstance(), 0,
+				iAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
+
+		AlarmManager am = (AlarmManager) getInstance().getSystemService(
+				Context.ALARM_SERVICE);
+
+		am.cancel(sender);
+		Log.d("redant pop","cancel alarm");
+	}
     @Override  
     public void onCreate() {  
         // TODO Auto-generated method stub  
@@ -31,8 +74,11 @@ public class RedAntApplication extends Application
 				.getSharedPreferences("POP", 0);
 
 		String aa = pref.getString("aa", null);
+		String name = pref.getString("station name", null);
 		if(!TextUtils.isEmpty(aa))
 			active_address = aa;
+		if(!TextUtils.isEmpty(name))
+			station_name = name;
         
         
 		Intent intentservice = new Intent();

+ 42 - 9
RedAnt Photo/src/com/usai/redant/photo/ServerSettingActivity.java

@@ -18,6 +18,7 @@ import android.media.ThumbnailUtils;
 import android.os.Bundle;
 import android.text.TextUtils;
 import android.util.Log;
+import android.view.Gravity;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
@@ -27,6 +28,7 @@ import android.widget.ImageButton;
 import android.widget.ImageView;
 import android.widget.RadioGroup;
 import android.widget.TextView;
+import android.widget.Toast;
 
 public class ServerSettingActivity extends Activity
 {
@@ -44,6 +46,7 @@ public class ServerSettingActivity extends Activity
 		String ia = pref.getString("ia", null);
 		String ea = pref.getString("ea", null);
 		String aa = pref.getString("aa", null);
+		String station_name = pref.getString("station name", null);
 		RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
 
 		if (!TextUtils.isEmpty(aa) && aa.equals(ia))
@@ -54,8 +57,10 @@ public class ServerSettingActivity extends Activity
 
 		EditText et_ia = (EditText) findViewById(R.id.et_ia);
 		EditText et_ea = (EditText) findViewById(R.id.et_ea);
+		EditText et_name = (EditText) findViewById(R.id.et_name);
 		et_ea.setText(ea);
 		et_ia.setText(ia);
+		et_name.setText(station_name);
 
 		ImageButton ibtn = (ImageButton) findViewById(R.id.ibtn_scan);
 		ibtn.setOnClickListener(new OnClickListener()
@@ -97,15 +102,21 @@ public class ServerSettingActivity extends Activity
 				if (jstr == null || jstr.length() <= 0)
 				{
 					//Log.d(TAG, "json is wrong");
+					toast();
 					return ;//Network.RESULT_NET_ERROR;
 				}	
 				String address[]=jstr.split(";");
-				if (address.length<2)
+				if (address.length<3)
+				{
+					toast();
 					return;
+				}
 				EditText et_ia = (EditText) findViewById(R.id.et_ia);
 				EditText et_ea = (EditText) findViewById(R.id.et_ea);
-				et_ea.setText(address[1].trim());
-				et_ia.setText(address[0].trim());
+				EditText et_name = (EditText) findViewById(R.id.et_name);
+				et_ea.setText(address[2].trim());
+				et_ia.setText(address[1].trim());
+				et_name.setText(address[0].trim());
 //				JSONObject jsobj;
 //				try
 //				{
@@ -161,17 +172,28 @@ public class ServerSettingActivity extends Activity
 		{
 			EditText et_ia = (EditText) findViewById(R.id.et_ia);
 			EditText et_ea = (EditText) findViewById(R.id.et_ea);
+			EditText et_name = (EditText) findViewById(R.id.et_name);
 			String ia = et_ia.getText().toString();
 			String ea = et_ea.getText().toString();
+			String name = et_name.getText().toString();
 			if (TextUtils.isEmpty(ia) && TextUtils.isEmpty(ea))
 			{
-				showdialog();
+				showdialog("Active server address can not be empty!");
 				return true;
 			}
+
 			SharedPreferences pref = RedAntApplication.getInstance()
 					.getSharedPreferences("POP", 0);
 			SharedPreferences.Editor editor = pref.edit();
-
+			if (TextUtils.isEmpty(name))
+			{
+				showdialog("Station name can not be empty!");
+				return true;
+			}
+			else {
+				
+				editor.putString("station name", name);
+			}
 			if (!TextUtils.isEmpty(ia))
 			{
 
@@ -195,7 +217,7 @@ public class ServerSettingActivity extends Activity
 				}
 				else
 				{
-					showdialog();
+					showdialog("Active server address can not be empty!");
 					return true;
 				}
 			}
@@ -208,23 +230,34 @@ public class ServerSettingActivity extends Activity
 				}
 				else
 				{
-					showdialog();
+					showdialog("Active server address can not be empty!");
 					return true;
 				}
 			}
+			RedAntApplication.station_name = name;
 
+			editor.remove("Lat");
+			editor.remove("Lon");
 			editor.commit();
+			//sendBroadcast(new Intent("REDANT.POP.RESET_LOCATION"));
 			finish();
 			return true;
 		}
 		return super.onOptionsItemSelected(item);
 	}
 
-	private void showdialog()
+	private void toast()
+	{
+		Toast toast = Toast.makeText(this,"Wrong server setting format",
+				Toast.LENGTH_LONG);
+		toast.setGravity(Gravity.CENTER, 0, 0);
+		toast.show();
+	}
+	private void showdialog(String msg)
 	{
 		new AlertDialog.Builder(ServerSettingActivity.this)
 				.setIcon(android.R.drawable.ic_dialog_info).setTitle("Error!")
-				.setMessage("Active server address can not be empty!")
+				.setMessage(msg)
 				.setPositiveButton("OK", new DialogInterface.OnClickListener()
 				{
 					public void onClick(DialogInterface dialog, int whichButton)

+ 294 - 22
RedAnt Photo/src/com/usai/redant/photo/UploadService.java

@@ -1,6 +1,8 @@
 package com.usai.redant.photo;
 
 import java.io.File;
+import java.text.SimpleDateFormat;
+import java.util.Date;
 
 import com.usai.util.Crypto;
 import com.usai.util.Network;
@@ -16,9 +18,13 @@ import android.content.IntentFilter;
 import android.content.SharedPreferences;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
+import android.location.Location;
+import android.location.LocationListener;
+import android.location.LocationManager;
 import android.net.ConnectivityManager;
 import android.net.NetworkInfo;
 import android.os.Bundle;
+import android.os.Environment;
 import android.os.IBinder;
 import android.text.TextUtils;
 import android.util.Log;
@@ -31,6 +37,10 @@ public class UploadService extends Service
 	private UploadThread	uploadThread;
 
 	private boolean			queue_changed	= false;
+	LocationManager			locationManager;
+	LocationListener		locationListener;
+
+	// private boolean location_changed = false;
 
 	@Override
 	public IBinder onBind(Intent intent)
@@ -55,21 +65,194 @@ public class UploadService extends Service
 		IntentFilter msgFilter = new IntentFilter();
 
 		msgFilter.addAction("REDANT.POP.MODIFY_QUEUE");
+		msgFilter.addAction("REDANT.POP.GPS_ON");
+		msgFilter.addAction("REDANT.POP.GPS_OFF");
+		msgFilter.addAction("REDANT.POP.REQUEST_LOCATION");
+		msgFilter.addAction("REDANT.POP.RESET_LOCATION");
 		msgFilter.addAction("REDANT.POP.QUERY_UPLOAD_STATE");
 		msgFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
-
+		msgFilter.addAction("REDANT.POP.RETRY_UPLOAD");
+		
 		registerReceiver(uploadReceiver, msgFilter);
 		// registerReceiver(uploadReceiver,
 		// new IntentFilter("modify upload queue"));
+
+		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
+
+		locationListener = new LocationListener()
+		{
+
+			// Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
+			@Override
+			public void onStatusChanged(String provider, int status,
+					Bundle extras)
+			{
+
+			}
+
+			// Provider被enable时触发此函数,比如GPS被打开
+			@Override
+			public void onProviderEnabled(String provider)
+			{
+
+			}
+
+			// Provider被disable时触发此函数,比如GPS被关闭
+			@Override
+			public void onProviderDisabled(String provider)
+			{
+
+			}
+
+			// 当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
+			@Override
+			public void onLocationChanged(Location location)
+			{
+				// if (location != null)
+				// {
+				// location_changed = true;
+				// Log.d("Map",
+				// "Location changed : Lat: " + location.getLatitude()
+				// + " Lng: " + location.getLongitude());
+				// }
+			}
+		};
+
 		super.onCreate();
 
 	}
 
-	void loadlist()
+	void gps_off()
+	{
+		locationManager.removeUpdates(locationListener);
+	}
+
+	void gps_on()
+	{
+		if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
+		{
+
+			locationManager.requestLocationUpdates(
+					LocationManager.GPS_PROVIDER, 60 * 1000, 50,
+					locationListener);
+
+		}
+		else if (locationManager
+				.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
+		{
+
+			locationManager.requestLocationUpdates(
+					LocationManager.NETWORK_PROVIDER, 30 * 1000, 50,
+					locationListener);
+
+		}
+	}
+
+	void update_location()
 	{
 
+		Location location = null;
+		if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
+		{
+
+			location = locationManager
+					.getLastKnownLocation(LocationManager.GPS_PROVIDER);
+
+		}
+		else if (locationManager
+				.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
+		{
+
+			location = locationManager
+					.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
+
+		}
+
+		SharedPreferences pref = RedAntApplication.getInstance()
+				.getSharedPreferences("POP", 0);
+
+		SharedPreferences.Editor editor = pref.edit();
+		if (location != null)
+		{
+
+			editor.putFloat("Lat", (float) location.getLatitude());
+			editor.putFloat("Lon", (float) location.getLongitude());
+
+		}
+		editor.commit();
+
 	}
 
+	void check_location()
+	{
+
+		Location location = null;
+		if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
+		{
+
+			location = locationManager
+					.getLastKnownLocation(LocationManager.GPS_PROVIDER);
+
+		}
+		else if (locationManager
+				.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
+		{
+
+			location = locationManager
+					.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
+
+		}
+
+		SharedPreferences pref = RedAntApplication.getInstance()
+				.getSharedPreferences("POP", 0);
+		double lat = pref.getFloat("Lat", 9999);
+		double lon = pref.getFloat("Lon", 9999);
+
+		// SharedPreferences.Editor editor = pref.edit();
+		if (location != null)
+		{
+			float[] result = new float[1];
+			if (lat != 9999 && lon != 9999)
+			{
+				Location.distanceBetween(lat, lon, location.getLatitude(),
+						location.getLongitude(), result);
+				if (result[0] > 1000)
+				{
+					sendBroadcast(new Intent("REDANT.POP.STATION_CHANGE"));
+					return;
+				}
+				else
+				{
+					sendBroadcast(new Intent("REDANT.POP.STATION_NOT_CHANGE"));
+					return;
+				}
+			}
+			sendBroadcast(new Intent("REDANT.POP.STATION_NOT_CHANGE"));
+			return;
+			// new Location();
+			//
+			// Location.distanceBetween(lat, lon, endLatitude, endLongitude,
+			// results)
+			//
+			// editor.putFloat("Lat", (float) location.getLatitude());
+			// editor.putFloat("Lon", (float) location.getLongitude());
+
+		}
+		else
+		{
+			sendBroadcast(new Intent("REDANT.POP.STATION_NOT_CHANGE"));
+			return;
+		}
+		// editor.commit();
+
+		// Log.d("location:", "lat:" + latitude + "lon:" + longitude);
+	}
+
+	// void loadlist()
+	// {
+	//
+	// }
+
 	public class UploadThread extends Thread
 	{
 		@Override
@@ -94,8 +277,8 @@ public class UploadService extends Service
 			// {
 			SQLiteDatabase dbr = dbUtil.OpenDB(UploadService.this, null, false);
 			Cursor cursor = dbr.query("pics", new String[] { "_id", "pid",
-					"local_path", "picker","server" }, "err_code!="
-					+ Network.AP_UPLOAD_SUCCESS, null, null, null, "_id", null);
+					"local_path", "picker", "server" }, "err_code!="
+					+ Network.AP_UPLOAD_SUCCESS+" and err_code!="+ Network.RESULT_LOCALFILE_ERROR, null, null, null, "_id", null);
 			while (cursor.moveToNext())
 			{
 				didupload = true;
@@ -104,8 +287,8 @@ public class UploadService extends Service
 					queue_changed = false;
 					dbUtil.CloseCursor(cursor);
 					cursor = dbr.query("pics", new String[] { "_id", "pid",
-							"local_path", "picker","server" }, "err_code!="
-							+ Network.AP_UPLOAD_SUCCESS, null, null, null,
+							"local_path", "picker", "server" }, "err_code!="
+							+ Network.AP_UPLOAD_SUCCESS+" and err_code!="+ Network.RESULT_LOCALFILE_ERROR, null, null, null,
 							"_id", null);
 					if (!cursor.moveToNext())
 						break;
@@ -146,25 +329,51 @@ public class UploadService extends Service
 					e.printStackTrace();
 				}
 
-				int uploadresult = Network.UploadImage(path, picker, pass, pid,server);
+				int uploadresult = Network.UploadImage(path, picker, pass, pid,
+						server);
 				if (uploadresult == Network.RESULT_TRUE)
 				{
+				
 					RedAntApplication.writeLock.lock();
 
-					{
-						// write sql process
-						SQLiteDatabase dbw = dbUtil.OpenDB(UploadService.this,
-								null, true);
-						// String sql = "update pics set err_code ="
-						// + Network.AP_UPLOAD_SUCCESS + " where _id="
-						// + _id;
-						String sql = "delete from pics  where _id=" + _id;
-						dbw.execSQL(sql);
-						File pic = new File(path);
-						pic.delete();
-						dbUtil.CloseDB(dbw);
-					}
+					// write sql process
+					SQLiteDatabase dbw = dbUtil.OpenDB(UploadService.this,
+							null, true);
+					// String sql = "update pics set err_code ="
+					// + Network.AP_UPLOAD_SUCCESS + " where _id="
+					// + _id;
+					String sql = "delete from pics  where _id=" + _id;
+					dbw.execSQL(sql);
+
+					String timeStamp = new SimpleDateFormat("yyyy_MM_dd")
+							.format(new Date());
+
+					File storageDir = new File(Environment
+							.getExternalStorageDirectory().getPath()
+							+ "/redant/pop/done/" + timeStamp);
+
+					if (!storageDir.exists())
+						storageDir.mkdirs();
+
+					File pic = new File(path);
+					// pic.delete();
+
+					dbUtil.CloseDB(dbw);
 					RedAntApplication.writeLock.unlock();
+					dbgUtil.fileLog("upload success move file "
+							+ pic.getName()
+							+ " to "
+							+ Environment.getExternalStorageDirectory()
+									.getPath() + "/redant/pop/done/"
+							+ timeStamp + File.separator);
+					pic.renameTo(new File(Environment
+							.getExternalStorageDirectory().getPath()
+							+ "/redant/pop/done/"
+							+ timeStamp
+							+ File.separator
+							+ pic.getName()));
+					
+
 				}
 				else
 				{
@@ -182,6 +391,10 @@ public class UploadService extends Service
 						dbUtil.CloseDB(dbw);
 					}
 					RedAntApplication.writeLock.unlock();
+					dbgUtil.fileLog("upload failed pid:" + pid + " file:"
+							+ path + " server:" + server + " err_code:"
+							+ uploadresult);
+					
 				}
 
 				sendBroadcast(new Intent("REDANT.POP.UPDATE_QUEUE_VIEW"));
@@ -206,10 +419,16 @@ public class UploadService extends Service
 			if (didupload)
 			{
 				if (error)
+				{
 					sendBroadcast(new Intent(
 							"REDANT.POP.FINISH_UPLOAD_QUEUE_WITH_ERROR"));
+					RedAntApplication.startalarm();
+				}
 				else
+				{
 					sendBroadcast(new Intent("REDANT.POP.FINISH_UPLOAD_QUEUE"));
+					RedAntApplication.cancelalarm();
+				}
 			}
 		}
 	}
@@ -225,11 +444,63 @@ public class UploadService extends Service
 																.getAction();
 														// 如果捕捉到的action是ACTION_BATTERY_CHANGED
 
-														if ("REDANT.POP.MODIFY_QUEUE"
+														if ("REDANT.POP.REQUEST_LOCATION"
+																.equals(action))
+														{
+															check_location();
+															// if
+															// (location_changed)
+															// sendBroadcast(new
+															// Intent(
+															// "REDANT.POP.CHANGE_STATION"));
+
+														}
+														else if ("REDANT.POP.RETRY_UPLOAD"
+																.equals(action))
+														{
+															if (uploadThread != null)
+															{
+																if (uploadThread
+																		.isAlive())
+																{
+
+																	Log.d("uploadReceiver",
+																			"upload thread alive");
+																}
+																else
+																{
+																	Log.d("uploadReceiver",
+																			"start upload thread");
+																	uploadThread = new UploadThread();
+																	uploadThread
+																			.start();
+																}
+															}
+														}
+														else if ("REDANT.POP.GPS_ON"
+																.equals(action))
+														{
+															gps_on();
+														}
+														else if ("REDANT.POP.GPS_OFF"
+																.equals(action))
+														{
+
+															gps_off();
+														}
+														else if ("REDANT.POP.RESET_LOCATION"
+																.equals(action))
+														{
+															update_location();
+															// location_changed
+															// = false;
+														}
+														else if ("REDANT.POP.MODIFY_QUEUE"
 																.equals(action))
 														{
 															Log.d("uploadservice",
 																	"recieve upload queue change");
+															// update_location();
 															if (uploadThread != null)
 															{
 																if (uploadThread
@@ -314,7 +585,8 @@ public class UploadService extends Service
 																		.isEmpty(aa))
 																	RedAntApplication.active_address = aa;
 															}
-															else {
+															else
+															{
 																if (!TextUtils
 																		.isEmpty(ea))
 																	RedAntApplication.active_address = ea;