Skip to content

Commit

Permalink
Persist ScanState to temp file then move to perm file
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgyoung committed Jul 30, 2017
1 parent b2df8be commit 966b5ca
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/main/java/org/altbeacon/beacon/service/ScanState.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.altbeacon.beacon.Region;
import org.altbeacon.beacon.logging.LogManager;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
Expand All @@ -34,6 +35,7 @@
public class ScanState implements Serializable {
private static final String TAG = ScanState.class.getSimpleName();
private static final String STATUS_PRESERVATION_FILE_NAME = "android-beacon-library-scan-state";
private static final String TEMP_STATUS_PRESERVATION_FILE_NAME = "android-beacon-library-scan-state-temp";
public static int MIN_SCAN_JOB_INTERVAL_MILLIS = 300000; // 5 minutes

private Map<Region, RangeState> mRangedRegionState = new HashMap<Region, RangeState>();
Expand Down Expand Up @@ -185,13 +187,22 @@ public void save() {
FileOutputStream outputStream = null;
ObjectOutputStream objectOutputStream = null;
try {
outputStream = mContext.openFileOutput(STATUS_PRESERVATION_FILE_NAME, MODE_PRIVATE);
outputStream = mContext.openFileOutput(TEMP_STATUS_PRESERVATION_FILE_NAME, MODE_PRIVATE);
objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(this);
File file = new File(mContext.getFilesDir(), STATUS_PRESERVATION_FILE_NAME);
File tempFile = new File(mContext.getFilesDir(), TEMP_STATUS_PRESERVATION_FILE_NAME);
LogManager.d(TAG, "Temp file is "+tempFile.getAbsolutePath());
LogManager.d(TAG, "Perm file is "+file.getAbsolutePath());

if (!file.delete()) {
LogManager.e(TAG, "Error while saving scan status to file: Cannot delete existing file.");
}
if (!tempFile.renameTo(file)) {
LogManager.e(TAG, "Error while saving scan status to file: Cannot rename temp file.");
}
} catch (IOException e) {
LogManager.e(TAG, "Error while saving scan status to file: ", e.getMessage());
Log.e(TAG, "error: ", e);
} finally {
if (null != outputStream) {
try {
Expand Down
57 changes: 57 additions & 0 deletions src/test/java/org/altbeacon/beacon/service/ScanStateTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.altbeacon.beacon.service;

/**
* Created by dyoung on 7/30/17.
*/

import android.annotation.TargetApi;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;

import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.logging.LogManager;
import org.altbeacon.beacon.logging.Loggers;
import org.altbeacon.beacon.service.scanner.CycledLeScanCallback;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ServiceController;

import java.util.concurrent.ThreadPoolExecutor;

import static org.junit.Assert.assertEquals;

/**
* Created by dyoung on 7/1/15.
*/
@RunWith(RobolectricTestRunner.class)
@Config(sdk = 18)
public class ScanStateTest {

@Before
public void before() {
org.robolectric.shadows.ShadowLog.stream = System.err;
LogManager.setLogger(Loggers.verboseLogger());
LogManager.setVerboseLoggingEnabled(true);
BeaconManager.setsManifestCheckingDisabled(true);
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Test
public void serializationTest() throws Exception {
Context context = ShadowApplication.getInstance().getApplicationContext();
ScanState scanState = new ScanState(context);
MonitoringStatus monitoringStatus = new MonitoringStatus(context);
scanState.setMonitoringStatus(monitoringStatus);
scanState.setLastScanStartTimeMillis(1234);
scanState.save();
ScanState scanState2 = ScanState.restore(context);
assertEquals("Scan start time should be restored",
scanState.getLastScanStartTimeMillis(), scanState2.getLastScanStartTimeMillis());
}
}

0 comments on commit 966b5ca

Please sign in to comment.