diff --git a/app/src/main/java/com/zcshou/gogogo/HistoryActivity.java b/app/src/main/java/com/zcshou/gogogo/HistoryActivity.java index f8e2006..e934a95 100644 --- a/app/src/main/java/com/zcshou/gogogo/HistoryActivity.java +++ b/app/src/main/java/com/zcshou/gogogo/HistoryActivity.java @@ -1,5 +1,7 @@ package com.zcshou.gogogo; +import android.app.Activity; +import android.content.Intent; import android.content.SharedPreferences; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; @@ -28,8 +30,6 @@ import java.math.RoundingMode; import java.util.ArrayList; import java.util.HashMap; -import java.util.Objects; -import java.util.Locale; import java.util.List; import java.util.Map; @@ -48,7 +48,6 @@ public class HistoryActivity extends BaseActivity { private LinearLayout mSearchLayout; private SQLiteDatabase mHistoryLocationDB; private List> mAllRecord; - private SharedPreferences sharedPreferences; @Override protected void onCreate(Bundle savedInstanceState) { @@ -68,8 +67,6 @@ protected void onCreate(Bundle savedInstanceState) { actionBar.setDisplayHomeAsUpEnabled(true); } - sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); - initLocationDataBase(); initSearchView(); @@ -175,6 +172,7 @@ private List> fetchAllRecord() { private void recordArchive() { double limits; try { + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); limits = Double.parseDouble(sharedPreferences.getString("setting_pos_history", getResources().getString(R.string.history_expiration))); } catch (NumberFormatException e) { // GOOD: The exception is caught. limits = 7; @@ -295,50 +293,20 @@ private void showInputDialog(String locID, String name) { builder.show(); } - private String[] randomOffset(String longitude, String latitude) { - String max_offset_default = getResources().getString(R.string.setting_random_offset_default); - double lon_max_offset = Double.parseDouble(Objects.requireNonNull(sharedPreferences.getString("setting_lon_max_offset", max_offset_default))); - double lat_max_offset = Double.parseDouble(Objects.requireNonNull(sharedPreferences.getString("setting_lat_max_offset", max_offset_default))); - double lon = Double.parseDouble(longitude); - double lat = Double.parseDouble(latitude); - - double randomLonOffset = (Math.random() * 2 - 1) * lon_max_offset; // Longitude offset (meters) - double randomLatOffset = (Math.random() * 2 - 1) * lat_max_offset; // Latitude offset (meters) - - lon += randomLonOffset / 111320; // (meters -> longitude) - lat += randomLatOffset / 110574; // (meters -> latitude) - - String offsetMessage = String.format(Locale.US, "经度偏移: %.2f米\n纬度偏移: %.2f米", randomLonOffset, randomLatOffset); - GoUtils.DisplayToast(this, offsetMessage); - - return new String[]{String.valueOf(lon), String.valueOf(lat)}; - } - private void initRecordListView() { noRecordText = findViewById(R.id.record_no_textview); mSearchLayout = findViewById(R.id.search_linear); mRecordListView = findViewById(R.id.record_list_view); mRecordListView.setOnItemClickListener((adapterView, view, i, l) -> { - String bd09Longitude; - String bd09Latitude; - String name; - name = (String) ((TextView) view.findViewById(R.id.LocationText)).getText(); String bd09LatLng = (String) ((TextView) view.findViewById(R.id.BDLatLngText)).getText(); bd09LatLng = bd09LatLng.substring(bd09LatLng.indexOf('[') + 1, bd09LatLng.indexOf(']')); String[] latLngStr = bd09LatLng.split(" "); - bd09Longitude = latLngStr[0].substring(latLngStr[0].indexOf(':') + 1); - bd09Latitude = latLngStr[1].substring(latLngStr[1].indexOf(':') + 1); - - // Random offset - if(sharedPreferences.getBoolean("setting_random_offset", false)) { - String[] offsetResult = randomOffset(bd09Longitude, bd09Latitude); - bd09Longitude = offsetResult[0]; - bd09Latitude = offsetResult[1]; - } - - if (!MainActivity.showLocation(name, bd09Longitude, bd09Latitude)) { - GoUtils.DisplayToast(this, getResources().getString(R.string.history_error_location)); - } + String bd09Longitude = latLngStr[0].substring(latLngStr[0].indexOf(':') + 1); + String bd09Latitude = latLngStr[1].substring(latLngStr[1].indexOf(':') + 1); + Intent resultIntent = new Intent(); + resultIntent.putExtra("bd09_lon", bd09Longitude); + resultIntent.putExtra("bd09_lat", bd09Latitude); + setResult(Activity.RESULT_OK, resultIntent); this.finish(); }); diff --git a/app/src/main/java/com/zcshou/gogogo/MainActivity.java b/app/src/main/java/com/zcshou/gogogo/MainActivity.java index cc73a04..cf2da95 100644 --- a/app/src/main/java/com/zcshou/gogogo/MainActivity.java +++ b/app/src/main/java/com/zcshou/gogogo/MainActivity.java @@ -1,5 +1,6 @@ package com.zcshou.gogogo; +import android.app.Activity; import android.app.DownloadManager; import android.content.BroadcastReceiver; import android.content.ClipData; @@ -46,6 +47,8 @@ import android.widget.SimpleAdapter; import android.widget.TextView; +import androidx.activity.result.ActivityResultLauncher; +import androidx.activity.result.contract.ActivityResultContracts; import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcompat.app.AlertDialog; @@ -94,7 +97,9 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map; +import java.util.Objects; import com.zcshou.service.ServiceGo; import com.zcshou.database.DataBaseHistoryLocation; @@ -225,10 +230,9 @@ public void onServiceDisconnected(ComponentName name) { DataBaseHistoryLocation.DB_COLUMN_ID + " > ?", new String[]{"0"}, null, null, DataBaseHistoryLocation.DB_COLUMN_TIMESTAMP + " DESC", null); if (cursor.moveToFirst()) { - String name = cursor.getString(1); String bd09Longitude = cursor.getString(5); String bd09Latitude = cursor.getString(6); - MainActivity.showLocation(name, bd09Longitude, bd09Latitude); + showLocation(bd09Longitude, bd09Latitude); isFirstLoc = false; } cursor.close(); @@ -431,14 +435,22 @@ public void onAccuracyChanged(Sensor sensor, int i) { /*============================== NavigationView 相关 ==============================*/ private void initNavigationView() { + ActivityResultLauncher historyLauncher = registerForActivityResult( + new ActivityResultContracts.StartActivityForResult(), + result -> { + if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) { + String lon = result.getData().getStringExtra("bd09_lon"); + String lat = result.getData().getStringExtra("bd09_lat"); + showLocation(lon, lat); + } + } + ); mNavigationView = findViewById(R.id.nav_view); mNavigationView.setNavigationItemSelectedListener(item -> { int id = item.getItemId(); - if (id == R.id.nav_history) { Intent intent = new Intent(MainActivity.this, HistoryActivity.class); - - startActivity(intent); + historyLauncher.launch(intent); } else if (id == R.id.nav_settings) { Intent intent = new Intent(MainActivity.this, SettingsActivity.class); startActivity(intent); @@ -1005,27 +1017,35 @@ private void resetMap() { // } // 在地图上显示位置 - public static boolean showLocation(String name, String bd09Longitude, String bd09Latitude) { - boolean ret = true; - + void showLocation(String bd09Longitude, String bd09Latitude) { try { - if (!bd09Longitude.isEmpty() && !bd09Latitude.isEmpty()) { - mMarkLatLngMap = new LatLng(Double.parseDouble(bd09Latitude), Double.parseDouble(bd09Longitude)); - MarkerOptions ooA = new MarkerOptions().position(mMarkLatLngMap).icon(mMapIndicator); - mBaiduMap.clear(); - mBaiduMap.addOverlay(ooA); - MapStatusUpdate mapstatusupdate = MapStatusUpdateFactory.newLatLng(mMarkLatLngMap); - mBaiduMap.setMapStatus(mapstatusupdate); - MapStatus.Builder builder = new MapStatus.Builder(); - builder.target(mMarkLatLngMap).zoom(18).rotate(0); - mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build())); + double lon = Double.parseDouble(bd09Longitude); + double lat = Double.parseDouble(bd09Latitude); + // Random offset + if (sharedPreferences.getBoolean("setting_random_offset", false)) { + String max_offset_default = getResources().getString(R.string.setting_random_offset_default); + double lon_max_offset = Double.parseDouble(Objects.requireNonNull(sharedPreferences.getString("setting_lon_max_offset", max_offset_default))); + double lat_max_offset = Double.parseDouble(Objects.requireNonNull(sharedPreferences.getString("setting_lat_max_offset", max_offset_default))); + double randomLonOffset = (Math.random() * 2 - 1) * lon_max_offset; // Longitude offset (meters) + double randomLatOffset = (Math.random() * 2 - 1) * lat_max_offset; // Latitude offset (meters) + lon += randomLonOffset / 111320; // (meters -> longitude) + lat += randomLatOffset / 110574; // (meters -> latitude) + String msg = String.format(Locale.US, "经度偏移: %.2f米\n纬度偏移: %.2f米", randomLonOffset, randomLatOffset); + GoUtils.DisplayToast(this, msg); } + mMarkLatLngMap = new LatLng(lat, lon); + MarkerOptions ooA = new MarkerOptions().position(mMarkLatLngMap).icon(mMapIndicator); + mBaiduMap.clear(); + mBaiduMap.addOverlay(ooA); + MapStatusUpdate mapstatusupdate = MapStatusUpdateFactory.newLatLng(mMarkLatLngMap); + mBaiduMap.setMapStatus(mapstatusupdate); + MapStatus.Builder builder = new MapStatus.Builder(); + builder.target(mMarkLatLngMap).zoom(18).rotate(0); + mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build())); } catch (Exception e) { - ret = false; XLog.e("ERROR: showHistoryLocation"); + GoUtils.DisplayToast(this, getResources().getString(R.string.history_error_location)); } - - return ret; } private void initGoBtn() {