OSDN Git Service

初版登録。
[gokigen/LegacyGpsAdapter.git] / gokigenlegacygpsadapterlib / src / main / java / net / osdn / gokigen / gps / adapter / legacy / LegacyGpsLocationPicker.java
1 package net.osdn.gokigen.gps.adapter.legacy;
2
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;
12
13 import androidx.core.content.ContextCompat;
14
15 import net.osdn.gokigen.gps.adapter.IGpsFeatureSwitch;
16 import net.osdn.gokigen.gps.adapter.IGpsLocationNotify;
17 import net.osdn.gokigen.gps.adapter.IGpsLocationPicker;
18
19 /**
20  *
21  *
22  */
23 public class LegacyGpsLocationPicker implements IGpsLocationPicker, LocationListener, GpsStatus.NmeaListener
24 {
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;
36
37     private static final long MINIMUM_INTERVAL = 3 * 1000;   // 3 * 1000 msec = 3sec
38     private static final float MINIMUM_DISTANCE = 30.0f;  // 30m
39
40     /**
41      *    コンストラクタ
42      *
43      */
44     public LegacyGpsLocationPicker(Context context, IGpsLocationNotify target)
45     {
46         Log.v(TAG, "GpsLocationPicker()");
47         this.context = context;
48         notifier = target;
49         locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
50     }
51
52     /**
53      *   GPS機能使用のための準備
54      *
55      * @return true : 使用可能 / false : 使用不可
56      */
57     @Override
58     public boolean prepare(IGpsFeatureSwitch propertyProvider)
59     {
60         try
61         {
62             if (locationManager == null)
63             {
64                 Log.v(TAG, "GPS is not Available");
65                 isAvailableGps = false;
66                 return (false);
67             }
68             this.gpsFeatureSwitch = propertyProvider;
69             isAvailableGps = true;
70             return (true);
71         }
72         catch (Exception e)
73         {
74             e.printStackTrace();
75         }
76         isAvailableGps = false;
77         return (false);
78     }
79
80     /**
81      *   GPSトラッキングの開始、終了を指示する
82      *
83      * @param isStart  : true 開始 / false 終了
84      */
85     public void controlGps(final boolean isStart)
86     {
87         if (locationManager == null)
88         {
89             Log.v(TAG, "GPS is not Available");
90             isAvailableGps = false;
91             return;
92         }
93         if ((ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED))
94         {
95             Log.v(TAG, "GPS is not Available (Permission)");
96             isAvailableGps = false;
97             return;
98         }
99
100         if (isStart)
101         {
102             // GPSトラッキングの開始 (トラッキング直後は未確定)
103             try
104             {
105                 locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MINIMUM_INTERVAL, MINIMUM_DISTANCE, this);
106                 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MINIMUM_INTERVAL, MINIMUM_DISTANCE, this);
107                 try
108                 {
109                     locationManager.addNmeaListener(this);
110                 }
111                 catch (Exception ee)
112                 {
113                     ee.printStackTrace();
114                 }
115                 currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
116                 isTrackingGps = true;
117                 isFixedGps = false;
118
119                 // 位置情報記録ON
120                 if (gpsFeatureSwitch != null)
121                 {
122                     gpsFeatureSwitch.setGpsFeature(true);
123                 }
124             }
125             catch (Exception e)
126             {
127                 e.printStackTrace();
128             }
129         }
130         else
131         {
132             // GPSトラッキングの終了
133             try
134             {
135                 locationManager.removeNmeaListener(this);
136             }
137             catch (Exception ee)
138             {
139                 ee.printStackTrace();
140             }
141             locationManager.removeUpdates(this);
142             isTrackingGps = false;
143             isFixedGps = false;
144
145             // 位置情報の記録OFF
146             if (gpsFeatureSwitch != null)
147             {
148                 gpsFeatureSwitch.setGpsFeature(false);
149             }
150             // GPS情報の非表示化
151             notifier.gpsLocationUpdate(0, null, "");
152         }
153     }
154
155     /**
156      *   GPS機能が使用可能か?
157      *
158      * @return  true : 使用可能 / false : 使用不可
159      */
160     @Override
161     public boolean hasGps()
162     {
163         return (isAvailableGps);
164     }
165
166     /**
167      *   位置をトラッキングしている状態か?
168      *
169      * @return  true : トラッキング中 / false : 停止中
170      */
171     @Override
172     public boolean isTracking()
173     {
174         return (isTrackingGps);
175     }
176
177     /**
178      *   位置情報が確定しているか?
179      *
180      * @return  true : 確定 / false : 未確定
181      */
182     @Override
183     public boolean isFixedLocation()
184     {
185         return (isFixedGps);
186     }
187
188     /**
189      *   インタフェース LocationListener の実装
190      *
191      */
192     @Override
193     public void onLocationChanged(Location location)
194     {
195         Log.v(TAG,"GpsLocationPicker::onLocationChanged() ");
196
197         // GPSの位置情報をクリアする
198         lastGPGGAstring = null;
199         lastGPRMCstring = null;
200         currentLocation = location;
201         isFixedGps = true;
202         notifier.gpsLocationFixed();
203     }
204
205     /**
206      *   インタフェース LocationListener の実装
207      *
208      */
209     @Override
210     public void onStatusChanged(String provider, int status, Bundle extras)
211     {
212         Log.v(TAG,"GpsLocationPicker::onStatusChanged() : " + provider + " (" + status + ")");
213     }
214
215     /**
216      *   インタフェース LocationListener の実装
217      *
218      */
219     @Override
220     public void onProviderEnabled(String provider)
221     {
222         Log.v(TAG, "GpsLocationPicker::onProviderEnabled() : " + provider);
223     }
224
225     /**
226      *   インタフェース LocationListener の実装
227      *
228      */
229     @Override
230     public void onProviderDisabled(String provider)
231     {
232         Log.v(TAG, "GpsLocationPicker::onProviderDisabled() : " + provider);
233     }
234
235     /**
236      *   GPS情報を受信した!
237      *
238      */
239     @Override
240     public void onNmeaReceived(long timestamp, String nmea)
241     {
242        if (!isFixedGps)
243         {
244             // まだ位置情報がFixしていない。
245             //Log.v(TAG,"GpsLocationPicker::onNmeaReceived() : not fixed location");
246             return;
247         }
248
249         // 位置情報が確定していたときは、そのデータを送る
250         //Log.v(TAG,"GpsLocationPicker::onNmeaReceived() " + nmea);
251         if (nmea.contains("$GPRMC"))
252         {
253             lastGPRMCstring = nmea;
254         }
255         if (nmea.contains("$GPGGA"))
256         {
257             lastGPGGAstring = nmea;
258         }
259         if ((lastGPRMCstring != null)&&(lastGPGGAstring != null))
260         {
261             notifier.gpsLocationUpdate(timestamp, currentLocation, (lastGPGGAstring + lastGPRMCstring));
262         }
263     }
264 }