From 5f9908374785d1618d93721824245cb587d01e31 Mon Sep 17 00:00:00 2001 From: Jimmy Shih Date: Sat, 28 Jan 2012 16:03:40 -0800 Subject: [PATCH] Fix comments for 4a7e14e5837e --- MyTracks/project.properties | 2 +- .../android/apps/mytracks/ChartActivity.java | 3 +- .../android/apps/mytracks/MapActivity.java | 3 +- .../android/apps/mytracks/MyTracks.java | 3 +- .../apps/mytracks/SettingsActivity.java | 10 +- .../android/apps/mytracks/StatsActivity.java | 3 +- .../sensors/BluetoothConnectionManager.java | 5 + .../services/tasks/StatusAnnouncerTask.java | 7 - .../apps/mytracks/util/Api10Adapter.java | 11 +- .../apps/mytracks/util/Api7Adapter.java | 3 +- .../apps/mytracks/util/ApiAdapter.java | 4 - .../apps/mytracks/util/ApiAdapterFactory.java | 11 +- .../mytracks/util/BluetoothDeviceUtils.java | 177 +++--------------- 13 files changed, 63 insertions(+), 179 deletions(-) diff --git a/MyTracks/project.properties b/MyTracks/project.properties index 81cd85e3e..c946f8ad0 100644 --- a/MyTracks/project.properties +++ b/MyTracks/project.properties @@ -11,5 +11,5 @@ split.density=false proguard.config=proguard.cfg # Project target. -target=Google Inc.:Google APIs:7 +target=Google Inc.:Google APIs:15 android.library.reference.1=../MyTracksLib diff --git a/MyTracks/src/com/google/android/apps/mytracks/ChartActivity.java b/MyTracks/src/com/google/android/apps/mytracks/ChartActivity.java index d9fbc2291..ddebba571 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/ChartActivity.java +++ b/MyTracks/src/com/google/android/apps/mytracks/ChartActivity.java @@ -39,6 +39,7 @@ import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.location.Location; import android.os.Bundle; +import android.speech.tts.TextToSpeech; import android.util.Log; import android.view.Menu; import android.view.MenuItem; @@ -115,7 +116,7 @@ public class ChartActivity extends Activity implements TrackDataListener { super.onCreate(savedInstanceState); // The volume we want to control is the Text-To-Speech volume - setVolumeControlStream(StatusAnnouncerTask.getVolumeStream()); + setVolumeControlStream(TextToSpeech.Engine.DEFAULT_STREAM); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.mytracks_charts); diff --git a/MyTracks/src/com/google/android/apps/mytracks/MapActivity.java b/MyTracks/src/com/google/android/apps/mytracks/MapActivity.java index e014eee32..a2ea43f70 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/MapActivity.java +++ b/MyTracks/src/com/google/android/apps/mytracks/MapActivity.java @@ -43,6 +43,7 @@ import android.net.Uri; import android.os.Bundle; import android.os.Parcelable; import android.provider.Settings; +import android.speech.tts.TextToSpeech; import android.util.Log; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; @@ -133,7 +134,7 @@ public class MapActivity extends com.google.android.maps.MapActivity super.onCreate(bundle); // The volume we want to control is the Text-To-Speech volume - setVolumeControlStream(StatusAnnouncerTask.getVolumeStream()); + setVolumeControlStream(TextToSpeech.Engine.DEFAULT_STREAM); // We don't need a window title bar: requestWindowFeature(Window.FEATURE_NO_TITLE); diff --git a/MyTracks/src/com/google/android/apps/mytracks/MyTracks.java b/MyTracks/src/com/google/android/apps/mytracks/MyTracks.java index 9af8df263..d789d0247 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/MyTracks.java +++ b/MyTracks/src/com/google/android/apps/mytracks/MyTracks.java @@ -46,6 +46,7 @@ import android.content.res.Resources; import android.net.Uri; import android.os.Bundle; import android.os.RemoteException; +import android.speech.tts.TextToSpeech; import android.util.Log; import android.view.Menu; import android.view.MenuItem; @@ -156,7 +157,7 @@ public class MyTracks extends TabActivity implements OnTouchListener { menuManager = new MenuManager(this); serviceConnection = new TrackRecordingServiceConnection(this, serviceBindCallback); - setVolumeControlStream(StatusAnnouncerTask.getVolumeStream()); + setVolumeControlStream(TextToSpeech.Engine.DEFAULT_STREAM); // We don't need a window title bar: requestWindowFeature(Window.FEATURE_NO_TITLE); diff --git a/MyTracks/src/com/google/android/apps/mytracks/SettingsActivity.java b/MyTracks/src/com/google/android/apps/mytracks/SettingsActivity.java index 1dd19c1cf..a61fd744d 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/SettingsActivity.java +++ b/MyTracks/src/com/google/android/apps/mytracks/SettingsActivity.java @@ -20,13 +20,13 @@ import static com.google.android.apps.mytracks.Constants.TAG; import com.google.android.apps.mytracks.io.backup.BackupActivityHelper; import com.google.android.apps.mytracks.io.backup.BackupPreferencesListener; import com.google.android.apps.mytracks.services.sensors.ant.AntUtils; -import com.google.android.apps.mytracks.services.tasks.StatusAnnouncerTask; import com.google.android.apps.mytracks.util.ApiAdapterFactory; import com.google.android.apps.mytracks.util.BluetoothDeviceUtils; import com.google.android.apps.mytracks.util.UnitConversions; import com.google.android.maps.mytracks.R; import android.app.AlertDialog; +import android.bluetooth.BluetoothAdapter; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.Intent; @@ -44,6 +44,7 @@ import android.preference.PreferenceCategory; import android.preference.PreferenceManager; import android.preference.PreferenceScreen; import android.provider.Settings; +import android.speech.tts.TextToSpeech; import android.util.Log; import android.widget.Toast; @@ -102,7 +103,7 @@ public class SettingsActivity extends PreferenceActivity { super.onCreate(icicle); // The volume we want to control is the Text-To-Speech volume - setVolumeControlStream(StatusAnnouncerTask.getVolumeStream()); + setVolumeControlStream(TextToSpeech.Engine.DEFAULT_STREAM); // Tell it where to read/write preferences PreferenceManager preferenceManager = getPreferenceManager(); @@ -566,7 +567,10 @@ public class SettingsActivity extends PreferenceActivity { List entryValues = new ArrayList(); // The actual devices - BluetoothDeviceUtils.getInstance().populateDeviceLists(entries, entryValues); + BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); + if (bluetoothAdapter != null) { + BluetoothDeviceUtils.populateDeviceLists(bluetoothAdapter, entries, entryValues); + } CharSequence[] entriesArray = entries.toArray(new CharSequence[entries.size()]); CharSequence[] entryValuesArray = entryValues.toArray(new CharSequence[entryValues.size()]); diff --git a/MyTracks/src/com/google/android/apps/mytracks/StatsActivity.java b/MyTracks/src/com/google/android/apps/mytracks/StatsActivity.java index e374187c0..adb62c2d1 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/StatsActivity.java +++ b/MyTracks/src/com/google/android/apps/mytracks/StatsActivity.java @@ -31,6 +31,7 @@ import android.content.Context; import android.content.SharedPreferences; import android.location.Location; import android.os.Bundle; +import android.speech.tts.TextToSpeech; import android.util.DisplayMetrics; import android.util.Log; import android.view.Window; @@ -104,7 +105,7 @@ public class StatsActivity extends Activity implements TrackDataListener { utils = new StatsUtilities(this); // The volume we want to control is the Text-To-Speech volume - setVolumeControlStream(StatusAnnouncerTask.getVolumeStream()); + setVolumeControlStream(TextToSpeech.Engine.DEFAULT_STREAM); // We don't need a window title bar: requestWindowFeature(Window.FEATURE_NO_TITLE); diff --git a/MyTracks/src/com/google/android/apps/mytracks/services/sensors/BluetoothConnectionManager.java b/MyTracks/src/com/google/android/apps/mytracks/services/sensors/BluetoothConnectionManager.java index eb174091e..558bfe423 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/services/sensors/BluetoothConnectionManager.java +++ b/MyTracks/src/com/google/android/apps/mytracks/services/sensors/BluetoothConnectionManager.java @@ -31,6 +31,7 @@ import android.util.Log; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.util.UUID; /** * This class does all the work for setting up and managing Bluetooth @@ -41,6 +42,10 @@ import java.io.OutputStream; * @author Sandor Dornbush */ public class BluetoothConnectionManager { + + // Unique Bluetooth UUID for My Tracks + public static final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); + private MessageParser parser; // Member fields diff --git a/MyTracks/src/com/google/android/apps/mytracks/services/tasks/StatusAnnouncerTask.java b/MyTracks/src/com/google/android/apps/mytracks/services/tasks/StatusAnnouncerTask.java index f6532249b..24bbd674b 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/services/tasks/StatusAnnouncerTask.java +++ b/MyTracks/src/com/google/android/apps/mytracks/services/tasks/StatusAnnouncerTask.java @@ -331,13 +331,6 @@ public class StatusAnnouncerTask implements PeriodicTask { } } - /** - * Returns the volume stream to use for controlling announcement volume. - */ - public static int getVolumeStream() { - return TextToSpeech.Engine.DEFAULT_STREAM; - } - /** * Gets a string to announce the time. * diff --git a/MyTracks/src/com/google/android/apps/mytracks/util/Api10Adapter.java b/MyTracks/src/com/google/android/apps/mytracks/util/Api10Adapter.java index 6e9250531..8bdece372 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/Api10Adapter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/Api10Adapter.java @@ -16,6 +16,7 @@ package com.google.android.apps.mytracks.util; import com.google.android.apps.mytracks.Constants; +import com.google.android.apps.mytracks.services.sensors.BluetoothConnectionManager; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; @@ -23,21 +24,21 @@ import android.util.Log; import java.io.IOException; - /** * API level 10 specific implementation of the {@link ApiAdapter}. * * @author Jimmy Shih */ public class Api10Adapter extends Api9Adapter { - + @Override public BluetoothSocket getBluetoothSocket(BluetoothDevice bluetoothDevice) throws IOException { - try { - return bluetoothDevice.createInsecureRfcommSocketToServiceRecord(SPP_UUID); + try { + return bluetoothDevice.createInsecureRfcommSocketToServiceRecord( + BluetoothConnectionManager.SPP_UUID); } catch (IOException e) { Log.d(Constants.TAG, "Unable to create insecure connection", e); } - return bluetoothDevice.createRfcommSocketToServiceRecord(SPP_UUID); + return bluetoothDevice.createRfcommSocketToServiceRecord(BluetoothConnectionManager.SPP_UUID); }; } diff --git a/MyTracks/src/com/google/android/apps/mytracks/util/Api7Adapter.java b/MyTracks/src/com/google/android/apps/mytracks/util/Api7Adapter.java index 50f1320fd..6b572d045 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/Api7Adapter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/Api7Adapter.java @@ -17,6 +17,7 @@ package com.google.android.apps.mytracks.util; import com.google.android.apps.mytracks.Constants; import com.google.android.apps.mytracks.io.backup.BackupPreferencesListener; +import com.google.android.apps.mytracks.services.sensors.BluetoothConnectionManager; import com.google.android.apps.mytracks.services.tasks.PeriodicTask; import com.google.android.apps.mytracks.services.tasks.StatusAnnouncerTask; import com.google.api.client.http.HttpTransport; @@ -96,6 +97,6 @@ public class Api7Adapter implements ApiAdapter { } catch (InvocationTargetException e) { Log.d(Constants.TAG, "Unable to create insecure connection", e); } - return bluetoothDevice.createRfcommSocketToServiceRecord(SPP_UUID); + return bluetoothDevice.createRfcommSocketToServiceRecord(BluetoothConnectionManager.SPP_UUID); } } diff --git a/MyTracks/src/com/google/android/apps/mytracks/util/ApiAdapter.java b/MyTracks/src/com/google/android/apps/mytracks/util/ApiAdapter.java index 0d4d2d286..868c43af1 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/ApiAdapter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/ApiAdapter.java @@ -25,7 +25,6 @@ import android.content.Context; import android.content.SharedPreferences; import java.io.IOException; -import java.util.UUID; /** * A set of methods that may be implemented differently depending on the Android @@ -35,9 +34,6 @@ import java.util.UUID; */ public interface ApiAdapter { - // Unique Bluetooth UUID for My Tracks - public static final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); - /** * Gets a status announcer task. *

