Improve TrackRecordingService by placing checkService inside canAccess

This commit is contained in:
Jimmy Shih
2011-10-06 10:09:25 -07:00
parent 2fa325dbf3
commit 1ffd77595d
@@ -1168,6 +1168,10 @@ public class TrackRecordingService extends Service {
} }
} }
/**
* Checks if the service is available. If not, throws an
* {@link IllegalStateException}.
*/
private void checkService() { private void checkService() {
if (service == null) { if (service == null) {
throw new IllegalStateException("The service has been already detached!"); throw new IllegalStateException("The service has been already detached!");
@@ -1180,6 +1184,10 @@ public class TrackRecordingService extends Service {
* RPCs. * RPCs.
*/ */
private boolean canAccess() { private boolean canAccess() {
// As a precondition for access, must check if the service is available.
checkService();
if (Process.myPid() == Binder.getCallingPid()) { if (Process.myPid() == Binder.getCallingPid()) {
return true; return true;
} else { } else {
@@ -1193,7 +1201,6 @@ public class TrackRecordingService extends Service {
@Override @Override
public boolean isRecording() { public boolean isRecording() {
checkService();
if (!canAccess()) { if (!canAccess()) {
return false; return false;
} }
@@ -1202,7 +1209,6 @@ public class TrackRecordingService extends Service {
@Override @Override
public long getRecordingTrackId() { public long getRecordingTrackId() {
checkService();
if (!canAccess()) { if (!canAccess()) {
return -1L; return -1L;
} }
@@ -1211,7 +1217,6 @@ public class TrackRecordingService extends Service {
@Override @Override
public long startNewTrack() { public long startNewTrack() {
checkService();
if (!canAccess()) { if (!canAccess()) {
return -1L; return -1L;
} }
@@ -1225,7 +1230,6 @@ public class TrackRecordingService extends Service {
* @return the unique ID of the inserted marker * @return the unique ID of the inserted marker
*/ */
public long insertWaypoint(WaypointCreationRequest request) { public long insertWaypoint(WaypointCreationRequest request) {
checkService();
if (!canAccess()) { if (!canAccess()) {
return -1L; return -1L;
} }
@@ -1234,7 +1238,6 @@ public class TrackRecordingService extends Service {
@Override @Override
public void endCurrentTrack() { public void endCurrentTrack() {
checkService();
if (!canAccess()) { if (!canAccess()) {
return; return;
} }
@@ -1243,7 +1246,6 @@ public class TrackRecordingService extends Service {
@Override @Override
public void recordLocation(Location loc) { public void recordLocation(Location loc) {
checkService();
if (!canAccess()) { if (!canAccess()) {
return; return;
} }
@@ -1252,7 +1254,6 @@ public class TrackRecordingService extends Service {
@Override @Override
public byte[] getSensorData() { public byte[] getSensorData() {
checkService();
if (!canAccess()) { if (!canAccess()) {
return null; return null;
} }
@@ -1269,7 +1270,6 @@ public class TrackRecordingService extends Service {
@Override @Override
public int getSensorState() { public int getSensorState() {
checkService();
if (!canAccess()) { if (!canAccess()) {
return Sensor.SensorState.NONE.getNumber(); return Sensor.SensorState.NONE.getNumber();
} }