ConfigActivity.java
10.7 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
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
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
package com.xgimi.smartscreen;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.xgimi.gimicinema.R;
import com.xgimi.smartscreen.confignetwork.ChTask;
import com.xgimi.smartscreen.confignetwork.IChListener;
import com.xgimi.smartscreen.confignetwork.IChResult;
import com.xgimi.smartscreen.confignetwork.IChTask;
import com.xgimi.smartscreen.confignetwork.task.__IChTask;
import com.xgimi.smartscreen.service.WifiAdminSimple;
import java.util.List;
public class ConfigActivity extends Activity {
private static final String TAG = "ConfigActivity";
private EditText mEdtApSsid;
private EditText mEdtApPassword;
private Button mBtnConfig;
private WifiAdminSimple mWifiAdmin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_config);
mWifiAdmin = new WifiAdminSimple(this);
mEdtApSsid = (EditText) findViewById(R.id.ssid_et);
mEdtApPassword = (EditText) findViewById(R.id.pwd_et);
mEdtApSsid.setText("LEMOMOFFICE2.4G");
mEdtApPassword.setText("88888888");
mBtnConfig = (Button) findViewById(R.id.btn_save);
mBtnConfig.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String apSsid = mEdtApSsid.getText().toString();
String apPassword = mEdtApPassword.getText().toString();
String apBssid = mWifiAdmin.getWifiConnectedBssid();
int[] tastResultCount = {0, 1, 2, 3, 4, 5};
String taskResultCountStr = Integer.toString(tastResultCount[1]); //设置任务的数量为1个
new AsyncTask3().execute(apSsid, apBssid, apPassword, "NO", taskResultCountStr);
}
});
}
@Override
protected void onResume() {
super.onResume();
// display the connected ap's ssid
String apSsid = mWifiAdmin.getWifiConnectedSsid();
if (apSsid != null) {
mEdtApSsid.setText(apSsid);
} else {
mEdtApSsid.setText("");
}
// check whether the wifi is connected
boolean isApSsidEmpty = TextUtils.isEmpty(apSsid);
mBtnConfig.setEnabled(!isApSsidEmpty);
}
public void matchScreen(View view) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
private class AsyncTask2 extends AsyncTask<String, Void, IChResult> {
private ProgressDialog mProgressDialog;
private IChTask mChTask;
// without the lock, if the user tap confirm and cancel quickly enough,
// the bug will arise. the reason is follows:
// 0. task is starting created, but not finished
// 1. the task is cancel for the task hasn't been created, it do nothing
// 2. task is created
// 3. Oops, the task should be cancelled, but it is running
private final Object mLock = new Object();
@Override
protected void onPreExecute() {
mProgressDialog = new ProgressDialog(ConfigActivity.this);
mProgressDialog.setMessage("正在配置,请稍侯...");
mProgressDialog.setCanceledOnTouchOutside(false);
mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
synchronized (mLock) {
if (__IChTask.DEBUG) {
Log.i(TAG, "progress dialog is canceled");
}
if (mChTask != null) {
mChTask.interrupt();
}
}
}
});
mProgressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
mProgressDialog.show();
mProgressDialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false);
}
@Override
protected IChResult doInBackground(String... params) {
synchronized (mLock) {
String apSsid = params[0];
String apBssid = params[1];
String apPassword = params[2];
String isSsidHiddenStr = params[3];
boolean isSsidHidden = false;
if (isSsidHiddenStr.equals("YES")) {
isSsidHidden = true;
}
mChTask = new ChTask(apSsid, apBssid, apPassword, isSsidHidden, ConfigActivity.this);
}
IChResult result = mChTask.executeForResult();
return result;
}
@Override
protected void onPostExecute(IChResult result) {
mProgressDialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(true);
mProgressDialog.getButton(DialogInterface.BUTTON_POSITIVE).setText("确认");
// it is unnecessary at the moment, add here just to show how to use isCancelled()
if (!result.isCancelled()) {
if (result.isSuc()) {
mProgressDialog.setMessage("配置成功" + "\n" + "BSSID = " + result.getBssid() + "\n" + "InetAddress = " + result.getInetAddress().getHostAddress());
} else {
mProgressDialog.setMessage("配置失败");
}
}
}
}
private void onChResultAddedPerform(final IChResult result) {
runOnUiThread(new Runnable() {
@Override
public void run() {
String text = result.getBssid() + " 已连接到wifi";
Toast.makeText(ConfigActivity.this, text, Toast.LENGTH_SHORT).show();
}
});
}
private IChListener myListener = new IChListener() {
@Override
public void onChResultAdded(final IChResult result) {
onChResultAddedPerform(result);
}
};
private class AsyncTask3 extends AsyncTask<String, Void, List<IChResult>> {
private ProgressDialog mProgressDialog;
private IChTask mChTask;
// without the lock, if the user tap confirm and cancel quickly enough,
// the bug will arise. the reason is follows:
// 0. task is starting created, but not finished
// 1. the task is cancel for the task hasn't been created, it do nothing
// 2. task is created
// 3. Oops, the task should be cancelled, but it is running
private final Object mLock = new Object();
@Override
protected void onPreExecute() {
mProgressDialog = new ProgressDialog(ConfigActivity.this);
mProgressDialog.setMessage("正在配置, 请稍候...");
mProgressDialog.setCanceledOnTouchOutside(false);
mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
synchronized (mLock) {
if (__IChTask.DEBUG) {
Log.i(TAG, "progress dialog is canceled");
}
if (mChTask != null) {
mChTask.interrupt();
}
}
}
});
mProgressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
if (!mProgressDialog.isShowing()) {
mProgressDialog.show();
mProgressDialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false);
}
}
@Override
protected List<IChResult> doInBackground(String... params) {
int taskResultCount = -1;
synchronized (mLock) {
String apSsid = params[0];
String apBssid = params[1];
String apPassword = params[2];
String isSsidHiddenStr = params[3];
String taskResultCountStr = params[4];
boolean isSsidHidden = false;
if (isSsidHiddenStr.equals("YES")) {
isSsidHidden = true;
}
taskResultCount = Integer.parseInt(taskResultCountStr);
mChTask = new ChTask(apSsid, apBssid, apPassword, isSsidHidden, ConfigActivity.this);
mChTask.setChListener(myListener);
}
List<IChResult> resultList = mChTask.executeForResults(taskResultCount);
return resultList;
}
@Override
protected void onPostExecute(List<IChResult> result) {
mProgressDialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(true);
mProgressDialog.getButton(DialogInterface.BUTTON_POSITIVE).setText("确认");
IChResult firstResult = result.get(0);
// check whether the task is cancelled and no results received
if (!firstResult.isCancelled()) {
int count = 0;
// max results to be displayed, if it is more than maxDisplayCount,
// just show the count of redundant ones
final int maxDisplayCount = 5;
// the task received some results including cancelled while
// executing before receiving enough results
if (firstResult.isSuc()) {
StringBuilder sb = new StringBuilder();
for (IChResult resultInList : result) {
sb.append("配置成功" + "\n" + "BSSID = " + resultInList.getBssid() + "\n" + "InetAddress = " + resultInList.getInetAddress().getHostAddress());
count++;
if (count >= maxDisplayCount) {
break;
}
}
if (count < result.size()) {
sb.append("\n有 " + (result.size() - count) + " 个结果未显示\n");
}
mProgressDialog.setMessage(sb.toString());
} else {
mProgressDialog.setMessage("配置失败");
}
}
}
}
}