diff --git a/MyTracks/src/com/google/android/apps/mytracks/util/ApiAdapterFactory.java b/MyTracks/src/com/google/android/apps/mytracks/util/ApiAdapterFactory.java index ef7eed0d2..5af16534e 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/ApiAdapterFactory.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/ApiAdapterFactory.java @@ -31,15 +31,18 @@ public class ApiAdapterFactory { */ public static ApiAdapter getApiAdapter() { if (apiAdapter == null) { - int apiLevel = Integer.parseInt(Build.VERSION.SDK); - if (apiLevel >= 10) { + if (Build.VERSION.SDK_INT >= 10) { apiAdapter = new Api10Adapter(); - } else if (apiLevel >= 9) { + return apiAdapter; + } else if (Build.VERSION.SDK_INT >= 9) { apiAdapter = new Api9Adapter(); - } else if (apiLevel >= 8) { + return apiAdapter; + } else if (Build.VERSION.SDK_INT >= 8) { apiAdapter = new Api8Adapter(); + return apiAdapter; } else { apiAdapter = new Api7Adapter(); + return apiAdapter; } } return apiAdapter; diff --git a/MyTracks/src/com/google/android/apps/mytracks/util/BluetoothDeviceUtils.java b/MyTracks/src/com/google/android/apps/mytracks/util/BluetoothDeviceUtils.java index 3b43adb41..548d873c7 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/BluetoothDeviceUtils.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/BluetoothDeviceUtils.java @@ -15,172 +15,49 @@ */ package com.google.android.apps.mytracks.util; -import com.google.android.apps.mytracks.Constants; - import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothDevice; -import android.util.Log; import java.util.List; import java.util.Set; /** * Utilities for dealing with bluetooth devices. - * This can be used safely even in systems that don't support bluetooth, - * in which case a dummy implementation will be used. * * @author Rodrigo Damazio */ -public abstract class BluetoothDeviceUtils { - public static final String ANY_DEVICE = "any"; - private static BluetoothDeviceUtils instance; +public class BluetoothDeviceUtils { + private BluetoothDeviceUtils() {} + /** - * Dummy implementation, for systems that don't support bluetooth. - */ - private static class DummyImpl extends BluetoothDeviceUtils { - @Override - public void populateDeviceLists(List deviceNames, List deviceAddresses) { - // Do nothing - no devices to add - } - - @Override - public BluetoothDevice findDeviceMatching(String targetDeviceAddress) { - return null; - } - } - - /** - * Real implementation, for systems that DO support bluetooth. - */ - private static class RealImpl extends BluetoothDeviceUtils { - private final BluetoothAdapter bluetoothAdapter; - - public RealImpl() { - bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); - - if (bluetoothAdapter == null) { - throw new IllegalStateException("Unable to get bluetooth adapter"); - } - } - - @Override - public void populateDeviceLists(List deviceNames, List deviceAddresses) { - ensureNotDiscovering(); - - Set pairedDevices = bluetoothAdapter.getBondedDevices(); - for (BluetoothDevice device : pairedDevices) { - BluetoothClass bluetoothClass = device.getBluetoothClass(); - if (bluetoothClass != null) { - // Not really sure what we want, but I know what we don't want. - switch(bluetoothClass.getMajorDeviceClass()) { - case BluetoothClass.Device.Major.COMPUTER: - case BluetoothClass.Device.Major.PHONE: - break; - default: - deviceAddresses.add(device.getAddress()); - deviceNames.add(device.getName()); - } - } - } - } - - @Override - public BluetoothDevice findDeviceMatching(String targetDeviceAddress) { - if (targetDeviceAddress.equals(ANY_DEVICE)) { - return findAnyDevice(); - } else { - return findDeviceByAddress(targetDeviceAddress); - } - } - - /** - * Finds and returns the first suitable bluetooth sensor. - */ - private BluetoothDevice findAnyDevice() { - ensureNotDiscovering(); - - Set pairedDevices = bluetoothAdapter.getBondedDevices(); - for (BluetoothDevice device : pairedDevices) { - // Look for the first paired computer device - if (isSuitableDevice(device)) { - return device; - } - } - - return null; - } - - /** - * Finds and returns a device with the given address, or null if it's not - * a suitable sensor. - */ - private BluetoothDevice findDeviceByAddress(String targetDeviceAddress) { - ensureNotDiscovering(); - - BluetoothDevice device = bluetoothAdapter.getRemoteDevice(targetDeviceAddress); - if (isSuitableDevice(device)) { - return device; - } - - return null; - } - - /** - * Ensures the bluetooth adapter is not in discovery mode. - */ - private void ensureNotDiscovering() { - // If it's in discovery mode, cancel that for now. - bluetoothAdapter.cancelDiscovery(); - } - - /** - * Checks whether the given device is a suitable sensor. - * - * @param device the device to check - * @return true if it's suitable, false otherwise - */ - private boolean isSuitableDevice(BluetoothDevice device) { - // Check that the device is bonded - if (device.getBondState() != BluetoothDevice.BOND_BONDED) { - return false; - } - - return true; - } - } - - /** - * Returns the proper (singleton) instance of this class. - */ - public static BluetoothDeviceUtils getInstance() { - if (instance == null) { - try { - instance = new RealImpl(); - } catch (IllegalStateException ise) { - Log.w(Constants.TAG, "Oops, I mean, using dummy bluetooth utils", ise); - instance = new DummyImpl(); - } - } - return instance; - } - - /** - * Populates the given lists with the names and addresses of all suitable + * Populates the device names and the device addresses with all the suitable * bluetooth devices. * - * @param deviceNames the list to populate with user-visible names - * @param deviceAddresses the list to populate with device addresses + * @param bluetoothAdapter the bluetooth adapter + * @param deviceNames list of device names + * @param deviceAddresses list of device addresses */ - public abstract void populateDeviceLists(List deviceNames, List deviceAddresses); + public static void populateDeviceLists( + BluetoothAdapter bluetoothAdapter, List deviceNames, List deviceAddresses) { + // Ensure the bluetooth adapter is not in discovery mode. + bluetoothAdapter.cancelDiscovery(); - /** - * Finds the bluetooth device with the given address. - * - * @param targetDeviceAddress the address of the device, or - * {@link #ANY_DEVICE} for using the first suitable device - * @return the device's descriptor, or null if not found - */ - public abstract BluetoothDevice findDeviceMatching(String targetDeviceAddress); + Set pairedDevices = bluetoothAdapter.getBondedDevices(); + for (BluetoothDevice device : pairedDevices) { + BluetoothClass bluetoothClass = device.getBluetoothClass(); + if (bluetoothClass != null) { + // Not really sure what we want, but I know what we don't want. + switch (bluetoothClass.getMajorDeviceClass()) { + case BluetoothClass.Device.Major.COMPUTER: + case BluetoothClass.Device.Major.PHONE: + break; + default: + deviceAddresses.add(device.getAddress()); + deviceNames.add(device.getName()); + } + } + } + } }