package org.altusmetrum.AltosDroid;
+import java.lang.ref.WeakReference;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
// The Handler that gets information back from the Telemetry Service
- class IncomingHandler extends Handler {
+ static class IncomingHandler extends Handler {
+ private final WeakReference<AltosDroid> mAltosDroid;
+ IncomingHandler(AltosDroid ad) { mAltosDroid = new WeakReference<AltosDroid>(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;
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;
package org.altusmetrum.AltosDroid;
+import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.concurrent.LinkedBlockingQueue;
LinkedBlockingQueue<AltosLine> telem;
// Handler of incoming messages from clients.
- class IncomingHandler extends Handler {
+ static class IncomingHandler extends Handler {
+ private final WeakReference<TelemetryService> service;
+ IncomingHandler(TelemetryService s) { service = new WeakReference<TelemetryService>(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);
}
}
- private void TeleBT_stop() {
+ private void stopAltosBluetooth() {
if (mAltosBluetooth != null) {
mAltosBluetooth.close();
mAltosBluetooth = null;
telem.clear();
}
- private void TeleBT_start(BluetoothDevice d) {
+ private void startAltosBluetooth(BluetoothDevice d) {
mAltosBluetooth = new AltosBluetooth(d);
mAltosBluetooth.add_monitor(telem);
}
public void onDestroy() {
// Stop the bluetooth Comms threads
- TeleBT_stop();
+ stopAltosBluetooth();
// Demote us from the foreground, and cancel the persistent notification.
stopForeground(true);