|
|
@@ -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;
|