Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CME in ModelSpecificDistanceCalculator #584

Merged
merged 7 commits into from
Sep 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
### Development

Bug Fixes:
- Fix ConcurrentModifictionExceptions starting ScanJobs. (#584, #588, David G. Young)
- Fix NullPointerException when BluetoothLeScanner cannot be obtained.
(#583, David G. Young)
### 2.12.2 / 2017-08-31

[Full Changelog](https://github.com/AltBeacon/android-beacon-library/compare/2.12.1...2.12.2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ void buildModelMapWithLock(String jsonString) throws JSONException {
}

private void buildModelMap(String jsonString) throws JSONException {
mModelMap = new HashMap<AndroidModel, DistanceCalculator>();
HashMap<AndroidModel, DistanceCalculator> map = new HashMap<AndroidModel, DistanceCalculator>();
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray array = jsonObject.getJSONArray("models");
for (int i = 0; i < array.length(); i++) {
Expand All @@ -290,19 +290,20 @@ private void buildModelMap(String jsonString) throws JSONException {
new CurveFittedDistanceCalculator(coefficient1,coefficient2,coefficient3);

AndroidModel androidModel = new AndroidModel(version, buildNumber, model, manufacturer);
mModelMap.put(androidModel, distanceCalculator);
map.put(androidModel, distanceCalculator);
if (defaultFlag) {
mDefaultModel = androidModel;
}
}
mModelMap = map;
}

private void loadDefaultModelMap() {
mModelMap = new HashMap<AndroidModel, DistanceCalculator>();
try {
buildModelMap(stringFromFilePath(CONFIG_FILE));
}
catch (Exception e) {
mModelMap = new HashMap<AndroidModel, DistanceCalculator>();
LogManager.e(e, TAG, "Cannot build model distance calculations");
}
}
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/org/altbeacon/beacon/service/MonitoringStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import static android.content.Context.MODE_PRIVATE;

Expand Down Expand Up @@ -141,7 +142,7 @@ private Map<Region, RegionMonitoringState> getRegionsStateMap() {

private void restoreOrInitializeMonitoringStatus() {
long millisSinceLastMonitor = System.currentTimeMillis() - getLastMonitoringStatusUpdateTime();
mRegionsStatesMap = new HashMap<Region, RegionMonitoringState>();
mRegionsStatesMap = new ConcurrentHashMap<Region, RegionMonitoringState>();
if (!mStatePreservationIsOn) {
LogManager.d(TAG, "Not restoring monitoring state because persistence is disabled");
}
Expand Down Expand Up @@ -179,10 +180,17 @@ protected void saveMonitoringStatusIfOn() {
try {
outputStream = mContext.openFileOutput(STATUS_PRESERVATION_FILE_NAME, MODE_PRIVATE);
objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(getRegionsStateMap());

Map<Region,RegionMonitoringState> map = getRegionsStateMap();
// Must convert ConcurrentHashMap to HashMap becasue attempting to serialize
// ConcurrentHashMap throws a java.io.NotSerializableException
HashMap<Region,RegionMonitoringState> serializableMap = new HashMap<Region,RegionMonitoringState>();
for (Region region : map.keySet()) {
serializableMap.put(region, map.get(region));
}
objectOutputStream.writeObject(serializableMap);
} catch (IOException e) {
LogManager.e(TAG, "Error while saving monitored region states to file. %s ", e.getMessage());
LogManager.e(TAG, "Error while saving monitored region states to file ", e);
e.printStackTrace(System.err);
} finally {
if (null != outputStream) {
try {
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/altbeacon/beacon/service/ScanHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanFilter;
import android.bluetooth.le.ScanSettings;
import android.content.Context;
Expand Down Expand Up @@ -185,7 +186,10 @@ void stopAndroidOBackgroundScan() {
} else if (!bluetoothAdapter.isEnabled()) {
LogManager.w(TAG, "BluetoothAdapter is not enabled");
} else {
bluetoothAdapter.getBluetoothLeScanner().stopScan(getScanCallbackIntent());
BluetoothLeScanner scanner = bluetoothAdapter.getBluetoothLeScanner();
if (scanner != null) {
scanner.stopScan(getScanCallbackIntent());
}
}
} catch (SecurityException e) {
LogManager.e(TAG, "SecurityException stopping Android O background scanner");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;
import android.util.Log;

import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.Region;
Expand All @@ -30,6 +31,7 @@
@RunWith(RobolectricTestRunner.class)
@Config(sdk = 18)
public class MonitoringStatusTest {
private static final String TAG = MonitoringStatusTest.class.getSimpleName();
@Before
public void before() {
org.robolectric.shadows.ShadowLog.stream = System.err;
Expand Down