Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Tracker.create, which provides a more straightforward interface…
…. No need to faff around with binding to a service or any of that nonsense.
- Loading branch information
1 parent
eb9eb00
commit 671c5c9
Showing
7 changed files
with
148 additions
and
44 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
libraries/cyclestreets-track/src/main/java/net/cyclestreets/track/Controller.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package net.cyclestreets.track; | ||
|
||
import android.content.ComponentName; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.ServiceConnection; | ||
import android.os.IBinder; | ||
|
||
class Controller | ||
implements TrackerControl, | ||
ServiceConnection { | ||
public static TrackerControl create(final Context context, final TrackListener listener) { | ||
Controller control = new Controller(context, listener); | ||
|
||
Intent rService = new Intent(context, RecordingService.class); | ||
context.bindService(rService, control, Context.BIND_AUTO_CREATE); | ||
|
||
return control; | ||
} // create | ||
|
||
private final Context context_; | ||
private final TrackListener listener_; | ||
private IRecordService rs_; | ||
private boolean shouldStart_; | ||
private boolean unbound_; | ||
|
||
private Controller( | ||
final Context context, | ||
final TrackListener listener) { | ||
context_ = context; | ||
listener_ = listener; | ||
} // Controller | ||
|
||
@Override | ||
public void onServiceConnected( | ||
final ComponentName name, | ||
final IBinder service) { | ||
rs_ = (IRecordService)service; | ||
rs_.setListener(listener_); | ||
|
||
if (shouldStart_ == true) | ||
rs_.startRecording(); | ||
} // onServiceConnected | ||
|
||
@Override | ||
public void onServiceDisconnected(ComponentName name) {} | ||
|
||
@Override | ||
public void start() { | ||
if (rs_ == null) | ||
shouldStart_ = true; | ||
else | ||
rs_.startRecording(); | ||
} // start | ||
|
||
@Override | ||
public void stop() { | ||
if (unbound_) | ||
return; | ||
|
||
unbound_ = true; | ||
|
||
final TripData trip = rs_.stopRecording(); | ||
context_.unbindService(this); | ||
|
||
if (listener_ != null) { | ||
if (trip.dataAvailable()) | ||
listener_.completed(trip); | ||
else | ||
listener_.abandoned(trip); | ||
} | ||
} // stop | ||
} // Controller |
4 changes: 2 additions & 2 deletions
4
libraries/cyclestreets-track/src/main/java/net/cyclestreets/track/IRecordService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
package net.cyclestreets.track; | ||
|
||
public interface IRecordService { | ||
interface IRecordService { | ||
public int getState(); | ||
|
||
public TripData startRecording(); | ||
public void stopRecording(); | ||
public TripData stopRecording(); | ||
|
||
public void setListener(TrackListener ra); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
libraries/cyclestreets-track/src/main/java/net/cyclestreets/track/TrackListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
libraries/cyclestreets-track/src/main/java/net/cyclestreets/track/TrackerControl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package net.cyclestreets.track; | ||
|
||
public interface TrackerControl { | ||
void start(); | ||
void stop(); | ||
} // TrackerControl |