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