Skip to content

Commit

Permalink
Improved the ScLocationService class
Browse files Browse the repository at this point in the history
  • Loading branch information
paroca72 committed Aug 7, 2016
1 parent f96d7e9 commit 7779276
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 113 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Add the dependency
```java
dependencies {
...
compile 'com.github.paroca72:sc-utils:1.2.5'
compile 'com.github.paroca72:sc-utils:1.2.6'
}
```

Expand Down
11 changes: 2 additions & 9 deletions ScLocationService.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,12 @@ Return the connectivity manager when available.
<code>true</code> if able to find the location by the GPS.
- **boolean check()**<br />
<code>true</code> if is possible to retrieve the current location.
- **boolean isLocationTrackerEnabled()**<br />
Check if the location tracker is enabled
- **boolean isGoogleAPIAvailable()**<br />
Check is the google API is available.
The google API needed for the focused location tracker.
- **void startLocationTracking(LocationListener listener)**<br />
Start the location tracking.
- **void stopLocationTracking()**<br />
Stop the location tracking.
- **Location getLocation()**<br />
Get the last known location.
- **LocationRequest getLocationRequest()**<br />
Get the location request settings object.
- **void start(LocationListener listener)**<br />
Start to tracking the location changes.


### Example
Expand Down
70 changes: 47 additions & 23 deletions app/src/main/java/com/sccomponents/utils/demo/LocationChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,49 +25,73 @@ protected void onCreate(Bundle savedInstanceState) {
this.mService.setCheckerListener(new ScChecker.CheckerListener() {
@Override
public void onSuccess() {
// Do nothing
// Write
LocationChecker.this.writeStatus();
}

@Override
public void onFail() {
// Do nothing
// Write
LocationChecker.this.writeStatus();
}

@Override
public void onChangeState(boolean result) {
// Write
LocationChecker.this.write();
// Do nothing
}
});
this.mService.startLocationTracking(new LocationListener() {

// Write the initial status
this.writeStatus();

// Start
this.mService.start(new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// Write
LocationChecker.this.writeLocation(location);
}
});
}

// Write the current status
private void writeStatus() {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
// Network
TextView network = (TextView) LocationChecker.this.findViewById(R.id.txtNetwork);
assert network != null;
network.setText(LocationChecker.this.mService.isNetworkEnabled() ? "ON" : "OFF");

// GPS
TextView gps = (TextView) LocationChecker.this.findViewById(R.id.txtGPS);
assert gps != null;
gps.setText(LocationChecker.this.mService.isGPSEnabled() ? "ON" : "OFF");

// GPS
TextView available = (TextView) LocationChecker.this.findViewById(R.id.txtAvailable);
assert available != null;
available.setText(LocationChecker.this.mService.check() ? "YES" : "NO");
}
});
}

// Write the current location
private void writeLocation(final Location location) {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
TextView text = (TextView) LocationChecker.this.findViewById(R.id.txtLocation);
assert text != null;

if (location == null) {
text.setText("Unknown location");
} else {
text.setText(location.getLatitude() + " : " + location.getLongitude());
}
}
});

// Write
this.write();
}

