From 77792765b0f7d3a30bc97944af3537d71d846cb8 Mon Sep 17 00:00:00 2001 From: paroca72 Date: Sun, 7 Aug 2016 21:37:25 +0700 Subject: [PATCH] Improved the ScLocationService class --- README.md | 2 +- ScLocationService.md | 11 +- .../utils/demo/LocationChecker.java | 70 +++++--- library/build.gradle | 4 +- .../sccomponents/utils/ScLocationService.java | 154 +++++++++--------- 5 files changed, 128 insertions(+), 113 deletions(-) diff --git a/README.md b/README.md index c1f3007..d032df8 100644 --- a/README.md +++ b/README.md @@ -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' } ``` diff --git a/ScLocationService.md b/ScLocationService.md index 1f38215..1020d65 100644 --- a/ScLocationService.md +++ b/ScLocationService.md @@ -16,19 +16,12 @@ Return the connectivity manager when available. true if able to find the location by the GPS. - **boolean check()**
true if is possible to retrieve the current location. -- **boolean isLocationTrackerEnabled()**
-Check if the location tracker is enabled -- **boolean isGoogleAPIAvailable()**
-Check is the google API is available. -The google API needed for the focused location tracker. -- **void startLocationTracking(LocationListener listener)**
-Start the location tracking. -- **void stopLocationTracking()**
-Stop the location tracking. - **Location getLocation()**
Get the last known location. - **LocationRequest getLocationRequest()**
Get the location request settings object. +- **void start(LocationListener listener)**
+Start to tracking the location changes. ### Example diff --git a/app/src/main/java/com/sccomponents/utils/demo/LocationChecker.java b/app/src/main/java/com/sccomponents/utils/demo/LocationChecker.java index 0772f91..3a90754 100644 --- a/app/src/main/java/com/sccomponents/utils/demo/LocationChecker.java +++ b/app/src/main/java/com/sccomponents/utils/demo/LocationChecker.java @@ -25,24 +25,66 @@ 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 { @@ -50,24 +92,6 @@ public void onLocationChanged(Location location) { } } }); - - // 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"); } } diff --git a/library/build.gradle b/library/build.gradle index c4499d4..9e894dc 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -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 { diff --git a/library/src/main/java/com/sccomponents/utils/ScLocationService.java b/library/src/main/java/com/sccomponents/utils/ScLocationService.java index 6ed7be9..a379974 100644 --- a/library/src/main/java/com/sccomponents/utils/ScLocationService.java +++ b/library/src/main/java/com/sccomponents/utils/ScLocationService.java @@ -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. *

- * v2.0.0 + * v3.0.0 */ public class ScLocationService extends ScChecker { @@ -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(); } @@ -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 @@ -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() { @@ -286,7 +249,7 @@ public LocationRequest getLocationRequest() { /********************************************************************************** - * Override + * Override and overloads */ // Override the check function. @@ -294,8 +257,43 @@ public LocationRequest getLocationRequest() { @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(); } }