From d58cdd28b28748cd968d8b79803bd55ea81cbca4 Mon Sep 17 00:00:00 2001 From: pablo Date: Sun, 1 Aug 2021 21:28:13 +0200 Subject: [PATCH] Initial commit --- .gitignore | 9 + .idea/.gitignore | 8 + .idea/compiler.xml | 6 + .idea/copyright/profiles_settings.xml | 3 + .idea/dictionaries/anupamchugh.xml | 3 + .idea/encodings.xml | 6 + .idea/gradle.xml | 22 ++ .idea/jarRepositories.xml | 25 ++ .idea/misc.xml | 105 ++++++++ .idea/modules.xml | 9 + .idea/runConfigurations.xml | 12 + .idea/sonarIssues.xml | 34 +++ .idea/sonarlint/issuestore/index.pb | 0 .idea/vcs.xml | 6 + app/.gitignore | 1 + app/build.gradle | 30 +++ app/proguard-rules.pro | 17 ++ .../ExampleInstrumentedTest.java | 26 ++ app/src/main/AndroidManifest.xml | 22 ++ .../gpslocationtracking/LocationTrack.java | 229 ++++++++++++++++++ .../gpslocationtracking/MainActivity.java | 157 ++++++++++++ app/src/main/res/layout/activity_main.xml | 19 ++ app/src/main/res/mipmap-hdpi/ic_launcher.png | Bin 0 -> 3418 bytes app/src/main/res/mipmap-mdpi/ic_launcher.png | Bin 0 -> 2206 bytes app/src/main/res/mipmap-xhdpi/ic_launcher.png | Bin 0 -> 4842 bytes .../main/res/mipmap-xxhdpi/ic_launcher.png | Bin 0 -> 7718 bytes .../main/res/mipmap-xxxhdpi/ic_launcher.png | Bin 0 -> 10486 bytes app/src/main/res/values-w820dp/dimens.xml | 6 + app/src/main/res/values/colors.xml | 6 + app/src/main/res/values/dimens.xml | 5 + app/src/main/res/values/strings.xml | 3 + app/src/main/res/values/styles.xml | 11 + .../gpslocationtracking/ExampleUnitTest.java | 17 ++ build.gradle | 31 +++ gradle.properties | 17 ++ gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 53636 bytes gradle/wrapper/gradle-wrapper.properties | 6 + gradlew | 160 ++++++++++++ gradlew.bat | 90 +++++++ settings.gradle | 1 + 40 files changed, 1102 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/compiler.xml create mode 100644 .idea/copyright/profiles_settings.xml create mode 100644 .idea/dictionaries/anupamchugh.xml create mode 100644 .idea/encodings.xml create mode 100644 .idea/gradle.xml create mode 100644 .idea/jarRepositories.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/runConfigurations.xml create mode 100644 .idea/sonarIssues.xml create mode 100644 .idea/sonarlint/issuestore/index.pb create mode 100644 .idea/vcs.xml create mode 100644 app/.gitignore create mode 100644 app/build.gradle create mode 100644 app/proguard-rules.pro create mode 100644 app/src/androidTest/java/com/journaldev/gpslocationtracking/ExampleInstrumentedTest.java create mode 100644 app/src/main/AndroidManifest.xml create mode 100644 app/src/main/java/com/journaldev/gpslocationtracking/LocationTrack.java create mode 100644 app/src/main/java/com/journaldev/gpslocationtracking/MainActivity.java create mode 100644 app/src/main/res/layout/activity_main.xml create mode 100644 app/src/main/res/mipmap-hdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-mdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxxhdpi/ic_launcher.png create mode 100644 app/src/main/res/values-w820dp/dimens.xml create mode 100644 app/src/main/res/values/colors.xml create mode 100644 app/src/main/res/values/dimens.xml create mode 100644 app/src/main/res/values/strings.xml create mode 100644 app/src/main/res/values/styles.xml create mode 100644 app/src/test/java/com/journaldev/gpslocationtracking/ExampleUnitTest.java create mode 100644 build.gradle create mode 100644 gradle.properties create mode 100644 gradle/wrapper/gradle-wrapper.jar create mode 100644 gradle/wrapper/gradle-wrapper.properties create mode 100644 gradlew create mode 100644 gradlew.bat create mode 100644 settings.gradle diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..39fb081 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +*.iml +.gradle +/local.properties +/.idea/workspace.xml +/.idea/libraries +.DS_Store +/build +/captures +.externalNativeBuild diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..61a9130 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/dictionaries/anupamchugh.xml b/.idea/dictionaries/anupamchugh.xml new file mode 100644 index 0000000..4a714c3 --- /dev/null +++ b/.idea/dictionaries/anupamchugh.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..97626ba --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..5ffd52a --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,22 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..3e0f12c --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..04ed8ac --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,105 @@ + + + + + + + + + + + + + + + + + + Android + + + Android Lint + + + + + Android + + + + + + + + + + + + + + + 1.8 + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..77f9ea1 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..7f68460 --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/.idea/sonarIssues.xml b/.idea/sonarIssues.xml new file mode 100644 index 0000000..e4ff678 --- /dev/null +++ b/.idea/sonarIssues.xml @@ -0,0 +1,34 @@ + + + + + + \ No newline at end of file diff --git a/.idea/sonarlint/issuestore/index.pb b/.idea/sonarlint/issuestore/index.pb new file mode 100644 index 0000000..e69de29 diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..42f2d41 --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,30 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 24 + buildToolsVersion "24.2.1" + defaultConfig { + minSdkVersion 19 + targetSdkVersion 24 + applicationId "com.journaldev.gpslocationtracking" + versionCode 1 + versionName "1.0" + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) + androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { + exclude group: 'com.android.support', module: 'support-annotations' + }) + compile 'com.android.support:appcompat-v7:24.2.1' + compile 'com.android.support:support-v4:24.2.1' + compile 'com.android.support:design:24.2.1' +} diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..71d9544 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,17 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in /Users/anupamchugh/Library/Android/sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/app/src/androidTest/java/com/journaldev/gpslocationtracking/ExampleInstrumentedTest.java b/app/src/androidTest/java/com/journaldev/gpslocationtracking/ExampleInstrumentedTest.java new file mode 100644 index 0000000..f6ed238 --- /dev/null +++ b/app/src/androidTest/java/com/journaldev/gpslocationtracking/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package com.journaldev.gpslocationtracking; + +import android.content.Context; +import android.support.test.InstrumentationRegistry; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumentation test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() throws Exception { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getTargetContext(); + + assertEquals("com.journaldev.gpslocationtracking", appContext.getPackageName()); + } +} diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..f8cef6c --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/journaldev/gpslocationtracking/LocationTrack.java b/app/src/main/java/com/journaldev/gpslocationtracking/LocationTrack.java new file mode 100644 index 0000000..40b1e47 --- /dev/null +++ b/app/src/main/java/com/journaldev/gpslocationtracking/LocationTrack.java @@ -0,0 +1,229 @@ +package com.journaldev.gpslocationtracking; + +import android.Manifest; +import android.app.Service; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.location.Location; +import android.location.LocationListener; +import android.location.LocationManager; +import android.os.Bundle; +import android.os.IBinder; +import android.provider.Settings; +import android.support.v4.app.ActivityCompat; +import android.support.v7.app.AlertDialog; +import android.widget.Toast; + +//import javax.naming.Context; +//import javax.xml.stream.Location; +//import javax.xml.ws.Service; + +/** + * Created by anupamchugh on 28/11/16. + */ + +public class LocationTrack extends Service implements LocationListener { + + private final Context mContext; + + + boolean checkGPS = false; + + + boolean checkNetwork = false; + + boolean canGetLocation = false; + + Location loc; + double latitude; + double longitude; + + + private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; + + + private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; + protected LocationManager locationManager; + + public LocationTrack(Context mContext) { + this.mContext = mContext; + getLocation(); + } + + private Location getLocation() { + + try { + locationManager = (LocationManager) mContext + .getSystemService(LOCATION_SERVICE); + + // get GPS status + checkGPS = locationManager + .isProviderEnabled(LocationManager.GPS_PROVIDER); + + // get network provider status + checkNetwork = locationManager + .isProviderEnabled(LocationManager.NETWORK_PROVIDER); + + if (!checkGPS && !checkNetwork) { + Toast.makeText(mContext, "No Service Provider is available", Toast.LENGTH_SHORT).show(); + } else { + this.canGetLocation = true; + + // if GPS Enabled get lat/long using GPS Services + if (checkGPS) { + + if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { + // TODO: Consider calling + // ActivityCompat#requestPermissions + // here to request the missing permissions, and then overriding + // public void onRequestPermissionsResult(int requestCode, String[] permissions, + // int[] grantResults) + // to handle the case where the user grants the permission. See the documentation + // for ActivityCompat#requestPermissions for more details. + } + locationManager.requestLocationUpdates( + LocationManager.GPS_PROVIDER, + MIN_TIME_BW_UPDATES, + MIN_DISTANCE_CHANGE_FOR_UPDATES, this); + if (locationManager != null) { + loc = locationManager + .getLastKnownLocation(LocationManager.GPS_PROVIDER); + if (loc != null) { + latitude = loc.getLatitude(); + longitude = loc.getLongitude(); + } + } + + + } + + + /*if (checkNetwork) { + + + if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { + // TODO: Consider calling + // ActivityCompat#requestPermissions + // here to request the missing permissions, and then overriding + // public void onRequestPermissionsResult(int requestCode, String[] permissions, + // int[] grantResults) + // to handle the case where the user grants the permission. See the documentation + // for ActivityCompat#requestPermissions for more details. + } + locationManager.requestLocationUpdates( + LocationManager.NETWORK_PROVIDER, + MIN_TIME_BW_UPDATES, + MIN_DISTANCE_CHANGE_FOR_UPDATES, this); + + if (locationManager != null) { + loc = locationManager + .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); + + } + + if (loc != null) { + latitude = loc.getLatitude(); + longitude = loc.getLongitude(); + } + }*/ + + } + + + } catch (Exception e) { + e.printStackTrace(); + } + + return loc; + } + + public double getLongitude() { + if (loc != null) { + longitude = loc.getLongitude(); + } + return longitude; + } + + public double getLatitude() { + if (loc != null) { + latitude = loc.getLatitude(); + } + return latitude; + } + + public boolean canGetLocation() { + return this.canGetLocation; + } + + public void showSettingsAlert() { + AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); + + + alertDialog.setTitle("GPS is not Enabled!"); + + alertDialog.setMessage("Do you want to turn on GPS?"); + + + alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); + mContext.startActivity(intent); + } + }); + + + alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + dialog.cancel(); + } + }); + + + alertDialog.show(); + } + + + public void stopListener() { + if (locationManager != null) { + + if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { + // TODO: Consider calling + // ActivityCompat#requestPermissions + // here to request the missing permissions, and then overriding + // public void onRequestPermissionsResult(int requestCode, String[] permissions, + // int[] grantResults) + // to handle the case where the user grants the permission. See the documentation + // for ActivityCompat#requestPermissions for more details. + return; + } + locationManager.removeUpdates(LocationTrack.this); + } + } + + @Override + public IBinder onBind(Intent intent) { + return null; + } + + @Override + public void onLocationChanged(Location location) { + + } + + @Override + public void onStatusChanged(String s, int i, Bundle bundle) { + + } + + @Override + public void onProviderEnabled(String s) { + + } + + @Override + public void onProviderDisabled(String s) { + + } +} diff --git a/app/src/main/java/com/journaldev/gpslocationtracking/MainActivity.java b/app/src/main/java/com/journaldev/gpslocationtracking/MainActivity.java new file mode 100644 index 0000000..3e45285 --- /dev/null +++ b/app/src/main/java/com/journaldev/gpslocationtracking/MainActivity.java @@ -0,0 +1,157 @@ +package com.journaldev.gpslocationtracking; + +import android.annotation.TargetApi; +import android.content.DialogInterface; +import android.content.pm.PackageManager; +import android.os.Build; +import android.support.v7.app.AlertDialog; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.widget.Button; +import android.widget.Toast; + +import java.util.ArrayList; + +import static android.Manifest.permission.ACCESS_COARSE_LOCATION; +import static android.Manifest.permission.ACCESS_FINE_LOCATION; + +public class MainActivity extends AppCompatActivity { + + + private ArrayList permissionsToRequest; + private ArrayList permissionsRejected = new ArrayList<>(); + private ArrayList permissions = new ArrayList<>(); + + private final static int ALL_PERMISSIONS_RESULT = 101; + LocationTrack locationTrack; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + permissions.add(ACCESS_FINE_LOCATION); + permissions.add(ACCESS_COARSE_LOCATION); + + permissionsToRequest = findUnAskedPermissions(permissions); + //get the permissions we have asked for before but are not granted.. + //we will store this in a global list to access later. + + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + + + if (permissionsToRequest.size() > 0) + requestPermissions(permissionsToRequest.toArray(new String[permissionsToRequest.size()]), ALL_PERMISSIONS_RESULT); + } + + + Button btn = (Button) findViewById(R.id.btn); + + + btn.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + + locationTrack = new LocationTrack(MainActivity.this); + + + if (locationTrack.canGetLocation()) { + + + double longitude = locationTrack.getLongitude(); + double latitude = locationTrack.getLatitude(); + + Toast.makeText(getApplicationContext(), "Longitude:" + Double.toString(longitude) + "\nLatitude:" + Double.toString(latitude), Toast.LENGTH_SHORT).show(); + } else { + + locationTrack.showSettingsAlert(); + } + + } + }); + + } + + + private ArrayList findUnAskedPermissions(ArrayList wanted) { + ArrayList result = new ArrayList(); + + for (String perm : wanted) { + if (!hasPermission(perm)) { + result.add(perm); + } + } + + return result; + } + + private boolean hasPermission(String permission) { + if (canMakeSmores()) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + return (checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED); + } + } + return true; + } + + private boolean canMakeSmores() { + return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1); + } + + + @TargetApi(Build.VERSION_CODES.M) + @Override + public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { + + switch (requestCode) { + + case ALL_PERMISSIONS_RESULT: + for (String perms : permissionsToRequest) { + if (!hasPermission(perms)) { + permissionsRejected.add(perms); + } + } + + if (permissionsRejected.size() > 0) { + + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + if (shouldShowRequestPermissionRationale(permissionsRejected.get(0))) { + showMessageOKCancel("These permissions are mandatory for the application. Please allow access.", + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + requestPermissions(permissionsRejected.toArray(new String[permissionsRejected.size()]), ALL_PERMISSIONS_RESULT); + } + } + }); + return; + } + } + + } + + break; + } + + } + + private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) { + new AlertDialog.Builder(MainActivity.this) + .setMessage(message) + .setPositiveButton("OK", okListener) + .setNegativeButton("Cancel", null) + .create() + .show(); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + locationTrack.stopListener(); + } +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..28db719 --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,19 @@ + + + +