Guest User

Untitled

a guest
Jul 18th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.22 KB | None | 0 0
  1. package testing.gps_location;
  2.  
  3. import android.Manifest;
  4. import android.annotation.SuppressLint;
  5. import android.app.Activity;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.content.pm.PackageManager;
  9. import android.location.Location;
  10. import android.location.LocationListener;
  11. import android.location.LocationManager;
  12. import android.net.Uri;
  13. import android.os.Bundle;
  14. import android.provider.Settings;
  15. import android.support.annotation.NonNull;
  16. import android.support.design.widget.Snackbar;
  17. import android.support.v4.app.ActivityCompat;
  18. import android.util.Log;
  19. import android.view.View;
  20.  
  21. import static android.content.Context.LOCATION_SERVICE;
  22.  
  23. /**
  24. * Created by amr mohamed on 4/14/2018.
  25. */
  26.  
  27. public class UsingLocation implements ActivityCompat.OnRequestPermissionsResultCallback {
  28.  
  29. private static final int REQUEST_PERMISSIONS_REQUEST_CODE = 34;
  30.  
  31.  
  32. //use this onnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnne
  33. /**
  34. * use this one please to determine weither user where in the room or note
  35. * @param roomLong the lecture room loc
  36. * @param roomLat the lec room location
  37. * @return true or false indicate weather the user inside the room or not
  38. */
  39.  
  40. boolean isInsideTheRoom(double roomLong, double roomLat) {
  41. configure_latLang();
  42. float[] dist = new float[1];
  43. Location.distanceBetween(getLat(), getlong()//these are the user location
  44. , roomLat, roomLong, dist);//todo this shuld be the room location
  45.  
  46. return dist[0] < 50; //50 is the radius of the room, change it as desired
  47. //i meeeeean this will check weather he were in radius of 50 meter or not
  48. }
  49.  
  50. private double lang,lat;
  51. private LocationManager locationManager;
  52. private LocationListener listener;
  53.  
  54. private Context AmRcontext;
  55. private Activity AmRactivity;
  56.  
  57.  
  58. UsingLocation(Context c){
  59. AmRcontext = c;
  60. AmRactivity = (Activity) c;
  61.  
  62. inistilizeLocationManger();
  63. }
  64.  
  65. public double getLat(){
  66. return lat;
  67. }
  68. public double getlong(){
  69. return lang;
  70. }
  71. // void updateLocation(){
  72. // configure_latLang();
  73. // }
  74. private void inistilizeLocationManger(){
  75.  
  76. locationManager = (LocationManager) AmRcontext.getSystemService(LOCATION_SERVICE);
  77. listener = new LocationListener() {
  78. @Override
  79. public void onLocationChanged(Location location) {
  80.  
  81. while (true)
  82. if (location != null && location.hasAccuracy() && location.getAccuracy() < 700) {
  83. lang = location.getLongitude();
  84. lat = location.getLatitude();
  85. locationManager.removeUpdates(listener);
  86. break;
  87. }
  88.  
  89. }
  90. //region none important
  91. @Override
  92. public void onStatusChanged(String s, int i, Bundle bundle) {
  93.  
  94. }
  95.  
  96. @Override
  97. public void onProviderEnabled(String s) {
  98.  
  99. }
  100. //endregion
  101. @Override
  102. public void onProviderDisabled(String s) {
  103. Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  104. AmRcontext.startActivity(i);
  105. }
  106. };
  107.  
  108. // Check if the user revoked runtime permissions.
  109. if (!checkPermissions()) {
  110. requestPermissions();
  111. }
  112. else
  113. configure_latLang();
  114. }
  115. //region ask user permission
  116. ///////////////////////////////////////***********************************************
  117. private void requestPermissions() {
  118. boolean shouldProvideRationale =
  119. ActivityCompat.shouldShowRequestPermissionRationale(AmRactivity,
  120. Manifest.permission.ACCESS_FINE_LOCATION);
  121.  
  122. // Provide an additional rationale to the user. This would happen if the user denied the
  123. // request previously, but didn't check the "Don't ask again" checkbox.
  124. if (shouldProvideRationale) {
  125. Snackbar.make(
  126. AmRactivity.findViewById(R.id.activity_main),
  127. R.string.permission_rationale,
  128. Snackbar.LENGTH_INDEFINITE)
  129. .setAction(R.string.ok, new View.OnClickListener() {
  130. @Override
  131. public void onClick(View view) {
  132. // Request permission
  133. ActivityCompat.requestPermissions(AmRactivity,
  134. new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
  135. REQUEST_PERMISSIONS_REQUEST_CODE);
  136. }
  137. })
  138. .show();
  139. } else {
  140. // Request permission. It's possible this can be auto answered if device policy
  141. // sets the permission in a given state or the user denied the permission
  142. // previously and checked "Never ask again".
  143. ActivityCompat.requestPermissions(AmRactivity,
  144. new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
  145. REQUEST_PERMISSIONS_REQUEST_CODE);
  146. }
  147. }
  148. /**
  149. * Return the current state of the permissions needed.
  150. */
  151. private boolean checkPermissions() {
  152. int permissionState = ActivityCompat.checkSelfPermission(AmRcontext,
  153. Manifest.permission.ACCESS_FINE_LOCATION);
  154. return permissionState == PackageManager.PERMISSION_GRANTED;
  155. }
  156. /**
  157. * Callback received when a permissions request has been completed.
  158. */
  159. @Override
  160. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
  161. @NonNull int[] grantResults) {
  162. if (requestCode == REQUEST_PERMISSIONS_REQUEST_CODE) {
  163. if (grantResults.length <= 0) {
  164. // If user interaction was interrupted, the permission request is cancelled and you
  165. // receive empty arrays.
  166. } else if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  167. // Permission was granted.
  168. configure_latLang();
  169. } else {
  170. // Permission denied.
  171.  
  172. // Notify the user via a SnackBar that they have rejected a core permission for the
  173. // app, which makes the Activity useless. In a real app, core permissions would
  174. // typically be best requested during a welcome-screen flow.
  175.  
  176. // Additionally, it is important to remember that a permission might have been
  177. // rejected without asking the user for permission (device policy or "Never ask
  178. // again" prompts). Therefore, a user interface affordance is typically implemented
  179. // when permissions are denied. Otherwise, your app could appear unresponsive to
  180. // touches or interactions which have required permissions.
  181. Snackbar.make(
  182. AmRactivity.findViewById(R.id.activity_main),
  183. R.string.permission_denied_explanation,
  184. Snackbar.LENGTH_INDEFINITE)
  185. .setAction(R.string.settings, new View.OnClickListener() {
  186. @Override
  187. public void onClick(View view) {
  188. // Build intent that displays the App settings screen.
  189. Intent intent = new Intent();
  190. intent.setAction(
  191. Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  192. Uri uri = Uri.fromParts("package",
  193. BuildConfig.APPLICATION_ID, null);
  194. intent.setData(uri);
  195. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  196. AmRcontext.startActivity(intent);
  197. }
  198. })
  199. .show();
  200. }
  201. }
  202. }
  203. /***************************************************************///////////////
  204. //endregion
  205. @SuppressLint("MissingPermission")
  206. public void configure_latLang(){
  207. locationManager.requestLocationUpdates("gps", 0, 0, listener);
  208. }
  209.  
  210.  
  211. }
Add Comment
Please, Sign In to add comment