upload service

chengzhenyu 8 anos atrás
pai
commit
c65c120ea3

+ 20 - 0
app/src/main/java/ai/pai/lensman/bean/PhotoBean.java

@@ -24,4 +24,24 @@ public class PhotoBean implements Serializable{
24 24
     public boolean isUploaded;
25 25
 
26 26
     public boolean isRawPhoto;
27
+
28
+    @Override
29
+    public boolean equals(Object obj) {
30
+        if(obj == this){
31
+            return true;
32
+        }
33
+        if(!(obj instanceof PhotoBean)){
34
+            return false;
35
+        }
36
+        return (((PhotoBean) obj).photoId==this.photoId);
37
+    }
38
+
39
+    @Override
40
+    public String toString() {
41
+        return "PhotoBean{" +
42
+                "photoId=" + photoId +
43
+                ", photoName='" + photoName + '\'' +
44
+                ", sessionSeq=" + sessionSeq +
45
+                '}';
46
+    }
27 47
 }

+ 45 - 6
app/src/main/java/ai/pai/lensman/service/UploadService.java

@@ -1,20 +1,59 @@
1 1
 package ai.pai.lensman.service;
2 2
 
3
-import android.app.IntentService;
3
+import android.app.Service;
4 4
 import android.content.Intent;
5
+import android.os.IBinder;
5 6
 
