diff options
Diffstat (limited to '')
5 files changed, 341 insertions, 301 deletions
diff --git a/src/main/java/de/pixart/messenger/Config.java b/src/main/java/de/pixart/messenger/Config.java index 0604b0dda..9d4296ed3 100644 --- a/src/main/java/de/pixart/messenger/Config.java +++ b/src/main/java/de/pixart/messenger/Config.java @@ -78,6 +78,9 @@ public final class Config { public static final int IMAGE_QUALITY = 75; public static final int DEFAULT_ZOOM = 15; //for locations + public final static long LOCATION_FIX_TIME_DELTA = 1000 * 10; // ms + public final static float LOCATION_FIX_SPACE_DELTA = 10; // m + public final static int LOCATION_FIX_SIGNIFICANT_TIME_DELTA = 1000 * 60 * 2; // ms public static final int MESSAGE_MERGE_WINDOW = 20; diff --git a/src/main/java/de/pixart/messenger/ui/LocationActivity.java b/src/main/java/de/pixart/messenger/ui/LocationActivity.java new file mode 100644 index 000000000..74e2b45f9 --- /dev/null +++ b/src/main/java/de/pixart/messenger/ui/LocationActivity.java @@ -0,0 +1,60 @@ +package de.pixart.messenger.ui; + +import android.app.Activity; +import android.content.Context; +import android.location.Location; +import android.location.LocationListener; +import android.location.LocationManager; +import android.os.Bundle; + +import de.pixart.messenger.Config; + +public abstract class LocationActivity extends Activity implements LocationListener { + private LocationManager locationManager; + + @Override + protected void onCreate(final Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + this.locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); + } + + protected abstract void gotoLoc() throws UnsupportedOperationException; + + protected abstract void setmLastLocation(final Location location); + + protected void requestLocationUpdates() { + final Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); + if (lastKnownLocation != null) { + setmLastLocation(lastKnownLocation); + try { + gotoLoc(); + } catch (final UnsupportedOperationException ignored) { + } + } + + locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, Config.LOCATION_FIX_TIME_DELTA, Config.LOCATION_FIX_SPACE_DELTA, this); + locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, Config.LOCATION_FIX_TIME_DELTA, Config.LOCATION_FIX_SPACE_DELTA, this); + + // If something else is also querying for location more frequently than we are, the battery is already being + // drained. Go ahead and use the existing locations as often as we can get them. + locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 0, 0, this); + } + + protected void pauseLocationUpdates() { + locationManager.removeUpdates(this); + } + + @Override + protected void onPause() { + super.onPause(); + pauseLocationUpdates(); + } + + @Override + protected void onResume() { + super.onResume(); + this.setmLastLocation(null); + + requestLocationUpdates(); + } +}
\ No newline at end of file diff --git a/src/main/java/de/pixart/messenger/ui/ShareLocationActivity.java b/src/main/java/de/pixart/messenger/ui/ShareLocationActivity.java index a9bfabff9..29142f26f 100644 --- a/src/main/java/de/pixart/messenger/ui/ShareLocationActivity.java +++ b/src/main/java/de/pixart/messenger/ui/ShareLocationActivity.java @@ -1,263 +1,235 @@ package de.pixart.messenger.ui; -import android.Manifest; import android.annotation.TargetApi; -import android.app.Activity; import android.content.Context; import android.content.Intent; -import android.content.pm.PackageManager; import android.location.Address; import android.location.Geocoder; import android.location.Location; +import android.location.LocationListener; +import android.location.LocationManager; import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.provider.Settings; import android.text.TextUtils; -import android.util.Log; import android.view.View; +import android.webkit.WebView; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.TextView; -import com.google.android.gms.common.ConnectionResult; -import com.google.android.gms.common.api.GoogleApiClient; -import com.google.android.gms.location.LocationListener; -import com.google.android.gms.location.LocationRequest; -import com.google.android.gms.location.LocationServices; -import com.google.android.gms.maps.CameraUpdateFactory; -import com.google.android.gms.maps.GoogleMap; -import com.google.android.gms.maps.MapFragment; -import com.google.android.gms.maps.OnMapReadyCallback; -import com.google.android.gms.maps.model.LatLng; - -import java.io.IOException; +import org.jetbrains.annotations.Nullable; + +import java.lang.ref.WeakReference; import java.util.List; import java.util.Locale; -import de.pixart.messenger.Config; import de.pixart.messenger.R; +import de.pixart.messenger.utils.LocationHelper; -public class ShareLocationActivity extends Activity implements OnMapReadyCallback, - GoogleApiClient.ConnectionCallbacks, - GoogleApiClient.OnConnectionFailedListener, - LocationListener { +public class ShareLocationActivity extends LocationActivity implements LocationListener { - private GoogleMap mGoogleMap; - private GoogleApiClient mGoogleApiClient; - private LocationRequest mLocationRequest; + LocationManager locationManager; private Location mLastLocation; private Button mCancelButton; private Button mShareButton; + private String mLocationName; private RelativeLayout mSnackbar; - private RelativeLayout mLocationInfo; - private TextView mSnackbarLocation; - private TextView mSnackbarAction; @Override - public void onCreate(Bundle savedInstanceState) { + protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); + setContentView(R.layout.activity_share_locaction); if (getActionBar() != null) { getActionBar().setHomeButtonEnabled(true); getActionBar().setDisplayHomeAsUpEnabled(true); } - setContentView(R.layout.activity_share_locaction); - MapFragment fragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map_fragment); - fragment.getMapAsync(this); - mGoogleApiClient = new GoogleApiClient.Builder(this) - .addApi(LocationServices.API) - .addConnectionCallbacks(this) - .addOnConnectionFailedListener(this) - .build(); + locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); + + mLocationName = getString(R.string.me); + mCancelButton = findViewById(R.id.cancel_button); - mCancelButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - setResult(RESULT_CANCELED); - finish(); - } + mCancelButton.setOnClickListener(view -> { + setResult(RESULT_CANCELED); + finish(); }); + mShareButton = findViewById(R.id.share_button); - mShareButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - if (mLastLocation != null) { - Intent result = new Intent(); - result.putExtra("latitude", mLastLocation.getLatitude()); - result.putExtra("longitude", mLastLocation.getLongitude()); - result.putExtra("altitude", mLastLocation.getAltitude()); - result.putExtra("accuracy", (int) mLastLocation.getAccuracy()); - setResult(RESULT_OK, result); - finish(); - } + mShareButton.setOnClickListener(view -> { + if (mLastLocation != null) { + Intent result = new Intent(); + result.putExtra("latitude", mLastLocation.getLatitude()); + result.putExtra("longitude", mLastLocation.getLongitude()); + result.putExtra("altitude", mLastLocation.getAltitude()); + result.putExtra("accuracy", (int) mLastLocation.getAccuracy()); + setResult(RESULT_OK, result); + finish(); } }); + mSnackbar = findViewById(R.id.snackbar); - mLocationInfo = findViewById(R.id.snackbar_location); - mSnackbarLocation = findViewById(R.id.snackbar_location_message); - mSnackbarAction = findViewById(R.id.snackbar_action); - mSnackbarAction.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); - } - }); + TextView snackbarAction = findViewById(R.id.snackbar_action); + snackbarAction.setOnClickListener(view -> startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS))); + + requestLocationUpdates(); } @Override - protected void onResume() { - super.onResume(); - this.mLastLocation = null; - if (isLocationEnabled()) { - this.mSnackbar.setVisibility(View.GONE); - } else { - this.mSnackbar.setVisibility(View.VISIBLE); - } - mShareButton.setEnabled(false); - mShareButton.setTextColor(0x8a000000); - mShareButton.setText(R.string.locating); - mGoogleApiClient.connect(); + protected void gotoLoc() throws UnsupportedOperationException { + new getAddressAsync(this).execute(); + } + + @Override + protected void setmLastLocation(Location location) { + this.mLastLocation = location; } @Override protected void onPause() { - mGoogleApiClient.disconnect(); super.onPause(); } @Override - public void onMapReady(GoogleMap googleMap) { - this.mGoogleMap = googleMap; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED - || checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { - this.mGoogleMap.setBuildingsEnabled(true); - this.mGoogleMap.setMyLocationEnabled(true); - } + protected void onResume() { + super.onResume(); + if (isLocationEnabled()) { + this.mSnackbar.setVisibility(View.GONE); } else { - this.mGoogleMap.setBuildingsEnabled(true); - this.mGoogleMap.setMyLocationEnabled(true); + this.mSnackbar.setVisibility(View.VISIBLE); } - } - - private void centerOnLocation(LatLng location) { - this.mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(location, Config.DEFAULT_ZOOM)); + setShareButtonEnabled(false); } @Override - public void onConnected(Bundle bundle) { - mLocationRequest = LocationRequest.create(); - mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); - mLocationRequest.setInterval(1000); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED - || checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { - LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); - } - } else { - LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); + public void onLocationChanged(final Location location) { + if (LocationHelper.isBetterLocation(location, this.mLastLocation)) { + setShareButtonEnabled(true); + this.mLastLocation = location; + gotoLoc(); } - } @Override - public void onConnectionSuspended(int i) { + public void onStatusChanged(final String provider, final int status, final Bundle extras) { } @Override - public void onConnectionFailed(ConnectionResult connectionResult) { + public void onProviderEnabled(final String provider) { } @Override - public void onLocationChanged(Location location) { - double longitude = location.getLongitude(); - double latitude = location.getLatitude(); + public void onProviderDisabled(final String provider) { - if (this.mLastLocation == null) { - centerOnLocation(new LatLng(location.getLatitude(), location.getLongitude())); - this.mShareButton.setEnabled(true); - this.mShareButton.setTextColor(0xde000000); - this.mShareButton.setText(R.string.share); - } - this.mLastLocation = location; - if (latitude != 0 && longitude != 0) { - Double[] lat_long = new Double[]{latitude, longitude}; - new ReverseGeocodingTask(getBaseContext()).execute(lat_long); - } } @TargetApi(Build.VERSION_CODES.KITKAT) private boolean isLocationEnabledKitkat() { try { - int locationMode = Settings.Secure.getInt(getContentResolver(), Settings.Secure.LOCATION_MODE); + final int locationMode = Settings.Secure.getInt(getContentResolver(), Settings.Secure.LOCATION_MODE); return locationMode != Settings.Secure.LOCATION_MODE_OFF; - } catch (Settings.SettingNotFoundException e) { + } catch (final Settings.SettingNotFoundException e) { return false; } } @SuppressWarnings("deprecation") private boolean isLocationEnabledLegacy() { - String locationProviders = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); + final String locationProviders = Settings.Secure.getString(getContentResolver(), + Settings.Secure.LOCATION_PROVIDERS_ALLOWED); return !TextUtils.isEmpty(locationProviders); } private boolean isLocationEnabled() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){ return isLocationEnabledKitkat(); } else { return isLocationEnabledLegacy(); } } - private class ReverseGeocodingTask extends AsyncTask<Double, Void, String> { - Context mContext; - - public ReverseGeocodingTask(Context context) { - super(); - mContext = context; + private void setShareButtonEnabled(final boolean enabled) { + if (enabled) { + this.mShareButton.setEnabled(true); + this.mShareButton.setTextColor(0xff2e4272); + this.mShareButton.setText(R.string.share); + } else { + this.mShareButton.setEnabled(false); + this.mShareButton.setTextColor(0x8a000000); + this.mShareButton.setText(R.string.locating); + showLocation(null, null); } + } - @Override - protected String doInBackground(Double... params) { - Geocoder geocoder = new Geocoder(mContext, Locale.getDefault()); - - double latitude = params[0]; - double longitude = params[1]; - - List<Address> addresses = null; - String address = ""; - + private static String getAddress(Context context, Location location) { + double longitude = location.getLongitude(); + double latitude = location.getLatitude(); + String address = ""; + if (latitude != 0 && longitude != 0) { + Geocoder geoCoder = new Geocoder(context, Locale.getDefault()); try { - addresses = geocoder.getFromLocation(latitude, longitude, 1); - } catch (IOException e) { + List<Address> addresses = geoCoder.getFromLocation(latitude, longitude, 1); + if (addresses != null && addresses.size() > 0) { + Address Address = addresses.get(0); + StringBuilder strAddress = new StringBuilder(""); + + if (Address.getAddressLine(0).length() > 0) { + strAddress.append(Address.getAddressLine(0)); + } + address = strAddress.toString().replace(", ", "<br>"); + } + } catch (Exception e) { e.printStackTrace(); } + } + return address; + } - if (addresses != null && addresses.size() > 0) { - Address Address = addresses.get(0); - StringBuilder strAddress = new StringBuilder(""); + private class getAddressAsync extends AsyncTask<Void, Void, Void> { + String address = null; - if (Address.getAddressLine(0).length() > 0) { - strAddress.append(Address.getAddressLine(0)); - } - address = strAddress.toString().replace(", ", "\n"); - } + private WeakReference<ShareLocationActivity> activityReference; - return address; + getAddressAsync(ShareLocationActivity context) { + activityReference = new WeakReference<>(context); + } + @Override + protected void onPreExecute() { + super.onPreExecute(); + showLocation(mLastLocation, null); } @Override - protected void onPostExecute(String address) { - // Setting address of the touched Position - if (address.length() > 0) { - mLocationInfo.setVisibility(View.VISIBLE); - mSnackbarLocation.setText(address); - Log.d(Config.LOGTAG, "Location: Address = " + address); - } + protected Void doInBackground(Void... params) { + address = getAddress(ShareLocationActivity.this, mLastLocation); + return null; + } + + @Override + protected void onPostExecute(Void result) { + super.onPostExecute(result); + showLocation(mLastLocation, address); + } + } + + private void showLocation (@Nullable Location location, @Nullable String address) { + if (location == null && TextUtils.isEmpty(address)) { // no location and no address available + final WebView webView = findViewById(R.id.webView); + webView.getSettings().setJavaScriptEnabled(true); + webView.loadUrl("file:///android_asset/map.html"); + } else if (location != null && TextUtils.isEmpty(address)) { // location but no address available + String LocationName = "<b>" + mLocationName + "</b>"; + final WebView webView = findViewById(R.id.webView); + webView.getSettings().setJavaScriptEnabled(true); + webView.loadUrl("file:///android_asset/map.html?lat=" + mLastLocation.getLatitude() + "&lon=" + mLastLocation.getLongitude() + "&name=" + LocationName); + } else if (location != null && !TextUtils.isEmpty(address)) { // location and address available + String LocationName = "<b>" + mLocationName + "</b><br>" + address; + final WebView webView = findViewById(R.id.webView); + webView.getSettings().setJavaScriptEnabled(true); + webView.loadUrl("file:///android_asset/map.html?lat=" + mLastLocation.getLatitude() + "&lon=" + mLastLocation.getLongitude() + "&name=" + LocationName); } } } diff --git a/src/main/java/de/pixart/messenger/ui/ShowLocationActivity.java b/src/main/java/de/pixart/messenger/ui/ShowLocationActivity.java index 27e48c0d3..67641238d 100644 --- a/src/main/java/de/pixart/messenger/ui/ShowLocationActivity.java +++ b/src/main/java/de/pixart/messenger/ui/ShowLocationActivity.java @@ -9,67 +9,33 @@ import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.location.Address; import android.location.Geocoder; +import android.location.Location; import android.net.Uri; import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.preference.PreferenceManager; +import android.text.TextUtils; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; -import android.view.View; -import android.widget.TextView; +import android.webkit.WebView; import android.widget.Toast; -import com.google.android.gms.maps.CameraUpdateFactory; -import com.google.android.gms.maps.GoogleMap; -import com.google.android.gms.maps.MapFragment; -import com.google.android.gms.maps.OnMapReadyCallback; -import com.google.android.gms.maps.model.LatLng; -import com.google.android.gms.maps.model.Marker; -import com.google.android.gms.maps.model.MarkerOptions; +import org.jetbrains.annotations.Nullable; +import java.lang.ref.WeakReference; import java.util.List; import java.util.Locale; -import de.pixart.messenger.Config; import de.pixart.messenger.R; import de.pixart.messenger.services.EmojiService; import static de.pixart.messenger.ui.SettingsActivity.USE_BUNDLED_EMOJIS; -public class ShowLocationActivity extends Activity implements OnMapReadyCallback { - - private GoogleMap mGoogleMap; - private LatLng mLocation; +public class ShowLocationActivity extends Activity { + private Location location; private String mLocationName; - private MarkerOptions options; - private Marker marker; - - class InfoWindowAdapter implements GoogleMap.InfoWindowAdapter { - - private final View InfoWindow; - - InfoWindowAdapter() { - InfoWindow = getLayoutInflater().inflate(R.layout.show_location_infowindow, null); - } - - @Override - public View getInfoWindow(Marker marker) { - return null; - } - - @Override - public View getInfoContents(Marker marker) { - - TextView Title = InfoWindow.findViewById(R.id.title); - Title.setText(marker.getTitle()); - TextView Snippet = InfoWindow.findViewById(R.id.snippet); - Snippet.setText(marker.getSnippet()); - - return InfoWindow; - } - } @Override protected void onCreate(Bundle savedInstanceState) { @@ -80,46 +46,8 @@ public class ShowLocationActivity extends Activity implements OnMapReadyCallback getActionBar().setHomeButtonEnabled(true); getActionBar().setDisplayHomeAsUpEnabled(true); } - setContentView(R.layout.activity_show_locaction); - MapFragment fragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map_fragment); - fragment.getMapAsync(this); - } - - protected SharedPreferences getPreferences() { - return PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - finish(); - return true; - case R.id.action_navigate: - double longitude = mLocation.longitude; - double latitude = mLocation.latitude; - try { - Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("geo:" + String.valueOf(latitude) + "," + String.valueOf(longitude))); - startActivity(intent); - } catch (ActivityNotFoundException e) { - Toast.makeText(this, R.string.no_application_found_to_display_location, Toast.LENGTH_SHORT).show(); - } - return true; - } - return super.onOptionsItemSelected(item); - } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - MenuInflater inflater = getMenuInflater(); - inflater.inflate(R.menu.showlocation, menu); - return true; - } - - @Override - protected void onResume() { - super.onResume(); Intent intent = getIntent(); this.mLocationName = intent != null ? intent.getStringExtra("name") : null; @@ -127,39 +55,59 @@ public class ShowLocationActivity extends Activity implements OnMapReadyCallback if (intent != null && intent.hasExtra("longitude") && intent.hasExtra("latitude")) { double longitude = intent.getDoubleExtra("longitude", 0); double latitude = intent.getDoubleExtra("latitude", 0); - this.mLocation = new LatLng(latitude, longitude); - if (this.mGoogleMap != null) { - markAndCenterOnLocation(this.mLocation, this.mLocationName); - } + this.location = new Location(""); + this.location.setLatitude(latitude); + this.location.setLongitude(longitude); } - } - - @Override - protected void onPause() { - super.onPause(); - } - @Override - public void onMapReady(GoogleMap googleMap) { - this.mGoogleMap = googleMap; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { - this.mGoogleMap.setBuildingsEnabled(true); - this.mGoogleMap.setMyLocationEnabled(true); + markAndCenterOnLocation(location); } } else { - this.mGoogleMap.setBuildingsEnabled(true); - this.mGoogleMap.setMyLocationEnabled(true); + markAndCenterOnLocation(location); } - if (this.mLocation != null) { - this.markAndCenterOnLocation(this.mLocation, this.mLocationName); + } + + private void markAndCenterOnLocation(final Location location) { + double longitude = location.getLongitude(); + double latitude = location.getLatitude(); + if (latitude != 0 && longitude != 0) { + new getAddressAsync(this).execute(); } } + private class getAddressAsync extends AsyncTask<Void, Void, Void> { + String address = null; + + private WeakReference<ShowLocationActivity> activityReference; + + getAddressAsync(ShowLocationActivity context) { + activityReference = new WeakReference<>(context); + } + + @Override + protected void onPreExecute() { + super.onPreExecute(); + showLocation(location, null); + } + + @Override + protected Void doInBackground(Void... params) { + address = getAddress(ShowLocationActivity.this, location); + return null; + } + + @Override + protected void onPostExecute(Void result) { + super.onPostExecute(result); + showLocation(location, address); + } + }; - private static String getAddress(Context context, LatLng location) { - double longitude = location.longitude; - double latitude = location.latitude; + private static String getAddress(Context context, Location location) { + double longitude = location.getLongitude(); + double latitude = location.getLatitude(); String address = ""; if (latitude != 0 && longitude != 0) { Geocoder geoCoder = new Geocoder(context, Locale.getDefault()); @@ -172,7 +120,7 @@ public class ShowLocationActivity extends Activity implements OnMapReadyCallback if (Address.getAddressLine(0).length() > 0) { strAddress.append(Address.getAddressLine(0)); } - address = strAddress.toString().replace(", ", "\n"); + address = strAddress.toString().replace(", ", "<br>"); } } catch (Exception e) { e.printStackTrace(); @@ -181,52 +129,58 @@ public class ShowLocationActivity extends Activity implements OnMapReadyCallback return address; } - private void markAndCenterOnLocation(final LatLng location, String name) { - mGoogleMap.clear(); - options = new MarkerOptions(); - options.position(location); - double longitude = mLocation.longitude; - double latitude = mLocation.latitude; - mGoogleMap.setInfoWindowAdapter(new InfoWindowAdapter()); - if (name != null) { - options.title(name); - } - if (latitude != 0 && longitude != 0) { - new AsyncTask<Void, Void, Void>() { - String address = null; - - @Override - protected void onPreExecute() { - super.onPreExecute(); - marker = mGoogleMap.addMarker(options); - marker.showInfoWindow(); - mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(location, Config.DEFAULT_ZOOM)); - } - @Override - protected Void doInBackground(Void... params) { - address = getAddress(ShowLocationActivity.this, location); - return null; - } + protected SharedPreferences getPreferences() { + return PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); + } - @Override - protected void onPostExecute(Void result) { - super.onPostExecute(result); - marker.remove(); - options.snippet(String.valueOf(address)); - marker = mGoogleMap.addMarker(options); - marker.showInfoWindow(); + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + finish(); + return true; + case R.id.action_navigate: + double longitude = location.getLongitude(); + double latitude = location.getLatitude(); + try { + Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("geo:" + String.valueOf(latitude) + "," + String.valueOf(longitude))); + startActivity(intent); + } catch (ActivityNotFoundException e) { + Toast.makeText(this, R.string.no_application_found_to_display_location, Toast.LENGTH_SHORT).show(); } - }.execute(); + return true; } + return super.onOptionsItemSelected(item); + } - mGoogleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() { - @Override - public void onMapClick(LatLng latLng) { - if (marker != null) { - marker.showInfoWindow(); - } - } - }); + @Override + public boolean onCreateOptionsMenu(Menu menu) { + MenuInflater inflater = getMenuInflater(); + inflater.inflate(R.menu.showlocation, menu); + return true; + } + + @Override + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + } + + private void showLocation (@Nullable Location location, @Nullable String address) { + if (location == null && TextUtils.isEmpty(address)) { // no location and no address available + final WebView webView = findViewById(R.id.webView); + webView.getSettings().setJavaScriptEnabled(true); + webView.loadUrl("file:///android_asset/map.html"); + } else if (location != null && TextUtils.isEmpty(address)) { // location but no address available + String LocationName = "<b>" + mLocationName + "</b>"; + final WebView webView = findViewById(R.id.webView); + webView.getSettings().setJavaScriptEnabled(true); + webView.loadUrl("file:///android_asset/map.html?lat=" + location.getLatitude() + "&lon=" + location.getLongitude()+ "&name=" + LocationName); + } else if (location != null && !TextUtils.isEmpty(address)) { // location and address available + String LocationName = "<b>" + mLocationName + "</b><br>" + address; + final WebView webView = findViewById(R.id.webView); + webView.getSettings().setJavaScriptEnabled(true); + webView.loadUrl("file:///android_asset/map.html?lat=" + location.getLatitude() + "&lon=" + location.getLongitude() + "&name=" + LocationName); + } } -} +}
\ No newline at end of file diff --git a/src/main/java/de/pixart/messenger/utils/LocationHelper.java b/src/main/java/de/pixart/messenger/utils/LocationHelper.java new file mode 100644 index 000000000..85226bcd1 --- /dev/null +++ b/src/main/java/de/pixart/messenger/utils/LocationHelper.java @@ -0,0 +1,51 @@ +package de.pixart.messenger.utils; + +import android.location.Location; + +import de.pixart.messenger.Config; + +public final class LocationHelper { + private static boolean isSameProvider(final String provider1, final String provider2) { + if (provider1 == null) { + return provider2 == null; + } + return provider1.equals(provider2); + } + + public static boolean isBetterLocation(final Location location, final Location prevLoc) { + if (prevLoc == null) { + return true; + } + + // Check whether the new location fix is newer or older + final long timeDelta = location.getTime() - prevLoc.getTime(); + final boolean isSignificantlyNewer = timeDelta > Config.LOCATION_FIX_SIGNIFICANT_TIME_DELTA; + final boolean isSignificantlyOlder = timeDelta < -Config.LOCATION_FIX_SIGNIFICANT_TIME_DELTA; + final boolean isNewer = timeDelta > 0; + + if (isSignificantlyNewer) { + return true; + } else if (isSignificantlyOlder) { + return false; + } + + // Check whether the new location fix is more or less accurate + final int accuracyDelta = (int) (location.getAccuracy() - prevLoc.getAccuracy()); + final boolean isLessAccurate = accuracyDelta > 0; + final boolean isMoreAccurate = accuracyDelta < 0; + final boolean isSignificantlyLessAccurate = accuracyDelta > 200; + + // Check if the old and new location are from the same provider + final boolean isFromSameProvider = isSameProvider(location.getProvider(), prevLoc.getProvider()); + + // Determine location quality using a combination of timeliness and accuracy + if (isMoreAccurate) { + return true; + } else if (isNewer && !isLessAccurate) { + return true; + } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { + return true; + } + return false; + } +} |