From 392c878000e9909d37dae6342df3d6cb8f217a1b Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Thu, 23 Feb 2012 16:41:26 +1300 Subject: Add TelemetryService.java and associated files Signed-off-by: Mike Beattie --- .../altusmetrum/AltosDroid/TelemetryService.java | 109 +++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java new file mode 100644 index 00000000..40dff354 --- /dev/null +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -0,0 +1,109 @@ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.altusmetrum.AltosDroid; + +import android.app.Notification; +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.app.Service; +import android.content.Intent; +import android.os.Binder; +import android.os.IBinder; +import android.util.Log; +import android.widget.Toast; + +// Need the following import to get access to the app resources, since this +// class is in a sub-package. +import org.altusmetrum.AltosDroid.R; + + + +public class TelemetryService extends Service { + private NotificationManager mNM; + + // Unique Identification Number for the Notification. + // We use it on Notification start, and to cancel it. + private int NOTIFICATION = R.string.telemetry_service_started; + + /** + * Class for clients to access. Because we know this service always + * runs in the same process as its clients, we don't need to deal with + * IPC. + */ + public class TelemetryBinder extends Binder { + TelemetryService getService() { + return TelemetryService.this; + } + } + + @Override + public void onCreate() { + mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); + + // Display a notification about us starting. We put an icon in the status bar. + showNotification(); + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + Log.i("TelemetryService", "Received start id " + startId + ": " + intent); + // We want this service to continue running until it is explicitly + // stopped, so return sticky. + return START_STICKY; + } + + @Override + public void onDestroy() { + // Cancel the persistent notification. + mNM.cancel(NOTIFICATION); + + // Tell the user we stopped. + Toast.makeText(this, R.string.telemetry_service_stopped, Toast.LENGTH_SHORT).show(); + } + + @Override + public IBinder onBind(Intent intent) { + return mBinder; + } + + // This is the object that receives interactions from clients. See + // RemoteService for a more complete example. + private final IBinder mBinder = new TelemetryBinder(); + + /** + * Show a notification while this service is running. + */ + private void showNotification() { + // In this sample, we'll use the same text for the ticker and the expanded notification + CharSequence text = getText(R.string.telemetry_service_started); + + // Set the icon, scrolling text and timestamp + Notification notification = new Notification(R.drawable.am_status, text, + System.currentTimeMillis()); + + // The PendingIntent to launch our activity if the user selects this notification + PendingIntent contentIntent = PendingIntent.getActivity(this, 0, + new Intent(this, TelemetryServiceActivities.Controller.class), 0); + + // Set the info for the views that show in the notification panel. + notification.setLatestEventInfo(this, getText(R.string.telemetry_service_label), + text, contentIntent); + + // Send the notification. + mNM.notify(NOTIFICATION, notification); + } +} -- cgit v1.2.3 From fc9aed1ef3485d259722c9b89e19969e0afe257c Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Sun, 26 Feb 2012 22:30:09 +1300 Subject: Adjust service to be persistent. * Run as a foreground service -> high priority * Notification set to display as "Ongoing". Changed logo to be in colour at keithp's request. Greyscale logos still present as *_g.png Signed-off-by: Mike Beattie --- altosdroid/res/drawable-hdpi/am_status.png | Bin 804 -> 0 bytes altosdroid/res/drawable-hdpi/am_status_c.png | Bin 0 -> 994 bytes altosdroid/res/drawable-hdpi/am_status_g.png | Bin 0 -> 804 bytes altosdroid/res/drawable-mdpi/am_status.png | Bin 595 -> 0 bytes altosdroid/res/drawable-mdpi/am_status_c.png | Bin 0 -> 703 bytes altosdroid/res/drawable-mdpi/am_status_g.png | Bin 0 -> 595 bytes altosdroid/res/values/strings.xml | 15 +++--- .../altusmetrum/AltosDroid/TelemetryService.java | 51 ++++++++++----------- 8 files changed, 32 insertions(+), 34 deletions(-) delete mode 100644 altosdroid/res/drawable-hdpi/am_status.png create mode 100644 altosdroid/res/drawable-hdpi/am_status_c.png create mode 100644 altosdroid/res/drawable-hdpi/am_status_g.png delete mode 100644 altosdroid/res/drawable-mdpi/am_status.png create mode 100644 altosdroid/res/drawable-mdpi/am_status_c.png create mode 100644 altosdroid/res/drawable-mdpi/am_status_g.png (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/res/drawable-hdpi/am_status.png b/altosdroid/res/drawable-hdpi/am_status.png deleted file mode 100644 index 03f9dd7d..00000000 Binary files a/altosdroid/res/drawable-hdpi/am_status.png and /dev/null differ diff --git a/altosdroid/res/drawable-hdpi/am_status_c.png b/altosdroid/res/drawable-hdpi/am_status_c.png new file mode 100644 index 00000000..d4393217 Binary files /dev/null and b/altosdroid/res/drawable-hdpi/am_status_c.png differ diff --git a/altosdroid/res/drawable-hdpi/am_status_g.png b/altosdroid/res/drawable-hdpi/am_status_g.png new file mode 100644 index 00000000..03f9dd7d Binary files /dev/null and b/altosdroid/res/drawable-hdpi/am_status_g.png differ diff --git a/altosdroid/res/drawable-mdpi/am_status.png b/altosdroid/res/drawable-mdpi/am_status.png deleted file mode 100644 index 07f7f073..00000000 Binary files a/altosdroid/res/drawable-mdpi/am_status.png and /dev/null differ diff --git a/altosdroid/res/drawable-mdpi/am_status_c.png b/altosdroid/res/drawable-mdpi/am_status_c.png new file mode 100644 index 00000000..30a8d29a Binary files /dev/null and b/altosdroid/res/drawable-mdpi/am_status_c.png differ diff --git a/altosdroid/res/drawable-mdpi/am_status_g.png b/altosdroid/res/drawable-mdpi/am_status_g.png new file mode 100644 index 00000000..07f7f073 Binary files /dev/null and b/altosdroid/res/drawable-mdpi/am_status_g.png differ diff --git a/altosdroid/res/values/strings.xml b/altosdroid/res/values/strings.xml index 249550fb..72a4ddec 100644 --- a/altosdroid/res/values/strings.xml +++ b/altosdroid/res/values/strings.xml @@ -39,24 +39,27 @@ Control Service (Un)Bind Service + + - AltOS Telemetry Service + AltosDroid Telemetry Service Telemetry Service Started Telemetry Service Stopped - - App/Service/Local Service Controller - This demonstrates how you can implement persistent services that - may be started and stopped as desired. + + Telemetry Service Controller + Use the following buttons to start and stop the Telemetry + service. Start Service Stop Service - App/Service/Local Service Binding + Telemetry Service Binding This demonstrates how you can connect with a persistent service. Notice how it automatically starts for you, and play around with the interaction between this and Local Service Controller. Bind Service Unbind Service + Connected to local service Disconnected from local service diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index 40dff354..c0a32c92 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -37,7 +37,7 @@ public class TelemetryService extends Service { // Unique Identification Number for the Notification. // We use it on Notification start, and to cancel it. - private int NOTIFICATION = R.string.telemetry_service_started; + private int NOTIFICATION = R.string.telemetry_service_label; /** * Class for clients to access. Because we know this service always @@ -52,15 +52,32 @@ public class TelemetryService extends Service { @Override public void onCreate() { + // Create a reference to the NotificationManager so that we can update our notifcation text later mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); - - // Display a notification about us starting. We put an icon in the status bar. - showNotification(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i("TelemetryService", "Received start id " + startId + ": " + intent); + + CharSequence text = getText(R.string.telemetry_service_started); + + // Create notification to be displayed while the service runs + Notification notification = new Notification(R.drawable.am_status_c, text, 0); + + // The PendingIntent to launch our activity if the user selects this notification + PendingIntent contentIntent = PendingIntent.getActivity(this, 0, + new Intent(this, TelemetryServiceActivities.Controller.class), 0); + + // Set the info for the views that show in the notification panel. + notification.setLatestEventInfo(this, getText(R.string.telemetry_service_label), text, contentIntent); + + // Set the notification to be in the "Ongoing" section. + notification.flags |= Notification.FLAG_ONGOING_EVENT; + + // Move us into the foreground. + startForeground(NOTIFICATION, notification); + // We want this service to continue running until it is explicitly // stopped, so return sticky. return START_STICKY; @@ -68,8 +85,8 @@ public class TelemetryService extends Service { @Override public void onDestroy() { - // Cancel the persistent notification. - mNM.cancel(NOTIFICATION); + // Demote us from the foreground, and cancel the persistent notification. + stopForeground(true); // Tell the user we stopped. Toast.makeText(this, R.string.telemetry_service_stopped, Toast.LENGTH_SHORT).show(); @@ -84,26 +101,4 @@ public class TelemetryService extends Service { // RemoteService for a more complete example. private final IBinder mBinder = new TelemetryBinder(); - /** - * Show a notification while this service is running. - */ - private void showNotification() { - // In this sample, we'll use the same text for the ticker and the expanded notification - CharSequence text = getText(R.string.telemetry_service_started); - - // Set the icon, scrolling text and timestamp - Notification notification = new Notification(R.drawable.am_status, text, - System.currentTimeMillis()); - - // The PendingIntent to launch our activity if the user selects this notification - PendingIntent contentIntent = PendingIntent.getActivity(this, 0, - new Intent(this, TelemetryServiceActivities.Controller.class), 0); - - // Set the info for the views that show in the notification panel. - notification.setLatestEventInfo(this, getText(R.string.telemetry_service_label), - text, contentIntent); - - // Send the notification. - mNM.notify(NOTIFICATION, notification); - } } -- cgit v1.2.3 From 0f3597389977f86a8c1bdff1b7f46107c43ef306 Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Sun, 26 Aug 2012 12:41:24 +1200 Subject: altosdroid: Update copyrights Signed-off-by: Mike Beattie --- altosdroid/AndroidManifest.xml | 28 ++++++++++++---------- .../org/altusmetrum/AltosDroid/AltosBluetooth.java | 1 + .../src/org/altusmetrum/AltosDroid/AltosDroid.java | 21 ++++++++-------- .../altusmetrum/AltosDroid/TelemetryService.java | 21 ++++++++-------- 4 files changed, 38 insertions(+), 33 deletions(-) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/AndroidManifest.xml b/altosdroid/AndroidManifest.xml index 96fe5ac7..e976555e 100644 --- a/altosdroid/AndroidManifest.xml +++ b/altosdroid/AndroidManifest.xml @@ -1,17 +1,19 @@ - + * Copyright © 2012 Mike Beattie * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java index b4725e22..37249fe6 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java @@ -1,17 +1,18 @@ /* - * Copyright (C) 2009 The Android Open Source Project + * Copyright © 2012 Mike Beattie * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. * - * http://www.apache.org/licenses/LICENSE-2.0 + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ package org.altusmetrum.AltosDroid; diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index c0a32c92..7bd9abc3 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -1,17 +1,18 @@ /* - * Copyright (C) 2007 The Android Open Source Project + * Copyright © 2012 Mike Beattie * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. * - * http://www.apache.org/licenses/LICENSE-2.0 + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ package org.altusmetrum.AltosDroid; -- cgit v1.2.3 From 7aab6e6b6e361455a7515fe6db7b0e9a6e4c786c Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Sun, 26 Aug 2012 14:29:36 +1200 Subject: altosdroid: whitespace (spaces to tabs) (part1) Signed-off-by: Mike Beattie --- .../src/org/altusmetrum/AltosDroid/AltosDroid.java | 260 +++++++++-------- .../altusmetrum/AltosDroid/DeviceListActivity.java | 308 ++++++++++----------- .../altusmetrum/AltosDroid/TelemetryService.java | 69 ++--- 3 files changed, 318 insertions(+), 319 deletions(-) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java index 37249fe6..17084863 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java @@ -45,9 +45,9 @@ import org.altusmetrum.AltosLib.*; * This is the main Activity that displays the current chat session. */ public class AltosDroid extends Activity { - // Debugging - private static final String TAG = "AltosDroid"; - private static final boolean D = true; + // Debugging + private static final String TAG = "AltosDroid"; + private static final boolean D = true; // Message types sent from the BluetoothChatService Handler public static final int MESSAGE_STATE_CHANGE = 1; @@ -60,76 +60,72 @@ public class AltosDroid extends Activity { public static final String DEVICE_NAME = "device_name"; public static final String TOAST = "toast"; - // Intent request codes - private static final int REQUEST_CONNECT_DEVICE = 1; - private static final int REQUEST_ENABLE_BT = 2; - // Layout Views - private TextView mTitle; - private TextView mSerialView; private EditText mOutEditText; private Button mSendButton; - // Name of the connected device - private String mConnectedDeviceName = null; // String buffer for outgoing messages private StringBuffer mOutStringBuffer; - // Local Bluetooth adapter - private BluetoothAdapter mBluetoothAdapter = null; // Member object for the chat services private BluetoothChatService mChatService = null; - + // Intent request codes + private static final int REQUEST_CONNECT_DEVICE = 1; + private static final int REQUEST_ENABLE_BT = 2; + + // Layout Views + private TextView mTitle; + private TextView mSerialView; + // Name of the connected device + private String mConnectedDeviceName = null; + // Local Bluetooth adapter + private BluetoothAdapter mBluetoothAdapter = null; @Override public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - if(D) Log.e(TAG, "+++ ON CREATE +++"); - - // Set up the window layout - requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); - setContentView(R.layout.main); - getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title); + super.onCreate(savedInstanceState); + if(D) Log.e(TAG, "+++ ON CREATE +++"); - // Set up the custom title - mTitle = (TextView) findViewById(R.id.title_left_text); - mTitle.setText(R.string.app_name); - mTitle = (TextView) findViewById(R.id.title_right_text); + // Set up the window layout + requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); + setContentView(R.layout.main); + getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title); // Get local Bluetooth adapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); + // Set up the custom title + mTitle = (TextView) findViewById(R.id.title_left_text); + mTitle.setText(R.string.app_name); + mTitle = (TextView) findViewById(R.id.title_right_text); // If the adapter is null, then Bluetooth is not supported - if (mBluetoothAdapter == null) { - Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show(); - finish(); - return; - } + if (mBluetoothAdapter == null) { + Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show(); + finish(); + return; + } } @Override public void onStart() { - super.onStart(); - if(D) Log.e(TAG, "++ ON START ++"); - - // If BT is not on, request that it be enabled. - // setupChat() will then be called during onActivityResult - if (!mBluetoothAdapter.isEnabled()) { - Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); - startActivityForResult(enableIntent, REQUEST_ENABLE_BT); - // Otherwise, setup the chat session - } else { + super.onStart(); + if(D) Log.e(TAG, "++ ON START ++"); + + if (!mBluetoothAdapter.isEnabled()) { + Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); + startActivityForResult(enableIntent, REQUEST_ENABLE_BT); + } else { if (mChatService == null) setupChat(); - } + } } @Override public synchronized void onResume() { - super.onResume(); - if(D) Log.e(TAG, "+ ON RESUME +"); + super.onResume(); + if(D) Log.e(TAG, "+ ON RESUME +"); - // Performing this check in onResume() covers the case in which BT was - // not enabled during onStart(), so we were paused to enable it... - // onResume() will be called when ACTION_REQUEST_ENABLE activity returns. + // Performing this check in onResume() covers the case in which BT was + // not enabled during onStart(), so we were paused to enable it... + // onResume() will be called when ACTION_REQUEST_ENABLE activity returns. if (mChatService != null) { // Only if the state is STATE_NONE, do we know that we haven't started already if (mChatService.getState() == BluetoothChatService.STATE_NONE) { @@ -141,89 +137,91 @@ public class AltosDroid extends Activity { @Override public synchronized void onPause() { - super.onPause(); - if(D) Log.e(TAG, "- ON PAUSE -"); + super.onPause(); + if(D) Log.e(TAG, "- ON PAUSE -"); } @Override public void onStop() { - super.onStop(); - if(D) Log.e(TAG, "-- ON STOP --"); + super.onStop(); + if(D) Log.e(TAG, "-- ON STOP --"); } @Override public void onDestroy() { - super.onDestroy(); + super.onDestroy(); // Stop the Bluetooth chat services if (mChatService != null) mChatService.stop(); - if(D) Log.e(TAG, "--- ON DESTROY ---"); + if(D) Log.e(TAG, "--- ON DESTROY ---"); } private void setupChat() { - Log.d(TAG, "setupChat()"); + Log.d(TAG, "setupChat()"); mSerialView = (TextView) findViewById(R.id.in); mSerialView.setMovementMethod(new ScrollingMovementMethod()); mSerialView.setClickable(false); mSerialView.setLongClickable(false); - // Initialize the compose field with a listener for the return key - mOutEditText = (EditText) findViewById(R.id.edit_text_out); - mOutEditText.setOnEditorActionListener(mWriteListener); + // Initialize the compose field with a listener for the return key + mOutEditText = (EditText) findViewById(R.id.edit_text_out); + mOutEditText.setOnEditorActionListener(mWriteListener); - // Initialize the send button with a listener that for click events - mSendButton = (Button) findViewById(R.id.button_send); - mSendButton.setOnClickListener(new OnClickListener() { - public void onClick(View v) { + // Initialize the send button with a listener that for click events + mSendButton = (Button) findViewById(R.id.button_send); + mSendButton.setOnClickListener(new OnClickListener() { + public void onClick(View v) { // Send a message using content of the edit text widget - TextView view = (TextView) findViewById(R.id.edit_text_out); - String message = view.getText().toString(); + TextView view = (TextView) findViewById(R.id.edit_text_out); + String message = view.getText().toString(); sendMessage(message); - } - }); + } + }); - // Initialize the BluetoothChatService to perform bluetooth connections - mChatService = new BluetoothChatService(this, mHandler); + // Initialize the BluetoothChatService to perform bluetooth connections + mChatService = new BluetoothChatService(this, mHandler); - // Initialize the buffer for outgoing messages - mOutStringBuffer = new StringBuffer(""); + // Initialize the buffer for outgoing messages + mOutStringBuffer = new StringBuffer(""); } /** - * Sends a message. - * @param message A string of text to send. - */ + * Sends a message. + * @param message A string of text to send. + */ + /* private void sendMessage(String message) { - // Check that we're actually connected before trying anything - if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) { - Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show(); - return; - } + // Check that we're actually connected before trying anything + if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) { + Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show(); + return; + } - // Check that there's actually something to send - if (message.length() > 0) { - // Get the message bytes and tell the BluetoothChatService to write - byte[] send = message.getBytes(); - mChatService.write(send); + // Check that there's actually something to send + if (message.length() > 0) { + // Get the message bytes and tell the BluetoothChatService to write + byte[] send = message.getBytes(); + mChatService.write(send); + + // Reset out string buffer to zero and clear the edit text field + mOutStringBuffer.setLength(0); + mOutEditText.setText(mOutStringBuffer); + } + } - // Reset out string buffer to zero and clear the edit text field - mOutStringBuffer.setLength(0); - mOutEditText.setText(mOutStringBuffer); - } - } // The action listener for the EditText widget, to listen for the return key private TextView.OnEditorActionListener mWriteListener = - new TextView.OnEditorActionListener() { - public boolean onEditorAction(TextView view, int actionId, KeyEvent event) { - // If the action is a key-up event on the return key, send the message - if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_UP) { - String message = view.getText().toString(); - sendMessage(message); - } - if(D) Log.i(TAG, "END onEditorAction"); + new TextView.OnEditorActionListener() { + public boolean onEditorAction(TextView view, int actionId, KeyEvent event) { + // If the action is a key-up event on the return key, send the message + if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_UP) { + String message = view.getText().toString(); + sendMessage(message); + } + if(D) Log.i(TAG, "END onEditorAction"); return true; } }; @@ -277,49 +275,49 @@ public class AltosDroid extends Activity { }; public void onActivityResult(int requestCode, int resultCode, Intent data) { - if(D) Log.d(TAG, "onActivityResult " + resultCode); - switch (requestCode) { - case REQUEST_CONNECT_DEVICE: - // When DeviceListActivity returns with a device to connect to - if (resultCode == Activity.RESULT_OK) { - connectDevice(data); - } - break; - case REQUEST_ENABLE_BT: - // When the request to enable Bluetooth returns - if (resultCode == Activity.RESULT_OK) { - // Bluetooth is now enabled, so set up a chat session + if(D) Log.d(TAG, "onActivityResult " + resultCode); + switch (requestCode) { + case REQUEST_CONNECT_DEVICE: + // When DeviceListActivity returns with a device to connect to + if (resultCode == Activity.RESULT_OK) { + connectDevice(data); + } + break; + case REQUEST_ENABLE_BT: + // When the request to enable Bluetooth returns + if (resultCode == Activity.RESULT_OK) { + // Bluetooth is now enabled, so set up a chat session setupChat(); - } else { - // User did not enable Bluetooth or an error occured - Log.d(TAG, "BT not enabled"); - Toast.makeText(this, R.string.bt_not_enabled_leaving, Toast.LENGTH_SHORT).show(); - finish(); - } - } + } else { + // User did not enable Bluetooth or an error occured + Log.d(TAG, "BT not enabled"); + stopService(new Intent(AltosDroid.this, TelemetryService.class)); + Toast.makeText(this, R.string.bt_not_enabled_leaving, Toast.LENGTH_SHORT).show(); + finish(); + } + } } private void connectDevice(Intent data) { - // Get the device MAC address - String address = data.getExtras() - .getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS); - // Get the BLuetoothDevice object - BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); - // Attempt to connect to the device + // Get the device MAC address + String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS); + // Get the BLuetoothDevice object + BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); + // Attempt to connect to the device mChatService.connect(device); } @Override public boolean onCreateOptionsMenu(Menu menu) { - MenuInflater inflater = getMenuInflater(); - inflater.inflate(R.menu.option_menu, menu); - return true; + MenuInflater inflater = getMenuInflater(); + inflater.inflate(R.menu.option_menu, menu); + return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { - Intent serverIntent = null; - switch (item.getItemId()) { + Intent serverIntent = null; + switch (item.getItemId()) { case R.id.telemetry_service_control: serverIntent = new Intent(this, TelemetryServiceActivities.Controller.class); startActivity(serverIntent); @@ -328,13 +326,13 @@ public class AltosDroid extends Activity { serverIntent = new Intent(this, TelemetryServiceActivities.Binding.class); startActivity(serverIntent); return true; - case R.id.connect_scan: - // Launch the DeviceListActivity to see devices and do scan - serverIntent = new Intent(this, DeviceListActivity.class); - startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE); - return true; - } - return false; + case R.id.connect_scan: + // Launch the DeviceListActivity to see devices and do scan + serverIntent = new Intent(this, DeviceListActivity.class); + startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE); + return true; + } + return false; } } diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/DeviceListActivity.java b/altosdroid/src/org/altusmetrum/AltosDroid/DeviceListActivity.java index af5d7f15..7b9cbde7 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/DeviceListActivity.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/DeviceListActivity.java @@ -45,159 +45,159 @@ import android.widget.AdapterView.OnItemClickListener; * Activity in the result Intent. */ public class DeviceListActivity extends Activity { - // Debugging - private static final String TAG = "DeviceListActivity"; - private static final boolean D = true; - - // Return Intent extra - public static String EXTRA_DEVICE_ADDRESS = "device_address"; - - // Member fields - private BluetoothAdapter mBtAdapter; - private ArrayAdapter mPairedDevicesArrayAdapter; - private ArrayAdapter mNewDevicesArrayAdapter; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - // Setup the window - requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); - setContentView(R.layout.device_list); - - // Set result CANCELED incase the user backs out - setResult(Activity.RESULT_CANCELED); - - // Initialize the button to perform device discovery - Button scanButton = (Button) findViewById(R.id.button_scan); - scanButton.setOnClickListener(new OnClickListener() { - public void onClick(View v) { - doDiscovery(); - v.setVisibility(View.GONE); - } - }); - - // Initialize array adapters. One for already paired devices and - // one for newly discovered devices - mPairedDevicesArrayAdapter = new ArrayAdapter(this, R.layout.device_name); - mNewDevicesArrayAdapter = new ArrayAdapter(this, R.layout.device_name); - - // Find and set up the ListView for paired devices - ListView pairedListView = (ListView) findViewById(R.id.paired_devices); - pairedListView.setAdapter(mPairedDevicesArrayAdapter); - pairedListView.setOnItemClickListener(mDeviceClickListener); - - // Find and set up the ListView for newly discovered devices - ListView newDevicesListView = (ListView) findViewById(R.id.new_devices); - newDevicesListView.setAdapter(mNewDevicesArrayAdapter); - newDevicesListView.setOnItemClickListener(mDeviceClickListener); - - // Register for broadcasts when a device is discovered - IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); - this.registerReceiver(mReceiver, filter); - - // Register for broadcasts when discovery has finished - filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); - this.registerReceiver(mReceiver, filter); - - // Get the local Bluetooth adapter - mBtAdapter = BluetoothAdapter.getDefaultAdapter(); - - // Get a set of currently paired devices - Set pairedDevices = mBtAdapter.getBondedDevices(); - - // If there are paired devices, add each one to the ArrayAdapter - if (pairedDevices.size() > 0) { - findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE); - for (BluetoothDevice device : pairedDevices) { - mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress()); - } - } else { - String noDevices = getResources().getText(R.string.none_paired).toString(); - mPairedDevicesArrayAdapter.add(noDevices); - } - } - - @Override - protected void onDestroy() { - super.onDestroy(); - - // Make sure we're not doing discovery anymore - if (mBtAdapter != null) { - mBtAdapter.cancelDiscovery(); - } - - // Unregister broadcast listeners - this.unregisterReceiver(mReceiver); - } - - /** - * Start device discover with the BluetoothAdapter - */ - private void doDiscovery() { - if (D) Log.d(TAG, "doDiscovery()"); - - // Indicate scanning in the title - setProgressBarIndeterminateVisibility(true); - setTitle(R.string.scanning); - - // Turn on sub-title for new devices - findViewById(R.id.title_new_devices).setVisibility(View.VISIBLE); - - // If we're already discovering, stop it - if (mBtAdapter.isDiscovering()) { - mBtAdapter.cancelDiscovery(); - } - - // Request discover from BluetoothAdapter - mBtAdapter.startDiscovery(); - } - - // The on-click listener for all devices in the ListViews - private OnItemClickListener mDeviceClickListener = new OnItemClickListener() { - public void onItemClick(AdapterView av, View v, int arg2, long arg3) { - // Cancel discovery because it's costly and we're about to connect - mBtAdapter.cancelDiscovery(); - - // Get the device MAC address, which is the last 17 chars in the View - String info = ((TextView) v).getText().toString(); - String address = info.substring(info.length() - 17); - - // Create the result Intent and include the MAC address - Intent intent = new Intent(); - intent.putExtra(EXTRA_DEVICE_ADDRESS, address); - - // Set result and finish this Activity - setResult(Activity.RESULT_OK, intent); - finish(); - } - }; - - // The BroadcastReceiver that listens for discovered devices and - // changes the title when discovery is finished - private final BroadcastReceiver mReceiver = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - String action = intent.getAction(); - - // When discovery finds a device - if (BluetoothDevice.ACTION_FOUND.equals(action)) { - // Get the BluetoothDevice object from the Intent - BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); - // If it's already paired, skip it, because it's been listed already - if (device.getBondState() != BluetoothDevice.BOND_BONDED) { - mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress()); - } - // When discovery is finished, change the Activity title - } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { - setProgressBarIndeterminateVisibility(false); - setTitle(R.string.select_device); - if (mNewDevicesArrayAdapter.getCount() == 0) { - String noDevices = getResources().getText(R.string.none_found).toString(); - mNewDevicesArrayAdapter.add(noDevices); - } - } - } - }; + // Debugging + private static final String TAG = "DeviceListActivity"; + private static final boolean D = true; + + // Return Intent extra + public static String EXTRA_DEVICE_ADDRESS = "device_address"; + + // Member fields + private BluetoothAdapter mBtAdapter; + private ArrayAdapter mPairedDevicesArrayAdapter; + private ArrayAdapter mNewDevicesArrayAdapter; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + // Setup the window + requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); + setContentView(R.layout.device_list); + + // Set result CANCELED incase the user backs out + setResult(Activity.RESULT_CANCELED); + + // Initialize the button to perform device discovery + Button scanButton = (Button) findViewById(R.id.button_scan); + scanButton.setOnClickListener(new OnClickListener() { + public void onClick(View v) { + doDiscovery(); + v.setVisibility(View.GONE); + } + }); + + // Initialize array adapters. One for already paired devices and + // one for newly discovered devices + mPairedDevicesArrayAdapter = new ArrayAdapter(this, R.layout.device_name); + mNewDevicesArrayAdapter = new ArrayAdapter(this, R.layout.device_name); + + // Find and set up the ListView for paired devices + ListView pairedListView = (ListView) findViewById(R.id.paired_devices); + pairedListView.setAdapter(mPairedDevicesArrayAdapter); + pairedListView.setOnItemClickListener(mDeviceClickListener); + + // Find and set up the ListView for newly discovered devices + ListView newDevicesListView = (ListView) findViewById(R.id.new_devices); + newDevicesListView.setAdapter(mNewDevicesArrayAdapter); + newDevicesListView.setOnItemClickListener(mDeviceClickListener); + + // Register for broadcasts when a device is discovered + IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); + this.registerReceiver(mReceiver, filter); + + // Register for broadcasts when discovery has finished + filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); + this.registerReceiver(mReceiver, filter); + + // Get the local Bluetooth adapter + mBtAdapter = BluetoothAdapter.getDefaultAdapter(); + + // Get a set of currently paired devices + Set pairedDevices = mBtAdapter.getBondedDevices(); + + // If there are paired devices, add each one to the ArrayAdapter + if (pairedDevices.size() > 0) { + findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE); + for (BluetoothDevice device : pairedDevices) { + mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress()); + } + } else { + String noDevices = getResources().getText(R.string.none_paired).toString(); + mPairedDevicesArrayAdapter.add(noDevices); + } + } + + @Override + protected void onDestroy() { + super.onDestroy(); + + // Make sure we're not doing discovery anymore + if (mBtAdapter != null) { + mBtAdapter.cancelDiscovery(); + } + + // Unregister broadcast listeners + this.unregisterReceiver(mReceiver); + } + + /** + * Start device discover with the BluetoothAdapter + */ + private void doDiscovery() { + if (D) Log.d(TAG, "doDiscovery()"); + + // Indicate scanning in the title + setProgressBarIndeterminateVisibility(true); + setTitle(R.string.scanning); + + // Turn on sub-title for new devices + findViewById(R.id.title_new_devices).setVisibility(View.VISIBLE); + + // If we're already discovering, stop it + if (mBtAdapter.isDiscovering()) { + mBtAdapter.cancelDiscovery(); + } + + // Request discover from BluetoothAdapter + mBtAdapter.startDiscovery(); + } + + // The on-click listener for all devices in the ListViews + private OnItemClickListener mDeviceClickListener = new OnItemClickListener() { + public void onItemClick(AdapterView av, View v, int arg2, long arg3) { + // Cancel discovery because it's costly and we're about to connect + mBtAdapter.cancelDiscovery(); + + // Get the device MAC address, which is the last 17 chars in the View + String info = ((TextView) v).getText().toString(); + String address = info.substring(info.length() - 17); + + // Create the result Intent and include the MAC address + Intent intent = new Intent(); + intent.putExtra(EXTRA_DEVICE_ADDRESS, address); + + // Set result and finish this Activity + setResult(Activity.RESULT_OK, intent); + finish(); + } + }; + + // The BroadcastReceiver that listens for discovered devices and + // changes the title when discovery is finished + private final BroadcastReceiver mReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + + // When discovery finds a device + if (BluetoothDevice.ACTION_FOUND.equals(action)) { + // Get the BluetoothDevice object from the Intent + BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); + // If it's already paired, skip it, because it's been listed already + if (device.getBondState() != BluetoothDevice.BOND_BONDED) { + mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress()); + } + // When discovery is finished, change the Activity title + } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { + setProgressBarIndeterminateVisibility(false); + setTitle(R.string.select_device); + if (mNewDevicesArrayAdapter.getCount() == 0) { + String noDevices = getResources().getText(R.string.none_found).toString(); + mNewDevicesArrayAdapter.add(noDevices); + } + } + } + }; } diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index 7bd9abc3..a4e85ad0 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -34,11 +34,6 @@ import org.altusmetrum.AltosDroid.R; public class TelemetryService extends Service { - private NotificationManager mNM; - - // Unique Identification Number for the Notification. - // We use it on Notification start, and to cancel it. - private int NOTIFICATION = R.string.telemetry_service_label; /** * Class for clients to access. Because we know this service always @@ -50,53 +45,59 @@ public class TelemetryService extends Service { return TelemetryService.this; } } + // Unique Identification Number for the Notification. + // We use it on Notification start, and to cancel it. + private int NOTIFICATION = R.string.telemetry_service_label; + private NotificationManager mNM; - @Override + @Override public void onCreate() { - // Create a reference to the NotificationManager so that we can update our notifcation text later - mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); - } + // Create a reference to the NotificationManager so that we can update our notifcation text later + mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); + } - @Override + @Override public int onStartCommand(Intent intent, int flags, int startId) { - Log.i("TelemetryService", "Received start id " + startId + ": " + intent); + Log.i("TelemetryService", "Received start id " + startId + ": " + intent); - CharSequence text = getText(R.string.telemetry_service_started); + CharSequence text = getText(R.string.telemetry_service_started); - // Create notification to be displayed while the service runs - Notification notification = new Notification(R.drawable.am_status_c, text, 0); + // Create notification to be displayed while the service runs + Notification notification = new Notification(R.drawable.am_status_c, text, 0); - // The PendingIntent to launch our activity if the user selects this notification - PendingIntent contentIntent = PendingIntent.getActivity(this, 0, + // The PendingIntent to launch our activity if the user selects this notification + PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, TelemetryServiceActivities.Controller.class), 0); - // Set the info for the views that show in the notification panel. - notification.setLatestEventInfo(this, getText(R.string.telemetry_service_label), text, contentIntent); + // Set the info for the views that show in the notification panel. + notification.setLatestEventInfo(this, getText(R.string.telemetry_service_label), text, contentIntent); - // Set the notification to be in the "Ongoing" section. - notification.flags |= Notification.FLAG_ONGOING_EVENT; + // Set the notification to be in the "Ongoing" section. + notification.flags |= Notification.FLAG_ONGOING_EVENT; - // Move us into the foreground. - startForeground(NOTIFICATION, notification); + // Move us into the foreground. + startForeground(NOTIFICATION, notification); - // We want this service to continue running until it is explicitly - // stopped, so return sticky. - return START_STICKY; - } + // We want this service to continue running until it is explicitly + // stopped, so return sticky. + return START_STICKY; + } - @Override + @Override public void onDestroy() { - // Demote us from the foreground, and cancel the persistent notification. - stopForeground(true); - // Tell the user we stopped. - Toast.makeText(this, R.string.telemetry_service_stopped, Toast.LENGTH_SHORT).show(); - } - @Override + // Demote us from the foreground, and cancel the persistent notification. + stopForeground(true); + + // Tell the user we stopped. + Toast.makeText(this, R.string.telemetry_service_stopped, Toast.LENGTH_SHORT).show(); + } + + @Override public IBinder onBind(Intent intent) { return mBinder; - } + } // This is the object that receives interactions from clients. See // RemoteService for a more complete example. -- cgit v1.2.3 From 24503eb330bf887f5c76afe2aaa9c9f2ce177460 Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Sun, 26 Aug 2012 14:44:09 +1200 Subject: altosdroid: whitespace (spaces to tabs) (part2) Signed-off-by: Mike Beattie --- .../src/org/altusmetrum/AltosDroid/AltosDroid.java | 83 +++++++++++----------- .../altusmetrum/AltosDroid/TelemetryService.java | 8 +-- 2 files changed, 47 insertions(+), 44 deletions(-) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java index 17084863..29d72d95 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java @@ -80,8 +80,8 @@ public class AltosDroid extends Activity { // Local Bluetooth adapter private BluetoothAdapter mBluetoothAdapter = null; - @Override - public void onCreate(Bundle savedInstanceState) { + @Override + public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if(D) Log.e(TAG, "+++ ON CREATE +++"); @@ -97,16 +97,17 @@ public class AltosDroid extends Activity { mTitle.setText(R.string.app_name); mTitle = (TextView) findViewById(R.id.title_right_text); - // If the adapter is null, then Bluetooth is not supported + // If the adapter is null, then Bluetooth is not supported if (mBluetoothAdapter == null) { Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show(); finish(); return; } - } - @Override - public void onStart() { + } + + @Override + public void onStart() { super.onStart(); if(D) Log.e(TAG, "++ ON START ++"); @@ -116,10 +117,10 @@ public class AltosDroid extends Activity { } else { if (mChatService == null) setupChat(); } - } + } - @Override - public synchronized void onResume() { + @Override + public synchronized void onResume() { super.onResume(); if(D) Log.e(TAG, "+ ON RESUME +"); @@ -133,31 +134,31 @@ public class AltosDroid extends Activity { mChatService.start(); } } - } + } - @Override - public synchronized void onPause() { + @Override + public synchronized void onPause() { super.onPause(); if(D) Log.e(TAG, "- ON PAUSE -"); - } + } - @Override - public void onStop() { + @Override + public void onStop() { super.onStop(); if(D) Log.e(TAG, "-- ON STOP --"); - } + } - @Override - public void onDestroy() { + @Override + public void onDestroy() { super.onDestroy(); // Stop the Bluetooth chat services if (mChatService != null) mChatService.stop(); if(D) Log.e(TAG, "--- ON DESTROY ---"); - } + } - private void setupChat() { + private void setupChat() { Log.d(TAG, "setupChat()"); mSerialView = (TextView) findViewById(R.id.in); @@ -173,10 +174,10 @@ public class AltosDroid extends Activity { mSendButton = (Button) findViewById(R.id.button_send); mSendButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { - // Send a message using content of the edit text widget + // Send a message using content of the edit text widget TextView view = (TextView) findViewById(R.id.edit_text_out); String message = view.getText().toString(); - sendMessage(message); + sendMessage(message); } }); @@ -185,14 +186,14 @@ public class AltosDroid extends Activity { // Initialize the buffer for outgoing messages mOutStringBuffer = new StringBuffer(""); - } + } - /** + /** * Sends a message. * @param message A string of text to send. */ /* - private void sendMessage(String message) { + private void sendMessage(String message) { // Check that we're actually connected before trying anything if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) { Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show(); @@ -212,8 +213,8 @@ public class AltosDroid extends Activity { } - // The action listener for the EditText widget, to listen for the return key - private TextView.OnEditorActionListener mWriteListener = + // The action listener for the EditText widget, to listen for the return key + private TextView.OnEditorActionListener mWriteListener = new TextView.OnEditorActionListener() { public boolean onEditorAction(TextView view, int actionId, KeyEvent event) { // If the action is a key-up event on the return key, send the message @@ -222,9 +223,10 @@ public class AltosDroid extends Activity { sendMessage(message); } if(D) Log.i(TAG, "END onEditorAction"); - return true; - } - }; + return true; + } + }; + */ // The Handler that gets information back from the BluetoothChatService private final Handler mHandler = new Handler() { @@ -274,7 +276,7 @@ public class AltosDroid extends Activity { } }; - public void onActivityResult(int requestCode, int resultCode, Intent data) { + public void onActivityResult(int requestCode, int resultCode, Intent data) { if(D) Log.d(TAG, "onActivityResult " + resultCode); switch (requestCode) { case REQUEST_CONNECT_DEVICE: @@ -296,26 +298,27 @@ public class AltosDroid extends Activity { finish(); } } - } + } - private void connectDevice(Intent data) { + private void connectDevice(Intent data) { // Get the device MAC address String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS); // Get the BLuetoothDevice object BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); // Attempt to connect to the device mChatService.connect(device); - } + } + - @Override - public boolean onCreateOptionsMenu(Menu menu) { + @Override + public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.option_menu, menu); return true; - } + } - @Override - public boolean onOptionsItemSelected(MenuItem item) { + @Override + public boolean onOptionsItemSelected(MenuItem item) { Intent serverIntent = null; switch (item.getItemId()) { case R.id.telemetry_service_control: @@ -333,6 +336,6 @@ public class AltosDroid extends Activity { return true; } return false; - } + } } diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index a4e85ad0..a1c5fede 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -51,13 +51,13 @@ public class TelemetryService extends Service { private NotificationManager mNM; @Override - public void onCreate() { + public void onCreate() { // Create a reference to the NotificationManager so that we can update our notifcation text later mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); } @Override - public int onStartCommand(Intent intent, int flags, int startId) { + public int onStartCommand(Intent intent, int flags, int startId) { Log.i("TelemetryService", "Received start id " + startId + ": " + intent); CharSequence text = getText(R.string.telemetry_service_started); @@ -84,7 +84,7 @@ public class TelemetryService extends Service { } @Override - public void onDestroy() { + public void onDestroy() { // Demote us from the foreground, and cancel the persistent notification. @@ -95,7 +95,7 @@ public class TelemetryService extends Service { } @Override - public IBinder onBind(Intent intent) { + public IBinder onBind(Intent intent) { return mBinder; } -- cgit v1.2.3 From 372840b4ebfd3da3cd713b6bc6a8ffc8cd6b6b8c Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Sun, 26 Aug 2012 14:48:48 +1200 Subject: altosdroid: remove TelemetryService activities service start/stop will be handled by AltosDroid/itself now. Signed-off-by: Mike Beattie --- altosdroid/AndroidManifest.xml | 22 --- .../res/layout/telemetry_service_binding.xml | 42 ----- .../res/layout/telemetry_service_controller.xml | 42 ----- altosdroid/res/menu/option_menu.xml | 6 - altosdroid/res/values/strings.xml | 20 --- .../src/org/altusmetrum/AltosDroid/AltosDroid.java | 8 - .../altusmetrum/AltosDroid/TelemetryService.java | 2 +- .../AltosDroid/TelemetryServiceActivities.java | 171 --------------------- 8 files changed, 1 insertion(+), 312 deletions(-) delete mode 100644 altosdroid/res/layout/telemetry_service_binding.xml delete mode 100644 altosdroid/res/layout/telemetry_service_controller.xml delete mode 100644 altosdroid/src/org/altusmetrum/AltosDroid/TelemetryServiceActivities.java (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/AndroidManifest.xml b/altosdroid/AndroidManifest.xml index e976555e..12391759 100644 --- a/altosdroid/AndroidManifest.xml +++ b/altosdroid/AndroidManifest.xml @@ -38,30 +38,8 @@ android:configChanges="orientation|keyboardHidden" /> - - - - - - - - - diff --git a/altosdroid/res/layout/telemetry_service_binding.xml b/altosdroid/res/layout/telemetry_service_binding.xml deleted file mode 100644 index 950d0d3a..00000000 --- a/altosdroid/res/layout/telemetry_service_binding.xml +++ /dev/null @@ -1,42 +0,0 @@ - - - - - - - - - - - - - - - diff --git a/altosdroid/res/layout/telemetry_service_controller.xml b/altosdroid/res/layout/telemetry_service_controller.xml deleted file mode 100644 index 189d2f6c..00000000 --- a/altosdroid/res/layout/telemetry_service_controller.xml +++ /dev/null @@ -1,42 +0,0 @@ - - - - - - - - - - - - - - - diff --git a/altosdroid/res/menu/option_menu.xml b/altosdroid/res/menu/option_menu.xml index feb5668e..6946e298 100644 --- a/altosdroid/res/menu/option_menu.xml +++ b/altosdroid/res/menu/option_menu.xml @@ -17,10 +17,4 @@ - - diff --git a/altosdroid/res/values/strings.xml b/altosdroid/res/values/strings.xml index 72a4ddec..e3234fc7 100644 --- a/altosdroid/res/values/strings.xml +++ b/altosdroid/res/values/strings.xml @@ -36,31 +36,11 @@ Connect a device - Control Service - (Un)Bind Service - - AltosDroid Telemetry Service Telemetry Service Started Telemetry Service Stopped - - Telemetry Service Controller - Use the following buttons to start and stop the Telemetry - service. - Start Service - Stop Service - - Telemetry Service Binding - This demonstrates how you can connect with a persistent - service. Notice how it automatically starts for you, and play around with the - interaction between this and Local Service Controller. - Bind Service - Unbind Service - - Connected to local service - Disconnected from local service diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java index 29d72d95..92ba5587 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java @@ -321,14 +321,6 @@ public class AltosDroid extends Activity { public boolean onOptionsItemSelected(MenuItem item) { Intent serverIntent = null; switch (item.getItemId()) { - case R.id.telemetry_service_control: - serverIntent = new Intent(this, TelemetryServiceActivities.Controller.class); - startActivity(serverIntent); - return true; - case R.id.telemetry_service_bind: - serverIntent = new Intent(this, TelemetryServiceActivities.Binding.class); - startActivity(serverIntent); - return true; case R.id.connect_scan: // Launch the DeviceListActivity to see devices and do scan serverIntent = new Intent(this, DeviceListActivity.class); diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index a1c5fede..95b655c2 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -67,7 +67,7 @@ public class TelemetryService extends Service { // The PendingIntent to launch our activity if the user selects this notification PendingIntent contentIntent = PendingIntent.getActivity(this, 0, - new Intent(this, TelemetryServiceActivities.Controller.class), 0); + new Intent(this, AltosDroid.class), 0); // Set the info for the views that show in the notification panel. notification.setLatestEventInfo(this, getText(R.string.telemetry_service_label), text, contentIntent); diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryServiceActivities.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryServiceActivities.java deleted file mode 100644 index 5191cfa9..00000000 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryServiceActivities.java +++ /dev/null @@ -1,171 +0,0 @@ -/* - * Copyright (C) 2007 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.altusmetrum.AltosDroid; - -import org.altusmetrum.AltosDroid.R; - -import android.app.Activity; -import android.content.ComponentName; -import android.content.Context; -import android.content.Intent; -import android.content.ServiceConnection; -import android.os.Bundle; -import android.os.IBinder; -import android.view.View; -import android.view.View.OnClickListener; -import android.widget.Button; -import android.widget.Toast; - -public class TelemetryServiceActivities { - /** - *

