99import androidx .appcompat .app .AlertDialog ;
1010import androidx .preference .PreferenceManager ;
1111
12+ import android .text .InputType ;
1213import android .text .TextUtils ;
1314import android .util .Log ;
1415import android .view .Menu ;
1920import android .widget .SearchView ;
2021import android .widget .SimpleAdapter ;
2122import android .widget .TextView ;
23+ import android .widget .EditText ;
24+ import android .widget .PopupMenu ;
25+ import android .view .Gravity ;
2226
2327import java .math .BigDecimal ;
2428import java .math .RoundingMode ;
2529import java .util .ArrayList ;
2630import java .util .HashMap ;
31+ import java .util .Objects ;
32+ import java .util .Locale ;
2733import java .util .List ;
2834import java .util .Map ;
2935
@@ -255,6 +261,59 @@ public boolean onQueryTextChange(String newText) {// 当搜索内容改变时触
255261 });
256262 }
257263
264+ private void showDeleteDialog (String locID ) {
265+ AlertDialog .Builder builder = new AlertDialog .Builder (this );
266+ builder .setTitle ("警告" );
267+ builder .setMessage ("确定要删除该项历史记录吗?" );
268+ builder .setPositiveButton ("确定" , (dialog , whichButton ) -> {
269+ boolean deleteRet = deleteRecord (Integer .parseInt (locID ));
270+ if (deleteRet ) {
271+ GoUtils .DisplayToast (HistoryActivity .this , getResources ().getString (R .string .history_delete_ok ));
272+ updateRecordList ();
273+ }
274+ });
275+ builder .setNegativeButton ("取消" , null );
276+
277+ builder .show ();
278+ }
279+
280+ private void showInputDialog (String locID , String name ) {
281+ final EditText input = new EditText (this );
282+ input .setInputType (InputType .TYPE_CLASS_TEXT );
283+ input .setText (name );
284+
285+ AlertDialog .Builder builder = new AlertDialog .Builder (this );
286+ builder .setTitle ("名称" );
287+ builder .setView (input );
288+ builder .setPositiveButton ("确认" , (dialog , whichButton ) -> {
289+ String userInput = input .getText ().toString ();
290+ DataBaseHistoryLocation .updateHistoryLocation (mHistoryLocationDB , locID , userInput );
291+ updateRecordList ();
292+ });
293+ builder .setNegativeButton ("取消" , null );
294+
295+ builder .show ();
296+ }
297+
298+ private String [] randomOffset (String longitude , String latitude ) {
299+ String max_offset_default = getResources ().getString (R .string .setting_random_offset_default );
300+ double lon_max_offset = Double .parseDouble (Objects .requireNonNull (sharedPreferences .getString ("setting_lon_max_offset" , max_offset_default )));
301+ double lat_max_offset = Double .parseDouble (Objects .requireNonNull (sharedPreferences .getString ("setting_lat_max_offset" , max_offset_default )));
302+ double lon = Double .parseDouble (longitude );
303+ double lat = Double .parseDouble (latitude );
304+
305+ double randomLonOffset = (Math .random () * 2 - 1 ) * lon_max_offset ; // Longitude offset (meters)
306+ double randomLatOffset = (Math .random () * 2 - 1 ) * lat_max_offset ; // Latitude offset (meters)
307+
308+ lon += randomLonOffset / 111320 ; // (meters -> longitude)
309+ lat += randomLatOffset / 110574 ; // (meters -> latitude)
310+
311+ String offsetMessage = String .format (Locale .US , "经度偏移: %.2f米\n 纬度偏移: %.2f米" , randomLonOffset , randomLatOffset );
312+ GoUtils .DisplayToast (this , offsetMessage );
313+
314+ return new String []{String .valueOf (lon ), String .valueOf (lat )};
315+ }
316+
258317 private void initRecordListView () {
259318 noRecordText = findViewById (R .id .record_no_textview );
260319 mSearchLayout = findViewById (R .id .search_linear );
@@ -270,30 +329,41 @@ private void initRecordListView() {
270329 bd09Longitude = latLngStr [0 ].substring (latLngStr [0 ].indexOf (':' ) + 1 );
271330 bd09Latitude = latLngStr [1 ].substring (latLngStr [1 ].indexOf (':' ) + 1 );
272331
332+ // Random offset
333+ if (sharedPreferences .getBoolean ("setting_random_offset" , false )) {
334+ String [] offsetResult = randomOffset (bd09Longitude , bd09Latitude );
335+ bd09Longitude = offsetResult [0 ];
336+ bd09Latitude = offsetResult [1 ];
337+ }
338+
273339 if (!MainActivity .showLocation (name , bd09Longitude , bd09Latitude )) {
274340 GoUtils .DisplayToast (this , getResources ().getString (R .string .history_error_location ));
275341 }
276342 this .finish ();
277343 });
278344
279345 mRecordListView .setOnItemLongClickListener ((parent , view , position , id ) -> {
280- new AlertDialog .Builder (HistoryActivity .this )
281- .setTitle ("警告" )//这里是表头的内容
282- .setMessage ("确定要删除该项历史记录吗?" )//这里是中间显示的具体信息
283- .setPositiveButton ("确定" ,
284- (dialog , which ) -> {
285- String locID = (String ) ((TextView ) view .findViewById (R .id .LocationID )).getText ();
286- boolean deleteRet = deleteRecord (Integer .parseInt (locID ));
346+ PopupMenu popupMenu = new PopupMenu (HistoryActivity .this , view );
347+ popupMenu .setGravity (Gravity .END | Gravity .BOTTOM );
348+ popupMenu .getMenu ().add ("编辑" );
349+ popupMenu .getMenu ().add ("删除" );
350+
351+ popupMenu .setOnMenuItemClickListener (item -> {
352+ String locID = ((TextView ) view .findViewById (R .id .LocationID )).getText ().toString ();
353+ String name = ((TextView ) view .findViewById (R .id .LocationText )).getText ().toString ();
354+ switch (item .getTitle ().toString ()) {
355+ case "编辑" :
356+ showInputDialog (locID , name );
357+ return true ;
358+ case "删除" :
359+ showDeleteDialog (locID );
360+ return true ;
361+ default :
362+ return false ;
363+ }
364+ });
287365
288- if (deleteRet ) {
289- GoUtils .DisplayToast (this , getResources ().getString (R .string .history_delete_ok ));
290- updateRecordList ();
291- }
292- })
293- .setNegativeButton ("取消" ,
294- (dialog , which ) -> {
295- })
296- .show ();
366+ popupMenu .show ();
297367 return true ;
298368 });
299369
0 commit comments