Created
July 18, 2018 11:43
-
-
Save AmrAbuelhamd/9ad7c656957dd85aaf616a28f1f4a0d6 to your computer and use it in GitHub Desktop.
this guest checks weather you are inside particular area or no
This file contains hidden or 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
| package testing.gps_location; | |
| import android.Manifest; | |
| import android.annotation.SuppressLint; | |
| import android.app.Activity; | |
| import android.content.Context; | |
| import android.content.Intent; | |
| import android.content.pm.PackageManager; | |
| import android.location.Location; | |
| import android.location.LocationListener; | |
| import android.location.LocationManager; | |
| import android.net.Uri; | |
| import android.os.Bundle; | |
| import android.provider.Settings; | |
| import android.support.annotation.NonNull; | |
| import android.support.design.widget.Snackbar; | |
| import android.support.v4.app.ActivityCompat; | |
| import android.util.Log; | |
| import android.view.View; | |
| import static android.content.Context.LOCATION_SERVICE; | |
| /** | |
| * Created by amr mohamed on 4/14/2018. | |
| */ | |
| public class UsingLocation implements ActivityCompat.OnRequestPermissionsResultCallback { | |
| private static final int REQUEST_PERMISSIONS_REQUEST_CODE = 34; | |
| //use this onnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnne | |
| /** | |
| * use this one please to determine weither user where in the room or note | |
| * @param roomLong the lecture room loc | |
| * @param roomLat the lec room location | |
| * @return true or false indicate weather the user inside the room or not | |
| */ | |
| boolean isInsideTheRoom(double roomLong, double roomLat) { | |
| configure_latLang(); | |
| float[] dist = new float[1]; | |
| Location.distanceBetween(getLat(), getlong()//these are the user location | |
| , roomLat, roomLong, dist);//todo this shuld be the room location | |
| return dist[0] < 50; //50 is the radius of the room, change it as desired | |
| //i meeeeean this will check weather he were in radius of 50 meter or not | |
| } | |
| private double lang,lat; | |
| private LocationManager locationManager; | |
| private LocationListener listener; | |
| private Context AmRcontext; | |
| private Activity AmRactivity; | |
| UsingLocation(Context c){ | |
| AmRcontext = c; | |
| AmRactivity = (Activity) c; | |
| inistilizeLocationManger(); | |
| } | |
| public double getLat(){ | |
| return lat; | |
| } | |
| public double getlong(){ | |
| return lang; | |
| } | |
| // void updateLocation(){ | |
| // configure_latLang(); | |
| // } | |
| private void inistilizeLocationManger(){ | |
| locationManager = (LocationManager) AmRcontext.getSystemService(LOCATION_SERVICE); | |
| listener = new LocationListener() { | |
| @Override | |
| public void onLocationChanged(Location location) { | |
| while (true) | |
| if (location != null && location.hasAccuracy() && location.getAccuracy() < 700) { | |
| lang = location.getLongitude(); | |
| lat = location.getLatitude(); | |
| locationManager.removeUpdates(listener); | |
| break; | |
| } | |
| } | |
| //region none important | |
| @Override | |
| public void onStatusChanged(String s, int i, Bundle bundle) { | |
| } | |
| @Override | |
| public void onProviderEnabled(String s) { | |
| } | |
| //endregion | |
| @Override | |
| public void onProviderDisabled(String s) { | |
| Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); | |
| AmRcontext.startActivity(i); | |
| } | |
| }; | |
| // Check if the user revoked runtime permissions. | |
| if (!checkPermissions()) { | |
| requestPermissions(); | |
| } | |
| else | |
| configure_latLang(); | |
| } | |
| //region ask user permission | |
| ///////////////////////////////////////*********************************************** | |
| private void requestPermissions() { | |
| boolean shouldProvideRationale = | |
| ActivityCompat.shouldShowRequestPermissionRationale(AmRactivity, | |
| Manifest.permission.ACCESS_FINE_LOCATION); | |
| // Provide an additional rationale to the user. This would happen if the user denied the | |
| // request previously, but didn't check the "Don't ask again" checkbox. | |
| if (shouldProvideRationale) { | |
| Snackbar.make( | |
| AmRactivity.findViewById(R.id.activity_main), | |
| R.string.permission_rationale, | |
| Snackbar.LENGTH_INDEFINITE) | |
| .setAction(R.string.ok, new View.OnClickListener() { | |
| @Override | |
| public void onClick(View view) { | |
| // Request permission | |
| ActivityCompat.requestPermissions(AmRactivity, | |
| new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, | |
| REQUEST_PERMISSIONS_REQUEST_CODE); | |
| } | |
| }) | |
| .show(); | |
| } else { | |
| // Request permission. It's possible this can be auto answered if device policy | |
| // sets the permission in a given state or the user denied the permission | |
| // previously and checked "Never ask again". | |
| ActivityCompat.requestPermissions(AmRactivity, | |
| new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, | |
| REQUEST_PERMISSIONS_REQUEST_CODE); | |
| } | |
| } | |
| /** | |
| * Return the current state of the permissions needed. | |
| */ | |
| private boolean checkPermissions() { | |
| int permissionState = ActivityCompat.checkSelfPermission(AmRcontext, | |
| Manifest.permission.ACCESS_FINE_LOCATION); | |
| return permissionState == PackageManager.PERMISSION_GRANTED; | |
| } | |
| /** | |
| * Callback received when a permissions request has been completed. | |
| */ | |
| @Override | |
| public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, | |
| @NonNull int[] grantResults) { | |
| if (requestCode == REQUEST_PERMISSIONS_REQUEST_CODE) { | |
| if (grantResults.length <= 0) { | |
| // If user interaction was interrupted, the permission request is cancelled and you | |
| // receive empty arrays. | |
| } else if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { | |
| // Permission was granted. | |
| configure_latLang(); | |
| } else { | |
| // Permission denied. | |
| // Notify the user via a SnackBar that they have rejected a core permission for the | |
| // app, which makes the Activity useless. In a real app, core permissions would | |
| // typically be best requested during a welcome-screen flow. | |
| // Additionally, it is important to remember that a permission might have been | |
| // rejected without asking the user for permission (device policy or "Never ask | |
| // again" prompts). Therefore, a user interface affordance is typically implemented | |
| // when permissions are denied. Otherwise, your app could appear unresponsive to | |
| // touches or interactions which have required permissions. | |
| Snackbar.make( | |
| AmRactivity.findViewById(R.id.activity_main), | |
| R.string.permission_denied_explanation, | |
| Snackbar.LENGTH_INDEFINITE) | |
| .setAction(R.string.settings, new View.OnClickListener() { | |
| @Override | |
| public void onClick(View view) { | |
| // Build intent that displays the App settings screen. | |
| Intent intent = new Intent(); | |
| intent.setAction( | |
| Settings.ACTION_APPLICATION_DETAILS_SETTINGS); | |
| Uri uri = Uri.fromParts("package", | |
| BuildConfig.APPLICATION_ID, null); | |
| intent.setData(uri); | |
| intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
| AmRcontext.startActivity(intent); | |
| } | |
| }) | |
| .show(); | |
| } | |
| } | |
| } | |
| /***************************************************************/////////////// | |
| //endregion | |
| @SuppressLint("MissingPermission") | |
| public void configure_latLang(){ | |
| locationManager.requestLocationUpdates("gps", 0, 0, listener); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment