2727
2828import cc .seeed .iot .MyApplication ;
2929import cc .seeed .iot .R ;
30- import cc .seeed .iot .datastruct .User ;
30+ import cc .seeed .iot .util .User ;
3131import cc .seeed .iot .udp .ConfigUdpSocket ;
3232import cc .seeed .iot .ui_main .MainScreenActivity ;
3333import cc .seeed .iot .webapi .IotApi ;
@@ -55,6 +55,7 @@ public class ApConnectActivity extends AppCompatActivity implements OnClickListe
5555 private String node_sn ;
5656 private String node_key ;
5757 private ConfigUdpSocket udpClient ;
58+ private ProgressDialog mProgressDialog ;
5859
5960 @ Override
6061 protected void onCreate (Bundle savedInstanceState ) {
@@ -74,6 +75,10 @@ protected void onCreate(Bundle savedInstanceState) {
7475 mConnectBtnView .setOnClickListener (this );
7576
7677 udpClient = new ConfigUdpSocket ();
78+
79+ mProgressDialog = new ProgressDialog (ApConnectActivity .this );
80+ mProgressDialog .setCanceledOnTouchOutside (false );
81+ mProgressDialog .setCancelable (false );
7782 }
7883
7984 @ Override
@@ -87,6 +92,12 @@ protected void onResume() {
8792 mSsidView .setText (ssid );
8893 }
8994
95+ @ Override
96+ protected void onDestroy () {
97+ dismissProgressDialog ();
98+ super .onDestroy ();
99+ }
100+
90101 @ Override
91102 public boolean onOptionsItemSelected (MenuItem item ) {
92103 int id = item .getItemId ();
@@ -112,19 +123,9 @@ public void onClick(View v) {
112123 mNodeNameView .setError ("Node name is empty" );
113124 return ;
114125 }
115- //APCFG: ssid\tpassword\tkey\tsn\t
116- // String cmd_connect = "APCFG: " + ssid + "\t" + password + "\t" +
117- // node_key + "\t" + node_sn + "\t";
118126 String ota_server_ip = ((MyApplication ) getApplication ()).getOtaServerIP ();
119127 String exchange_server_ip = ota_server_ip ;
120128
121- // if (ota_server.equals(Common.OTA_CHINA_URL)) {
122- // ota_server = Common.OTA_CHINA_IP;
123- // exchange_server = Common.EXCHANGE_CHINA_IP;
124- // } else if (ota_server.equals(Common.OTA_INTERNATIONAL_URL)) {
125- // ota_server = Common.OTA_INTERNATIONAL_IP;
126- // exchange_server = Common.EXCHANGE_INTERNATIONAL_IP;
127- // }
128129 String cmd_connect = "APCFG: " + ssid + "\t " + password + "\t " +
129130 node_key + "\t " + node_sn + "\t " + exchange_server_ip + "\t "
130131 + ota_server_ip + "\t " ;
@@ -140,10 +141,7 @@ private class SetNodeSn extends AsyncTask<String, Void, Boolean> {
140141
141142 @ Override
142143 protected void onPreExecute () {
143- mProgressDialog = new ProgressDialog (ApConnectActivity .this );
144- mProgressDialog .setMessage ("Sending wifi password to Wio..." );
145- mProgressDialog .setCanceledOnTouchOutside (false );
146- mProgressDialog .show ();
144+ showProgressDialog ("Sending wifi password to Wio..." );
147145 }
148146
149147 @ Override
@@ -162,7 +160,7 @@ protected Boolean doInBackground(String... params) {
162160 } catch (SocketTimeoutException e ) {
163161 udpClient .setSoTimeout (3000 );
164162 udpClient .sendData (cmd , ipAddr );
165- continue ;
163+
166164 } catch (IOException e ) {
167165 Log .e (TAG , "Error[SetNodeSn]:" + e );
168166 return false ;
@@ -174,8 +172,6 @@ protected Boolean doInBackground(String... params) {
174172
175173 @ Override
176174 protected void onPostExecute (Boolean b ) {
177- mProgressDialog .dismiss ();
178-
179175 //remove Wio wifi config
180176 WifiManager wifiManager = (WifiManager ) getSystemService (Context .WIFI_SERVICE );
181177 List <WifiConfiguration > wifiConfigurations = wifiManager .getConfiguredNetworks ();
@@ -185,21 +181,16 @@ protected void onPostExecute(Boolean b) {
185181 wifiManager .saveConfiguration ();
186182 }
187183 }
188-
189184 new checkNodeIsOnline ().execute ();
190185 }
191186 }
192187
193- private class checkNodeIsOnline extends AsyncTask <Void , Void , Boolean > {
194- private ProgressDialog mProgressDialog ;
188+ private class checkNodeIsOnline extends AsyncTask <Void , Integer , Boolean > {
195189 private Boolean state_online = false ;
196190
197191 @ Override
198192 protected void onPreExecute () {
199- mProgressDialog = new ProgressDialog (ApConnectActivity .this );
200- mProgressDialog .setMessage ("Waiting Wio get ip address..." );
201- mProgressDialog .setCanceledOnTouchOutside (false );
202- mProgressDialog .show ();
193+ showProgressDialog ("Waiting Wio get ip address..." );
203194 }
204195
205196 @ Override
@@ -233,13 +224,23 @@ public void failure(RetrofitError error) {
233224 } catch (InterruptedException e ) {
234225 e .printStackTrace ();
235226 }
227+ publishProgress (30 -i );
236228 }
237229 return state_online ;
238230 }
239231
232+ @ Override
233+ protected void onProgressUpdate (Integer ... values ) {
234+ int i = values [0 ];
235+ showProgressDialog ("Waiting Wio get ip address...[" + i + "]" );
236+ }
237+
240238 @ Override
241239 protected void onPostExecute (Boolean state_online ) {
242- mProgressDialog .dismiss ();
240+ if (ApConnectActivity .this .isFinishing ()) { // or call isFinishing() if min sdk version < 17
241+ return ;
242+ }
243+ dismissProgressDialog ();
243244
244245 if (state_online ) {
245246 attemptRename (node_name );
@@ -289,4 +290,18 @@ public void failure(RetrofitError error) {
289290 });
290291 }
291292 }
293+
294+ private void showProgressDialog (String message ) {
295+ mProgressDialog .setMessage (message );
296+ // mProgressDialog.setIndeterminate(false);
297+ if (!mProgressDialog .isShowing ()) {
298+ mProgressDialog .show ();
299+ }
300+ }
301+
302+ private void dismissProgressDialog () {
303+ if (mProgressDialog != null && mProgressDialog .isShowing ()) {
304+ mProgressDialog .dismiss ();
305+ }
306+ }
292307}
0 commit comments