From 3c515b714556915375b70115c7ac18cceb2eea5f Mon Sep 17 00:00:00 2001
From: kishoreraju2 <kishoreraju.1998@gmail.com>
Date: Wed, 19 Apr 2017 23:09:56 +0530
Subject: [PATCH] Location Updated

---
 app/src/main/AndroidManifest.xml              |   2 +
 .../com/mapps/seproject/ComposeFragment.java  |  30 ++-
 .../java/com/mapps/seproject/TrackGPS.java    | 246 ++++++++++++++++++
 app/src/main/res/layout/fragment_compose.xml  |   8 +
 4 files changed, 285 insertions(+), 1 deletion(-)
 create mode 100644 app/src/main/java/com/mapps/seproject/TrackGPS.java

diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index cf70c46..0d263e4 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -5,6 +5,8 @@
     <uses-permission android:name="android.permission.INTERNET" />
     <uses-feature android:name="android.hardware.camera2"/>
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
+    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
+    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
 
     <application
         android:allowBackup="true"
diff --git a/app/src/main/java/com/mapps/seproject/ComposeFragment.java b/app/src/main/java/com/mapps/seproject/ComposeFragment.java
index 17f0f50..0f8be58 100644
--- a/app/src/main/java/com/mapps/seproject/ComposeFragment.java
+++ b/app/src/main/java/com/mapps/seproject/ComposeFragment.java
@@ -37,6 +37,12 @@ public class ComposeFragment extends Fragment implements View.OnClickListener{
     private static int RESULT_LOAD_IMAGE = 1;
     private static int RESULT_MAIL = 2;
 
+    private Button b_get;
+    private com.mapps.seproject.TrackGPS gps;
+    double longitude;
+    double latitude;
+    String city;
+    String postalCode;
 
 
 
@@ -52,10 +58,11 @@ public class ComposeFragment extends Fragment implements View.OnClickListener{
         bComposeMail = (Button) view.findViewById(R.id.bComposeMail);
         emailText = (TextView) view.findViewById(R.id.tvEmailMessage);
         bAddImage = (Button) view.findViewById(R.id.bAddImage);
-
+        b_get = (Button) view.findViewById(R.id.button2);
 
         bAddImage.setOnClickListener(this);
         bComposeMail.setOnClickListener(this);
+        b_get.setOnClickListener(this);
         return view;
     }
 
@@ -141,6 +148,27 @@ public class ComposeFragment extends Fragment implements View.OnClickListener{
 
         }
 
+        //Location
+	    if(v == b_get){
+            gps = new TrackGPS(getActivity());
+
+
+            if(gps.canGetLocation()){
+
+
+                longitude = gps.getLongitude();
+                latitude = gps .getLatitude();
+                city = gps.getCity();
+                postalCode = gps.getPostalCode();
+                Toast.makeText(getActivity(),"Longitude:"+Double.toString(longitude)+"\nLatitude:"+Double.toString(latitude)+"\nCity:"+city+"\nPostal:"+postalCode,Toast.LENGTH_SHORT).show();
+            }
+            else
+            {
+
+                gps.showSettingsAlert();
+            }
+        }
+
 
 
 
diff --git a/app/src/main/java/com/mapps/seproject/TrackGPS.java b/app/src/main/java/com/mapps/seproject/TrackGPS.java
new file mode 100644
index 0000000..2ee027b
--- /dev/null
+++ b/app/src/main/java/com/mapps/seproject/TrackGPS.java
@@ -0,0 +1,246 @@
+package com.mapps.seproject;
+
+import android.app.AlertDialog;
+import android.app.Service;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.content.Intent;
+
+import android.location.Address;
+import android.location.Geocoder;
+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.util.Log;
+import android.widget.Toast;
+
+import java.util.List;
+import java.util.Locale;
+
+/**
+ * Created by Kishore on 8/8/2016.
+ */
+
+public class TrackGPS extends Service implements LocationListener {
+
+    private final Context mContext;
+
+
+    boolean checkGPS = false;
+
+
+    boolean checkNetwork = false;
+
+    boolean canGetLocation = false;
+
+    Location loc;
+    double latitude;
+    double longitude;
+
+    String city;
+    String postalCode;
+
+
+
+
+
+
+
+    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 TrackGPS(Context mContext) {
+        this.mContext = mContext;
+        getLocation();
+    }
+
+    private Location getLocation() {
+
+        try {
+            locationManager = (LocationManager) mContext
+                    .getSystemService(LOCATION_SERVICE);
+
+            // getting GPS status
+            checkGPS = locationManager
+                    .isProviderEnabled(LocationManager.GPS_PROVIDER);
+
+
+
+            // getting network status
+            checkNetwork = locationManager
+                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
+
+            if (!checkGPS && !checkNetwork) {
+                Toast.makeText(mContext, "No Service Provider Available", Toast.LENGTH_SHORT).show();
+            } else {
+                this.canGetLocation = true;
+                // First get location from Network Provider
+                if (checkNetwork) {
+                    //Toast.makeText(mContext, "Network", Toast.LENGTH_SHORT).show();
+
+                    try {
+                        locationManager.requestLocationUpdates(
+                                LocationManager.NETWORK_PROVIDER,1000,0, this);
+                        Log.d("Network", "Network");
+                        if (locationManager != null) {
+                            loc = locationManager
+                                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
+
+                        }
+
+                        if (loc != null) {
+                            Toast.makeText(mContext,"Gps enabled",Toast.LENGTH_SHORT).show();
+
+                            latitude = loc.getLatitude();
+                            longitude = loc.getLongitude();
+                            Geocoder geocoder= new Geocoder(mContext,Locale.getDefault());
+                            List<Address> addresses;
+
+                            addresses = geocoder.getFromLocation(latitude, longitude, 1);
+                          //  String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
+                            city = addresses.get(0).getLocality();
+                            //String state = addresses.get(0).getAdminArea();
+                            //String country = addresses.get(0).getCountryName();
+                            postalCode = addresses.get(0).getPostalCode();
+                            //String knownName = addresses.get(0).getFeatureName();
+                        }
+                    }
+                    catch(SecurityException e){
+
+                    }
+                }
+            }
+            // if GPS Enabled get lat/long using GPS Services
+            if (checkGPS) {
+                if (loc == null) {
+                    try {
+                        locationManager.requestLocationUpdates(
+                                LocationManager.GPS_PROVIDER,1000,0, this);
+                        Log.d("GPS Enabled", "GPS Enabled");
+                        if (locationManager != null) {
+                            loc = locationManager
+                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
+                            if (loc != null) {
+                                latitude = loc.getLatitude();
+                                longitude = loc.getLongitude();
+                                Geocoder geocoder= new Geocoder(mContext,Locale.getDefault());
+                                List<Address> addresses;
+
+                                addresses = geocoder.getFromLocation(latitude, longitude, 1);
+                                //  String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
+                                city = addresses.get(0).getLocality();
+                                //String state = addresses.get(0).getAdminArea();
+                                //String country = addresses.get(0).getCountryName();
+                                postalCode = addresses.get(0).getPostalCode();
+                                //String knownName = addresses.get(0).getFeatureName();
+                            }
+                        }
+
+                    } catch (SecurityException e) {
+
+                    }
+                }
+            }
+
+
+        } 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 String getCity(){
+        return this.city;
+    }
+
+    public String getPostalCode(){
+        return this.postalCode;
+    }
+
+    public boolean canGetLocation() {
+        return this.canGetLocation;
+    }
+
+    public void showSettingsAlert() {
+        AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
+
+
+        alertDialog.setTitle("GPS Not Enabled");
+
+        alertDialog.setMessage("Do you wants 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 stopUsingGPS() {
+        if (locationManager != null) {
+
+            locationManager.removeUpdates(TrackGPS.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) {
+
+    }
+}
\ No newline at end of file
diff --git a/app/src/main/res/layout/fragment_compose.xml b/app/src/main/res/layout/fragment_compose.xml
index a80ca10..021a446 100644
--- a/app/src/main/res/layout/fragment_compose.xml
+++ b/app/src/main/res/layout/fragment_compose.xml
@@ -53,6 +53,14 @@
         android:layout_marginBottom="19dp"
         android:hint="Enter your complaint here" />
 
+    <Button
+        android:id="@+id/button2"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_alignTop="@+id/tvEmailMessage"
+        android:layout_centerHorizontal="true"
+        android:text="Get Location" />
+
 
 </RelativeLayout>
     </FrameLayout>
-- 
GitLab