Blame view

C5/app/src/main/java/com/sunvote/xpadapp/fragments/DownloadFragment.java 7.1 KB
fac86401   孙向锦   初始化C5 Vote
1
2
3
4
5
6
7
8
9
10
  package com.sunvote.xpadapp.fragments;
  
  import android.os.Bundle;
  import android.os.Handler;
  import android.os.Message;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.view.ViewGroup;
53509e93   孙向锦   更新下载页面
11
  import android.widget.ImageView;
fac86401   孙向锦   初始化C5 Vote
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  import android.widget.TextView;
  import android.widget.Toast;
  
  import com.sunvote.util.LogUtil;
  import com.sunvote.xpadapp.MainActivity;
  import com.sunvote.xpadapp.R;
  import com.sunvote.xpadapp.base.BaseFragment;
  import com.sunvote.xpadcomm.DownloadFileModule;
  import com.sunvote.xpadcomm.FileReciverInterface;
  
  import java.util.Random;
  
  public class DownloadFragment extends BaseFragment implements FileReciverInterface {
  	private final String TAG = "DownloadFragment";
  	private String serverIp = "127.0.0.1";
  	private int serverPort = 15154;
  
  	private String ssid = "";
  	private String pwd = "";
  	private int keypadId ;
  	private boolean isConnectWifi;
  	private TextView tv;
53509e93   孙向锦   更新下载页面
34
  	private ImageView download_img;
fac86401   孙向锦   初始化C5 Vote
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
  
  	private final int msgConnectError = 1;
  	private final int msgOnDownload = 2;
  	private final int msgDownloadOver = 3;
  	private final int msgDownloadDataError = 4;
  	private final int msgDownloadUserStop = 5;
  	private final int msgOnUnzipError = 6;
  	private final int msgGoBack= 7;
  
  	private boolean isUiActive;
  	private int downloadRetryCount ;
  
  	public boolean isFinishDownload;
  
  	public void setInfo(String wifiSsid, String wifiPassword, String ip, int port,int deviceId) {
  		if (wifiSsid != null) {
  			ssid = wifiSsid;
  		}
  		if (wifiPassword != null) {
  			pwd = wifiPassword;
  		}
  		if (ip != null) {
  			serverIp = ip;
  		}
  		if (port != 0) {
  			serverPort = port;
  		}
  		keypadId = deviceId;
  		isFinishDownload = false;
  	}
  
  	@Override
  	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  		Log.d(TAG, "onCreateView");
  		View view = inflater.inflate(R.layout.fragment_download, container, false);
  		view.setOnClickListener(new OnClickListener() {
  			@Override
  			public void onClick(View v) {
  				;
  			}
  		});
  		tv = (TextView) view.findViewById(R.id.download_title);
53509e93   孙向锦   更新下载页面
77
  		download_img = view.findViewById(R.id.download_img);
fac86401   孙向锦   初始化C5 Vote
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
  		downloadRetryCount = 0;
  		tv.setOnClickListener(new OnClickListener() {
  
  			@Override
  			public void onClick(View v) {
  				// connectWifiThread.run();
  				// connectWifi();
  			}
  		});
  
  		return view;
  	}
  
  	@Override
  	public void onResume() {
  		super.onResume();
  		isUiActive = true;
  	}
  
  	@Override
  	public void onPause() {
  		super.onPause();
  		isUiActive = false;
  	}
  
  	@Override
  	public void onActivityCreated(Bundle savedInstanceState) {
  		super.onActivityCreated(savedInstanceState);
  		LogUtil.d(TAG, "onActivityCreated");
  		connectServer();
  	}
  
  	@Override
  	public void onDestroy() {
  		super.onDestroy();
  	}
  
  	private Handler mHandler = new Handler() {
  
  		public void handleMessage(Message msg) {
  			switch (msg.what) {
  			case msgConnectError:
  				stopDownload();
  				if(isUiActive) {
1c5059dd   孙向锦   下载文件UI更新
122
  					download_img.setImageResource(R.drawable.download_fail);
fac86401   孙向锦   初始化C5 Vote
123
124
125
126
127
128
129
130
  					tv.setText(getString(R.string.connect_server_fail));
  				}
  				break;
  			case msgOnDownload:
  				LogUtil.d(TAG, getString(R.string.downloading) + msg.arg1 + "%");
  				if(isUiActive) {
  					int percent = msg.arg1;
  					tv.setText(getString(R.string.downloading) + percent + "%");
53509e93   孙向锦   更新下载页面
131
  					download_img.setImageResource(R.drawable.downloading);
fac86401   孙向锦   初始化C5 Vote
132
133
134
135
136
137
138
139
140
  					if(percent == 100){
  						tv.setText(getString(R.string.download_over_unzip));
  					}
  				}
  				break;
  			case msgDownloadOver:
  				try {
  					LogUtil.d(TAG, "on msgDownloadOver");
  					if(isUiActive) {
53509e93   孙向锦   更新下载页面
141
  						download_img.setImageResource(R.drawable.download_complete);
fac86401   孙向锦   初始化C5 Vote
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
  						tv.setText(getString(R.string.download_over));
  					}
  					isFinishDownload = true;
  					if(mMainActivity.dbm != null){
  						mMainActivity.dbm.closeDB();
  						mMainActivity.dbm = null;
  					}
  					Message message = new Message();
  					message.what = msgGoBack;
  					mHandler.sendMessageDelayed(message,10000);
  				} catch (Exception e) {
  					LogUtil.e(TAG,e);
  				}
  
  				break;
  				case msgDownloadDataError:
  					if(isUiActive) {
  						if(++downloadRetryCount >= 3){//重试 n 次
1c5059dd   孙向锦   下载文件UI更新
160
  							download_img.setImageResource(R.drawable.download_fail);
fac86401   孙向锦   初始化C5 Vote
161
162
163
164
  							tv.setText(getString(R.string.download_fail));
  							break;
  						}
  						stopDownload();
1c5059dd   孙向锦   下载文件UI更新
165
  						download_img.setImageResource(R.drawable.download_connect);
fac86401   孙向锦   初始化C5 Vote
166
167
168
169
170
171
  						tv.setText(getString(R.string.download_retry));
  						getMeetingFilesFromServer();
  					}
  					break;
  				case msgOnUnzipError:
  					Log.d(TAG, "handleMessage: msgOnUnzipError");
1c5059dd   孙向锦   下载文件UI更新
172
  					download_img.setImageResource(R.drawable.download_fail);
fac86401   孙向锦   初始化C5 Vote
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
  					tv.setText(getString(R.string.unzip_error));
  					Toast.makeText(mMainActivity,getString(R.string.unzip_error),Toast.LENGTH_LONG).show();
  					isFinishDownload = true;
  					break;
  				case msgDownloadUserStop:
  					stopDownload();
  					break;
  				case msgGoBack:
  					goBack();
  			}
  
  			super.handleMessage(msg);
  		}
  	};
  
  
  
  	public void forceStopDownload(){
  		LogUtil.i(TAG, "forceStopDownload: ");
  		DownloadFileModule.getInstance().clean();
  	}
  
  	public void getMeetingFilesFromServer() {
  		LogUtil.d(TAG, "getMeetingFilesFromServer");
  		if(!DownloadFileModule.getInstance().isWork()) {
  			DownloadFileModule.getInstance().setFileReciverInterface(this);
  			DownloadFileModule.getInstance().downloadFile(serverIp, serverPort, keypadId);
  		}
  	}
  
  	private void connectServer() {
  		if(isUiActive) {
1c5059dd   孙向锦   下载文件UI更新
205
  			download_img.setImageResource(R.drawable.download_connect);
fac86401   孙向锦   初始化C5 Vote
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
  			tv.setText(getString(R.string.connect_server_ing));
  		}
  		LogUtil.d(TAG, getString(R.string.connect_server_ing));
  		getMeetingFilesFromServer();
  	}
  
  	@Override
  	public void onConnectServerError() {
  		Message message = new Message();
  		message.what = msgConnectError;
  		mHandler.sendMessage(message);
  	}
  
  	@Override
  	public void onDownload(long percent) {
  		Message message = new Message();
  		message.what = msgOnDownload;
  		message.arg1 = (int) percent;
  		mHandler.sendMessage(message);
  
  	}
  
  	@Override
  	public void onDownloadSuccess() {
  		Message message = new Message();
  		message.what = msgDownloadOver;
  		mHandler.sendMessage(message);
  	}
  
  	@Override
  	public void stopDownload() {
  		LogUtil.i(TAG, "stopDownload...");
  		DownloadFileModule.getInstance().clean();
  	}
  
  	@Override
  	public void onDownloadDataError() {
  		LogUtil.d(TAG,"onDownloadDataError");
  		Message message = new Message();
  		message.what = msgDownloadDataError;
  		mHandler.sendMessage(message);
  	}
  
  	@Override
  	public void onDownloadUserStop() {
  		LogUtil.d(TAG,"onDownloadUserStop");
  
  		Message message = new Message();
  		message.what = msgDownloadUserStop;
  		mHandler.sendMessage(message);
  	}
  
  	@Override
  	public void onUnzipError() {
  
  		LogUtil.d(TAG,"onUnzipError");
  		Message message = new Message();
  		message.what = msgOnUnzipError;
  		mHandler.sendMessage(message);
  	}
  
  	private void goBack() {
  //		FragmentManager fm = getFragmentManager();
  //		FragmentTransaction tx = fm.beginTransaction();
  //		tx.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
  //		//tx.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
  //		tx.remove(DownloadFragment.this);
  //		tx.commit();
  		((MainActivity)getActivity()).myHandler.sendEmptyMessage(MainActivity.Msg_StopDownload);
  
  	}
  
  
  
  
  
  }