ProgressSubscriber.java
3.4 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package com.huaheng.mmsrf.https.Subscribers;
import android.content.Context;
import android.widget.Toast;
import com.huaheng.mmsrf.LoginActivity;
import com.huaheng.mmsrf.MainActivity;
import com.huaheng.mmsrf.R;
import com.huaheng.mmsrf.https.Subscribers.SubscriberOnNextListener;
import com.huaheng.mmsrf.util.WMSLog;
import com.huaheng.mmsrf.https.ProgressCancelListener;
import com.huaheng.mmsrf.https.ProgressDialogHandler;
import com.huaheng.mmsrf.util.SoundUtils;
import com.huaheng.mmsrf.util.WMSUtils;
import rx.Subscriber;
public class ProgressSubscriber<T> extends Subscriber<T> implements ProgressCancelListener {
    private SubscriberOnNextListener mListener;
    private ProgressDialogHandler progressHandler;
    private Context context;
    private boolean showError = true;
    private boolean playSound = true;
    private boolean showDialog = true;
    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");
        if(showDialog) {
            showProgressDialog();
        }
    }
    @Override
    public void onCompleted() {
        WMSLog.d("onCompleted");
        dismissProgressDialog();
    }
    @Override
    public void onError(Throwable e) {
        dismissProgressDialog();
        e.printStackTrace();
        if(isPlaySound()) {
            SoundUtils.getInstance(context).errorSound();
        }
        if(e.getMessage() != null) {
            WMSLog.d("onError:" + e.getMessage());
            if (e.getMessage().contains(context.getString(R.string.login_again))) {
                WMSUtils.startActivity(context, LoginActivity.class);
            }
            if(showError) {
                Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        } else {
            if(e.toString() != null && e.toString().contains("SocketTimeoutException")) {
                Toast.makeText(context, context.getString(R.string.http_sockettime), 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();
        }
    }
    public boolean isShowError() {
        return showError;
    }
    public void setShowError(boolean showError) {
        this.showError = showError;
    }
    public boolean isPlaySound() {
        return playSound;
    }
    public void setPlaySound(boolean playSound) {
        this.playSound = playSound;
    }
    public boolean isShowDialog() {
        return showDialog;
    }
    public void setShowDialog(boolean showDialog) {
        this.showDialog = showDialog;
    }
}