2
2
3
3
import android .app .Activity ;
4
4
5
+ import android .app .ActivityManager ;
5
6
import android .content .ComponentName ;
6
7
import android .content .Context ;
7
8
import android .content .Intent ;
16
17
import android .widget .Button ;
17
18
import android .widget .ProgressBar ;
18
19
import android .widget .TextView ;
19
- import android .widget .Toast ;
20
20
21
21
import com .axiros .axact .AxirosService ;
22
22
23
- public class MainActivity extends Activity implements View .OnClickListener , AxirosService .AxirosEventsListener {
24
- Intent mServiceIntent ;
25
- AxirosService mService ;
26
- boolean mBound = false ;
23
+ public class MainActivity extends BaseActivity implements View .OnClickListener , AxirosService .AxirosEventsListener {
24
+ private static AxirosService mService ;
25
+ private static AxirosService .LocalBinder mServiceBinder ;
26
+ private static Intent mServiceIntent ;
27
+
28
+ private static boolean mBound ;
27
29
28
30
MainActivity mActivity ;
29
31
Button buttonStart , buttonStop ;
@@ -113,11 +115,55 @@ private String getNetworkType() {
113
115
return networkType ;
114
116
}
115
117
118
+ private boolean isServiceRunning () {
119
+ ActivityManager manager = (ActivityManager ) getSystemService (ACTIVITY_SERVICE );
120
+ for (ActivityManager .RunningServiceInfo service : manager .getRunningServices (Integer .MAX_VALUE )){
121
+ if ("com.axiros.axact.AxirosService" .equals (service .service .getClassName ())) {
122
+ return true ;
123
+ }
124
+ }
125
+ return false ;
126
+ }
127
+
128
+ /** Defines callbacks for service binding, passed to bindService() */
129
+ private ServiceConnection mConnection = new ServiceConnection () {
130
+
131
+ @ Override
132
+ public void onServiceConnected (ComponentName className , IBinder service ) {
133
+ mServiceBinder = (AxirosService .LocalBinder )service ;
134
+ mService = mServiceBinder .getServiceInstance ();
135
+ mService .registerEventsListener (mActivity );
136
+ textViewConnection .setText (String .format ("Connection type: %s" , getNetworkType ()));
137
+ }
138
+
139
+ @ Override
140
+ public void onServiceDisconnected (ComponentName arg0 ) {
141
+ mService .unregisterEventsListener (mActivity );
142
+ mService = null ;
143
+ mServiceBinder = null ;
144
+ }
145
+ };
146
+
116
147
@ Override
117
- protected void onCreate (Bundle savedInstanceState ) {
148
+ public void onCreate (Bundle savedInstanceState )
149
+ {
118
150
super .onCreate (savedInstanceState );
119
151
setContentView (R .layout .activity_main );
120
152
153
+ init ();
154
+
155
+ Intent intent = new Intent (this , AxirosService .class );
156
+ bindService (intent , mConnection , Context .BIND_AUTO_CREATE );
157
+ }
158
+
159
+ @ Override
160
+ protected void onDestroy () {
161
+ unbindService (mConnection );
162
+ super .onDestroy ();
163
+ }
164
+
165
+ private void init ()
166
+ {
121
167
mActivity = this ;
122
168
123
169
buttonStart = (Button ) findViewById (R .id .buttonStart );
@@ -132,8 +178,17 @@ protected void onCreate(Bundle savedInstanceState) {
132
178
133
179
textViewConnection = (TextView ) findViewById (R .id .textViewConnection );
134
180
135
- buttonStart .setEnabled (true );
136
- buttonStop .setEnabled (false );
181
+ if (isServiceRunning ())
182
+ {
183
+ buttonStart .setEnabled (false );
184
+ buttonStop .setEnabled (true );
185
+ }
186
+ else
187
+ {
188
+ buttonStart .setEnabled (true );
189
+ buttonStop .setEnabled (false );
190
+ }
191
+
137
192
138
193
buttonStart .setOnClickListener (this );
139
194
buttonStop .setOnClickListener (this );
@@ -143,24 +198,6 @@ protected void onCreate(Bundle savedInstanceState) {
143
198
144
199
mProgressBarDownload =(ProgressBar )findViewById (R .id .progressBarDownload );
145
200
mProgressBarDownload .setProgress (0 );
146
-
147
- mServiceIntent = new Intent (this , AxirosService .class );
148
- bindService (mServiceIntent , mConnection , 0 );
149
-
150
- // About this key https://wiki.axiros.com/display/EFI/INTERNAL
151
- mServiceIntent .putExtra ("key" , "zptkrc8uJaud1spndstrqhCwb/MGaAj72Oiv2WcU43EaawEHu1bGoqrbLdpqF/EQX1ChYOT7dUuKYssVivAHcQ==" );
152
-
153
- // only integrators with url need to use it
154
- //mServiceIntent.putExtra("cert", "eaq.com.br.crt");
155
- }
156
-
157
- @ Override
158
- protected void onDestroy () {
159
- Toast .makeText (this , "Stoping AXACT Service." , Toast .LENGTH_LONG ).show ();
160
- mService .unregisterEventsListener (mActivity );
161
- stopService (mServiceIntent );
162
- unbindService (mConnection );
163
- super .onDestroy ();
164
201
}
165
202
166
203
private void clearView () {
@@ -179,21 +216,34 @@ private void clearView() {
179
216
180
217
@ Override
181
218
public void onClick (View view ) {
219
+
220
+
221
+ if (mServiceIntent == null )
222
+ {
223
+ mServiceIntent = new Intent (this , AxirosService .class );
224
+ // About this key https://wiki.axiros.com/display/EFI/INTERNAL
225
+ mServiceIntent .putExtra ("key" , "zptkrc8uJaud1spndstrqhCwb/MGaAj72Oiv2WcU43EaawEHu1bGoqrbLdpqF/EQX1ChYOT7dUuKYssVivAHcQ==" );
226
+
227
+ // only integrators with url need to use it
228
+ mServiceIntent .putExtra ("cert" , "eaq.com.br.crt" );
229
+ }
230
+
182
231
switch (view .getId ()) {
232
+
183
233
case R .id .buttonStart :
184
- if (!mBound ) {
185
- startService (mServiceIntent );
186
- }
187
234
buttonStart .setEnabled (false );
188
235
buttonStop .setEnabled (true );
236
+
237
+ startService (mServiceIntent );
238
+
189
239
break ;
190
240
case R .id .buttonStop :
191
- if (mBound ) {
192
- stopService (mServiceIntent );
193
- clearView ();
194
- }
195
241
buttonStart .setEnabled (true );
196
242
buttonStop .setEnabled (false );
243
+
244
+ stopService (mServiceIntent );
245
+
246
+ clearView ();
197
247
break ;
198
248
}
199
249
}
@@ -244,7 +294,7 @@ public void onTick(long millisUntilFinished) {
244
294
public void onFinish () {
245
295
//Do what you want
246
296
countUp ++;
247
- mProgressBarUpload .setProgress (100 );
297
+ mProgressBarUpload .setProgress (0 );
248
298
}
249
299
};
250
300
mCountDownTimerUL .start ();
@@ -257,6 +307,7 @@ public void udpEchoConfigured() {
257
307
runOnUiThread (new Runnable () {
258
308
@ Override
259
309
public void run () {
310
+ clearView ();;
260
311
textViewUDPStatus .setText ("Running UDP echo test" );
261
312
}
262
313
});
@@ -269,6 +320,7 @@ public void downloadDiagnosticsResult(long bps) {
269
320
@ Override
270
321
public void run () {
271
322
downloadView .setText ( String .format ("Download Result: %d" , result ));
323
+ mCountDownTimerDL .cancel ();
272
324
}
273
325
});
274
326
}
@@ -280,6 +332,9 @@ public void uploadDiagnosticsResult(long bps) {
280
332
@ Override
281
333
public void run () {
282
334
uploadView .setText ( String .format ("Upload Result: %d" , result ));
335
+ if (mCountDownTimerUL != null ) {
336
+ mCountDownTimerUL .cancel ();
337
+ }
283
338
}
284
339
});
285
340
}
@@ -295,28 +350,5 @@ public void run() {
295
350
});
296
351
}
297
352
298
- /** Defines callbacks for service binding, passed to bindService() */
299
- private ServiceConnection mConnection = new ServiceConnection () {
300
-
301
- @ Override
302
- public void onServiceConnected (ComponentName className , IBinder service ) {
303
- if (!mBound ) {
304
- mBound = true ;
305
- AxirosService .LocalBinder binder = (AxirosService .LocalBinder ) service ;
306
- mService = binder .getServiceInstance ();
307
- mService .registerEventsListener (mActivity );
308
- textViewConnection .setText (String .format ("Connection type: %s" , getNetworkType ()));
309
- Toast .makeText (mActivity , "AXACT Service started." , Toast .LENGTH_LONG ).show ();
310
- }
311
- }
312
-
313
- @ Override
314
- public void onServiceDisconnected (ComponentName arg0 ) {
315
- if (mBound ) {
316
- mBound = false ;
317
- Toast .makeText (mActivity , "AXACT Service stoped." , Toast .LENGTH_LONG ).show ();
318
- }
319
- }
320
- };
321
353
322
354
}
0 commit comments