From 54baecc208a40606e3242b2cbd5e66567053646f Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Sun, 26 Aug 2012 23:12:48 +1200 Subject: [PATCH] altosdroid: Convert handlers to use weakreferences * Also renamed bluetooth start/stop methods Signed-off-by: Mike Beattie --- .../altusmetrum/AltosDroid/AltosDroid.java | 27 +++++++++++-------- .../AltosDroid/TelemetryService.java | 20 ++++++++------ 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java index c1b9c654..d23d504f 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java @@ -17,6 +17,7 @@ package org.altusmetrum.AltosDroid; +import java.lang.ref.WeakReference; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; @@ -85,24 +86,28 @@ public class AltosDroid extends Activity { // The Handler that gets information back from the Telemetry Service - class IncomingHandler extends Handler { + static class IncomingHandler extends Handler { + private final WeakReference mAltosDroid; + IncomingHandler(AltosDroid ad) { mAltosDroid = new WeakReference(ad); } + @Override public void handleMessage(Message msg) { + AltosDroid ad = mAltosDroid.get(); switch (msg.what) { case MSG_STATE_CHANGE: if(D) Log.i(TAG, "MSG_STATE_CHANGE: " + msg.arg1); switch (msg.arg1) { case TelemetryService.STATE_CONNECTED: - mTitle.setText(R.string.title_connected_to); - mTitle.append(mConnectedDeviceName); - mSerialView.setText(""); + ad.mTitle.setText(R.string.title_connected_to); + ad.mTitle.append(ad.mConnectedDeviceName); + ad.mSerialView.setText(""); break; case TelemetryService.STATE_CONNECTING: - mTitle.setText(R.string.title_connecting); + ad.mTitle.setText(R.string.title_connecting); break; case TelemetryService.STATE_READY: case TelemetryService.STATE_NONE: - mTitle.setText(R.string.title_not_connected); + ad.mTitle.setText(R.string.title_not_connected); break; } break; @@ -110,17 +115,17 @@ public class AltosDroid extends Activity { byte[] buf = (byte[]) msg.obj; // construct a string from the buffer String telem = new String(buf); - mSerialView.append(telem); + ad.mSerialView.append(telem); break; case MSG_DEVNAME: // save the connected device's name - mConnectedDeviceName = msg.getData().getString(KEY_DEVNAME); - Toast.makeText(getApplicationContext(), "Connected to " - + mConnectedDeviceName, Toast.LENGTH_SHORT).show(); + ad.mConnectedDeviceName = msg.getData().getString(KEY_DEVNAME); + Toast.makeText(ad.getApplicationContext(), "Connected to " + + ad.mConnectedDeviceName, Toast.LENGTH_SHORT).show(); break; case MSG_TOAST: Toast.makeText( - getApplicationContext(), + ad.getApplicationContext(), msg.getData().getString(KEY_TOAST), Toast.LENGTH_SHORT).show(); break; diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index 2ee7fe58..1c0e94b3 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -17,6 +17,7 @@ package org.altusmetrum.AltosDroid; +import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.concurrent.LinkedBlockingQueue; @@ -68,22 +69,25 @@ public class TelemetryService extends Service { LinkedBlockingQueue telem; // Handler of incoming messages from clients. - class IncomingHandler extends Handler { + static class IncomingHandler extends Handler { + private final WeakReference service; + IncomingHandler(TelemetryService s) { service = new WeakReference(s); } + @Override public void handleMessage(Message msg) { + TelemetryService s = service.get(); switch (msg.what) { case MSG_REGISTER_CLIENT: - mClients.add(msg.replyTo); + s.mClients.add(msg.replyTo); if (D) Log.d(TAG, "Client bound to service"); break; case MSG_UNREGISTER_CLIENT: - mClients.remove(msg.replyTo); + s.mClients.remove(msg.replyTo); if (D) Log.d(TAG, "Client unbound from service"); break; case MSG_CONNECT_TELEBT: if (D) Log.d(TAG, "Connect command received"); - TeleBT_stop(); - TeleBT_start((BluetoothDevice) msg.obj); + s.startAltosBluetooth((BluetoothDevice) msg.obj); break; default: super.handleMessage(msg); @@ -91,7 +95,7 @@ public class TelemetryService extends Service { } } - private void TeleBT_stop() { + private void stopAltosBluetooth() { if (mAltosBluetooth != null) { mAltosBluetooth.close(); mAltosBluetooth = null; @@ -99,7 +103,7 @@ public class TelemetryService extends Service { telem.clear(); } - private void TeleBT_start(BluetoothDevice d) { + private void startAltosBluetooth(BluetoothDevice d) { mAltosBluetooth = new AltosBluetooth(d); mAltosBluetooth.add_monitor(telem); } @@ -141,7 +145,7 @@ public class TelemetryService extends Service { public void onDestroy() { // Stop the bluetooth Comms threads - TeleBT_stop(); + stopAltosBluetooth(); // Demote us from the foreground, and cancel the persistent notification. stopForeground(true); -- 2.30.2