Skip to content
This repository has been archived by the owner on May 2, 2021. It is now read-only.

Commit

Permalink
continue logging to disk if interrupted (wake-lock)
Browse files Browse the repository at this point in the history
When the app goes out of foreground while logging to disk, e.g.,
because the screen turns off or another app is started, acquire partial
wake-lock and continue logging.

If not logging to disk, behavior is unchanged, i.e., updates are
suspended.

This feature is (particularly) experimental and definitely needs
testing and/or a review.

See #1
  • Loading branch information
tjanson committed Mar 12, 2015
1 parent 8751617 commit 10b4a8a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<application
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.location.Location;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.Button;
Expand Down Expand Up @@ -74,7 +75,10 @@ public class MainActivity extends Activity implements

// toggles logging location+wifi to file
// debug logs may be created regardless of this
boolean logToFile = false;
boolean loggingToFile = false;

// wake-lock to (hopefully) continue logging while screen is off
PowerManager.WakeLock wakeLock;

// UI Elements
Button loggingButton;
Expand Down Expand Up @@ -110,6 +114,9 @@ public void onCreate(Bundle savedInstanceState) {
// restore state on Activity recreation
updateValuesFromBundle(savedInstanceState);

PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakelockTag");

buildGoogleApiClient();

initWifiScan();
Expand Down Expand Up @@ -141,15 +148,28 @@ void updateUI() {
wifiTV.setText(wifiListString);
}

if (logToFile) {
if (loggingToFile) {
loggingButton.setText(R.string.logging_stop);
} else {
loggingButton.setText(R.string.logging_start);
}
}

/**
* Toggles logging data points to file and aquires wake-lock to do so while screen is off.
*/
public void toggleLogging(View view) {
logToFile = !logToFile;
loggingToFile = !loggingToFile;
log.info((loggingToFile ? "En" : "Dis") + "abled logging to disk");

if (loggingToFile) {
wakeLock.acquire();
log.debug("Acquired wake-lock");
} else if (wakeLock.isHeld()) {
wakeLock.release();
log.debug("Released wake-lock");
}

updateUI();
}

Expand Down Expand Up @@ -244,26 +264,33 @@ protected void onResume() {
if (googleApiClient.isConnected()) {
startLocationUpdates();
}
this.registerReceiver(wifiBroadcastReceiver, wifiIntentFilter);
log.debug("Registered wifiBroadcastReceiver");
wifiManager.startScan();

if (!loggingToFile) {
this.registerReceiver(wifiBroadcastReceiver, wifiIntentFilter);
log.trace("Registered WifiBroadcastReceiver");
wifiManager.startScan();
}
}

@Override
protected void onPause() {
super.onPause();
this.unregisterReceiver(wifiBroadcastReceiver);
log.debug("Unregistered wifiBroadcastReceiver");
if (googleApiClient.isConnected()) {

if (!loggingToFile) {
this.unregisterReceiver(wifiBroadcastReceiver);
log.debug("Unregistered WifiBroadcastReceiver");
stopLocationUpdates();
} else {
log.debug("onPause called, but logging to file; we'll keep going");
}
}

@Override
protected void onStop() {
super.onStop();
if (googleApiClient.isConnected()) {
googleApiClient.disconnect();

if (!loggingToFile) {
disconnectGoogleApiClient();
}

// save preferences or other persisting stuff
Expand All @@ -272,6 +299,25 @@ protected void onStop() {
prefEditor.apply();
}

@Override
protected void onDestroy() {
super.onDestroy();

disconnectGoogleApiClient();

if (loggingToFile) {
this.unregisterReceiver(wifiBroadcastReceiver);
log.trace("Unregistered WifiBroadcastReceiver");
}
}

private void disconnectGoogleApiClient() {
if (googleApiClient.isConnected()) {
googleApiClient.disconnect();
log.trace("GoogleApiClient disconnected");
}
}

/**
* Runs when a GoogleApiClient object successfully connects.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void run() {
}

private void log(ScanResult wifi) {
if (m.logToFile) {
if (m.loggingToFile) {
m.diskLog.info(m.currentLocation.getLatitude()
+ "," + m.currentLocation.getLongitude()
+ "," + m.currentLocation.getAltitude()
Expand Down

0 comments on commit 10b4a8a

Please sign in to comment.