77import android .net .wifi .WifiManager ;
88import android .os .Build ;
99import android .os .Bundle ;
10+ import android .os .CountDownTimer ;
1011import android .support .annotation .NonNull ;
1112import android .support .v7 .app .AppCompatActivity ;
1213import android .view .View ;
2021
2122import java .io .IOException ;
2223import java .util .List ;
23- import java .util .Timer ;
24- import java .util .TimerTask ;
2524
2625import butterknife .BindView ;
2726import butterknife .ButterKnife ;
@@ -56,6 +55,7 @@ public class PredictZoneActivity extends AppCompatActivity {
5655 Float updateInterval = 2.0f ;
5756 AcquireCurrentZoneFromServer acquireCurrentZoneFromServer ;
5857 String currentZone = "" ;
58+ CountDownTimer timer ;
5959
6060 @ Override
6161 protected void onCreate (Bundle savedInstanceState ) {
@@ -83,13 +83,22 @@ protected void onCreate(Bundle savedInstanceState) {
8383 public void onClick (View v ) {
8484 try {
8585 updateInterval = Float .parseFloat (editTextUpdateInterval .getText ().toString ());
86+ if (updateInterval < 0.5f ) {
87+ updateInterval = 2.0f ;
88+ editTextUpdateInterval .setText ("2.0" );
89+ Toast toast = Toast .makeText (PredictZoneActivity .this ,
90+ "Must be greater than 0.5" , Toast .LENGTH_SHORT );
91+ toast .show ();
92+ }
8693 } catch (NumberFormatException e ) {
8794 updateInterval = 2.0f ;
8895 editTextUpdateInterval .setText ("2.0" );
8996 Toast toast = Toast .makeText (PredictZoneActivity .this ,
9097 "Invalid format" , Toast .LENGTH_SHORT );
9198 toast .show ();
9299 }
100+
101+
93102 }
94103 });
95104
@@ -98,12 +107,15 @@ public void onClick(View v) {
98107 * This part of the code schedules the scan and calls it after the interval suggested
99108 * by the user. It is called endlessly.
100109 */
101- final Timer timer = new Timer ();
102-
103- timer .schedule (new TimerTask () {
110+ timer = new CountDownTimer ((long ) (updateInterval * 1000 ), (long ) (updateInterval * 5 )) {
104111 @ Override
105- public void run () {
112+ public void onTick (long millisUntilFinished ) {
113+ progressBar .setMax ((int ) (updateInterval * 1000 ));
114+ progressBar .setProgress ((int ) (updateInterval * 1000 - millisUntilFinished ));
115+ }
106116
117+ @ Override
118+ public void onFinish () {
107119 if (!wManager .isWifiEnabled ()) {
108120 wManager .setWifiEnabled (true );
109121 }
@@ -115,10 +127,8 @@ public void run() {
115127 e .printStackTrace ();
116128 }
117129 }
118-
119130 }
120- }, 0 , (long ) (updateInterval * 1000 ));
121-
131+ }.start ();
122132
123133 }
124134
@@ -163,7 +173,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
163173
164174 private class AcquireCurrentZoneFromServer {
165175
166- public void run (OkHttpClient client , List <ScanResult > scanResults ) throws Exception {
176+ void run (OkHttpClient client , List <ScanResult > scanResults ) throws Exception {
167177
168178 JSONObject requestBodyJSON = new JSONObject ();
169179 JSONObject apJSON ;
@@ -192,22 +202,39 @@ public void run(OkHttpClient client, List<ScanResult> scanResults) throws Except
192202 public void onFailure (Call call , IOException e ) {
193203 e .printStackTrace ();
194204
195- Toast toast = Toast .makeText (getApplicationContext (),
196- "Something went wrong, try again later" , Toast .LENGTH_LONG );
197- toast .show ();
205+ runOnUiThread (new Runnable () {
206+ @ Override
207+ public void run () {
208+ Toast toast = Toast .makeText (PredictZoneActivity .this ,
209+ "Something went wrong, try again later" , Toast .LENGTH_LONG );
210+ toast .show ();
211+ }
212+ });
198213 }
199214
200215 @ Override
201216 public void onResponse (Call call , Response response ) throws IOException {
202- if (!response .isSuccessful ())
217+ if (!response .isSuccessful ()) {
218+ runOnUiThread (new Runnable () {
219+ @ Override
220+ public void run () {
221+ Toast toast = Toast .makeText (PredictZoneActivity .this ,
222+ "Something went wrong, try again later" , Toast .LENGTH_LONG );
223+ toast .show ();
224+ }
225+ });
203226 throw new IOException ("Unexpected code " + response );
227+ }
228+
204229
205230 currentZone = response .body ().string ();
231+ currentZone = currentZone .substring (2 , currentZone .length () - 2 );
206232
207233 runOnUiThread (new Runnable () {
208234 @ Override
209235 public void run () {
210236 zoneName .setText (currentZone );
237+ timer .start ();
211238 }
212239 });
213240
0 commit comments