Place sensor settings in SensorSettingsActivity.

This commit is contained in:
Jimmy Shih
2012-05-04 17:48:33 -07:00
parent 3008b335a7
commit 77903ed2c9
16 changed files with 287 additions and 223 deletions
@@ -19,12 +19,12 @@ import static com.google.android.apps.mytracks.Constants.TAG;
import com.google.android.apps.mytracks.Constants;
import com.google.android.apps.mytracks.content.Sensor;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.maps.mytracks.R;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
@@ -127,16 +127,20 @@ public class BluetoothSensorManager extends SensorManager {
Log.w(Constants.TAG, "Disabled manager onStartTrack");
return;
}
SharedPreferences prefs = context.getSharedPreferences(
Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
String address =
prefs.getString(context.getString(R.string.bluetooth_sensor_key), "");
if (address == null || address.equals("")) {
String address = PreferencesUtils.getString(
context, R.string.bluetooth_sensor_key, PreferencesUtils.BLUETOOTH_SENSOR_DEFAULT);
if (PreferencesUtils.BLUETOOTH_SENSOR_DEFAULT.equals(address)) {
return;
}
Log.w(Constants.TAG, "Connecting to bluetooth sensor: " + address);
// Get the BluetoothDevice object
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address);
BluetoothDevice device;
try {
device = bluetoothAdapter.getRemoteDevice(address);
} catch (IllegalArgumentException e) {
Log.d(Constants.TAG, "Invalid address " + address, e);
return;
}
// Attempt to connect to the device
connectionManager.connect(device);
@@ -61,33 +61,35 @@ public class SensorManagerFactory {
context = context.getApplicationContext();
String sensor = prefs.getString(context.getString(R.string.sensor_type_key), null);
Log.i(Constants.TAG, "Creating sensor of type: " + sensor);
String sensorTypeValueNone = context.getString(R.string.sensor_type_value_none);
String sensorType = prefs.getString(
context.getString(R.string.sensor_type_key), sensorTypeValueNone);
Log.i(Constants.TAG, "Creating sensor of type: " + sensorType);
if (sensor == null) {
if (sensorTypeValueNone.equals(sensorType)) {
reset();
return null;
}
if (sensor.equals(activeSensorType)) {
if (sensorType.equals(activeSensorType)) {
Log.i(Constants.TAG, "Returning existing sensor manager.");
refCount++;
return activeSensorManager;
}
reset();
if (sensor.equals(context.getString(R.string.sensor_type_value_ant))) {
if (sensorType.equals(context.getString(R.string.sensor_type_value_ant))) {
activeSensorManager = new AntDirectSensorManager(context);
} else if (sensor.equals(context.getString(R.string.sensor_type_value_srm_ant_bridge))) {
} else if (sensorType.equals(context.getString(R.string.sensor_type_value_srm_ant_bridge))) {
activeSensorManager = new AntSrmBridgeSensorManager(context);
} else if (sensor.equals(context.getString(R.string.sensor_type_value_zephyr))) {
} else if (sensorType.equals(context.getString(R.string.sensor_type_value_zephyr))) {
activeSensorManager = new ZephyrSensorManager(context);
} else if (sensor.equals(context.getString(R.string.sensor_type_value_polar))) {
} else if (sensorType.equals(context.getString(R.string.sensor_type_value_polar))) {
activeSensorManager = new PolarSensorManager(context);
} else {
Log.w(Constants.TAG, "Unable to find sensor type: " + sensor);
Log.w(Constants.TAG, "Unable to find sensor type: " + sensorType);
return null;
}
activeSensorType = sensor;
activeSensorType = sensorType;
refCount = 1;
activeSensorManager.onStartTrack();
return activeSensorManager;
@@ -20,13 +20,11 @@ import static com.google.android.apps.mytracks.Constants.TAG;
import com.dsi.ant.AntDefine;
import com.dsi.ant.AntMesg;
import com.dsi.ant.exception.AntInterfaceException;
import com.google.android.apps.mytracks.Constants;
import com.google.android.apps.mytracks.content.Sensor;
import com.google.android.apps.mytracks.util.ApiAdapterFactory;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.maps.mytracks.R;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
/**
@@ -107,11 +105,7 @@ public class AntDirectSensorManager extends AntSensorManager
}
Log.i(TAG, "Found ANT device id: " + deviceNumber + " on channel: " + channel);
SharedPreferences prefs = context.getSharedPreferences(
Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(context.getString(sensorIdKeys[channel]), deviceNumber);
ApiAdapterFactory.getApiAdapter().applyPreferenceChanges(editor);
PreferencesUtils.setInt(context, sensorIdKeys[channel], deviceNumber);
return deviceNumber;
}
@@ -171,12 +165,8 @@ public class AntDirectSensorManager extends AntSensorManager
protected void setupAntSensorChannels() {
short devIds[] = new short[sensorIdKeys.length];
SharedPreferences prefs = context.getSharedPreferences(
Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
if (prefs != null) {
for (int i = 0; i < sensorIdKeys.length; ++i) {
devIds[i] = (short) prefs.getInt(context.getString(sensorIdKeys[i]), WILDCARD);
}
for (int i = 0; i < sensorIdKeys.length; ++i) {
devIds[i] = (short) PreferencesUtils.getInt(context, sensorIdKeys[i], WILDCARD);
}
sensors = new AntSensorBase[] {
@@ -22,7 +22,7 @@ import com.dsi.ant.AntMesg;
import com.dsi.ant.exception.AntInterfaceException;
import com.google.android.apps.mytracks.Constants;
import com.google.android.apps.mytracks.content.Sensor;
import com.google.android.apps.mytracks.util.ApiAdapterFactory;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.maps.mytracks.BuildConfig;
import com.google.android.maps.mytracks.R;
@@ -71,9 +71,8 @@ public class AntSrmBridgeSensorManager extends AntSensorManager {
SharedPreferences prefs = context.getSharedPreferences(
Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
if (prefs != null) {
deviceNumber =
(short) prefs.getInt(context.getString(
R.string.ant_srm_bridge_sensor_id_key), 0);
deviceNumber = (short) PreferencesUtils.getInt(
context, R.string.ant_srm_bridge_sensor_id_key, WILDCARD);
}
Log.i(TAG, "Will pair with device: " + deviceNumber);
}
@@ -194,12 +193,7 @@ public class AntSrmBridgeSensorManager extends AntSensorManager {
AntChannelIdMessage message = new AntChannelIdMessage(rawMessage);
deviceNumber = message.getDeviceNumber();
Log.d(TAG, "Found device id: " + deviceNumber);
SharedPreferences prefs = context.getSharedPreferences(
Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(context.getString(R.string.ant_srm_bridge_sensor_id_key), deviceNumber);
ApiAdapterFactory.getApiAdapter().applyPreferenceChanges(editor);
PreferencesUtils.setInt(context, R.string.ant_srm_bridge_sensor_id_key, deviceNumber);
}
private void handleMessageResponse(byte[] rawMessage) {
@@ -0,0 +1,141 @@
/*
* Copyright 2012 Google Inc.
*
* 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 com.google.android.apps.mytracks.settings;
import com.google.android.apps.mytracks.services.sensors.ant.AntUtils;
import com.google.android.apps.mytracks.util.BluetoothDeviceUtils;
import com.google.android.maps.mytracks.R;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceScreen;
import android.provider.Settings;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* An activity for accessing sensor settings.
*
* @author Jimmy Shih
*/
public class SensorSettingsActivity extends AbstractSettingsActivity {
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
addPreferencesFromResource(R.xml.sensor_settings);
ListPreference sensorTypeListPreference = (ListPreference) findPreference(
getString(R.string.sensor_type_key));
List<String> sensorTypeEntries = Arrays.asList(getResources().getStringArray(
AntUtils.hasAntSupport(this) ? R.array.sensor_type_all_options
: R.array.sensor_type_bluetooth_options));
List<String> sensorTypeEntryValues = Arrays.asList(getResources().getStringArray(
AntUtils.hasAntSupport(this) ? R.array.sensor_type_all_values
: R.array.sensor_type_bluetooth_values));
sensorTypeListPreference.setEntries(sensorTypeEntries.toArray(
new CharSequence[sensorTypeEntries.size()]));
sensorTypeListPreference.setEntryValues(
sensorTypeEntryValues.toArray(new CharSequence[sensorTypeEntryValues.size()]));
sensorTypeListPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
updateUiBySensorType((String) newValue);
return true;
}
});
updateUiBySensorType(sensorTypeListPreference.getValue());
findPreference(getString(R.string.settings_sensor_bluetooth_pairing_key))
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
Intent settingsIntent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS);
startActivity(settingsIntent);
return true;
}
});
if (!AntUtils.hasAntSupport(this)) {
PreferenceScreen rootPreferenceScreen = (PreferenceScreen) findPreference(
getString(R.string.settings_sensor_root_key));
rootPreferenceScreen.removePreference(
findPreference(getString(R.string.settings_sensor_ant_key)));
}
}
/**
* Updates the UI based on the sensor type.
*
* @param sensorType the sensor type
*/
@SuppressWarnings("deprecation")
private void updateUiBySensorType(String sensorType) {
boolean isBluetooth = getString(R.string.sensor_type_value_polar).equals(sensorType)
|| getString(R.string.sensor_type_value_zephyr).equals(sensorType);
findPreference(getString(R.string.settings_sensor_bluetooth_key)).setEnabled(isBluetooth);
Preference antHeartRateSensorId = findPreference(
getString(R.string.ant_heart_rate_sensor_id_key));
if (antHeartRateSensorId != null) {
antHeartRateSensorId.setEnabled(getString(R.string.sensor_type_value_ant).equals(sensorType));
}
Preference antSrmBridgetSensorId = findPreference(
getString(R.string.ant_srm_bridge_sensor_id_key));
if (antSrmBridgetSensorId != null) {
antSrmBridgetSensorId.setEnabled(
getString(R.string.sensor_type_value_srm_ant_bridge).equals(sensorType));
}
}
@Override
protected void onResume() {
super.onResume();
updateBluetoothSensorListPreference();
}
/**
* Updates the bluetooth sensor list preference.
*/
private void updateBluetoothSensorListPreference() {
@SuppressWarnings("deprecation")
ListPreference bluetoothSensorListPreference = (ListPreference) findPreference(
getString(R.string.bluetooth_sensor_key));
List<String> entries = new ArrayList<String>();
List<String> entryValues = new ArrayList<String>();
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter != null) {
BluetoothDeviceUtils.populateDeviceLists(bluetoothAdapter, entries, entryValues);
}
bluetoothSensorListPreference.setEntries(entries.toArray(new CharSequence[entries.size()]));
bluetoothSensorListPreference.setEntryValues(entryValues.toArray(
new CharSequence[entryValues.size()]));
if (entries.size() == 1) {
bluetoothSensorListPreference.setValueIndex(0);
}
}
}
@@ -19,9 +19,7 @@ package com.google.android.apps.mytracks.settings;
import static com.google.android.apps.mytracks.Constants.TAG;
import com.google.android.apps.mytracks.Constants;
import com.google.android.apps.mytracks.services.sensors.ant.AntUtils;
import com.google.android.apps.mytracks.util.ApiAdapterFactory;
import com.google.android.apps.mytracks.util.BluetoothDeviceUtils;
import com.google.android.apps.mytracks.util.DialogUtils;
import com.google.android.apps.mytracks.util.IntentUtils;
import com.google.android.apps.mytracks.util.PreferencesUtils;
@@ -29,7 +27,6 @@ import com.google.android.apps.mytracks.util.UnitConversions;
import com.google.android.maps.mytracks.R;
import android.app.Dialog;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
@@ -42,16 +39,9 @@ import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceCategory;
import android.preference.PreferenceScreen;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* An activity for accessing settings.
*
@@ -67,7 +57,6 @@ public class SettingsActivity extends AbstractSettingsActivity {
super.onCreate(bundle);
addPreferencesFromResource(R.xml.preferences);
customizeSensorOptionsPreferences();
customizeTrackColorModePreferences();
Preference recordingPreference = findPreference(getString(R.string.settings_recording_key));
@@ -90,6 +79,16 @@ public class SettingsActivity extends AbstractSettingsActivity {
return true;
}
});
Preference sensorPreference = findPreference(getString(R.string.settings_sensor_key));
sensorPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Intent intent = IntentUtils.newIntent(SettingsActivity.this, SensorSettingsActivity.class);
startActivity(intent);
return true;
}
});
Preference backupPreference = findPreference(getString(R.string.settings_backup_key));
backupPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@@ -128,53 +127,6 @@ public class SettingsActivity extends AbstractSettingsActivity {
}
}
private void customizeSensorOptionsPreferences() {
ListPreference sensorTypePreference =
(ListPreference) findPreference(getString(R.string.sensor_type_key));
sensorTypePreference.setOnPreferenceChangeListener(
new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
updateSensorSettings((String) newValue);
return true;
}
});
updateSensorSettings(sensorTypePreference.getValue());
if (!AntUtils.hasAntSupport(this)) {
// The sensor options screen has a few ANT-specific options which we
// need to remove. First, we need to remove the ANT sensor types.
// Second, we need to remove the ANT unpairing options.
Set<Integer> toRemove = new HashSet<Integer>();
String[] antValues = getResources().getStringArray(R.array.sensor_type_ant_values);
for (String antValue : antValues) {
toRemove.add(sensorTypePreference.findIndexOfValue(antValue));
}
CharSequence[] entries = sensorTypePreference.getEntries();
CharSequence[] entryValues = sensorTypePreference.getEntryValues();
CharSequence[] filteredEntries = new CharSequence[entries.length - toRemove.size()];
CharSequence[] filteredEntryValues = new CharSequence[filteredEntries.length];
for (int i = 0, last = 0; i < entries.length; i++) {
if (!toRemove.contains(i)) {
filteredEntries[last] = entries[i];
filteredEntryValues[last++] = entryValues[i];
}
}
sensorTypePreference.setEntries(filteredEntries);
sensorTypePreference.setEntryValues(filteredEntryValues);
PreferenceScreen sensorOptionsScreen =
(PreferenceScreen) findPreference(getString(R.string.sensor_options_key));
sensorOptionsScreen.removePreference(findPreference(getString(R.string.ant_options_key)));
}
}
private void customizeTrackColorModePreferences() {
ListPreference trackColorModePreference =
(ListPreference) findPreference(getString(R.string.track_color_mode_key));
@@ -203,7 +155,6 @@ public class SettingsActivity extends AbstractSettingsActivity {
@Override
protected void onResume() {
super.onResume();
configureBluetoothPreferences();
Preference resetPreference = findPreference(getString(R.string.reset_key));
boolean recording = PreferencesUtils.getLong(this, R.string.recording_track_id_key) != -1;
resetPreference.setEnabled(!recording);
@@ -212,27 +163,6 @@ public class SettingsActivity extends AbstractSettingsActivity {
: R.string.settings_reset_summary);
}
private void updateSensorSettings(String sensorType) {
boolean usesBluetooth =
getString(R.string.sensor_type_value_zephyr).equals(sensorType)
|| getString(R.string.sensor_type_value_polar).equals(sensorType);
findPreference(
getString(R.string.bluetooth_sensor_key)).setEnabled(usesBluetooth);
findPreference(
getString(R.string.bluetooth_pairing_key)).setEnabled(usesBluetooth);
// Update the ANT+ sensors.
// TODO: Only enable on phones that have ANT+.
Preference antHrm = findPreference(getString(R.string.ant_heart_rate_sensor_id_key));
Preference antSrm = findPreference(getString(R.string.ant_srm_bridge_sensor_id_key));
if (antHrm != null && antSrm != null) {
antHrm
.setEnabled(getString(R.string.sensor_type_value_ant).equals(sensorType));
antSrm
.setEnabled(getString(R.string.sensor_type_value_srm_ant_bridge).equals(sensorType));
}
}
private void updateTrackColorModeSettings(String trackColorMode) {
boolean usesFixedSpeed =
trackColorMode.equals(getString(R.string.display_track_color_value_fixed));
@@ -247,45 +177,6 @@ public class SettingsActivity extends AbstractSettingsActivity {
.setEnabled(usesDynamicSpeed);
}
/**
* Configures preference actions related to bluetooth.
*/
private void configureBluetoothPreferences() {
// Populate the list of bluetooth devices
populateBluetoothDeviceList();
// Make the pair devices preference go to the system preferences
findPreference(getString(R.string.bluetooth_pairing_key)).setOnPreferenceClickListener(
new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
Intent settingsIntent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS);
startActivity(settingsIntent);
return false;
}
});
}
/**
* Populates the list preference with all available bluetooth devices.
*/
private void populateBluetoothDeviceList() {
// Build the list of entries and their values
List<String> entries = new ArrayList<String>();
List<String> entryValues = new ArrayList<String>();
// The actual devices
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()]);
ListPreference devicesPreference =
(ListPreference) findPreference(getString(R.string.bluetooth_sensor_key));
devicesPreference.setEntryValues(entryValuesArray);
devicesPreference.setEntries(entriesArray);
}
/** Callback for when user confirms resetting all settings. */
private void onResetPreferencesConfirmed() {
// Change preferences in a separate thread.
@@ -24,17 +24,17 @@ import java.util.Set;
/**
* Utilities for dealing with bluetooth devices.
*
*
* @author Rodrigo Damazio
*/
public class BluetoothDeviceUtils {
private BluetoothDeviceUtils() {}
/**
* Populates the device names and the device addresses with all the suitable
* bluetooth devices.
*
*
* @param bluetoothAdapter the bluetooth adapter
* @param deviceNames list of device names
* @param deviceAddresses list of device addresses
@@ -41,6 +41,7 @@ public class PreferencesUtils {
public static final int AUTO_RESUME_TRACK_TIMEOUT_DEFAULT = 10;
public static final int AUTO_RESUME_TRACK_TIMEOUT_NEVER = 0;
public static final String BLUETOOTH_SENSOR_DEFAULT = "";
public static final String DEFAULT_ACTIVITY_DEFAULT = "";
public static final boolean DEFAULT_MAP_PUBLIC_DEFAULT = true;
public static final int MAX_RECORDING_DISTANCE_DEFAULT = 200;
@@ -168,4 +169,19 @@ public class PreferencesUtils {
Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
return sharedPreferences.getString(getKey(context, keyId), defaultValue);
}
/**
* Sets a string preference value.
*
* @param context the context
* @param keyId the key id
* @param value the value
*/
public static void setString(Context context, int keyId, String value) {
SharedPreferences sharedPreferences = context.getSharedPreferences(
Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.putString(getKey(context, keyId), value);
ApiAdapterFactory.getApiAdapter().applyPreferenceChanges(editor);
}
}