// Write the current status
private void write() {
// Network
TextView network = (TextView) this.findViewById(R.id.txtNetwork);
network.setText(this.mService.isNetworkEnabled() ? "ON" : "OFF");

// GPS
TextView gps = (TextView) this.findViewById(R.id.txtGPS);
gps.setText(this.mService.isGPSEnabled() ? "ON" : "OFF");

// GPS
TextView available = (TextView) this.findViewById(R.id.txtAvailable);
available.setText(this.mService.check() ? "YES" : "NO");
}

}
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
versionCode 10
versionName '1.2.5'
versionCode 11
versionName '1.2.6'
multiDexEnabled true
}
buildTypes {
Expand Down
154 changes: 76 additions & 78 deletions library/src/main/java/com/sccomponents/utils/ScLocationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* Call the function NetworkService or GPS for check the current status or call check get true is
* at least one connection is alive.
* <p/>
* v2.0.0
* v3.0.0
*/
public class ScLocationService
extends ScChecker {
Expand Down Expand Up @@ -55,9 +55,11 @@ public ScLocationService(Context context) {
this.mLocationRequest = new LocationRequest();
this.mStartingLocationTracking = false;

// Request the permission
// Check
this.requestPermissions();
// Initialize the google API client
this.isGoogleAPIAvailable();

// Initialize
this.initializeGoogleAPIClient();
}

Expand All @@ -66,6 +68,39 @@ public ScLocationService(Context context) {
* Private methods
*/

/*
* Check is the google API is available.
* The google API needed for the focused location tracker.
*/
private boolean isGoogleAPIAvailable() {
// Check for empty values
if (this.mContext == null) return false;

// Get the google API checker
GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();
// Request if the play service is available and hold the result
int result = googleAPI.isGooglePlayServicesAvailable(this.mContext);
// If success
if (result == ConnectionResult.SUCCESS) {
// Continue
return true;
}
// If not available
else {
// Check if possible to have a resolution
if (googleAPI.isUserResolvableError(result) && this.mContext instanceof Activity) {
// Open the error dialog
googleAPI.getErrorDialog(
(Activity) this.mContext,
result,
ScLocationService.CONNECTION_FAILURE_RESOLUTION_REQUEST
).show();
}
// Return
return false;
}
}

// Request user permission
private void requestPermissions() {
// If version if minor than M we need to have an explicit permission from the user
Expand Down Expand Up @@ -188,78 +223,6 @@ public LocationManager getLocationManager() {
* Location tracking
*/

// Check if the location tracker is enabled
@SuppressWarnings("unused")
public boolean isLocationTrackerEnabled() {
// Check for empty value
if (ScLocationService.mGoogleApiClient != null) {
return ScLocationService.mGoogleApiClient.isConnected();
}
// Return false
return false;
}

/*
* Check is the google API is available.
* The google API needed for the focused location tracker.
*/
@SuppressWarnings("unused")
public boolean isGoogleAPIAvailable() {
// Check for empty values
if (this.mContext == null) return false;

// Get the google API checker
GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();
// Request if the play service is available and hold the result
int result = googleAPI.isGooglePlayServicesAvailable(this.mContext);
// If success
if (result == ConnectionResult.SUCCESS) {
// Continue
return true;
}
// If not available
else {
// Check if possible to have a resolution
if (googleAPI.isUserResolvableError(result) && this.mContext instanceof Activity) {
// Open the error dialog
googleAPI.getErrorDialog(
(Activity) this.mContext,
result,
ScLocationService.CONNECTION_FAILURE_RESOLUTION_REQUEST
).show();
}
// Return
return false;
}
}

// Start the location tracking
@SuppressWarnings("unused")
public void startLocationTracking(LocationListener listener) {
// Hold the listener and set the trigger
this.mLocationListener = listener;
this.mStartingLocationTracking = true;

// Check for the connection
if (!ScLocationService.mGoogleApiClient.isConnected()) {
// Try to connect and the tracker will attached once the connection will done
ScLocationService.mGoogleApiClient.connect();
}
// Else attach the location tracker directly
else {
this.internalStartLocationTracking();
}
}

// Stop the location tracking
@SuppressWarnings("unused")
public void stopLocationTracking() {
// Stop the tracking
this.internalStopLocationTracking();
// Stop the service
ScLocationService.mGoogleApiClient.disconnect();
}

// Get the last known location
@SuppressWarnings("unused")
public Location getLocation() {
Expand All @@ -286,16 +249,51 @@ public LocationRequest getLocationRequest() {


/**********************************************************************************
* Override
* Override and overloads
*/

// Override the check function.
// As this class extend the ScChecker this methods can be auto-checked periodically.
@Override
@SuppressWarnings("unused")
public boolean check() {
// Return the current status
return this.isGPSEnabled() || this.isNetworkEnabled();
// Check for empty value
if (ScLocationService.mGoogleApiClient != null) {
return ScLocationService.mGoogleApiClient.isConnected();
}
// Return false
return false;
}

// Overload start methods
@SuppressWarnings("unused")
public void start(LocationListener listener) {
// Hold the listener and set the trigger
this.mLocationListener = listener;
this.mStartingLocationTracking = true;

// Check for the connection
if (!ScLocationService.mGoogleApiClient.isConnected()) {
// Try to connect and the tracker will attached once the connection will done
ScLocationService.mGoogleApiClient.connect();
}
// Else attach the location tracker directly
else {
this.internalStartLocationTracking();
}

// Super
super.start();
}

@Override
public void stop() {
// Super
super.stop();

// Stop the tracking
this.internalStopLocationTracking();
ScLocationService.mGoogleApiClient.disconnect();
}

}

0 comments on commit 7779276

Please sign in to comment.