6
-public class UploadService extends IntentService {
7
+import com.android.common.executors.ThreadExecutor;
8
+import com.android.common.utils.LogHelper;
7 9
 
8
-    private static final String TAG = "UploadService";
10
+import ai.pai.lensman.bean.PhotoBean;
11
+import ai.pai.lensman.db.DBService;
9 12
 
10
-    public UploadService(){
11
-        super(TAG);
13
+public class UploadService extends Service implements UploadTask.OnPhotoUploadListener{
14
+
15
+    private PhotoBean currentPhoto;
16
+
17
+    @Override
18
+    public IBinder onBind(Intent intent) {
19
+        return null;
12 20
     }
13 21
 
14 22
     @Override
15
-    protected void onHandleIntent(Intent intent) {
23
+    public int onStartCommand(Intent intent, int flags, int startId) {
24
+        startUpload();
25
+        return super.onStartCommand(intent, flags, startId);
26
+    }
16 27
 
28
+    private synchronized void startUpload(){
29
+        if(currentPhoto!=null){
30
+            LogHelper.d("czy","当前有图片正在上传"+currentPhoto);
31
+            return;
32
+        }
33
+         currentPhoto = DBService.getInstance().getNextUploadPhoto();
34
+        if(currentPhoto==null){
35
+            LogHelper.d("czy","本地图片已全部上传");
36
+            return;
37
+        }
38
+        LogHelper.d("czy","即将上传的照片是"+currentPhoto);
39
+        new UploadTask(currentPhoto,this).executeOnExecutor(ThreadExecutor.getInstance().getExecutor());
40
+    }
17 41
 
42
+    @Override
43
+    public void onPhotoUploadSucceed(PhotoBean bean) {
44
+        if(bean.equals(currentPhoto)){
45
+            currentPhoto.isUploaded = true;
46
+            DBService.getInstance().updatePhotoBean(currentPhoto);
47
+            currentPhoto = null;
48
+        }
49
+        startUpload();
18 50
     }
19 51
 
52
+    @Override
53
+    public void onPhotoUploadFail(PhotoBean bean) {
54
+        if(bean.equals(currentPhoto)){
55
+            currentPhoto = null;
56
+        }
57
+        startUpload();
58
+    }
20 59
 }

+ 36 - 37
app/src/main/java/ai/pai/lensman/service/UploadTask.java

@@ -1,79 +1,78 @@
1 1
 package ai.pai.lensman.service;
2 2
 
3
-import android.content.Context;
4 3
 import android.os.AsyncTask;
5 4
 
6 5
 import com.android.common.utils.LogHelper;
7
-import com.android.common.utils.NetworkUtil;
8 6
 
9 7
 import org.json.JSONObject;
10 8
 
11 9
 import java.io.File;
12 10
 
11
+import ai.pai.lensman.bean.PhotoBean;
13 12
 import ai.pai.lensman.db.Preferences;
14 13
 import ai.pai.lensman.utils.UrlContainer;
15 14
 
16
-public class UploadTask extends AsyncTask<String,Integer,Boolean> {
15
+public class UploadTask extends AsyncTask<Void, Integer, Boolean> {
17 16
 
18 17
     private static final String TAG = "czy";
19
-    private Context context;
20
-    private String photoPath;
18
+    private PhotoBean bean;
21 19
     private OnPhotoUploadListener listener;
22
-    private String groupId;
23 20
 
24
-    public UploadTask(Context context ,String groupId, OnPhotoUploadListener listener){
25
-        this.context = context;
21
+    public UploadTask(PhotoBean bean, OnPhotoUploadListener listener) {
22
+        this.bean = bean;
26 23
         this.listener = listener;
27
-        this.groupId = groupId;
28 24
     }
29 25
 
30 26
     @Override
31
-    protected Boolean doInBackground(String... params) {
32
-        if(!NetworkUtil.isNetworkConnected(context)){
33
-            return false;
34
-        }
35
-        photoPath = params[0];
36
-        try{
37
-            File photo = new File(photoPath);
38
-            LogHelper.d(TAG,"上传照片,路径为 = "+photoPath);
39
-            if(photo.exists() &&photo.isFile()){
40
-                PhotoUploadUtils photoUploadUtils = new PhotoUploadUtils(UrlContainer.PHOTO_UPLOAD_URL+"?timestamp="+System.currentTimeMillis());
27
+    protected Boolean doInBackground(Void... params) {
28
+        try {
29
+            String dirPath = ai.pai.lensman.utils.Constants.APP_IMAGE_DIR + File.separator + bean.sessionId
30
+                    + File.separator + ai.pai.lensman.utils.Constants.THUMBNAIL_DIR_NAME;
31
+            File dir = new File(dirPath);
32
+            dir.mkdirs();
33
+            File photo = new File(dir, bean.photoName);
34
+            LogHelper.d(TAG, "上传照片 " + bean);
35
+            if (photo.exists() && photo.isFile()) {
36
+                PhotoUploadUtils photoUploadUtils = new PhotoUploadUtils(UrlContainer.PHOTO_UPLOAD_URL + "?timestamp=" + System.currentTimeMillis());
41 37
                 photoUploadUtils.addFileParameter("photo", photo);
42
-                photoUploadUtils.addTextParameter("user_id", Preferences.getInstance().getLensManId());
43
-                photoUploadUtils.addTextParameter("group_id", groupId);
44
-                photoUploadUtils.addTextParameter("nickname", Preferences.getInstance().getUserName());
45
-                String result=new String(photoUploadUtils.send(),"UTF-8");
38
+                photoUploadUtils.addTextParameter("user_id", bean.lensmanId);
39
+                photoUploadUtils.addTextParameter("session_id", bean.sessionId);
40
+                photoUploadUtils.addTextParameter("photo_id", String.valueOf(bean.photoId));
41
+                photoUploadUtils.addTextParameter("nickname",Preferences.getInstance().getUserName());
42
+
43
+                String result = new String(photoUploadUtils.send(), "UTF-8");
46 44
                 JSONObject resultObj = new JSONObject(result);
47
-                if(resultObj.getInt("status")==200){
48
-                    LogHelper.d(TAG,"上传 UploadTask upload result ok ");
45
+                if (resultObj.getInt("status") == 200) {
46
+                    LogHelper.d(TAG, "上传 UploadTask upload result ok ");
49 47
                     JSONObject info = resultObj.getJSONObject("data");
50 48
                     return true;
51 49
                 }
52
-            }else{
50
+            } else {
53 51
                 return false;
54 52
             }
55 53
 
56
-        }catch (Exception e){
57
-            LogHelper.d(TAG, "上传发生异常 UploadTask  photo path:"+photoPath+ " error:" + e);
54
+        } catch (Exception e) {
55
+            LogHelper.d(TAG, "上传发生异常 UploadTask :" + bean + " error:" + e);
58 56
         }
59 57
         return false;
60 58
     }
61 59
 
62 60
     @Override
63 61
     protected void onPostExecute(Boolean result) {
64
-        if(listener!=null){
65
-            if(result){
66
-                LogHelper.d(TAG, "上传 UploadTask  photo path:"+photoPath+ "  success ");
67
-                listener.onPhotoUploadSucceed(photoPath);
68
-            }else{
69
-                LogHelper.d(TAG, "上传 UploadTask  photo path:"+photoPath+ "  fail ");
70
-                listener.onPhotoUploadFail(photoPath,groupId);
62
+        if (listener != null) {
63
+            if (result) {
64
+                LogHelper.d(TAG, "上传 UploadTask " + bean + "  success ");
65
+                listener.onPhotoUploadSucceed(bean);
66
+            } else {
67
+                LogHelper.d(TAG, "上传 UploadTask " + bean + "  fail ");
68
+                listener.onPhotoUploadFail(bean);
71 69
             }
72 70
         }
73 71
     }
74 72
 
75 73
     public interface OnPhotoUploadListener {
76
-        void onPhotoUploadSucceed(String photoPath);
77
-        void onPhotoUploadFail(String photoPath, String groupId);
74
+        void onPhotoUploadSucceed(PhotoBean bean);
75
+
76
+        void onPhotoUploadFail(PhotoBean bean);
78 77
     }
79 78
 }

+ 6 - 0
app/src/main/java/ai/pai/lensman/upload/UploadPresenter.java

@@ -1,12 +1,16 @@
1 1
 package ai.pai.lensman.upload;
2 2
 
3
+import android.content.Intent;
4
+
3 5
 import java.text.SimpleDateFormat;
4 6
 import java.util.ArrayList;
5 7
 import java.util.Date;
6 8
 
9
+import ai.pai.lensman.App;
7 10
 import ai.pai.lensman.base.BaseInteractor;
8 11
 import ai.pai.lensman.bean.SessionBean;
9 12
 import ai.pai.lensman.db.DBService;
13
+import ai.pai.lensman.service.UploadService;
10 14
 
11 15
 public class UploadPresenter implements UploadContract.Presenter,BaseInteractor.InteractorListener<ArrayList<String>> {
12 16
 
@@ -43,6 +47,8 @@ public class UploadPresenter implements UploadContract.Presenter,BaseInteractor.
43 47
             uploadView.refreshSessionViews(sessionList);
44 48
         }
45 49
         sessionSeq = sessionList.size();
50
+
51
+        App.getAppContext().startService(new Intent(App.getAppContext(), UploadService.class));
46 52
     }
47 53
 
48 54
     @Override