Subversion Repository Public Repository

Nextrek

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package it.fedeloper.smartcharging.Manager;

/**
 * Created by federico on 22/07/2015.
 */

import android.content.Context;
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.Handler;
import android.widget.Toast;
import it.fedeloper.smartcharging.R;

import java.io.IOException;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MyLocation {

    public static Location mylocation = null;
    static Timer timer1;
    static LocationManager locationManager;
    static LocationResult locationResult;
    static boolean gps_enabled = false;
    static boolean network_enabled = false;
    static boolean passive_enabled = false;
static int countGps =0;
    private static boolean gpsToastShownOnce = false;

    /**
     * Check if gps is enabled, show a toast when disabled
     *
     * @param context context (this in Activity / getActivity() in Fragment)
     * @return true if gps is enabled
     */
    public static boolean isGpsEnabled(final Context context,boolean showToast) {
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        Boolean isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        if (!isGpsEnabled) {
            if(!gpsToastShownOnce){
               if (showToast) Utils.toast(context, R.string.enable_gps_toast);
                gpsToastShownOnce = true;
            }
            if (countGps == 0)
                countGps++;
            else if (countGps == 1) {
                countGps++;
            }
        }
        return isGpsEnabled;

    }
    static LocationListener locationListenerPassive = new LocationListener() {
        public void onLocationChanged(Location location) {
            timer1.cancel();
            mylocation = location;
            locationResult.gotLocation(location);
            locationManager.removeUpdates(this);
            locationManager.removeUpdates(locationListenerGps);
            locationManager.removeUpdates(locationListenerNetwork);
        }
        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };

    static LocationListener locationListenerNetwork = new LocationListener() {
        public void onLocationChanged(Location location) {
            timer1.cancel();
            mylocation = location;
            locationResult.gotLocation(location);
            locationManager.removeUpdates(this);
            locationManager.removeUpdates(locationListenerGps);
            locationManager.removeUpdates(locationListenerPassive);
        }
        public void onProviderDisabled(String provider) {
        }

        public void onProviderEnabled(String provider) {
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    };

    static LocationListener locationListenerGps = new LocationListener() {
        public void onLocationChanged(Location location) {
            timer1.cancel();
            mylocation = location;
            locationResult.gotLocation(location);
            locationManager.removeUpdates(this);
            locationManager.removeUpdates(locationListenerNetwork);
            locationManager.removeUpdates(locationListenerPassive);
        }

        public void onProviderDisabled(String provider) {
        }

        public void onProviderEnabled(String provider) {
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    };

    public static boolean getLocation(Context context, LocationResult result) {

        isGpsEnabled(context,true);
        if (mylocation == null) {
            //I use LocationResult callback class to pass location value from MyLocation to user code.
            locationResult = result;
            if (locationManager == null)
                locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

            //exceptions will be thrown if provider is not permitted.
            try {
                gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            } catch (Exception ex) {
            }
            try {
                network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            } catch (Exception ex) {
            }
            try {
                network_enabled = locationManager.isProviderEnabled(LocationManager.PASSIVE_PROVIDER);
            } catch (Exception ex) {
            }

            if (passive_enabled && locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER) != null) {
                locationResult.gotLocation(locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER));
            } else {
                //don't start listeners if no provider is enabled
                if (!gps_enabled && !network_enabled)
                    return false;

                if (gps_enabled)
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
                if (network_enabled)
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
                if (passive_enabled)
                    locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 0, 0, locationListenerPassive);
                timer1 = new Timer();
                timer1.schedule(new GetLastLocation(), 20000);
            }
        } else {
            locationResult = result;
            locationResult.gotLocation(mylocation);
        }


        return true;
    }

    public static Address getAddress(final Context context, final Location location) {
        if (location == null)
            return null;

        final Geocoder geocoder = new Geocoder(context);
        final List<Address> addresses;
        try {
            addresses = geocoder.getFromLocation(location.getLatitude(),
                    location.getLongitude(), 1);
        } catch (IOException e) {
            return null;
        }
        if (addresses != null && !addresses.isEmpty())
            return addresses.get(0);
        else
            return null;
    }

    static class GetLastLocation extends TimerTask {
        @Override
        public void run() {
            locationManager.removeUpdates(locationListenerGps);
            locationManager.removeUpdates(locationListenerNetwork);
            locationManager.removeUpdates(locationListenerPassive);
            Location net_loc = null, gps_loc = null;
            if (gps_enabled)
                gps_loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if (network_enabled)
                net_loc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

            //if there are both values use the latest one
            if (gps_loc != null && net_loc != null) {
                if (gps_loc.getTime() > net_loc.getTime()) {
                    mylocation = gps_loc;
                    locationResult.gotLocation(gps_loc);
                } else {
                    mylocation = net_loc;
                    locationResult.gotLocation(net_loc);
                }

                return;
            }

            if (gps_loc != null) {
                mylocation = gps_loc;
                locationResult.gotLocation(gps_loc);
                return;
            }
            if (net_loc != null) {
                mylocation = net_loc;
                locationResult.gotLocation(net_loc);
                return;
            }
            locationResult.gotLocation(null);
        }
    }

    public static abstract class LocationResult {
        public abstract void gotLocation(Location location);
    }


}

Commits for Nextrek/Android/SmartCharging/SmartCharging/app/src/main/java/it/fedeloper/smartcharging/Manager/MyLocation.java

Diff revisions: vs.
Revision Author Commited Message
762 Diff Diff FFontana picture FFontana Fri 18 Sep, 2015 16:18:57 +0000
633 Diff Diff MStefanelli picture MStefanelli Tue 01 Sep, 2015 10:35:01 +0000

Strizz

599 Diff Diff MStefanelli picture MStefanelli Fri 28 Aug, 2015 07:16:44 +0000
548 Diff Diff FFontana picture FFontana Tue 25 Aug, 2015 10:17:34 +0000
546 Diff Diff FFontana picture FFontana Tue 25 Aug, 2015 09:40:38 +0000
542 Diff Diff FFontana picture FFontana Mon 24 Aug, 2015 21:40:56 +0000
522 Diff Diff MStefanelli picture MStefanelli Fri 21 Aug, 2015 16:02:26 +0000
521 Diff Diff MStefanelli picture MStefanelli Fri 21 Aug, 2015 12:38:59 +0000
518 Diff Diff FFontana picture FFontana Thu 20 Aug, 2015 14:36:46 +0000
505 FFontana picture FFontana Wed 19 Aug, 2015 08:54:46 +0000