1 package net.osdn.gokigen.gps.adapter.legacy;
3 import android.Manifest;
4 import android.content.Context;
5 import android.content.pm.PackageManager;
6 import android.location.GpsStatus;
7 import android.location.Location;
8 import android.location.LocationListener;
9 import android.location.LocationManager;
10 import android.os.Bundle;
11 import android.util.Log;
13 import androidx.core.content.ContextCompat;
15 import net.osdn.gokigen.gps.adapter.IGpsFeatureSwitch;
16 import net.osdn.gokigen.gps.adapter.IGpsLocationNotify;
17 import net.osdn.gokigen.gps.adapter.IGpsLocationPicker;
23 public class LegacyGpsLocationPicker implements IGpsLocationPicker, LocationListener, GpsStatus.NmeaListener
25 private final String TAG = toString();
26 private final Context context;
27 private final IGpsLocationNotify notifier;
28 private boolean isAvailableGps = false;
29 private boolean isTrackingGps = false;
30 private boolean isFixedGps = false;
31 private final LocationManager locationManager;
32 private IGpsFeatureSwitch gpsFeatureSwitch = null;
33 private Location currentLocation = null;
34 private String lastGPGGAstring = null;
35 private String lastGPRMCstring = null;
37 private static final long MINIMUM_INTERVAL = 3 * 1000; // 3 * 1000 msec = 3sec
38 private static final float MINIMUM_DISTANCE = 30.0f; // 30m
44 public LegacyGpsLocationPicker(Context context, IGpsLocationNotify target)
46 Log.v(TAG, "GpsLocationPicker()");
47 this.context = context;
49 locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
55 * @return true : 使用可能 / false : 使用不可
58 public boolean prepare(IGpsFeatureSwitch propertyProvider)
62 if (locationManager == null)
64 Log.v(TAG, "GPS is not Available");
65 isAvailableGps = false;
68 this.gpsFeatureSwitch = propertyProvider;
69 isAvailableGps = true;
76 isAvailableGps = false;
81 * GPSトラッキングの開始、終了を指示する
83 * @param isStart : true 開始 / false 終了
85 public void controlGps(final boolean isStart)
87 if (locationManager == null)
89 Log.v(TAG, "GPS is not Available");
90 isAvailableGps = false;
93 if ((ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED))
95 Log.v(TAG, "GPS is not Available (Permission)");
96 isAvailableGps = false;
102 // GPSトラッキングの開始 (トラッキング直後は未確定)
105 locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MINIMUM_INTERVAL, MINIMUM_DISTANCE, this);
106 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MINIMUM_INTERVAL, MINIMUM_DISTANCE, this);
109 locationManager.addNmeaListener(this);
113 ee.printStackTrace();
115 currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
116 isTrackingGps = true;
120 if (gpsFeatureSwitch != null)
122 gpsFeatureSwitch.setGpsFeature(true);
135 locationManager.removeNmeaListener(this);
139 ee.printStackTrace();
141 locationManager.removeUpdates(this);
142 isTrackingGps = false;
146 if (gpsFeatureSwitch != null)
148 gpsFeatureSwitch.setGpsFeature(false);
151 notifier.gpsLocationUpdate(0, null, "");
158 * @return true : 使用可能 / false : 使用不可
161 public boolean hasGps()
163 return (isAvailableGps);
169 * @return true : トラッキング中 / false : 停止中
172 public boolean isTracking()
174 return (isTrackingGps);
180 * @return true : 確定 / false : 未確定
183 public boolean isFixedLocation()
189 * インタフェース LocationListener の実装
193 public void onLocationChanged(Location location)
195 Log.v(TAG,"GpsLocationPicker::onLocationChanged() ");
198 lastGPGGAstring = null;
199 lastGPRMCstring = null;
200 currentLocation = location;
202 notifier.gpsLocationFixed();
206 * インタフェース LocationListener の実装
210 public void onStatusChanged(String provider, int status, Bundle extras)
212 Log.v(TAG,"GpsLocationPicker::onStatusChanged() : " + provider + " (" + status + ")");
216 * インタフェース LocationListener の実装
220 public void onProviderEnabled(String provider)
222 Log.v(TAG, "GpsLocationPicker::onProviderEnabled() : " + provider);
226 * インタフェース LocationListener の実装
230 public void onProviderDisabled(String provider)
232 Log.v(TAG, "GpsLocationPicker::onProviderDisabled() : " + provider);
240 public void onNmeaReceived(long timestamp, String nmea)
245 //Log.v(TAG,"GpsLocationPicker::onNmeaReceived() : not fixed location");
249 // 位置情報が確定していたときは、そのデータを送る
250 //Log.v(TAG,"GpsLocationPicker::onNmeaReceived() " + nmea);
251 if (nmea.contains("$GPRMC"))
253 lastGPRMCstring = nmea;
255 if (nmea.contains("$GPGGA"))
257 lastGPGGAstring = nmea;
259 if ((lastGPRMCstring != null)&&(lastGPGGAstring != null))
261 notifier.gpsLocationUpdate(timestamp, currentLocation, (lastGPGGAstring + lastGPRMCstring));