Fix comments for 4a7e14e5837e

This commit is contained in:
Jimmy Shih
2012-01-28 16:03:40 -08:00
parent da013e9d51
commit 5f99083747
13 changed files with 63 additions and 179 deletions
+1 -1
View File
@@ -11,5 +11,5 @@
split.density=false split.density=false
proguard.config=proguard.cfg proguard.config=proguard.cfg
# Project target. # Project target.
target=Google Inc.:Google APIs:7 target=Google Inc.:Google APIs:15
android.library.reference.1=../MyTracksLib android.library.reference.1=../MyTracksLib
@@ -39,6 +39,7 @@ import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnClickListener;
import android.location.Location; import android.location.Location;
import android.os.Bundle; import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log; import android.util.Log;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
@@ -115,7 +116,7 @@ public class ChartActivity extends Activity implements TrackDataListener {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
// The volume we want to control is the Text-To-Speech volume // 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); requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.mytracks_charts); setContentView(R.layout.mytracks_charts);
@@ -43,6 +43,7 @@ import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Parcelable; import android.os.Parcelable;
import android.provider.Settings; import android.provider.Settings;
import android.speech.tts.TextToSpeech;
import android.util.Log; import android.util.Log;
import android.view.ContextMenu; import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo; import android.view.ContextMenu.ContextMenuInfo;
@@ -133,7 +134,7 @@ public class MapActivity extends com.google.android.maps.MapActivity
super.onCreate(bundle); super.onCreate(bundle);
// The volume we want to control is the Text-To-Speech volume // 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: // We don't need a window title bar:
requestWindowFeature(Window.FEATURE_NO_TITLE); requestWindowFeature(Window.FEATURE_NO_TITLE);
@@ -46,6 +46,7 @@ import android.content.res.Resources;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.RemoteException; import android.os.RemoteException;
import android.speech.tts.TextToSpeech;
import android.util.Log; import android.util.Log;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
@@ -156,7 +157,7 @@ public class MyTracks extends TabActivity implements OnTouchListener {
menuManager = new MenuManager(this); menuManager = new MenuManager(this);
serviceConnection = new TrackRecordingServiceConnection(this, serviceBindCallback); serviceConnection = new TrackRecordingServiceConnection(this, serviceBindCallback);
setVolumeControlStream(StatusAnnouncerTask.getVolumeStream()); setVolumeControlStream(TextToSpeech.Engine.DEFAULT_STREAM);
// We don't need a window title bar: // We don't need a window title bar:
requestWindowFeature(Window.FEATURE_NO_TITLE); requestWindowFeature(Window.FEATURE_NO_TITLE);
@@ -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.BackupActivityHelper;
import com.google.android.apps.mytracks.io.backup.BackupPreferencesListener; 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.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.ApiAdapterFactory;
import com.google.android.apps.mytracks.util.BluetoothDeviceUtils; import com.google.android.apps.mytracks.util.BluetoothDeviceUtils;
import com.google.android.apps.mytracks.util.UnitConversions; import com.google.android.apps.mytracks.util.UnitConversions;
import com.google.android.maps.mytracks.R; import com.google.android.maps.mytracks.R;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnClickListener;
import android.content.Intent; import android.content.Intent;
@@ -44,6 +44,7 @@ import android.preference.PreferenceCategory;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.preference.PreferenceScreen; import android.preference.PreferenceScreen;
import android.provider.Settings; import android.provider.Settings;
import android.speech.tts.TextToSpeech;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
@@ -102,7 +103,7 @@ public class SettingsActivity extends PreferenceActivity {
super.onCreate(icicle); super.onCreate(icicle);
// The volume we want to control is the Text-To-Speech volume // 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 // Tell it where to read/write preferences
PreferenceManager preferenceManager = getPreferenceManager(); PreferenceManager preferenceManager = getPreferenceManager();
@@ -566,7 +567,10 @@ public class SettingsActivity extends PreferenceActivity {
List<String> entryValues = new ArrayList<String>(); List<String> entryValues = new ArrayList<String>();
// The actual devices // 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[] entriesArray = entries.toArray(new CharSequence[entries.size()]);
CharSequence[] entryValuesArray = entryValues.toArray(new CharSequence[entryValues.size()]); CharSequence[] entryValuesArray = entryValues.toArray(new CharSequence[entryValues.size()]);
@@ -31,6 +31,7 @@ import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.location.Location; import android.location.Location;
import android.os.Bundle; import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.util.Log; import android.util.Log;
import android.view.Window; import android.view.Window;
@@ -104,7 +105,7 @@ public class StatsActivity extends Activity implements TrackDataListener {
utils = new StatsUtilities(this); utils = new StatsUtilities(this);
// The volume we want to control is the Text-To-Speech volume // 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: // We don't need a window title bar:
requestWindowFeature(Window.FEATURE_NO_TITLE); requestWindowFeature(Window.FEATURE_NO_TITLE);
@@ -31,6 +31,7 @@ import android.util.Log;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.UUID;
/** /**
* This class does all the work for setting up and managing Bluetooth * This class does all the work for setting up and managing Bluetooth
@@ -41,6 +42,10 @@ import java.io.OutputStream;
* @author Sandor Dornbush * @author Sandor Dornbush
*/ */
public class BluetoothConnectionManager { 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; private MessageParser parser;
// Member fields // Member fields
@@ -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. * Gets a string to announce the time.
* *
@@ -16,6 +16,7 @@
package com.google.android.apps.mytracks.util; package com.google.android.apps.mytracks.util;
import com.google.android.apps.mytracks.Constants; import com.google.android.apps.mytracks.Constants;
import com.google.android.apps.mytracks.services.sensors.BluetoothConnectionManager;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket; import android.bluetooth.BluetoothSocket;
@@ -23,7 +24,6 @@ import android.util.Log;
import java.io.IOException; import java.io.IOException;
/** /**
* API level 10 specific implementation of the {@link ApiAdapter}. * API level 10 specific implementation of the {@link ApiAdapter}.
* *
@@ -34,10 +34,11 @@ public class Api10Adapter extends Api9Adapter {
@Override @Override
public BluetoothSocket getBluetoothSocket(BluetoothDevice bluetoothDevice) throws IOException { public BluetoothSocket getBluetoothSocket(BluetoothDevice bluetoothDevice) throws IOException {
try { try {
return bluetoothDevice.createInsecureRfcommSocketToServiceRecord(SPP_UUID); return bluetoothDevice.createInsecureRfcommSocketToServiceRecord(
BluetoothConnectionManager.SPP_UUID);
} catch (IOException e) { } catch (IOException e) {
Log.d(Constants.TAG, "Unable to create insecure connection", e); Log.d(Constants.TAG, "Unable to create insecure connection", e);
} }
return bluetoothDevice.createRfcommSocketToServiceRecord(SPP_UUID); return bluetoothDevice.createRfcommSocketToServiceRecord(BluetoothConnectionManager.SPP_UUID);
}; };
} }
@@ -17,6 +17,7 @@ package com.google.android.apps.mytracks.util;
import com.google.android.apps.mytracks.Constants; import com.google.android.apps.mytracks.Constants;
import com.google.android.apps.mytracks.io.backup.BackupPreferencesListener; 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.PeriodicTask;
import com.google.android.apps.mytracks.services.tasks.StatusAnnouncerTask; import com.google.android.apps.mytracks.services.tasks.StatusAnnouncerTask;
import com.google.api.client.http.HttpTransport; import com.google.api.client.http.HttpTransport;
@@ -96,6 +97,6 @@ public class Api7Adapter implements ApiAdapter {
} catch (InvocationTargetException e) { } catch (InvocationTargetException e) {
Log.d(Constants.TAG, "Unable to create insecure connection", e); Log.d(Constants.TAG, "Unable to create insecure connection", e);
} }
return bluetoothDevice.createRfcommSocketToServiceRecord(SPP_UUID); return bluetoothDevice.createRfcommSocketToServiceRecord(BluetoothConnectionManager.SPP_UUID);
} }
} }
@@ -25,7 +25,6 @@ import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import java.io.IOException; import java.io.IOException;
import java.util.UUID;
/** /**
* A set of methods that may be implemented differently depending on the Android * A set of methods that may be implemented differently depending on the Android
@@ -35,9 +34,6 @@ import java.util.UUID;
*/ */
public interface ApiAdapter { 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. * Gets a status announcer task.
* <p> * <p>
@@ -31,15 +31,18 @@ public class ApiAdapterFactory {
*/ */
public static ApiAdapter getApiAdapter() { public static ApiAdapter getApiAdapter() {
if (apiAdapter == null) { if (apiAdapter == null) {
int apiLevel = Integer.parseInt(Build.VERSION.SDK); if (Build.VERSION.SDK_INT >= 10) {
if (apiLevel >= 10) {
apiAdapter = new Api10Adapter(); apiAdapter = new Api10Adapter();
} else if (apiLevel >= 9) { return apiAdapter;
} else if (Build.VERSION.SDK_INT >= 9) {
apiAdapter = new Api9Adapter(); apiAdapter = new Api9Adapter();
} else if (apiLevel >= 8) { return apiAdapter;
} else if (Build.VERSION.SDK_INT >= 8) {
apiAdapter = new Api8Adapter(); apiAdapter = new Api8Adapter();
return apiAdapter;
} else { } else {
apiAdapter = new Api7Adapter(); apiAdapter = new Api7Adapter();
return apiAdapter;
} }
} }
return apiAdapter; return apiAdapter;
@@ -15,172 +15,49 @@
*/ */
package com.google.android.apps.mytracks.util; package com.google.android.apps.mytracks.util;
import com.google.android.apps.mytracks.Constants;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
import android.util.Log;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
/** /**
* Utilities for dealing with bluetooth devices. * 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 * @author Rodrigo Damazio
*/ */
public abstract class BluetoothDeviceUtils { public class BluetoothDeviceUtils {
public static final String ANY_DEVICE = "any";
private static BluetoothDeviceUtils instance; private BluetoothDeviceUtils() {}
/** /**
* Dummy implementation, for systems that don't support bluetooth. * Populates the device names and the device addresses with all the suitable
*/
private static class DummyImpl extends BluetoothDeviceUtils {
@Override
public void populateDeviceLists(List<String> deviceNames, List<String> 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<String> deviceNames, List<String> deviceAddresses) {
ensureNotDiscovering();
Set<BluetoothDevice> 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<BluetoothDevice> 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
* bluetooth devices. * bluetooth devices.
* *
* @param deviceNames the list to populate with user-visible names * @param bluetoothAdapter the bluetooth adapter
* @param deviceAddresses the list to populate with device addresses * @param deviceNames list of device names
* @param deviceAddresses list of device addresses
*/ */
public abstract void populateDeviceLists(List<String> deviceNames, List<String> deviceAddresses); public static void populateDeviceLists(
BluetoothAdapter bluetoothAdapter, List<String> deviceNames, List<String> deviceAddresses) {
// Ensure the bluetooth adapter is not in discovery mode.
bluetoothAdapter.cancelDiscovery();
/** Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
* Finds the bluetooth device with the given address. for (BluetoothDevice device : pairedDevices) {
* BluetoothClass bluetoothClass = device.getBluetoothClass();
* @param targetDeviceAddress the address of the device, or if (bluetoothClass != null) {
* {@link #ANY_DEVICE} for using the first suitable device // Not really sure what we want, but I know what we don't want.
* @return the device's descriptor, or null if not found switch (bluetoothClass.getMajorDeviceClass()) {
*/ case BluetoothClass.Device.Major.COMPUTER:
public abstract BluetoothDevice findDeviceMatching(String targetDeviceAddress); case BluetoothClass.Device.Major.PHONE:
break;
default:
deviceAddresses.add(device.getAddress());
deviceNames.add(device.getName());
}
}
}
}
} }