-
Notifications
You must be signed in to change notification settings - Fork 1
/
BlockingSocketFragment.java
214 lines (185 loc) · 7.4 KB
/
BlockingSocketFragment.java
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package com.catherine.webservices.fragments.socket;
import android.Manifest;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.catherine.webservices.Constants;
import com.catherine.webservices.R;
import com.catherine.webservices.components.DialogManager;
import com.catherine.webservices.fragments.LazyFragment;
import com.catherine.webservices.interfaces.MainInterface;
import com.catherine.webservices.interfaces.OnRequestPermissionsListener;
import com.catherine.webservices.network.MyTCPSocket;
import com.catherine.webservices.network.NetworkHelper;
import com.catherine.webservices.network.SocketListener;
import java.net.ConnectException;
import java.util.List;
/**
* Created by Catherine on 2017/9/19.
* Soft-World Inc.
* catherine919@soft-world.com.tw
*/
public class BlockingSocketFragment extends LazyFragment {
public final static String TAG = BlockingSocketFragment.class.getSimpleName();
private MainInterface mainInterface;
private TextView tv_history, tv_state;
private EditText et_input;
private Button bt_send;
private FloatingActionButton fab_disconnect, fab_settings;
private boolean isFABOpen;
private NetworkHelper helper;
private MyTCPSocket myTCPSocket;
public static BlockingSocketFragment newInstance(boolean isLazyLoad) {
Bundle args = new Bundle();
args.putBoolean(LazyFragment.INTENT_BOOLEAN_LAZYLOAD, isLazyLoad);
BlockingSocketFragment fragment = new BlockingSocketFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreateViewLazy(Bundle savedInstanceState) {
super.onCreateViewLazy(savedInstanceState);
setContentView(R.layout.f_common_socket);
helper = new NetworkHelper();
mainInterface = (MainInterface) getActivity();
init();
}
private void init() {
mainInterface.getPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, new OnRequestPermissionsListener() {
@Override
public void onGranted() {
initComponent();
initSocket();
}
@Override
public void onDenied(@Nullable List<String> deniedPermissions) {
StringBuilder context = new StringBuilder();
if (deniedPermissions != null) {
for (String p : deniedPermissions) {
context.append(p);
context.append(", ");
}
}
context.deleteCharAt(context.length() - 1);
DialogManager.showPermissionDialog(getActivity(), String.format(getActivity().getResources().getString(R.string.permission_request), context), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
getActivity().finish();
}
});
}
@Override
public void onRetry() {
init();
}
});
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
private void initSocket() {
myTCPSocket = new MyTCPSocket.Builder()
.host(Constants.SOCKET_HOST)
.port(Constants.TCP_SOCKET_PORT)
.initListener(new InitListener())
.inputListener(new InputListener())
.outputListener(new OutputListener())
.build();
}
private void initComponent() {
fab_disconnect = (FloatingActionButton) findViewById(R.id.fab_disconnect);
fab_settings = (FloatingActionButton) findViewById(R.id.fab_settings);
tv_state = (TextView) findViewById(R.id.tv_state);
tv_history = (TextView) findViewById(R.id.tv_history);
mainInterface.restoreBottomLayout();
mainInterface.addBottomLayout(R.layout.bottom_socket);
View bottom = mainInterface.getBottomLayout();
et_input = bottom.findViewById(R.id.et_input);
bt_send = bottom.findViewById(R.id.bt_send);
bt_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myTCPSocket.send(et_input.getText().toString());
}
});
fab_disconnect.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myTCPSocket.send("*#DISCONNECT11223#*");
tv_state.setText(Constants.SOCKET_HOST + " disconnected.");
myTCPSocket.release();
}
});
fab_settings.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (!isFABOpen) {
isFABOpen = true;
fab_disconnect.animate().translationY(-getResources().getDimension(R.dimen.fab_01_m_b));
} else {
isFABOpen = false;
fab_disconnect.animate().translationY(0);
}
}
});
}
private class InitListener implements SocketListener {
@Override
public void connectSuccess(String message) {
tv_state.setText(message);
}
@Override
public void connectFailure(Exception e) {
e.printStackTrace();
if (e instanceof ConnectException) {
if (!helper.isNetworkHealthy()) {
tv_state.setText(getResources().getString(R.string.offline));
}
} else if (e instanceof NullPointerException) {
//Server error
tv_state.setText(e.getMessage());
}
}
}
private class OutputListener implements SocketListener {
@Override
public void connectSuccess(String message) {
et_input.setText("");
tv_history.setText(String.format("%s\nYou sent: %s", tv_history.getText(), message));
}
@Override
public void connectFailure(Exception e) {
e.printStackTrace();
}
}
private class InputListener implements SocketListener {
@Override
public void connectSuccess(String message) {
if ("*#DISCONNECT11223#*".equals(message)) {
tv_state.setText(Constants.SOCKET_HOST + " disconnected.");
myTCPSocket.release();
} else {
// 读取socket输入流的内容并打印
tv_history.setText(String.format("%s\nYou got: %s", tv_history.getText(), message));
}
}
@Override
public void connectFailure(Exception e) {
e.printStackTrace();
if (e instanceof ConnectException) {
if (!helper.isNetworkHealthy()) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
tv_state.setText(getResources().getString(R.string.offline));
}
});
}
}
}
}
}