ProgressSubscriber.java
3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.lijinji.call.https.Subscribers;
import android.content.Context;
import android.widget.Toast;
import com.lijinji.call.LoginActivity;
import com.lijinji.call.R;
import com.lijinji.call.https.ProgressCancelListener;
import com.lijinji.call.https.ProgressDialogHandler;
import com.lijinji.call.util.SoundUtils;
import com.lijinji.call.util.WMSLog;
import com.lijinji.call.util.WMSUtils;
import org.json.JSONObject;
import java.io.IOException;
import retrofit2.adapter.rxjava.HttpException;
import rx.Subscriber;
public class ProgressSubscriber<T> extends Subscriber<T> implements ProgressCancelListener {
private SubscriberOnNextListener mListener;
private ProgressDialogHandler progressHandler;
private Context context;
public ProgressSubscriber(Context context, SubscriberOnNextListener mListener) {
this.context = context;
this.mListener = mListener;
progressHandler = new ProgressDialogHandler(context, this, false);
}
private void showProgressDialog() {
if (progressHandler != null) {
progressHandler.obtainMessage(ProgressDialogHandler.SHOW_PROGRESS_DIALOG).sendToTarget();
}
}
private void dismissProgressDialog() {
if (progressHandler != null) {
progressHandler.obtainMessage(ProgressDialogHandler.DISMISS_PROGRESS_DIALOG).sendToTarget();
progressHandler = null;
}
}
@Override
public void onStart() {
WMSLog.d("onStart");
showProgressDialog();
}
@Override
public void onCompleted() {
WMSLog.d("onCompleted");
dismissProgressDialog();
}
@Override
public void onError(Throwable e) {
dismissProgressDialog();
SoundUtils.getInstance(context).errorSound();
e.printStackTrace();
if(e.getMessage() != null) {
if (e.getMessage().contains(context.getString(R.string.login_again))) {
WMSUtils.startActivity(context, LoginActivity.class);
}
} else {
if(e.toString() != null && e.toString().contains("SocketTimeoutException")) {
Toast.makeText(context, context.getString(R.string.http_sockettime), Toast.LENGTH_SHORT).show();
}
}
if(e instanceof HttpException) {
HttpException httpException = (HttpException) e;
try {
String str = httpException.response().errorBody().string();
JSONObject jsonObject = new JSONObject(str);
String msg = jsonObject.getString("msg");
WMSLog.d("onError msg:" + msg);
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
mListener.onError(msg);
} catch (Exception e1) {
e1.printStackTrace();
}
} else {
WMSLog.d("onError:" + e.getMessage());
if(e.getMessage() != null && !e.getMessage().equals("null")) {
Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
mListener.onError(e.getMessage());
}
}
}
@Override
public void onNext(T t) {
WMSLog.d("onNext t:" + t);
mListener.onNext(t);
}
@Override
public void onCancelProgress() {
WMSLog.d("onCancelProgress");
if (!this.isUnsubscribed()) {
this.unsubscribe();
}
}
}