-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Samuelford24
authored and
Samuelford24
committed
Apr 22, 2021
1 parent
6765841
commit 7d11b87
Showing
11 changed files
with
312 additions
and
40 deletions.
There are no files selected for viewing
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
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
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
8 changes: 8 additions & 0 deletions
8
app/src/main/java/com/sf/stormwaterutilityandroid/WaterWay/AppConstants.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,8 @@ | ||
package com.sf.stormwaterutilityandroid.WaterWay; | ||
|
||
public class AppConstants { | ||
|
||
public static final int LOCATION_REQUEST = 1000; | ||
public static final int GPS_REQUEST = 1001; | ||
} | ||
|
105 changes: 105 additions & 0 deletions
105
app/src/main/java/com/sf/stormwaterutilityandroid/WaterWay/GpsUtils.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,105 @@ | ||
package com.sf.stormwaterutilityandroid.WaterWay; | ||
|
||
import android.annotation.SuppressLint; | ||
import android.app.Activity; | ||
import android.content.Context; | ||
import android.content.IntentSender; | ||
import android.location.LocationManager; | ||
import android.util.Log; | ||
import android.widget.Toast; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import com.google.android.gms.common.api.ApiException; | ||
import com.google.android.gms.common.api.ResolvableApiException; | ||
import com.google.android.gms.location.LocationRequest; | ||
import com.google.android.gms.location.LocationServices; | ||
import com.google.android.gms.location.LocationSettingsRequest; | ||
import com.google.android.gms.location.LocationSettingsResponse; | ||
import com.google.android.gms.location.LocationSettingsStatusCodes; | ||
import com.google.android.gms.location.SettingsClient; | ||
import com.google.android.gms.tasks.OnFailureListener; | ||
import com.google.android.gms.tasks.OnSuccessListener; | ||
|
||
public class GpsUtils { | ||
|
||
|
||
private static final String TAG = "GPSUTILS" ; | ||
private Context context; | ||
private SettingsClient mSettingsClient; | ||
private LocationSettingsRequest mLocationSettingsRequest; | ||
private LocationManager locationManager; | ||
private LocationRequest locationRequest; | ||
|
||
public GpsUtils(Context context) { | ||
this.context = context; | ||
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); | ||
mSettingsClient = LocationServices.getSettingsClient(context); | ||
|
||
locationRequest = LocationRequest.create(); | ||
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); | ||
locationRequest.setInterval(10 * 1000); | ||
locationRequest.setFastestInterval(2 * 1000); | ||
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() | ||
.addLocationRequest(locationRequest); | ||
mLocationSettingsRequest = builder.build(); | ||
|
||
//************************** | ||
builder.setAlwaysShow(true); //this is the key ingredient | ||
//************************** | ||
} | ||
|
||
// method for turn on GPS | ||
public void turnGPSOn(onGpsListener onGpsListener) { | ||
|
||
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { | ||
if (onGpsListener != null) { | ||
onGpsListener.gpsStatus(true); | ||
} | ||
} else { | ||
mSettingsClient | ||
.checkLocationSettings(mLocationSettingsRequest) | ||
.addOnSuccessListener((Activity) context, new OnSuccessListener<LocationSettingsResponse>() { | ||
@SuppressLint("MissingPermission") | ||
@Override | ||
public void onSuccess(LocationSettingsResponse locationSettingsResponse) { | ||
|
||
// GPS is already enable, callback GPS status through listener | ||
if (onGpsListener != null) { | ||
onGpsListener.gpsStatus(true); | ||
} | ||
} | ||
}) | ||
.addOnFailureListener((Activity) context, new OnFailureListener() { | ||
@Override | ||
public void onFailure(@NonNull Exception e) { | ||
int statusCode = ((ApiException) e).getStatusCode(); | ||
switch (statusCode) { | ||
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: | ||
|
||
try { | ||
// Show the dialog by calling startResolutionForResult(), and check the | ||
// result in onActivityResult(). | ||
ResolvableApiException rae = (ResolvableApiException) e; | ||
rae.startResolutionForResult((Activity) context, AppConstants.GPS_REQUEST); | ||
} catch (IntentSender.SendIntentException sie) { | ||
Log.i(TAG, "PendingIntent unable to execute request."); | ||
} | ||
break; | ||
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: | ||
String errorMessage = "Location settings are inadequate, and cannot be " + | ||
"fixed here. Fix in Settings."; | ||
Log.e(TAG, errorMessage); | ||
|
||
Toast.makeText((Activity) context, errorMessage, Toast.LENGTH_LONG).show(); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
|
||
public interface onGpsListener { | ||
void gpsStatus(boolean isGPSEnable); | ||
} | ||
|
||
} |
Oops, something went wrong.