In TrackRecordingService, use SystemUtils to aquire wake lock

This commit is contained in:
Jimmy Shih
2013-06-14 13:00:57 -07:00
parent d558e8f507
commit 86a3f407ce
7 changed files with 50 additions and 95 deletions
@@ -74,14 +74,6 @@ public class ImportAsyncTask extends AsyncTask<Void, Integer, Boolean> {
this.path = path;
context = importActivity.getApplicationContext();
// Get the wake lock if not recording or paused
if (PreferencesUtils.getLong(importActivity, R.string.recording_track_id_key)
== PreferencesUtils.RECORDING_TRACK_ID_DEFAULT || PreferencesUtils.getBoolean(
importActivity, R.string.recording_track_paused_key,
PreferencesUtils.RECORDING_TRACK_PAUSED_DEFAULT)) {
wakeLock = SystemUtils.acquireWakeLock(importActivity, wakeLock);
}
completed = false;
successCount = 0;
totalCount = 0;
@@ -110,6 +102,16 @@ public class ImportAsyncTask extends AsyncTask<Void, Integer, Boolean> {
@Override
protected Boolean doInBackground(Void... params) {
try {
// Get the wake lock if not recording or paused
boolean isRecording =
PreferencesUtils.getLong(importActivity, R.string.recording_track_id_key)
!= PreferencesUtils.RECORDING_TRACK_ID_DEFAULT;
boolean isPaused = PreferencesUtils.getBoolean(importActivity,
R.string.recording_track_paused_key, PreferencesUtils.RECORDING_TRACK_PAUSED_DEFAULT);
if (!isRecording || isPaused) {
wakeLock = SystemUtils.acquireWakeLock(importActivity, wakeLock);
}
List<File> files = getFiles();
totalCount = files.size();
if (totalCount == 0) {
@@ -128,7 +130,6 @@ public class ImportAsyncTask extends AsyncTask<Void, Integer, Boolean> {
}
return true;
} finally {
// Release the wake lock if obtained
if (wakeLock != null && wakeLock.isHeld()) {
wakeLock.release();
}
@@ -83,13 +83,6 @@ public class SaveAsyncTask extends AsyncTask<Void, Integer, Boolean> {
context = saveActivity.getApplicationContext();
myTracksProviderUtils = MyTracksProviderUtils.Factory.get(context);
// Get the wake lock if not recording or paused
if (PreferencesUtils.getLong(saveActivity, R.string.recording_track_id_key)
== PreferencesUtils.RECORDING_TRACK_ID_DEFAULT || PreferencesUtils.getBoolean(saveActivity,
R.string.recording_track_paused_key, PreferencesUtils.RECORDING_TRACK_PAUSED_DEFAULT)) {
wakeLock = SystemUtils.acquireWakeLock(saveActivity, wakeLock);
}
completed = false;
successCount = 0;
totalCount = 0;
@@ -118,6 +111,14 @@ public class SaveAsyncTask extends AsyncTask<Void, Integer, Boolean> {
@Override
protected Boolean doInBackground(Void... params) {
try {
boolean isRecording = PreferencesUtils.getLong(saveActivity, R.string.recording_track_id_key)
!= PreferencesUtils.RECORDING_TRACK_ID_DEFAULT;
boolean isPaused = PreferencesUtils.getBoolean(saveActivity,
R.string.recording_track_paused_key, PreferencesUtils.RECORDING_TRACK_PAUSED_DEFAULT);
// Get the wake lock if not recording or paused
if (!isRecording || isPaused) {
wakeLock = SystemUtils.acquireWakeLock(saveActivity, wakeLock);
}
if (trackIds.length == 1 && trackIds[0] == -1L) {
return saveAllTracks();
} else {
@@ -138,7 +139,6 @@ public class SaveAsyncTask extends AsyncTask<Void, Integer, Boolean> {
}
}
} finally {
// Release the wake lock if obtained
if (wakeLock != null && wakeLock.isHeld()) {
wakeLock.release();
}
@@ -39,6 +39,7 @@ import com.google.android.apps.mytracks.stats.TripStatisticsUpdater;
import com.google.android.apps.mytracks.util.IntentUtils;
import com.google.android.apps.mytracks.util.LocationUtils;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.apps.mytracks.util.SystemUtils;
import com.google.android.apps.mytracks.util.TrackIconUtils;
import com.google.android.apps.mytracks.util.TrackNameUtils;
import com.google.android.gms.common.ConnectionResult;
@@ -65,7 +66,6 @@ import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.os.Process;
import android.support.v4.app.NotificationCompat;
@@ -718,7 +718,7 @@ public class TrackRecordingService extends Service {
* Starts gps.
*/
private void startGps() {
acquireWakeLock();
wakeLock = SystemUtils.acquireWakeLock(this, wakeLock);
registerLocationListener();
showNotification(true);
}
@@ -1052,34 +1052,6 @@ public class TrackRecordingService extends Service {
myTracksLocationManager.removeLocationUpdates(locationListener);
}
/**
* Acquires the wake lock.
*/
private void acquireWakeLock() {
try {
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (powerManager == null) {
Log.e(TAG, "powerManager is null.");
return;
}
if (wakeLock == null) {
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
if (wakeLock == null) {
Log.e(TAG, "wakeLock is null.");
return;
}
}
if (!wakeLock.isHeld()) {
wakeLock.acquire();
if (!wakeLock.isHeld()) {
Log.e(TAG, "Unable to hold wakeLock.");
}
}
} catch (RuntimeException e) {
Log.e(TAG, "Caught unexpected exception", e);
}
}
/**
* Releases the wake lock.
*/
@@ -13,11 +13,12 @@
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.android.apps.mytracks.util;
import com.google.android.apps.mytracks.Constants;
import android.app.Activity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
@@ -27,24 +28,27 @@ import android.os.PowerManager.WakeLock;
import android.util.Log;
/**
* Utility class for acessing basic Android functionality.
*
* Utility class for accessing basic Android functionality.
*
* @author Rodrigo Damazio
*/
public class SystemUtils {
private static final String TAG = SystemUtils.class.getSimpleName();
private SystemUtils() {}
/**
* Get the My Tracks version from the manifest.
*
*
* @return the version, or an empty string in case of failure.
*/
public static String getMyTracksVersion(Context context) {
try {
PackageInfo pi = context.getPackageManager().getPackageInfo(
"com.google.android.maps.mytracks",
PackageManager.GET_META_DATA);
PackageInfo pi = context.getPackageManager()
.getPackageInfo("com.google.android.maps.mytracks", PackageManager.GET_META_DATA);
return pi.versionName;
} catch (NameNotFoundException e) {
} catch (NameNotFoundException e) {
Log.w(Constants.TAG, "Failed to get version info.", e);
return "";
}
@@ -54,37 +58,38 @@ public class SystemUtils {
* Tries to acquire a partial wake lock if not already acquired. Logs errors
* and gives up trying in case the wake lock cannot be acquired.
*/
public static WakeLock acquireWakeLock(Activity activity, WakeLock wakeLock) {
Log.i(Constants.TAG, "LocationUtils: Acquiring wake lock.");
/**
* Acquire a wake lock if not already acquired.
*
* @param context the context
* @param wakeLock wake lock or null
*/
@SuppressLint("Wakelock")
public static WakeLock acquireWakeLock(Context context, WakeLock wakeLock) {
Log.i(TAG, "Acquiring wake lock.");
try {
PowerManager pm = (PowerManager) activity
.getSystemService(Context.POWER_SERVICE);
if (pm == null) {
Log.e(Constants.TAG, "LocationUtils: Power manager not found!");
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (powerManager == null) {
Log.e(TAG, "Power manager null.");
return wakeLock;
}
if (wakeLock == null) {
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
Constants.TAG);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, Constants.TAG);
if (wakeLock == null) {
Log.e(Constants.TAG,
"LocationUtils: Could not create wake lock (null).");
Log.e(TAG, "Cannot create a new wake lock.");
}
return wakeLock;
}
if (!wakeLock.isHeld()) {
wakeLock.acquire();
if (!wakeLock.isHeld()) {
Log.e(Constants.TAG,
"LocationUtils: Could not acquire wake lock.");
Log.e(TAG, "Cannot acquire wake lock.");
}
}
} catch (RuntimeException e) {
Log.e(Constants.TAG,
"LocationUtils: Caught unexpected exception: " + e.getMessage(), e);
Log.e(TAG, e.getMessage(), e);
}
return wakeLock;
}
private SystemUtils() {}
}
@@ -21,7 +21,6 @@ import com.google.android.apps.mytracks.util.StringUtils;
import com.google.android.maps.mytracks.R;
import android.test.AndroidTestCase;
import android.util.Pair;
/**
* Tests for {@link DescriptionGeneratorImpl}.
@@ -157,7 +156,7 @@ public class DescriptionGeneratorImplTest extends AndroidTestCase {
}
/**
* Tests {@link DescriptionGeneratorImpl#writePace(Pair, StringBuilder, int,
* Tests {@link DescriptionGeneratorImpl#writePace(double, StringBuilder, int,
* String)}.
*/
public void testWritePace() {
@@ -18,14 +18,12 @@ package com.google.android.apps.mytracks.endtoendtest.common;
import com.google.android.apps.mytracks.TrackListActivity;
import com.google.android.apps.mytracks.endtoendtest.EndToEndTestUtils;
import com.google.android.apps.mytracks.endtoendtest.GoogleUtils;
import com.google.android.gms.auth.GoogleAuthException;
import com.google.android.maps.mytracks.R;
import com.google.api.services.drive.model.File;
import android.app.Instrumentation;
import android.test.ActivityInstrumentationTestCase2;
import java.io.IOException;
import java.util.List;
/**
@@ -56,9 +54,6 @@ public class ExportSingleTrackTest extends ActivityInstrumentationTestCase2<Trac
/**
* Checks all services and send to google.
*
* @throws GoogleAuthException
* @throws IOException
*/
public void testCreateAndSendTrack_send() {
// Create a new track.
@@ -69,9 +64,6 @@ public class ExportSingleTrackTest extends ActivityInstrumentationTestCase2<Trac
/**
* Checks all services and send to google.
*
* @throws GoogleAuthException
* @throws IOException
*/
public void testCreateAndSendTrack_sendPausedTrack() {
EndToEndTestUtils.deleteAllTracks();
@@ -109,9 +101,6 @@ public class ExportSingleTrackTest extends ActivityInstrumentationTestCase2<Trac
/**
* Checks the process of sending track to google.
*
* @throws GoogleAuthException
* @throws IOException
*/
private void checkSendTrackToGoogle() {
if (!sendToGoogle(activityMyTracks.getString(R.string.export_google_maps))
@@ -185,9 +174,6 @@ public class ExportSingleTrackTest extends ActivityInstrumentationTestCase2<Trac
/**
* Sends a large track to Google.
*
* @throws GoogleAuthException
* @throws IOException
*/
public void testSendLargeTrackToGoogle() {
EndToEndTestUtils.deleteAllTracks();
@@ -19,14 +19,12 @@ import com.google.android.apps.mytracks.TrackListActivity;
import com.google.android.apps.mytracks.endtoendtest.EndToEndTestUtils;
import com.google.android.apps.mytracks.endtoendtest.GoogleUtils;
import com.google.android.apps.mytracks.endtoendtest.sync.SyncTestUtils;
import com.google.android.gms.auth.GoogleAuthException;
import com.google.android.maps.mytracks.R;
import android.app.Instrumentation;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.CheckBox;
import java.io.IOException;
import java.util.ArrayList;
/**
@@ -53,9 +51,6 @@ public class ShareTest extends ActivityInstrumentationTestCase2<TrackListActivit
/**
* Tests quitting the share.
*
* @throws GoogleAuthException
* @throws IOException
*/
public void testQuitShare() {
EndToEndTestUtils.resetAllSettings(activityMyTracks, false);
@@ -70,9 +65,6 @@ public class ShareTest extends ActivityInstrumentationTestCase2<TrackListActivit
/**
* Tests the share one track.
*
* @throws GoogleAuthException
* @throws IOException
*/
public void testShare() {
EndToEndTestUtils.resetAllSettings(activityMyTracks, false);