Example of explicitly starting and stopping the local service. - * This demonstrates the implementation of a service that runs in the same - * process as the rest of the application, which is explicitly started and stopped - * as desired.

- * - *

Note that this is implemented as an inner class only keep the sample - * all together; typically this code would appear in some separate class. - */ - public static class Controller extends Activity { - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - setContentView(R.layout.telemetry_service_controller); - - // Watch for button clicks. - Button button = (Button)findViewById(R.id.start); - button.setOnClickListener(mStartListener); - button = (Button)findViewById(R.id.stop); - button.setOnClickListener(mStopListener); - } - - private OnClickListener mStartListener = new OnClickListener() { - public void onClick(View v) { - // Make sure the service is started. It will continue running - // until someone calls stopService(). The Intent we use to find - // the service explicitly specifies our service component, because - // we want it running in our own process and don't want other - // applications to replace it. - startService(new Intent(Controller.this, - TelemetryService.class)); - } - }; - - private OnClickListener mStopListener = new OnClickListener() { - public void onClick(View v) { - // Cancel a previous call to startService(). Note that the - // service will not actually stop at this point if there are - // still bound clients. - stopService(new Intent(Controller.this, - TelemetryService.class)); - } - }; - } - - // ---------------------------------------------------------------------- - - /** - * Example of binding and unbinding to the local service. - * This demonstrates the implementation of a service which the client will - * bind to, receiving an object through which it can communicate with the service.

- * - *

Note that this is implemented as an inner class only keep the sample - * all together; typically this code would appear in some separate class. - */ - public static class Binding extends Activity { - private boolean mIsBound; - - - private TelemetryService mBoundService; - - private ServiceConnection mConnection = new ServiceConnection() { - public void onServiceConnected(ComponentName className, IBinder service) { - // This is called when the connection with the service has been - // established, giving us the service object we can use to - // interact with the service. Because we have bound to a explicit - // service that we know is running in our own process, we can - // cast its IBinder to a concrete class and directly access it. - mBoundService = ((TelemetryService.TelemetryBinder)service).getService(); - - // Tell the user about this for our demo. - Toast.makeText(Binding.this, R.string.telemetry_service_connected, - Toast.LENGTH_SHORT).show(); - } - - public void onServiceDisconnected(ComponentName className) { - // This is called when the connection with the service has been - // unexpectedly disconnected -- that is, its process crashed. - // Because it is running in our same process, we should never - // see this happen. - mBoundService = null; - Toast.makeText(Binding.this, R.string.telemetry_service_disconnected, - Toast.LENGTH_SHORT).show(); - } - }; - - void doBindService() { - // Establish a connection with the service. We use an explicit - // class name because we want a specific service implementation that - // we know will be running in our own process (and thus won't be - // supporting component replacement by other applications). - bindService(new Intent(Binding.this, - TelemetryService.class), mConnection, Context.BIND_AUTO_CREATE); - mIsBound = true; - } - - void doUnbindService() { - if (mIsBound) { - // Detach our existing connection. - unbindService(mConnection); - mIsBound = false; - } - } - - @Override - protected void onDestroy() { - super.onDestroy(); - doUnbindService(); - } - - - private OnClickListener mBindListener = new OnClickListener() { - public void onClick(View v) { - doBindService(); - } - }; - - private OnClickListener mUnbindListener = new OnClickListener() { - public void onClick(View v) { - doUnbindService(); - } - }; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - setContentView(R.layout.telemetry_service_binding); - - // Watch for button clicks. - Button button = (Button)findViewById(R.id.bind); - button.setOnClickListener(mBindListener); - button = (Button)findViewById(R.id.unbind); - button.setOnClickListener(mUnbindListener); - } - } -} -- cgit v1.2.3 From c9689a3ef65ea9da5a7009834add789737ffb6a9 Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Sun, 26 Aug 2012 15:03:54 +1200 Subject: altosdroid: Clean up imports in TelemetryService * Begin adding AltosLib usage Signed-off-by: Mike Beattie --- .../src/org/altusmetrum/AltosDroid/TelemetryService.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index 95b655c2..7d0f7a70 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -29,12 +29,15 @@ import android.widget.Toast; // Need the following import to get access to the app resources, since this // class is in a sub-package. -import org.altusmetrum.AltosDroid.R; - +//import org.altusmetrum.AltosDroid.R; +import org.altusmetrum.AltosLib.*; public class TelemetryService extends Service { + private static final String TAG = "TelemetryService"; + private static final boolean D = true; + /** * Class for clients to access. Because we know this service always * runs in the same process as its clients, we don't need to deal with @@ -50,6 +53,13 @@ public class TelemetryService extends Service { private int NOTIFICATION = R.string.telemetry_service_label; private NotificationManager mNM; + + // Name of the connected device + private String mConnectedDeviceName = null; + private AltosBluetooth mAltosBluetooth = null; + + LinkedBlockingQueue telem; + @Override public void onCreate() { // Create a reference to the NotificationManager so that we can update our notifcation text later -- cgit v1.2.3 From a33333b97e810f50db36f345aab71a3200feccc3 Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Sun, 26 Aug 2012 15:24:19 +1200 Subject: altosdroid: remove old Binder from TelemetryService Signed-off-by: Mike Beattie --- .../src/org/altusmetrum/AltosDroid/TelemetryService.java | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index 7d0f7a70..15293b9e 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -22,7 +22,7 @@ import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Intent; -import android.os.Binder; +//import android.os.Binder; import android.os.IBinder; import android.util.Log; import android.widget.Toast; @@ -38,16 +38,6 @@ public class TelemetryService extends Service { private static final String TAG = "TelemetryService"; private static final boolean D = true; - /** - * Class for clients to access. Because we know this service always - * runs in the same process as its clients, we don't need to deal with - * IPC. - */ - public class TelemetryBinder extends Binder { - TelemetryService getService() { - return TelemetryService.this; - } - } // Unique Identification Number for the Notification. // We use it on Notification start, and to cancel it. private int NOTIFICATION = R.string.telemetry_service_label; @@ -106,11 +96,7 @@ public class TelemetryService extends Service { @Override public IBinder onBind(Intent intent) { - return mBinder; } - // This is the object that receives interactions from clients. See - // RemoteService for a more complete example. - private final IBinder mBinder = new TelemetryBinder(); } -- cgit v1.2.3 From 6ffcc82d8d18d3f05d4f5881e50dda298b43c114 Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Sun, 26 Aug 2012 15:28:09 +1200 Subject: altosdroid: begin adding IPC to TelemetryService * And add imports for LinkedBlockingQueue... oops! Signed-off-by: Mike Beattie --- .../altusmetrum/AltosDroid/TelemetryService.java | 53 ++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index 15293b9e..337aece1 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -17,13 +17,20 @@ package org.altusmetrum.AltosDroid; +import java.util.ArrayList; +import java.util.concurrent.LinkedBlockingQueue; + import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; +import android.bluetooth.BluetoothDevice; import android.content.Intent; //import android.os.Binder; import android.os.IBinder; +import android.os.Handler; +import android.os.Message; +import android.os.Messenger; import android.util.Log; import android.widget.Toast; @@ -38,11 +45,17 @@ public class TelemetryService extends Service { private static final String TAG = "TelemetryService"; private static final boolean D = true; + static final int MSG_REGISTER_CLIENT = 1; + static final int MSG_UNREGISTER_CLIENT = 2; + static final int MSG_CONNECT_TELEBT = 3; + // Unique Identification Number for the Notification. // We use it on Notification start, and to cancel it. private int NOTIFICATION = R.string.telemetry_service_label; private NotificationManager mNM; + ArrayList mClients = new ArrayList(); // Keeps track of all current registered clients. + final Messenger mMessenger = new Messenger(new IncomingHandler()); // Target we publish for clients to send messages to IncomingHandler. // Name of the connected device private String mConnectedDeviceName = null; @@ -50,6 +63,43 @@ public class TelemetryService extends Service { LinkedBlockingQueue telem; + // Handler of incoming messages from clients. + class IncomingHandler extends Handler { + @Override + public void handleMessage(Message msg) { + switch (msg.what) { + case MSG_REGISTER_CLIENT: + mClients.add(msg.replyTo); + if (D) Log.d(TAG, "Client bound to service"); + break; + case MSG_UNREGISTER_CLIENT: + 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); + break; + default: + super.handleMessage(msg); + } + } + } + + private void TeleBT_stop() { + if (mAltosBluetooth != null) { + mAltosBluetooth.close(); + mAltosBluetooth = null; + } + telem.clear(); + } + + private void TeleBT_start(BluetoothDevice d) { + mAltosBluetooth = new AltosBluetooth(d); + mAltosBluetooth.add_monitor(telem); + } + @Override public void onCreate() { // Create a reference to the NotificationManager so that we can update our notifcation text later @@ -86,6 +136,8 @@ public class TelemetryService extends Service { @Override public void onDestroy() { + // Stop the bluetooth Comms threads + TeleBT_stop(); // Demote us from the foreground, and cancel the persistent notification. stopForeground(true); @@ -96,6 +148,7 @@ public class TelemetryService extends Service { @Override public IBinder onBind(Intent intent) { + return mMessenger.getBinder(); } -- cgit v1.2.3 From 5f4c47389a3d0d10d659a2e00fc74a150b5fed88 Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Sun, 26 Aug 2012 15:28:36 +1200 Subject: altosdroid: Add State constants for future usage Signed-off-by: Mike Beattie --- altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index 337aece1..9059ca79 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -49,6 +49,11 @@ public class TelemetryService extends Service { static final int MSG_UNREGISTER_CLIENT = 2; static final int MSG_CONNECT_TELEBT = 3; + public static final int STATE_NONE = 0; + public static final int STATE_READY = 1; + public static final int STATE_CONNECTING = 2; + public static final int STATE_CONNECTED = 3; + // Unique Identification Number for the Notification. // We use it on Notification start, and to cancel it. private int NOTIFICATION = R.string.telemetry_service_label; -- cgit v1.2.3 From bf7def1a7b93867dfe16fe6499ee028747634c41 Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Sun, 26 Aug 2012 15:28:58 +1200 Subject: altosdroid: Remove Binder import from TelemetryService Signed-off-by: Mike Beattie --- altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java | 1 - 1 file changed, 1 deletion(-) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index 9059ca79..2ee7fe58 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -26,7 +26,6 @@ import android.app.PendingIntent; import android.app.Service; import android.bluetooth.BluetoothDevice; import android.content.Intent; -//import android.os.Binder; import android.os.IBinder; import android.os.Handler; import android.os.Message; -- cgit v1.2.3 From 54baecc208a40606e3242b2cbd5e66567053646f Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Sun, 26 Aug 2012 23:12:48 +1200 Subject: altosdroid: Convert handlers to use weakreferences * Also renamed bluetooth start/stop methods Signed-off-by: Mike Beattie --- .../src/org/altusmetrum/AltosDroid/AltosDroid.java | 27 +++++++++++++--------- .../altusmetrum/AltosDroid/TelemetryService.java | 20 +++++++++------- 2 files changed, 28 insertions(+), 19 deletions(-) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') 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); -- cgit v1.2.3 From 21359f600354e8ee840e839e61ef97d30f3586fc Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Sun, 26 Aug 2012 23:13:33 +1200 Subject: altosdroid: disable NotificationManager stuff for now Signed-off-by: Mike Beattie --- altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index 1c0e94b3..60500a2c 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -22,7 +22,7 @@ import java.util.ArrayList; import java.util.concurrent.LinkedBlockingQueue; import android.app.Notification; -import android.app.NotificationManager; +//import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.bluetooth.BluetoothDevice; @@ -57,7 +57,7 @@ public class TelemetryService extends Service { // Unique Identification Number for the Notification. // We use it on Notification start, and to cancel it. private int NOTIFICATION = R.string.telemetry_service_label; - private NotificationManager mNM; + //private NotificationManager mNM; ArrayList mClients = new ArrayList(); // Keeps track of all current registered clients. final Messenger mMessenger = new Messenger(new IncomingHandler()); // Target we publish for clients to send messages to IncomingHandler. @@ -111,7 +111,7 @@ public class TelemetryService extends Service { @Override public void onCreate() { // Create a reference to the NotificationManager so that we can update our notifcation text later - mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); + //mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); } @Override -- cgit v1.2.3 From a6373e84393312ed0fbf22285c704819c2011588 Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Sun, 26 Aug 2012 23:14:09 +1200 Subject: altosdroid: init telem blocking list.. oops! Signed-off-by: Mike Beattie --- altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java | 2 ++ 1 file changed, 2 insertions(+) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index 60500a2c..89c45d6c 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -112,6 +112,8 @@ public class TelemetryService extends Service { public void onCreate() { // Create a reference to the NotificationManager so that we can update our notifcation text later //mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); + + telem = new LinkedBlockingQueue(); } @Override -- cgit v1.2.3 From 2c5513c51b187ad26a59b193b401f38c35141d27 Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Sun, 26 Aug 2012 23:16:04 +1200 Subject: altosdroid: Rename Connect message, add connected message Signed-off-by: Mike Beattie --- altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index 89c45d6c..cf7ae6da 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -47,7 +47,8 @@ public class TelemetryService extends Service { static final int MSG_REGISTER_CLIENT = 1; static final int MSG_UNREGISTER_CLIENT = 2; - static final int MSG_CONNECT_TELEBT = 3; + static final int MSG_CONNECT = 3; + static final int MSG_CONNECTED = 4; public static final int STATE_NONE = 0; public static final int STATE_READY = 1; @@ -85,10 +86,13 @@ public class TelemetryService extends Service { s.mClients.remove(msg.replyTo); if (D) Log.d(TAG, "Client unbound from service"); break; - case MSG_CONNECT_TELEBT: + case MSG_CONNECT: if (D) Log.d(TAG, "Connect command received"); s.startAltosBluetooth((BluetoothDevice) msg.obj); break; + case MSG_CONNECTED: + if (D) Log.d(TAG, "Connected to device"); + break; default: super.handleMessage(msg); } -- cgit v1.2.3 From 215d78f06093bd8a8b08a85cae0f1f34aee2a6ec Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Sun, 26 Aug 2012 23:19:06 +1200 Subject: altosdroid: begin adding state support Signed-off-by: Mike Beattie --- .../src/org/altusmetrum/AltosDroid/TelemetryService.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index cf7ae6da..6a23dca3 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -66,7 +66,7 @@ public class TelemetryService extends Service { // Name of the connected device private String mConnectedDeviceName = null; private AltosBluetooth mAltosBluetooth = null; - + private int state = STATE_NONE; LinkedBlockingQueue telem; // Handler of incoming messages from clients. @@ -92,6 +92,7 @@ public class TelemetryService extends Service { break; case MSG_CONNECTED: if (D) Log.d(TAG, "Connected to device"); + s.setState(STATE_CONNECTED); break; default: super.handleMessage(msg); @@ -100,6 +101,7 @@ public class TelemetryService extends Service { } private void stopAltosBluetooth() { + setState(STATE_READY); if (mAltosBluetooth != null) { mAltosBluetooth.close(); mAltosBluetooth = null; @@ -110,6 +112,14 @@ public class TelemetryService extends Service { private void startAltosBluetooth(BluetoothDevice d) { mAltosBluetooth = new AltosBluetooth(d); mAltosBluetooth.add_monitor(telem); + setState(STATE_CONNECTING); + } + + private synchronized void setState(int s) { + if (D) Log.d(TAG, "setState() " + state + " -> " + s); + state = s; + + sendMessageToClients(Message.obtain(null, AltosDroid.MSG_STATE_CHANGE, state, -1)); } @Override @@ -118,6 +128,7 @@ public class TelemetryService extends Service { //mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); telem = new LinkedBlockingQueue(); + setState(STATE_READY); } @Override -- cgit v1.2.3 From a9ec3c96288b7ea4e40586321a0a98edf0c8fee5 Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Sun, 26 Aug 2012 23:23:33 +1200 Subject: altosdroid: Need access to handler inside AltosBluetooth * Also move add_monitor() call Signed-off-by: Mike Beattie --- altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java | 6 +++++- altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java | 7 ++++--- 2 files changed, 9 insertions(+), 4 deletions(-) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java index c16e3cf5..3bfa3488 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java @@ -26,6 +26,7 @@ import java.util.UUID; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; +import android.os.Handler; import android.util.Log; import org.altusmetrum.AltosLib.*; @@ -39,6 +40,8 @@ public class AltosBluetooth extends AltosLink { private ConnectThread connect_thread = null; private Thread input_thread = null; + private Handler handler; + private BluetoothAdapter adapter; private BluetoothDevice device; private BluetoothSocket socket; @@ -46,9 +49,10 @@ public class AltosBluetooth extends AltosLink { private OutputStream output; // Constructor - public AltosBluetooth(BluetoothDevice in_device) { + public AltosBluetooth(BluetoothDevice in_device, Handler in_handler) { adapter = BluetoothAdapter.getDefaultAdapter(); device = in_device; + handler = in_handler; connect_thread = new ConnectThread(device); connect_thread.start(); diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index 6a23dca3..1903cc1d 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -61,7 +61,8 @@ public class TelemetryService extends Service { //private NotificationManager mNM; ArrayList mClients = new ArrayList(); // Keeps track of all current registered clients. - final Messenger mMessenger = new Messenger(new IncomingHandler()); // Target we publish for clients to send messages to IncomingHandler. + final Handler mHandler = new IncomingHandler(this); + final Messenger mMessenger = new Messenger(mHandler); // Target we publish for clients to send messages to IncomingHandler. // Name of the connected device private String mConnectedDeviceName = null; @@ -93,6 +94,7 @@ public class TelemetryService extends Service { case MSG_CONNECTED: if (D) Log.d(TAG, "Connected to device"); s.setState(STATE_CONNECTED); + s.mAltosBluetooth.add_monitor(s.telem); break; default: super.handleMessage(msg); @@ -110,8 +112,7 @@ public class TelemetryService extends Service { } private void startAltosBluetooth(BluetoothDevice d) { - mAltosBluetooth = new AltosBluetooth(d); - mAltosBluetooth.add_monitor(telem); + mAltosBluetooth = new AltosBluetooth(d, mHandler); setState(STATE_CONNECTING); } -- cgit v1.2.3 From 31bffa435cec2098c7ab5c42c829ba6e1578b5d2 Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Sun, 26 Aug 2012 23:24:13 +1200 Subject: altosdroid: need sendMessageToClients() for setState().. oops! Signed-off-by: Mike Beattie --- .../src/org/altusmetrum/AltosDroid/TelemetryService.java | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index 1903cc1d..9c2fde97 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -102,6 +102,16 @@ public class TelemetryService extends Service { } } + private void sendMessageToClients(Message m) { + for (int i=mClients.size()-1; i>=0; i--) { + try { + mClients.get(i).send(m); + } catch (RemoteException e) { + mClients.remove(i); + } + } + } + private void stopAltosBluetooth() { setState(STATE_READY); if (mAltosBluetooth != null) { -- cgit v1.2.3 From 3d6fc5fe462531e05ca4b9be1a421490e067a28b Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Sun, 26 Aug 2012 23:26:58 +1200 Subject: altosdroid: lots of debugging statements Signed-off-by: Mike Beattie --- .../org/altusmetrum/AltosDroid/AltosBluetooth.java | 22 +++++++++++++++++----- .../altusmetrum/AltosDroid/TelemetryService.java | 3 +++ 2 files changed, 20 insertions(+), 5 deletions(-) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java index 3bfa3488..3071c8f1 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java @@ -76,7 +76,7 @@ public class AltosBluetooth extends AltosLink { } public void run() { - if (D) Log.i(TAG, "BEGIN ConnectThread"); + if (D) Log.i(TAG, "ConnectThread: BEGIN"); setName("ConnectThread"); // Always cancel discovery because it will slow down a connection @@ -123,28 +123,31 @@ public class AltosBluetooth extends AltosLink { if (socket != null) socket.close(); } catch (IOException e) { - if (D) Log.e(TAG, "close() of connect socket failed", e); + if (D) Log.e(TAG, "ConnectThread: close() of connect socket failed", e); } } } private synchronized void wait_connected() throws InterruptedException { + if (D) Log.i(TAG, "wait_connected(): begin"); if (input == null) { + if (D) Log.i(TAG, "wait_connected(): waiting"); wait(); + if (D) Log.i(TAG, "wait_connected(): wait ended.."); } } private void connection_failed() { - if (D) Log.i(TAG, "Bluetooth Connection failed!"); + if (D) Log.e(TAG, "Bluetooth Socket IO failed!"); } public void print(String data) { byte[] bytes = data.getBytes(); + if (D) Log.i(TAG, "print(): begin"); try { - if (D) Log.i(TAG, "Entering print();"); wait_connected(); output.write(bytes); - if (D) Log.i(TAG, "Writing bytes: '" + data + "'"); + if (D) Log.i(TAG, "print(): Wrote bytes: '" + data.replace('\n', '\\') + "'"); } catch (IOException e) { connection_failed(); } catch (InterruptedException e) { @@ -153,8 +156,10 @@ public class AltosBluetooth extends AltosLink { } public int getchar() { + if (D) Log.i(TAG, "getchar(): begin"); try { wait_connected(); + if (D) Log.i(TAG, "getchar(): proceeding"); return input.read(); } catch (IOException e) { connection_failed(); @@ -165,14 +170,21 @@ public class AltosBluetooth extends AltosLink { } public void close() { + if (D) Log.i(TAG, "close(): begin"); synchronized(this) { + if (D) Log.i(TAG, "close(): synched"); + if (connect_thread != null) { + if (D) Log.i(TAG, "close(): stopping connect_thread"); connect_thread.cancel(); connect_thread = null; } if (input_thread != null) { + if (D) Log.i(TAG, "close(): stopping input_thread"); try { + if (D) Log.i(TAG, "close(): input_thread.interrupt()....."); input_thread.interrupt(); + if (D) Log.i(TAG, "close(): input_thread.join()....."); input_thread.join(); } catch (Exception e) {} input_thread = null; diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index 9c2fde97..a61a1eda 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -113,8 +113,10 @@ public class TelemetryService extends Service { } private void stopAltosBluetooth() { + if (D) Log.i(TAG, "Stopping BT"); setState(STATE_READY); if (mAltosBluetooth != null) { + if (D) Log.i(TAG, "Closing AltosBluetooth"); mAltosBluetooth.close(); mAltosBluetooth = null; } @@ -122,6 +124,7 @@ public class TelemetryService extends Service { } private void startAltosBluetooth(BluetoothDevice d) { + if (D) Log.i(TAG, "Connecting to " + d.getName()); mAltosBluetooth = new AltosBluetooth(d, mHandler); setState(STATE_CONNECTING); } -- cgit v1.2.3 From d95b84b56c63002788939b93b6ce949d921a4892 Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Sun, 26 Aug 2012 23:30:45 +1200 Subject: altosdroid: Send current state to client on connect Signed-off-by: Mike Beattie --- altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index a61a1eda..7cd233ab 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -81,6 +81,11 @@ public class TelemetryService extends Service { switch (msg.what) { case MSG_REGISTER_CLIENT: s.mClients.add(msg.replyTo); + try { + msg.replyTo.send(Message.obtain(null, AltosDroid.MSG_STATE_CHANGE, s.state, -1)); + } catch (RemoteException e) { + s.mClients.remove(msg.replyTo); + } if (D) Log.d(TAG, "Client bound to service"); break; case MSG_UNREGISTER_CLIENT: -- cgit v1.2.3 From 30d107882b62edf4e6d48923209da6ce3dabeef7 Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Sun, 26 Aug 2012 23:34:28 +1200 Subject: altosdroid: set devicename and pass on to clients Signed-off-by: Mike Beattie --- .../src/org/altusmetrum/AltosDroid/TelemetryService.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index 7cd233ab..218c6d33 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -27,10 +27,12 @@ import android.app.PendingIntent; import android.app.Service; import android.bluetooth.BluetoothDevice; import android.content.Intent; +import android.os.Bundle; import android.os.IBinder; import android.os.Handler; import android.os.Message; import android.os.Messenger; +import android.os.RemoteException; import android.util.Log; import android.widget.Toast; @@ -55,6 +57,10 @@ public class TelemetryService extends Service { public static final int STATE_CONNECTING = 2; public static final int STATE_CONNECTED = 3; + // Key names received from the TelemetryService Handler + public static final String KEY_DEVNAME = "key_devname"; + public static final String KEY_TOAST = "key_toast"; + // Unique Identification Number for the Notification. // We use it on Notification start, and to cancel it. private int NOTIFICATION = R.string.telemetry_service_label; @@ -98,6 +104,12 @@ public class TelemetryService extends Service { break; case MSG_CONNECTED: if (D) Log.d(TAG, "Connected to device"); + s.mConnectedDeviceName = msg.getData().getString(KEY_DEVNAME); + Message m = Message.obtain(null, AltosDroid.MSG_DEVNAME); + Bundle b = new Bundle(); + b.putString(AltosDroid.KEY_DEVNAME, s.mConnectedDeviceName); + m.setData(b); + s.sendMessageToClients(m); s.setState(STATE_CONNECTED); s.mAltosBluetooth.add_monitor(s.telem); break; -- cgit v1.2.3 From 38827db5131a2681243649c76bfd1d7d9801f9ba Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Sun, 26 Aug 2012 23:34:45 +1200 Subject: altosdroid: add handling when restarting BT. delay start after stop. Signed-off-by: Mike Beattie --- altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index 218c6d33..3181d161 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -141,9 +141,14 @@ public class TelemetryService extends Service { } private void startAltosBluetooth(BluetoothDevice d) { + if (mAltosBluetooth == null) { if (D) Log.i(TAG, "Connecting to " + d.getName()); mAltosBluetooth = new AltosBluetooth(d, mHandler); setState(STATE_CONNECTING); + } else { + stopAltosBluetooth(); + mHandler.sendMessageDelayed(Message.obtain(null, MSG_CONNECT, d), 1000); + } } private synchronized void setState(int s) { -- cgit v1.2.3 From dba71db022ae4a9f7c5fd128b90caa73aa4e99da Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Sun, 26 Aug 2012 23:34:59 +1200 Subject: altosdroid: remove old commented code Signed-off-by: Mike Beattie --- altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java | 4 ---- 1 file changed, 4 deletions(-) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index 3181d161..e3f0a739 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -36,10 +36,6 @@ import android.os.RemoteException; import android.util.Log; import android.widget.Toast; -// Need the following import to get access to the app resources, since this -// class is in a sub-package. -//import org.altusmetrum.AltosDroid.R; - import org.altusmetrum.AltosLib.*; public class TelemetryService extends Service { -- cgit v1.2.3 From 6441437d3b0e848b225a3d6c78ab00e2590c6988 Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Mon, 27 Aug 2012 19:37:16 +1200 Subject: altosdroid: remove complexity around message passing * Don't really need to use bundles * TelemetryService: Use a local variable to store the bluetooth device object Signed-off-by: Mike Beattie --- .../org/altusmetrum/AltosDroid/AltosBluetooth.java | 10 +++------ .../src/org/altusmetrum/AltosDroid/AltosDroid.java | 5 +++-- .../altusmetrum/AltosDroid/TelemetryService.java | 24 +++++++++++----------- 3 files changed, 18 insertions(+), 21 deletions(-) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java index 37e977ad..bb188d80 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java @@ -26,9 +26,9 @@ import java.util.UUID; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; -import android.os.Bundle; +//import android.os.Bundle; import android.os.Handler; -import android.os.Message; +//import android.os.Message; import android.util.Log; import org.altusmetrum.AltosLib.*; @@ -116,11 +116,7 @@ public class AltosBluetooth extends AltosLink { // Send the device name back to the Telemetry Service name = device.getName(); - Message msg = handler.obtainMessage(TelemetryService.MSG_CONNECTED); - Bundle b = new Bundle(); - b.putString(TelemetryService.KEY_DEVNAME, name); - msg.setData(b); - handler.sendMessage(msg); + handler.obtainMessage(TelemetryService.MSG_CONNECTED).sendToTarget(); // Notify other waiting threads, now that we're connected AltosBluetooth.this.notifyAll(); diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java index 3e9998e5..5631ffb7 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java @@ -123,8 +123,9 @@ public class AltosDroid extends Activity { break; case MSG_DEVNAME: // save the connected device's name - ad.mConnectedDeviceName = msg.getData().getString(KEY_DEVNAME); - Toast.makeText(ad.getApplicationContext(), "Connected to " + ad.mConnectedDeviceName = (String) msg.obj; + if (ad.mConnectedDeviceName != null) + Toast.makeText(ad.getApplicationContext(), "Connected to " + ad.mConnectedDeviceName, Toast.LENGTH_SHORT).show(); break; case MSG_TOAST: diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index e3f0a739..c809f733 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -27,7 +27,7 @@ import android.app.PendingIntent; import android.app.Service; import android.bluetooth.BluetoothDevice; import android.content.Intent; -import android.os.Bundle; +//import android.os.Bundle; import android.os.IBinder; import android.os.Handler; import android.os.Message; @@ -67,6 +67,7 @@ public class TelemetryService extends Service { final Messenger mMessenger = new Messenger(mHandler); // Target we publish for clients to send messages to IncomingHandler. // Name of the connected device + private BluetoothDevice device = null; private String mConnectedDeviceName = null; private AltosBluetooth mAltosBluetooth = null; private int state = STATE_NONE; @@ -96,16 +97,13 @@ public class TelemetryService extends Service { break; case MSG_CONNECT: if (D) Log.d(TAG, "Connect command received"); - s.startAltosBluetooth((BluetoothDevice) msg.obj); + s.device = (BluetoothDevice) msg.obj; + s.startAltosBluetooth(); break; case MSG_CONNECTED: if (D) Log.d(TAG, "Connected to device"); - s.mConnectedDeviceName = msg.getData().getString(KEY_DEVNAME); - Message m = Message.obtain(null, AltosDroid.MSG_DEVNAME); - Bundle b = new Bundle(); - b.putString(AltosDroid.KEY_DEVNAME, s.mConnectedDeviceName); - m.setData(b); - s.sendMessageToClients(m); + s.mConnectedDeviceName = s.device.getName(); + s.sendMessageToClients(Message.obtain(null, AltosDroid.MSG_DEVNAME, s.mConnectedDeviceName)); s.setState(STATE_CONNECTED); s.mAltosBluetooth.add_monitor(s.telem); break; @@ -133,17 +131,19 @@ public class TelemetryService extends Service { mAltosBluetooth.close(); mAltosBluetooth = null; } + mConnectedDeviceName = null; + device = null; telem.clear(); } - private void startAltosBluetooth(BluetoothDevice d) { + private void startAltosBluetooth() { if (mAltosBluetooth == null) { - if (D) Log.i(TAG, "Connecting to " + d.getName()); - mAltosBluetooth = new AltosBluetooth(d, mHandler); + if (D) Log.i(TAG, "Connecting to " + device.getName()); + mAltosBluetooth = new AltosBluetooth(device, mHandler); setState(STATE_CONNECTING); } else { stopAltosBluetooth(); - mHandler.sendMessageDelayed(Message.obtain(null, MSG_CONNECT, d), 1000); + mHandler.sendMessageDelayed(Message.obtain(null, MSG_CONNECT, device), 1000); } } -- cgit v1.2.3 From 18c380120fe37a4bdc8f295e86c6c4413d1aa037 Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Mon, 27 Aug 2012 19:39:09 +1200 Subject: altosdroid: really don't need to store a local copy of the device name Signed-off-by: Mike Beattie --- altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index c809f733..14e41ba0 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -68,7 +68,6 @@ public class TelemetryService extends Service { // Name of the connected device private BluetoothDevice device = null; - private String mConnectedDeviceName = null; private AltosBluetooth mAltosBluetooth = null; private int state = STATE_NONE; LinkedBlockingQueue telem; @@ -102,8 +101,7 @@ public class TelemetryService extends Service { break; case MSG_CONNECTED: if (D) Log.d(TAG, "Connected to device"); - s.mConnectedDeviceName = s.device.getName(); - s.sendMessageToClients(Message.obtain(null, AltosDroid.MSG_DEVNAME, s.mConnectedDeviceName)); + s.sendMessageToClients(Message.obtain(null, AltosDroid.MSG_DEVNAME, s.device.getName())); s.setState(STATE_CONNECTED); s.mAltosBluetooth.add_monitor(s.telem); break; @@ -131,7 +129,6 @@ public class TelemetryService extends Service { mAltosBluetooth.close(); mAltosBluetooth = null; } - mConnectedDeviceName = null; device = null; telem.clear(); } -- cgit v1.2.3 From e121ec3ae634f41979717281a28af5e4a38e8f3a Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Mon, 27 Aug 2012 19:40:30 +1200 Subject: altosdroid: don't need keys for Bundles anymore Signed-off-by: Mike Beattie --- altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java | 4 ---- 1 file changed, 4 deletions(-) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index 14e41ba0..e9254bad 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -53,10 +53,6 @@ public class TelemetryService extends Service { public static final int STATE_CONNECTING = 2; public static final int STATE_CONNECTED = 3; - // Key names received from the TelemetryService Handler - public static final String KEY_DEVNAME = "key_devname"; - public static final String KEY_TOAST = "key_toast"; - // Unique Identification Number for the Notification. // We use it on Notification start, and to cancel it. private int NOTIFICATION = R.string.telemetry_service_label; -- cgit v1.2.3 From 9a41508d92f95012a37bb75603e6e48a2c405204 Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Mon, 27 Aug 2012 19:41:29 +1200 Subject: altosdroid: Add Connected/Connect_failed messages Signed-off-by: Mike Beattie --- altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java | 2 ++ .../src/org/altusmetrum/AltosDroid/TelemetryService.java | 10 ++++++++++ 2 files changed, 12 insertions(+) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java index bb188d80..19b2a2a5 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java @@ -103,6 +103,7 @@ public class AltosBluetooth extends AltosLink { input = null; output = null; AltosBluetooth.this.notifyAll(); + handler.obtainMessage(TelemetryService.MSG_CONNECT_FAILED).sendToTarget(); if (D) Log.e(TAG, "ConnectThread: Failed to establish connection"); return; } @@ -147,6 +148,7 @@ public class AltosBluetooth extends AltosLink { private void connection_failed() { if (D) Log.e(TAG, "Bluetooth Socket IO failed!"); + handler.obtainMessage(TelemetryService.MSG_DISCONNECTED).sendToTarget(); } public void print(String data) { diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index e9254bad..ecafc74a 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -47,6 +47,8 @@ public class TelemetryService extends Service { static final int MSG_UNREGISTER_CLIENT = 2; static final int MSG_CONNECT = 3; static final int MSG_CONNECTED = 4; + static final int MSG_CONNECT_FAILED = 5; + static final int MSG_DISCONNECTED = 6; public static final int STATE_NONE = 0; public static final int STATE_READY = 1; @@ -101,6 +103,14 @@ public class TelemetryService extends Service { s.setState(STATE_CONNECTED); s.mAltosBluetooth.add_monitor(s.telem); break; + case MSG_CONNECT_FAILED: + if (D) Log.d(TAG, "Connection failed... retrying"); + s.startAltosBluetooth(); + break; + case MSG_DISCONNECTED: + if (D) Log.d(TAG, "Disconnected from " + s.device.getName()); + s.stopAltosBluetooth(); + break; default: super.handleMessage(msg); } -- cgit v1.2.3 From fa8668931cbbc1506560222f2db7e427b514a351 Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Mon, 27 Aug 2012 19:41:47 +1200 Subject: altosdroid: Let a freshly connected client know what the device name is! Signed-off-by: Mike Beattie --- altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java | 1 + 1 file changed, 1 insertion(+) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index ecafc74a..88ba805c 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -82,6 +82,7 @@ public class TelemetryService extends Service { case MSG_REGISTER_CLIENT: s.mClients.add(msg.replyTo); try { + msg.replyTo.send(Message.obtain(null, AltosDroid.MSG_DEVNAME, s.device.getName())); msg.replyTo.send(Message.obtain(null, AltosDroid.MSG_STATE_CHANGE, s.state, -1)); } catch (RemoteException e) { s.mClients.remove(msg.replyTo); -- cgit v1.2.3 From ed653a1e6dc8884cb171af1406fd0999ef125a4d Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Tue, 28 Aug 2012 17:26:09 +1200 Subject: altosdroid: create connected() method Signed-off-by: Mike Beattie --- altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index 88ba805c..a1b1915a 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -100,8 +100,7 @@ public class TelemetryService extends Service { break; case MSG_CONNECTED: if (D) Log.d(TAG, "Connected to device"); - s.sendMessageToClients(Message.obtain(null, AltosDroid.MSG_DEVNAME, s.device.getName())); - s.setState(STATE_CONNECTED); + s.connected(); s.mAltosBluetooth.add_monitor(s.telem); break; case MSG_CONNECT_FAILED: @@ -158,6 +157,12 @@ public class TelemetryService extends Service { sendMessageToClients(Message.obtain(null, AltosDroid.MSG_STATE_CHANGE, state, -1)); } + private void connected() { + sendMessageToClients(Message.obtain(null, AltosDroid.MSG_DEVNAME, device.getName())); + setState(STATE_CONNECTED); + } + + @Override public void onCreate() { // Create a reference to the NotificationManager so that we can update our notifcation text later -- cgit v1.2.3 From 03563c765d8b0ab3689c91b2b533c68e11650577 Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Tue, 28 Aug 2012 17:35:11 +1200 Subject: altosdroid: Add new "TelemetryReader" class to handle Telemetry * Add MSG_TELEMETRY messages to both AltosDroid and TelemetryService to handle passing of AltosState object all the way back to the UI. * Remove linkedblockinglist from TelemetryService * (MSG_TELEMETRY is a rename of MSG_INCOMING_TELEM in AltosDroid) * commented code in case statement inside AltosDroind - won't work with the objects it is currently passed. * Add new "MSG_DEVCONFIG" message to AltosDroid - allows TelemetryService to pass information about the connected device back to the UI. Signed-off-by: Mike Beattie --- .../src/org/altusmetrum/AltosDroid/AltosDroid.java | 14 ++-- .../altusmetrum/AltosDroid/TelemetryReader.java | 75 ++++++++++++++++++++++ .../altusmetrum/AltosDroid/TelemetryService.java | 23 +++++-- 3 files changed, 100 insertions(+), 12 deletions(-) create mode 100644 altosdroid/src/org/altusmetrum/AltosDroid/TelemetryReader.java (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java index 33da9346..cf982eac 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java @@ -60,8 +60,9 @@ public class AltosDroid extends Activity { // Message types sent from the TelemetryService Handler public static final int MSG_STATE_CHANGE = 1; public static final int MSG_DEVNAME = 2; - public static final int MSG_INCOMING_TELEM = 3; - public static final int MSG_TOAST = 4; + public static final int MSG_TOAST = 3; + public static final int MSG_DEVCONFIG = 4; + public static final int MSG_TELEMETRY = 5; // Intent request codes private static final int REQUEST_CONNECT_DEVICE = 1; @@ -111,11 +112,12 @@ public class AltosDroid extends Activity { break; } break; - case MSG_INCOMING_TELEM: - byte[] buf = (byte[]) msg.obj; + case MSG_DEVCONFIG: + case MSG_TELEMETRY: + //byte[] buf = (byte[]) msg.obj; // construct a string from the buffer - String telem = new String(buf); - ad.mSerialView.append(telem); + //String telem = new String(buf); + //ad.mSerialView.append(telem); break; case MSG_DEVNAME: // save the connected device's name diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryReader.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryReader.java new file mode 100644 index 00000000..bfa5db5c --- /dev/null +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryReader.java @@ -0,0 +1,75 @@ +package org.altusmetrum.AltosDroid; + +import java.text.*; +import java.io.*; +import java.util.concurrent.*; +import android.util.Log; +import android.os.Handler; + +import org.altusmetrum.AltosLib.*; + + +public class TelemetryReader extends Thread { + + private static final String TAG = "TelemetryReader"; + + int crc_errors; + + Handler handler; + + AltosLink link; + AltosRecord previous; + + LinkedBlockingQueue telem; + + public AltosRecord read() throws ParseException, AltosCRCException, InterruptedException, IOException { + AltosLine l = telem.take(); + if (l.line == null) + throw new IOException("IO error"); + AltosRecord next = AltosTelemetry.parse(l.line, previous); + previous = next; + return next; + } + + public void close() { + previous = null; + link.remove_monitor(telem); + link = null; + telem.clear(); + telem = null; + } + + public void run() { + AltosState state = null; + + try { + for (;;) { + try { + AltosRecord record = read(); + if (record == null) + break; + state = new AltosState(record, state); + + handler.obtainMessage(TelemetryService.MSG_TELEMETRY, state).sendToTarget(); + } catch (ParseException pp) { + Log.e(TAG, String.format("Parse error: %d \"%s\"", pp.getErrorOffset(), pp.getMessage())); + } catch (AltosCRCException ce) { + ++crc_errors; + } + } + } catch (InterruptedException ee) { + } catch (IOException ie) { + } finally { + close(); + } + } + + public TelemetryReader (AltosLink in_link, Handler in_handler) { + link = in_link; + handler = in_handler; + + previous = null; + telem = new LinkedBlockingQueue(); + link.add_monitor(telem); + } +} diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index a1b1915a..42198b6b 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -19,7 +19,6 @@ package org.altusmetrum.AltosDroid; import java.lang.ref.WeakReference; import java.util.ArrayList; -import java.util.concurrent.LinkedBlockingQueue; import android.app.Notification; //import android.app.NotificationManager; @@ -36,7 +35,7 @@ import android.os.RemoteException; import android.util.Log; import android.widget.Toast; -import org.altusmetrum.AltosLib.*; +//import org.altusmetrum.AltosLib.*; public class TelemetryService extends Service { @@ -49,6 +48,7 @@ public class TelemetryService extends Service { static final int MSG_CONNECTED = 4; static final int MSG_CONNECT_FAILED = 5; static final int MSG_DISCONNECTED = 6; + static final int MSG_TELEMETRY = 7; public static final int STATE_NONE = 0; public static final int STATE_READY = 1; @@ -67,8 +67,9 @@ public class TelemetryService extends Service { // Name of the connected device private BluetoothDevice device = null; private AltosBluetooth mAltosBluetooth = null; + private TelemetryReader mTelemetryReader = null; + private int state = STATE_NONE; - LinkedBlockingQueue telem; // Handler of incoming messages from clients. static class IncomingHandler extends Handler { @@ -101,7 +102,6 @@ public class TelemetryService extends Service { case MSG_CONNECTED: if (D) Log.d(TAG, "Connected to device"); s.connected(); - s.mAltosBluetooth.add_monitor(s.telem); break; case MSG_CONNECT_FAILED: if (D) Log.d(TAG, "Connection failed... retrying"); @@ -111,6 +111,9 @@ public class TelemetryService extends Service { if (D) Log.d(TAG, "Disconnected from " + s.device.getName()); s.stopAltosBluetooth(); break; + case MSG_TELEMETRY: + s.sendMessageToClients(Message.obtain(null, AltosDroid.MSG_TELEMETRY, msg.obj)); + break; default: super.handleMessage(msg); } @@ -130,13 +133,20 @@ public class TelemetryService extends Service { private void stopAltosBluetooth() { if (D) Log.i(TAG, "Stopping BT"); setState(STATE_READY); + if (mTelemetryReader != null) { + mTelemetryReader.interrupt(); + try { + mTelemetryReader.join(); + } catch (InterruptedException e) { + } + mTelemetryReader = null; + } if (mAltosBluetooth != null) { if (D) Log.i(TAG, "Closing AltosBluetooth"); mAltosBluetooth.close(); mAltosBluetooth = null; } device = null; - telem.clear(); } private void startAltosBluetooth() { @@ -160,6 +170,8 @@ public class TelemetryService extends Service { private void connected() { sendMessageToClients(Message.obtain(null, AltosDroid.MSG_DEVNAME, device.getName())); setState(STATE_CONNECTED); + mTelemetryReader = new TelemetryReader(mAltosBluetooth, mHandler); + mTelemetryReader.start(); } @@ -168,7 +180,6 @@ public class TelemetryService extends Service { // Create a reference to the NotificationManager so that we can update our notifcation text later //mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); - telem = new LinkedBlockingQueue(); setState(STATE_READY); } -- cgit v1.2.3 From ae03d8e87985b9f746e9e22b2394a0a5b4f39f1c Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Tue, 28 Aug 2012 17:36:41 +1200 Subject: altosdroid: Add passing of Device Config at connect Signed-off-by: Mike Beattie --- altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index 42198b6b..3ae87bb1 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -19,6 +19,7 @@ package org.altusmetrum.AltosDroid; import java.lang.ref.WeakReference; import java.util.ArrayList; +import java.util.concurrent.TimeoutException; import android.app.Notification; //import android.app.NotificationManager; @@ -170,6 +171,11 @@ public class TelemetryService extends Service { private void connected() { sendMessageToClients(Message.obtain(null, AltosDroid.MSG_DEVNAME, device.getName())); setState(STATE_CONNECTED); + try { + sendMessageToClients(Message.obtain(null, AltosDroid.MSG_DEVCONFIG, mAltosBluetooth.config_data())); + } catch (InterruptedException e) { + } catch (TimeoutException e) { + } mTelemetryReader = new TelemetryReader(mAltosBluetooth, mHandler); mTelemetryReader.start(); } -- cgit v1.2.3 From 162c640d382b9f823573578fe97584adc94cd9b6 Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Tue, 28 Aug 2012 17:37:45 +1200 Subject: altosdroid: miscellaneous cleanup * Copyright info * whitespace * comments * unused imports Signed-off-by: Mike Beattie --- .../org/altusmetrum/AltosDroid/AltosBluetooth.java | 4 ++-- .../src/org/altusmetrum/AltosDroid/AltosDroid.java | 3 ++- .../org/altusmetrum/AltosDroid/TelemetryReader.java | 19 +++++++++++++++++++ .../org/altusmetrum/AltosDroid/TelemetryService.java | 1 + 4 files changed, 24 insertions(+), 3 deletions(-) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java index 19b2a2a5..18581142 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java @@ -1,6 +1,6 @@ /* - * Copyright © 2011 Keith Packard - * Copyright © 2012 Mike Beattie + * Copyright © 2011 Keith Packard + * Copyright © 2012 Mike Beattie * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java index cf982eac..3855f6f9 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java @@ -18,6 +18,7 @@ package org.altusmetrum.AltosDroid; import java.lang.ref.WeakReference; + import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; @@ -47,7 +48,7 @@ import android.view.Window; //import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; -import org.altusmetrum.AltosDroid.R; +//import org.altusmetrum.AltosDroid.R; /** * This is the main Activity that displays the current chat session. diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryReader.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryReader.java index bfa5db5c..c47e4942 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryReader.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryReader.java @@ -1,3 +1,22 @@ +/* + * Copyright © 2011 Keith Packard + * Copyright © 2012 Mike Beattie + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ + + package org.altusmetrum.AltosDroid; import java.text.*; diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index 3ae87bb1..4701ba1f 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -70,6 +70,7 @@ public class TelemetryService extends Service { private AltosBluetooth mAltosBluetooth = null; private TelemetryReader mTelemetryReader = null; + // internally track state of bluetooth connection private int state = STATE_NONE; // Handler of incoming messages from clients. -- cgit v1.2.3 From 150a726e125aa7d181c00348ddd1791fd84164e5 Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Tue, 28 Aug 2012 21:53:01 +1200 Subject: altosdroid: Miscellaneous comments/debug/etc cleanup Signed-off-by: Mike Beattie --- .../org/altusmetrum/AltosDroid/AltosBluetooth.java | 30 +++--- .../src/org/altusmetrum/AltosDroid/AltosDroid.java | 108 ++------------------- .../altusmetrum/AltosDroid/TelemetryService.java | 9 +- 3 files changed, 28 insertions(+), 119 deletions(-) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java index 18581142..5a8ed096 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java @@ -78,7 +78,7 @@ public class AltosBluetooth extends AltosLink { } public void run() { - if (D) Log.i(TAG, "ConnectThread: BEGIN"); + if (D) Log.d(TAG, "ConnectThread: BEGIN"); setName("ConnectThread"); // Always cancel discovery because it will slow down a connection @@ -122,7 +122,7 @@ public class AltosBluetooth extends AltosLink { // Notify other waiting threads, now that we're connected AltosBluetooth.this.notifyAll(); - if (D) Log.i(TAG, "ConnectThread: Connect completed"); + if (D) Log.d(TAG, "ConnectThread: Connect completed"); } } @@ -137,27 +137,24 @@ public class AltosBluetooth extends AltosLink { } private synchronized void wait_connected() throws InterruptedException, IOException { - if (D) Log.i(TAG, "wait_connected(): begin"); if (input == null) { - if (D) Log.i(TAG, "wait_connected(): waiting"); wait(); - if (D) Log.i(TAG, "wait_connected(): wait ended.."); if (input == null) throw new IOException(); } } private void connection_failed() { - if (D) Log.e(TAG, "Bluetooth Socket IO failed!"); + if (D) Log.e(TAG, "Connection lost during I/O"); handler.obtainMessage(TelemetryService.MSG_DISCONNECTED).sendToTarget(); } public void print(String data) { byte[] bytes = data.getBytes(); - if (D) Log.i(TAG, "print(): begin"); + if (D) Log.d(TAG, "print(): begin"); try { wait_connected(); output.write(bytes); - if (D) Log.i(TAG, "print(): Wrote bytes: '" + data.replace('\n', '\\') + "'"); + if (D) Log.d(TAG, "print(): Wrote bytes: '" + data.replace('\n', '\\') + "'"); } catch (IOException e) { connection_failed(); } catch (InterruptedException e) { @@ -166,10 +163,8 @@ public class AltosBluetooth extends AltosLink { } public int getchar() { - if (D) Log.i(TAG, "getchar(): begin"); try { wait_connected(); - if (D) Log.i(TAG, "getchar(): proceeding"); return input.read(); } catch (IOException e) { connection_failed(); @@ -180,27 +175,27 @@ public class AltosBluetooth extends AltosLink { } public void close() { - if (D) Log.i(TAG, "close(): begin"); + if (D) Log.d(TAG, "close(): begin"); synchronized(this) { - if (D) Log.i(TAG, "close(): synched"); + if (D) Log.d(TAG, "close(): synched"); if (connect_thread != null) { - if (D) Log.i(TAG, "close(): stopping connect_thread"); + if (D) Log.d(TAG, "close(): stopping connect_thread"); connect_thread.cancel(); connect_thread = null; } - if (D) Log.i(TAG, "close(): Closing socket"); + if (D) Log.d(TAG, "close(): Closing socket"); try { socket.close(); } catch (IOException e) { if (D) Log.e(TAG, "close(): unable to close() socket"); } if (input_thread != null) { - if (D) Log.i(TAG, "close(): stopping input_thread"); + if (D) Log.d(TAG, "close(): stopping input_thread"); try { - if (D) Log.i(TAG, "close(): input_thread.interrupt()....."); + if (D) Log.d(TAG, "close(): input_thread.interrupt()....."); input_thread.interrupt(); - if (D) Log.i(TAG, "close(): input_thread.join()....."); + if (D) Log.d(TAG, "close(): input_thread.join()....."); input_thread.join(); } catch (Exception e) {} input_thread = null; @@ -214,6 +209,7 @@ public class AltosBluetooth extends AltosLink { //public void flush_output() { super.flush_output(); } + // Stubs of required methods when extending AltosLink public boolean can_cancel_reply() { return false; } public boolean show_reply_timeout() { return true; } public void hide_reply_timeout() { } diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java index 3855f6f9..bbb2970f 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java @@ -36,19 +36,12 @@ import android.speech.tts.TextToSpeech; import android.speech.tts.TextToSpeech.OnInitListener; import android.text.method.ScrollingMovementMethod; import android.util.Log; -//import android.view.KeyEvent; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; -//import android.view.View; import android.view.Window; -//import android.view.View.OnClickListener; -//import android.view.inputmethod.EditorInfo; -//import android.widget.Button; -//import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; -//import org.altusmetrum.AltosDroid.R; /** * This is the main Activity that displays the current chat session. @@ -58,7 +51,7 @@ public class AltosDroid extends Activity { private static final String TAG = "AltosDroid"; private static final boolean D = true; - // Message types sent from the TelemetryService Handler + // Message types received by our Handler public static final int MSG_STATE_CHANGE = 1; public static final int MSG_DEVNAME = 2; public static final int MSG_TOAST = 3; @@ -72,11 +65,10 @@ public class AltosDroid extends Activity { // Layout Views private TextView mTitle; private TextView mSerialView; - //private EditText mOutEditText; - //private Button mSendButton; - private boolean mIsBound; - Messenger mService = null; + // Service + private boolean mIsBound = false; + private Messenger mService = null; final Messenger mMessenger = new Messenger(new IncomingHandler(this)); // Name of the connected device @@ -84,7 +76,8 @@ public class AltosDroid extends Activity { // Local Bluetooth adapter private BluetoothAdapter mBluetoothAdapter = null; - private TextToSpeech tts; + // Text to Speech + private TextToSpeech tts = null; private boolean tts_enabled = false; // The Handler that gets information back from the Telemetry Service @@ -97,7 +90,7 @@ public class AltosDroid extends Activity { AltosDroid ad = mAltosDroid.get(); switch (msg.what) { case MSG_STATE_CHANGE: - if(D) Log.i(TAG, "MSG_STATE_CHANGE: " + msg.arg1); + if(D) Log.d(TAG, "MSG_STATE_CHANGE: " + msg.arg1); switch (msg.arg1) { case TelemetryService.STATE_CONNECTED: ad.mTitle.setText(R.string.title_connected_to); @@ -172,6 +165,7 @@ public class AltosDroid extends Activity { mTitle.setText(R.string.app_name); mTitle = (TextView) findViewById(R.id.title_right_text); + // Set up the temporary Text View mSerialView = (TextView) findViewById(R.id.in); mSerialView.setMovementMethod(new ScrollingMovementMethod()); mSerialView.setClickable(false); @@ -209,8 +203,6 @@ public class AltosDroid extends Activity { if (!mBluetoothAdapter.isEnabled()) { Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableIntent, REQUEST_ENABLE_BT); - } else { - //if (mChatService == null) setupChat(); } } @@ -219,16 +211,6 @@ public class AltosDroid extends Activity { super.onResume(); if(D) Log.e(TAG, "+ ON RESUME +"); - // Performing this check in onResume() covers the case in which BT was - // not enabled during onStart(), so we were paused to enable it... - // onResume() will be called when ACTION_REQUEST_ENABLE activity returns. - //if (mChatService != null) { - // Only if the state is STATE_NONE, do we know that we haven't started already - //if (mChatService.getState() == BluetoothChatService.STATE_NONE) { - // Start the Bluetooth chat services - //mChatService.start(); - //} - //} } @Override @@ -256,72 +238,6 @@ public class AltosDroid extends Activity { -/* - private void setupChat() { - Log.d(TAG, "setupChat()"); - - // Initialize the compose field with a listener for the return key - mOutEditText = (EditText) findViewById(R.id.edit_text_out); - mOutEditText.setOnEditorActionListener(mWriteListener); - - // Initialize the send button with a listener that for click events - mSendButton = (Button) findViewById(R.id.button_send); - mSendButton.setOnClickListener(new OnClickListener() { - public void onClick(View v) { - // Send a message using content of the edit text widget - TextView view = (TextView) findViewById(R.id.edit_text_out); - String message = view.getText().toString(); - sendMessage(message); - } - }); - - // Initialize the BluetoothChatService to perform bluetooth connections - mChatService = new BluetoothChatService(this, mHandler); - - // Initialize the buffer for outgoing messages - mOutStringBuffer = new StringBuffer(""); - } -*/ - - /** - * Sends a message. - * @param message A string of text to send. - */ - /* - private void sendMessage(String message) { - // Check that we're actually connected before trying anything - if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) { - Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show(); - return; - } - - // Check that there's actually something to send - if (message.length() > 0) { - // Get the message bytes and tell the BluetoothChatService to write - byte[] send = message.getBytes(); - mChatService.write(send); - - // Reset out string buffer to zero and clear the edit text field - mOutStringBuffer.setLength(0); - mOutEditText.setText(mOutStringBuffer); - } - } - - - // The action listener for the EditText widget, to listen for the return key - private TextView.OnEditorActionListener mWriteListener = - new TextView.OnEditorActionListener() { - public boolean onEditorAction(TextView view, int actionId, KeyEvent event) { - // If the action is a key-up event on the return key, send the message - if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_UP) { - String message = view.getText().toString(); - sendMessage(message); - } - if(D) Log.i(TAG, "END onEditorAction"); - return true; - } - }; - */ public void onActivityResult(int requestCode, int resultCode, Intent data) { if(D) Log.d(TAG, "onActivityResult " + resultCode); @@ -339,7 +255,7 @@ public class AltosDroid extends Activity { //setupChat(); } else { // User did not enable Bluetooth or an error occured - Log.d(TAG, "BT not enabled"); + Log.e(TAG, "BT not enabled"); stopService(new Intent(AltosDroid.this, TelemetryService.class)); Toast.makeText(this, R.string.bt_not_enabled, Toast.LENGTH_SHORT).show(); finish(); @@ -355,13 +271,9 @@ public class AltosDroid extends Activity { BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address); // Attempt to connect to the device try { - //Message msg = Message.obtain(null, TelemetryService.MSG_CONNECT_TELEBT); - //msg.obj = device; - //mService.send(msg); - if (D) Log.i(TAG, "Connecting to " + device.getName()); + if (D) Log.d(TAG, "Connecting to " + device.getName()); mService.send(Message.obtain(null, TelemetryService.MSG_CONNECT, device)); } catch (RemoteException e) { - e.printStackTrace(); } } diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index 4701ba1f..ccb04de0 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -133,9 +133,10 @@ public class TelemetryService extends Service { } private void stopAltosBluetooth() { - if (D) Log.i(TAG, "Stopping BT"); + if (D) Log.d(TAG, "stopAltosBluetooth(): begin"); setState(STATE_READY); if (mTelemetryReader != null) { + if (D) Log.d(TAG, "stopAltosBluetooth(): stopping TelemetryReader"); mTelemetryReader.interrupt(); try { mTelemetryReader.join(); @@ -144,7 +145,7 @@ public class TelemetryService extends Service { mTelemetryReader = null; } if (mAltosBluetooth != null) { - if (D) Log.i(TAG, "Closing AltosBluetooth"); + if (D) Log.d(TAG, "stopAltosBluetooth(): stopping AltosBluetooth"); mAltosBluetooth.close(); mAltosBluetooth = null; } @@ -153,7 +154,7 @@ public class TelemetryService extends Service { private void startAltosBluetooth() { if (mAltosBluetooth == null) { - if (D) Log.i(TAG, "Connecting to " + device.getName()); + if (D) Log.d(TAG, String.format("startAltosBluetooth(): Connecting to %s (%s)", device.getName(), device.getAddress())); mAltosBluetooth = new AltosBluetooth(device, mHandler); setState(STATE_CONNECTING); } else { @@ -163,7 +164,7 @@ public class TelemetryService extends Service { } private synchronized void setState(int s) { - if (D) Log.d(TAG, "setState() " + state + " -> " + s); + if (D) Log.d(TAG, "setState(): " + state + " -> " + s); state = s; sendMessageToClients(Message.obtain(null, AltosDroid.MSG_STATE_CHANGE, state, -1)); -- cgit v1.2.3 From c7bef83fd553987f83c0bf7ff37ef941872564fe Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Tue, 28 Aug 2012 21:54:05 +1200 Subject: altosdroid: fix double call of stopAltosBluetooth() Signed-off-by: Mike Beattie --- altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index ccb04de0..10c2e26f 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -110,8 +110,11 @@ public class TelemetryService extends Service { s.startAltosBluetooth(); break; case MSG_DISCONNECTED: - if (D) Log.d(TAG, "Disconnected from " + s.device.getName()); - s.stopAltosBluetooth(); + // Only do the following if we haven't been shutdown elsewhere.. + if (s.device != null) { + if (D) Log.d(TAG, "Disconnected from " + s.device.getName()); + s.stopAltosBluetooth(); + } break; case MSG_TELEMETRY: s.sendMessageToClients(Message.obtain(null, AltosDroid.MSG_TELEMETRY, msg.obj)); -- cgit v1.2.3 From 502b24eb2c9c76e4e2bdcc79be0b71a869488b37 Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Tue, 28 Aug 2012 21:55:40 +1200 Subject: altosdroid: fix a connection retry having a null pointer Signed-off-by: Mike Beattie --- altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index 10c2e26f..1fa62d87 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -161,8 +161,14 @@ public class TelemetryService extends Service { mAltosBluetooth = new AltosBluetooth(device, mHandler); setState(STATE_CONNECTING); } else { + // This is a bit of a hack - if it appears we're still connected, we treat this as a restart. + // So, to give a suitable delay to teardown/bringup, we just schedule a resend of a message + // to ourselves in a few seconds time that will ultimately call this method again. + // ... then we tear down the existing connection. + // We do it this way around so that we don't lose a reference to the device when this method + // is called on reception of MSG_CONNECT_FAILED in the handler above. + mHandler.sendMessageDelayed(Message.obtain(null, MSG_CONNECT, device), 3000); stopAltosBluetooth(); - mHandler.sendMessageDelayed(Message.obtain(null, MSG_CONNECT, device), 1000); } } -- cgit v1.2.3 From 5ce8c63850dbc6462d7c41ce917e0e06672ec0ab Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Tue, 28 Aug 2012 22:00:14 +1200 Subject: altosdroid: whitespace Signed-off-by: Mike Beattie --- altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index 1fa62d87..cd47c6f9 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -66,8 +66,8 @@ public class TelemetryService extends Service { final Messenger mMessenger = new Messenger(mHandler); // Target we publish for clients to send messages to IncomingHandler. // Name of the connected device - private BluetoothDevice device = null; - private AltosBluetooth mAltosBluetooth = null; + private BluetoothDevice device = null; + private AltosBluetooth mAltosBluetooth = null; private TelemetryReader mTelemetryReader = null; // internally track state of bluetooth connection -- cgit v1.2.3 From 5c7370dcd7a65c81a3c903a71167e07cfcbade53 Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Tue, 28 Aug 2012 22:07:39 +1200 Subject: altosdroid: stop sending device name, just send config data * Kinda complicated, but ultimately more sensible * Just send the config data as an arg to MSG_CONNECTED * keep retrying connection till we actually get config data Signed-off-by: Mike Beattie --- .../src/org/altusmetrum/AltosDroid/AltosDroid.java | 14 ++++++++---- .../altusmetrum/AltosDroid/TelemetryService.java | 26 ++++++++++++++++------ 2 files changed, 29 insertions(+), 11 deletions(-) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java index bbb2970f..df2263ce 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java @@ -43,6 +43,8 @@ import android.view.Window; import android.widget.TextView; import android.widget.Toast; +import org.altusmetrum.AltosLib.*; + /** * This is the main Activity that displays the current chat session. */ @@ -71,8 +73,8 @@ public class AltosDroid extends Activity { private Messenger mService = null; final Messenger mMessenger = new Messenger(new IncomingHandler(this)); - // Name of the connected device - private String mConnectedDeviceName = null; + // TeleBT Config data + private AltosConfigData mConfigData = null; // Local Bluetooth adapter private BluetoothAdapter mBluetoothAdapter = null; @@ -93,16 +95,20 @@ public class AltosDroid extends Activity { if(D) Log.d(TAG, "MSG_STATE_CHANGE: " + msg.arg1); switch (msg.arg1) { case TelemetryService.STATE_CONNECTED: + ad.mConfigData = (AltosConfigData) msg.obj; + String str = String.format(" %s S/N: %d", ad.mConfigData.product, ad.mConfigData.serial); ad.mTitle.setText(R.string.title_connected_to); - ad.mTitle.append(ad.mConnectedDeviceName); - ad.mSerialView.setText(""); + ad.mTitle.append(str); + Toast.makeText(ad.getApplicationContext(), "Connected to " + str, Toast.LENGTH_SHORT).show(); break; case TelemetryService.STATE_CONNECTING: ad.mTitle.setText(R.string.title_connecting); break; case TelemetryService.STATE_READY: case TelemetryService.STATE_NONE: + ad.mConfigData = null; ad.mTitle.setText(R.string.title_not_connected); + ad.mSerialView.setText(""); break; } break; diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index cd47c6f9..d11fc53a 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -36,7 +36,7 @@ import android.os.RemoteException; import android.util.Log; import android.widget.Toast; -//import org.altusmetrum.AltosLib.*; +import org.altusmetrum.AltosLib.*; public class TelemetryService extends Service { @@ -68,6 +68,7 @@ public class TelemetryService extends Service { // Name of the connected device private BluetoothDevice device = null; private AltosBluetooth mAltosBluetooth = null; + private AltosConfigData mConfigData = null; private TelemetryReader mTelemetryReader = null; // internally track state of bluetooth connection @@ -85,8 +86,9 @@ public class TelemetryService extends Service { case MSG_REGISTER_CLIENT: s.mClients.add(msg.replyTo); try { - msg.replyTo.send(Message.obtain(null, AltosDroid.MSG_DEVNAME, s.device.getName())); - msg.replyTo.send(Message.obtain(null, AltosDroid.MSG_STATE_CHANGE, s.state, -1)); + // Now we try to send the freshly connected UI any relavant information about what + // we're talking to - Basically state and Config Data. + msg.replyTo.send(Message.obtain(null, AltosDroid.MSG_STATE_CHANGE, s.state, -1, s.mConfigData)); } catch (RemoteException e) { s.mClients.remove(msg.replyTo); } @@ -153,6 +155,7 @@ public class TelemetryService extends Service { mAltosBluetooth = null; } device = null; + mConfigData = null; } private void startAltosBluetooth() { @@ -176,17 +179,26 @@ public class TelemetryService extends Service { if (D) Log.d(TAG, "setState(): " + state + " -> " + s); state = s; - sendMessageToClients(Message.obtain(null, AltosDroid.MSG_STATE_CHANGE, state, -1)); + // This shouldn't be required - mConfigData should be null for any non-connected + // state, but to be safe and to reduce message size + AltosConfigData acd = (state == STATE_CONNECTED) ? mConfigData : null; + + sendMessageToClients(Message.obtain(null, AltosDroid.MSG_STATE_CHANGE, state, -1, acd)); } private void connected() { - sendMessageToClients(Message.obtain(null, AltosDroid.MSG_DEVNAME, device.getName())); - setState(STATE_CONNECTED); try { - sendMessageToClients(Message.obtain(null, AltosDroid.MSG_DEVCONFIG, mAltosBluetooth.config_data())); + mConfigData = mAltosBluetooth.config_data(); } catch (InterruptedException e) { } catch (TimeoutException e) { + // If this timed out, then we really want to retry it, but + // probably safer to just retry the connection from scratch. + mHandler.obtainMessage(MSG_CONNECT_FAILED).sendToTarget(); + return; } + + setState(STATE_CONNECTED); + mTelemetryReader = new TelemetryReader(mAltosBluetooth, mHandler); mTelemetryReader.start(); } -- cgit v1.2.3 From 6c985c2b0433a08add3bbf55fdb30102157b4ede Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Tue, 28 Aug 2012 22:10:26 +1200 Subject: altosdroid: add timer to stop service * Stops when no UI clients, and no bluetooth connection remains Signed-off-by: Mike Beattie --- .../altusmetrum/AltosDroid/TelemetryService.java | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index d11fc53a..ffe96946 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -20,6 +20,8 @@ package org.altusmetrum.AltosDroid; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.concurrent.TimeoutException; +import java.util.Timer; +import java.util.TimerTask; import android.app.Notification; //import android.app.NotificationManager; @@ -61,6 +63,9 @@ public class TelemetryService extends Service { private int NOTIFICATION = R.string.telemetry_service_label; //private NotificationManager mNM; + // Timer - we wake up every now and then to decide if the service should stop + private Timer timer = new Timer(); + ArrayList mClients = new ArrayList(); // Keeps track of all current registered clients. final Handler mHandler = new IncomingHandler(this); final Messenger mMessenger = new Messenger(mHandler); // Target we publish for clients to send messages to IncomingHandler. @@ -204,12 +209,28 @@ public class TelemetryService extends Service { } + private void onTimerTick() { + if (D) Log.d(TAG, "Timer wakeup"); + try { + if (mClients.size() <= 0 && state != STATE_CONNECTED) { + stopSelf(); + } + } catch (Throwable t) { + Log.e(TAG, "Timer failed: ", t); + } + } + + @Override public void onCreate() { // Create a reference to the NotificationManager so that we can update our notifcation text later //mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); setState(STATE_READY); + + // Start our timer - first event in 10 seconds, then every 10 seconds after that. + timer.scheduleAtFixedRate(new TimerTask(){ public void run() {onTimerTick();}}, 10000L, 10000L); + } @Override @@ -248,6 +269,9 @@ public class TelemetryService extends Service { // Demote us from the foreground, and cancel the persistent notification. stopForeground(true); + // Stop our timer + if (timer != null) {timer.cancel();} + // Tell the user we stopped. Toast.makeText(this, R.string.telemetry_service_stopped, Toast.LENGTH_SHORT).show(); } -- cgit v1.2.3 From 543ecb530d6fdf188a746ac59b72544e69bad830 Mon Sep 17 00:00:00 2001 From: Mike Beattie Date: Fri, 31 Aug 2012 16:38:21 +1200 Subject: altosdroid: complete frequency change dialog Also implement Service IPC to action request. Signed-off-by: Mike Beattie --- altosdroid/res/menu/option_menu.xml | 2 +- altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java | 6 +++++- .../src/org/altusmetrum/AltosDroid/TelemetryService.java | 10 ++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/res/menu/option_menu.xml b/altosdroid/res/menu/option_menu.xml index 525541e1..d7ba8305 100644 --- a/altosdroid/res/menu/option_menu.xml +++ b/altosdroid/res/menu/option_menu.xml @@ -18,6 +18,6 @@ android:icon="@android:drawable/ic_menu_search" android:title="@string/connect_device" /> diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java index 27760162..de0b02a6 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java @@ -355,7 +355,10 @@ public class AltosDroid extends Activity { } void setFrequency(double freq) { - + try { + mService.send(Message.obtain(null, TelemetryService.MSG_SETFREQUENCY, freq)); + } catch (RemoteException e) { + } } void setFrequency(String freq) { @@ -399,6 +402,7 @@ public class AltosDroid extends Activity { } }); AlertDialog alert = builder.create(); + alert.show(); return true; } return false; diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index ffe96946..6a1f1c5a 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -52,6 +52,7 @@ public class TelemetryService extends Service { static final int MSG_CONNECT_FAILED = 5; static final int MSG_DISCONNECTED = 6; static final int MSG_TELEMETRY = 7; + static final int MSG_SETFREQUENCY = 8; public static final int STATE_NONE = 0; public static final int STATE_READY = 1; @@ -126,6 +127,15 @@ public class TelemetryService extends Service { case MSG_TELEMETRY: s.sendMessageToClients(Message.obtain(null, AltosDroid.MSG_TELEMETRY, msg.obj)); break; + case MSG_SETFREQUENCY: + if (s.state == STATE_CONNECTED) { + try { + s.mAltosBluetooth.set_radio_frequency((Double) msg.obj); + } catch (InterruptedException e) { + } catch (TimeoutException e) { + } + } + break; default: super.handleMessage(msg); } -- cgit v1.2.3 From 4420d4a9fc011ed970af506ef771dfb81580b666 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Tue, 11 Sep 2012 00:49:48 -0700 Subject: Fix Latin-1 encoded copyright symbols in AltosDroid java code Otherwise, we get complaints when compiling these files. Signed-off-by: Keith Packard --- altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java | 4 ++-- altosdroid/src/org/altusmetrum/AltosDroid/AltosVoice.java | 4 ++-- altosdroid/src/org/altusmetrum/AltosDroid/TelemetryReader.java | 4 ++-- altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) (limited to 'altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java') diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java index 4c8136aa..9fcc4eba 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/AltosBluetooth.java @@ -1,6 +1,6 @@ /* - * Copyright © 2011 Keith Packard - * Copyright © 2012 Mike Beattie + * Copyright © 2011 Keith Packard + * Copyright © 2012 Mike Beattie * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosVoice.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosVoice.java index 3f7c5979..3382d551 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/AltosVoice.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/AltosVoice.java @@ -1,6 +1,6 @@ /* - * Copyright © 2011 Keith Packard - * Copyright © 2012 Mike Beattie + * Copyright © 2011 Keith Packard + * Copyright © 2012 Mike Beattie * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryReader.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryReader.java index c47e4942..66e9c6bd 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryReader.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryReader.java @@ -1,6 +1,6 @@ /* - * Copyright © 2011 Keith Packard - * Copyright © 2012 Mike Beattie + * Copyright © 2011 Keith Packard + * Copyright © 2012 Mike Beattie * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java index 6a1f1c5a..393fd2f6 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java @@ -1,5 +1,5 @@ /* - * Copyright © 2012 Mike Beattie + * Copyright © 2012 Mike Beattie * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by -- cgit v1.2.3