diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/docs/DocsTagBuilder.java b/MyTracks/src/com/google/android/apps/mytracks/io/docs/DocsTagBuilder.java index 1b752cc79..9e0c627f4 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/docs/DocsTagBuilder.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/docs/DocsTagBuilder.java @@ -48,9 +48,7 @@ import java.util.Locale; class DocsTagBuilder { private static final DecimalFormatSymbols FORMAT_SYMBOLS = - ApiFeatures.getInstance().hasDecimalFormatSymbolsGetInstance() - ? DecimalFormatSymbols.getInstance(Locale.ENGLISH) - : new DecimalFormatSymbols(Locale.ENGLISH); + ApiFeatures.getInstance().getApiPlatformAdapter().getDecimalFormatSysmbols(Locale.ENGLISH); private static final NumberFormat LARGE_UNIT_FORMAT = new DecimalFormat("#,###,###.00", FORMAT_SYMBOLS); private static final NumberFormat SMALL_UNIT_FORMAT = diff --git a/MyTracks/src/com/google/android/apps/mytracks/services/sensors/ZephyrMessageParser.java b/MyTracks/src/com/google/android/apps/mytracks/services/sensors/ZephyrMessageParser.java index 4ea554d48..aa96f2e48 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/services/sensors/ZephyrMessageParser.java +++ b/MyTracks/src/com/google/android/apps/mytracks/services/sensors/ZephyrMessageParser.java @@ -24,7 +24,7 @@ import java.util.Arrays; * An implementation of a Sensor MessageParser for Zephyr. * * @author Sandor Dornbush - * @author Dominik R�ttsches + * @author Dominik Ršttsches */ public class ZephyrMessageParser implements MessageParser { @@ -57,24 +57,13 @@ public class ZephyrMessageParser implements MessageParser { return sds.build(); } - /** - * Copies a byte array. In case Arrays.copyOfRange is not available. - */ - private byte[] copyByteArray(byte[] input, int from, int to) { - int length = to - from; - byte[] output = new byte[length]; - System.arraycopy(input, from, output, 0, length); - return output; - } - private void setCadence(Sensor.SensorDataSet.Builder sds, byte[] buffer) { // Device Firmware ID, Firmware Version, Hardware ID, Hardware Version // 0x1A00316550003162 produces erroneous values for Cadence and needs // a workaround based on the stride counter. // Firmware values range from field 3 to 10 (inclusive) of the byte buffer. - byte[] hardwareFirmwareId = ApiFeatures.getInstance().hasArraysCopyOfRange() - ? Arrays.copyOfRange(buffer, 3, 11) - : copyByteArray(buffer, 3, 11); + byte[] hardwareFirmwareId = ApiFeatures.getInstance().getApiPlatformAdapter() + .copyByteArray(buffer, 3, 11); Sensor.SensorData.Builder cadence = Sensor.SensorData.newBuilder(); if (Arrays.equals(hardwareFirmwareId, CADENCE_BUG_FW_ID)) { diff --git a/MyTracks/src/com/google/android/apps/mytracks/util/ApiFeatures.java b/MyTracks/src/com/google/android/apps/mytracks/util/ApiFeatures.java index 362385d0f..ad87ac45a 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/ApiFeatures.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/ApiFeatures.java @@ -24,9 +24,6 @@ import android.bluetooth.BluetoothDevice; import android.os.Build; import android.util.Log; -import java.text.DecimalFormatSymbols; -import java.util.Arrays; - /** * Utility class for determining if newer-API features are available on the * current device. @@ -133,20 +130,6 @@ public class ApiFeatures { return getApiLevel() >= 9; } - /** - * Returns true if {@link DecimalFormatSymbols#getInstance} is available. - */ - public boolean hasDecimalFormatSymbolsGetInstance() { - return getApiLevel() >= 9; - } - - /** - * Returns true if {@link Arrays#copyOfRange} is available. - */ - public boolean hasArraysCopyOfRange() { - return getApiLevel() >= 9; - } - // API Level 10 changes /** diff --git a/MyTracks/src/com/google/android/apps/mytracks/util/ApiPlatformAdapter.java b/MyTracks/src/com/google/android/apps/mytracks/util/ApiPlatformAdapter.java index f432fe8fa..02a225dd5 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/ApiPlatformAdapter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/ApiPlatformAdapter.java @@ -20,6 +20,9 @@ import android.app.NotificationManager; import android.app.Service; import android.content.SharedPreferences; +import java.text.DecimalFormatSymbols; +import java.util.Locale; + /** * A set of methods that may be implemented in a platform specific way. * @@ -60,4 +63,25 @@ public interface ApiPlatformAdapter { * Enables strict mode where supported, only if this is a development build. */ void enableStrictMode(); + + /** + * Copies elements from the input byte array into a new byte array, from + * indexes start (inclusive) to end (exclusive). The end index must be less + * than or equal to input.length. + * + * @param input the input byte array + * @param start the start index + * @param end the end index + * @return a new array containing elements from the input byte array + */ + byte[] copyByteArray(byte[] input, int start, int end); + + + /** + * Gets an instance of {@link DecimalFormatSymbols} for a locale. + * + * @param locale the locale + * @return a new instance of {@link DecimalFormatSymbols} + */ + DecimalFormatSymbols getDecimalFormatSymbols(Locale locale); } diff --git a/MyTracks/src/com/google/android/apps/mytracks/util/CupcakePlatformAdapter.java b/MyTracks/src/com/google/android/apps/mytracks/util/CupcakePlatformAdapter.java index fe6ec0b47..6f78b4bce 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/CupcakePlatformAdapter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/CupcakePlatformAdapter.java @@ -25,6 +25,8 @@ import android.util.Log; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.text.DecimalFormatSymbols; +import java.util.Locale; /** * The Cupcake (API level 3) specific implementation of the @@ -79,4 +81,17 @@ public class CupcakePlatformAdapter implements ApiPlatformAdapter { public void enableStrictMode() { // Not supported } + + @Override + public byte[] copyByteArray(byte[] input, int start, int end) { + int length = end - start; + byte[] output = new byte[length]; + System.arraycopy(input, start, output, 0, length); + return output; + } + + @Override + public DecimalFormatSymbols getDecimalFormatSysmbols(Locale locale) { + return new DecimalFormatSymbols(locale); + } } diff --git a/MyTracks/src/com/google/android/apps/mytracks/util/EclairPlatformAdapter.java b/MyTracks/src/com/google/android/apps/mytracks/util/EclairPlatformAdapter.java index 55540e1a4..dcb005eb6 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/EclairPlatformAdapter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/EclairPlatformAdapter.java @@ -18,7 +18,6 @@ package com.google.android.apps.mytracks.util; import android.app.Notification; import android.app.NotificationManager; import android.app.Service; -import android.content.SharedPreferences.Editor; /** * The Eclair (API level 5) specific implementation of the @@ -26,7 +25,7 @@ import android.content.SharedPreferences.Editor; * * @author Bartlomiej Niechwiej */ -public class EclairPlatformAdapter implements ApiPlatformAdapter { +public class EclairPlatformAdapter extends CupcakePlatformAdapter { @Override public void startForeground(Service service, @@ -40,14 +39,4 @@ public class EclairPlatformAdapter implements ApiPlatformAdapter { NotificationManager notificationManager, int id) { service.stopForeground(id != -1); } - - @Override - public void applyPreferenceChanges(Editor editor) { - editor.commit(); - } - - @Override - public void enableStrictMode() { - // Not supported - } } diff --git a/MyTracks/src/com/google/android/apps/mytracks/util/GingerbreadPlatformAdapter.java b/MyTracks/src/com/google/android/apps/mytracks/util/GingerbreadPlatformAdapter.java index 35d7eb3d6..c97a17c1c 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/GingerbreadPlatformAdapter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/GingerbreadPlatformAdapter.java @@ -6,6 +6,10 @@ import android.content.SharedPreferences.Editor; import android.os.StrictMode; import android.util.Log; +import java.text.DecimalFormatSymbols; +import java.util.Arrays; +import java.util.Locale; + /** * The Gingerbread (API level 9) specific implememntation of the * {@link ApiPlatformAdapter}. @@ -13,6 +17,7 @@ import android.util.Log; * @author Rodrigo Damazio */ public class GingerbreadPlatformAdapter extends EclairPlatformAdapter { + @Override public void applyPreferenceChanges(Editor editor) { // Apply asynchronously @@ -32,4 +37,14 @@ public class GingerbreadPlatformAdapter extends EclairPlatformAdapter { .penaltyLog() .build()); } + + @Override + public byte[] copyByteArray(byte[] input, int start, int end) { + return Arrays.copyOfRange(input, start, end); + } + + @Override + public DecimalFormatSymbols getDecimalFormatSysmbols(Locale locale) { + return DecimalFormatSymbols.getInstance(locale); + } }