Skip to Content.
Sympa Menu

ndt-dev - [ndt-dev] [ndt] r411 committed - Start to re-work interaction between service, activity to use a member...

Subject: NDT-DEV email list created

List archive

[ndt-dev] [ndt] r411 committed - Start to re-work interaction between service, activity to use a member...


Chronological Thread 
  • From:
  • To:
  • Subject: [ndt-dev] [ndt] r411 committed - Start to re-work interaction between service, activity to use a member...
  • Date: Tue, 05 Jul 2011 20:58:49 +0000

Revision: 411
Author:

Date: Tue Jul 5 13:57:42 2011
Log: Start to re-work interaction between service, activity to use a member receiver.
http://code.google.com/p/ndt/source/detail?r=411

Modified:
/branches/android/Android/src/net/measurementlab/ndt/NdtService.java
/branches/android/Android/src/net/measurementlab/ndt/ServerLocation.java

=======================================
--- /branches/android/Android/src/net/measurementlab/ndt/NdtService.java Tue Jul 5 12:53:24 2011
+++ /branches/android/Android/src/net/measurementlab/ndt/NdtService.java Tue Jul 5 13:57:42 2011
@@ -12,6 +12,10 @@
public static final int DOWNLOADING = 2;
public static final int COMPLETE = 3;

+ public static final String INTENT_UPDATE = "net.measurementlab.ndt.Update";
+
+ private Intent intent;
+
private TestReporter testReporter = new TestReporter();

private String networkType;
@@ -28,6 +32,7 @@
public void onCreate() {
super.onCreate();
Log.i("ndt", "Service created.");
+ intent = new Intent(INTENT_UPDATE);
}

@Override
@@ -79,16 +84,19 @@
if (str.contains("client-to-server") && 0 == viewId) {
Log.i("ndt", "Starting upload test.");
testReporter.setState(UPLOADING);
+ sendBroadcast(intent);
}

if (str.contains("server-to-client") && 0 == viewId) {
Log.i("ndt", "Starting upload test.");
testReporter.setState(DOWNLOADING);
+ sendBroadcast(intent);
}

if (str.contains("Test ended") && 0 == viewId) {
Log.i("ndt", "Starting upload test.");
testReporter.setState(COMPLETE);
+ sendBroadcast(intent);
}
}

=======================================
--- /branches/android/Android/src/net/measurementlab/ndt/ServerLocation.java Tue Jul 5 12:53:24 2011
+++ /branches/android/Android/src/net/measurementlab/ndt/ServerLocation.java Tue Jul 5 13:57:42 2011
@@ -5,9 +5,11 @@
import java.util.concurrent.TimeUnit;

import android.app.Activity;
+import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
+import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
@@ -24,7 +26,8 @@
public class ServerLocation extends Activity {
private ITestReporter testReporter;
private boolean bound;
- private boolean running;
+ private BroadcastReceiver receiver;
+
private ServiceConnection connection = new ServiceConnection() {

@Override
@@ -58,50 +61,55 @@
Intent intent = new Intent(getApplicationContext(),
NdtService.class);
intent.putExtra("networkType", getNetworkType());
startService(intent);
-
+
bindService(new Intent(getApplicationContext(),
NdtService.class),
this.connection, Context.BIND_AUTO_CREATE);
bound = true;

- running = true;
- new Thread() {
-
+ receiver = new BroadcastReceiver() {
+
@Override
- public void run() {
+ public void onReceive(Context context, Intent intent)
{
try {
- while (true == running && (null == testReporter || NdtService.COMPLETE != testReporter.getState())) {
- TimeUnit.SECONDS.sleep(1l);
- Log.d("ndt", String.format(
- "Checking test
state, %1$d.",
- (null ==
testReporter) ? -1 : testReporter
-
.getState()));
- switch ((null ==
testReporter) ? 0 : testReporter.getState()) {
- case NdtService.PREPARING:
-
updateHeader("Preparing...");
- break;
- case NdtService.UPLOADING:
- updateHeader("Testing
upload...");
- break;
- case NdtService.DOWNLOADING:
- updateHeader("Testing
download...");
- break;
- }
- }
- updateHeader("Test complete.");
+ switch ((null == testReporter) ? 0 :
testReporter
+ .getState()) {
+ case NdtService.PREPARING:
+ updateHeader("Preparing...");
+ break;
+ case NdtService.UPLOADING:
+ updateHeader("Testing
upload...");
+ break;
+ case NdtService.DOWNLOADING:
+ updateHeader("Testing
download...");
+ break;
+ case NdtService.COMPLETE:
+ updateHeader("Test
complete.");
+ break;
+ }
} catch (RemoteException e) {
Log.e("ndt", "Error in busy-wait
loop.", e);
- } catch (InterruptedException e) {
- Log.e("ndt", "Sleep interrupted.", e);
}
}
- }.start();
+ };
}

@Override
- protected void onResume() {
+ protected void onStart() {
super.onStart();
+ Log.i("ndt", "Server location started.");
+ if (!bound) {
+ registerReceiver(receiver, new
IntentFilter(NdtService.INTENT_UPDATE));
+ bindService(new Intent(ITestReporter.class.getName()),
+ this.connection,
Context.BIND_AUTO_CREATE);
+ }
+ }
+
+ @Override
+ protected void onResume() {
+ super.onResume();
Log.i("ndt", "Server location resumed.");
if (!bound) {
+ registerReceiver(receiver, new
IntentFilter(NdtService.INTENT_UPDATE));
bindService(new Intent(ITestReporter.class.getName()),
this.connection,
Context.BIND_AUTO_CREATE);
}
@@ -114,16 +122,10 @@
if (bound) {
bound = false;
unbindService(this.connection);
- }
- running = false;
- }
-
- @Override
- protected void onDestroy() {
- super.onDestroy();
- running = false;
- }
-
+ unregisterReceiver(receiver);
+ }
+ }
+
private void updateHeader(String labelText) {
TextView textView = (TextView)
findViewById(R.id.NdtServerLocationLabel);
textView.setText(labelText);


  • [ndt-dev] [ndt] r411 committed - Start to re-work interaction between service, activity to use a member..., ndt, 07/05/2011

Archive powered by MHonArc 2.6.16.

Top of Page