use resolveActivityInfo to display nagivate to button

resolveActivity on the other hand only finds apps that are category_default

fixes #4375
This commit is contained in:
Daniel Gultsch 2022-09-09 16:55:47 +02:00
parent f7996a6c3c
commit a95d0fa8d3
2 changed files with 188 additions and 168 deletions

View file

@ -64,6 +64,9 @@
<action android:name="android.intent.action.VIEW" />
<data android:mimeType="resource/folder" />
</intent>
<intent>
<action android:name="android.intent.action.VIEW" />
</intent>
</queries>

View file

@ -3,8 +3,8 @@ package eu.siacs.conversations.ui;
import android.content.ActivityNotFoundException;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.location.Location;
import android.location.LocationListener;
import android.net.Uri;
@ -17,6 +17,7 @@ import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import org.jetbrains.annotations.NotNull;
import org.osmdroid.util.GeoPoint;
import java.util.HashMap;
@ -32,13 +33,11 @@ import eu.siacs.conversations.ui.widget.Marker;
import eu.siacs.conversations.ui.widget.MyLocation;
import eu.siacs.conversations.utils.LocationProvider;
public class ShowLocationActivity extends LocationActivity implements LocationListener {
private GeoPoint loc = LocationProvider.FALLBACK;
private ActivityShowLocationBinding binding;
private Uri createGeoUri() {
return Uri.parse("geo:" + this.loc.getLatitude() + "," + this.loc.getLongitude());
}
@ -47,7 +46,7 @@ public class ShowLocationActivity extends LocationActivity implements LocationLi
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.binding = DataBindingUtil.setContentView(this,R.layout.activity_show_location);
this.binding = DataBindingUtil.setContentView(this, R.layout.activity_show_location);
setSupportActionBar(binding.toolbar);
configureActionBar(getSupportActionBar());
@ -74,7 +73,8 @@ public class ShowLocationActivity extends LocationActivity implements LocationLi
// Attempt to set zoom level if the geo URI specifies it
if (geoUri != null) {
final HashMap<String, String> query = UriHelper.parseQueryString(geoUri.getQuery());
final HashMap<String, String> query =
UriHelper.parseQueryString(geoUri.getQuery());
// Check for zoom level.
final String z = query.get("z");
@ -89,11 +89,16 @@ public class ShowLocationActivity extends LocationActivity implements LocationLi
boolean posInQuery = false;
final String q = query.get("q");
if (q != null) {
final Pattern latlng = Pattern.compile("/^([-+]?[0-9]+(\\.[0-9]+)?),([-+]?[0-9]+(\\.[0-9]+)?)(\\(.*\\))?/");
final Pattern latlng =
Pattern.compile(
"/^([-+]?[0-9]+(\\.[0-9]+)?),([-+]?[0-9]+(\\.[0-9]+)?)(\\(.*\\))?/");
final Matcher m = latlng.matcher(q);
if (m.matches()) {
try {
this.loc = new GeoPoint(Double.valueOf(m.group(1)), Double.valueOf(m.group(3)));
this.loc =
new GeoPoint(
Double.valueOf(m.group(1)),
Double.valueOf(m.group(3)));
posInQuery = true;
} catch (final Exception ignored) {
}
@ -103,7 +108,8 @@ public class ShowLocationActivity extends LocationActivity implements LocationLi
final String schemeSpecificPart = geoUri.getSchemeSpecificPart();
if (schemeSpecificPart != null && !schemeSpecificPart.isEmpty()) {
try {
final GeoPoint latlong = LocationHelper.parseLatLong(schemeSpecificPart);
final GeoPoint latlong =
LocationHelper.parseLatLong(schemeSpecificPart);
if (latlong != null && !posInQuery) {
this.loc = latlong;
}
@ -129,7 +135,8 @@ public class ShowLocationActivity extends LocationActivity implements LocationLi
}
@Override
public void onRequestPermissionsResult(final int requestCode,
public void onRequestPermissionsResult(
final int requestCode,
@NonNull final String[] permissions,
@NonNull final int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
@ -142,7 +149,7 @@ public class ShowLocationActivity extends LocationActivity implements LocationLi
}
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
public boolean onCreateOptionsMenu(@NotNull final Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_show_location, menu);
updateUi();
@ -167,11 +174,14 @@ public class ShowLocationActivity extends LocationActivity implements LocationLi
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case R.id.action_copy_location:
final ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
final ClipboardManager clipboard =
(ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
if (clipboard != null) {
final ClipData clip = ClipData.newPlainText("location", createGeoUri().toString());
final ClipData clip =
ClipData.newPlainText("location", createGeoUri().toString());
clipboard.setPrimaryClip(clip);
Toast.makeText(this,R.string.url_copied_to_clipboard,Toast.LENGTH_SHORT).show();
Toast.makeText(this, R.string.url_copied_to_clipboard, Toast.LENGTH_SHORT)
.show();
}
return true;
case R.id.action_share_location:
@ -182,8 +192,13 @@ public class ShowLocationActivity extends LocationActivity implements LocationLi
try {
startActivity(Intent.createChooser(shareIntent, getText(R.string.share_with)));
} catch (final ActivityNotFoundException e) {
//This should happen only on faulty androids because normally chooser is always available
Toast.makeText(this, R.string.no_application_found_to_open_file, Toast.LENGTH_SHORT).show();
// This should happen only on faulty androids because normally chooser is always
// available
Toast.makeText(
this,
R.string.no_application_found_to_open_file,
Toast.LENGTH_SHORT)
.show();
}
return true;
}
@ -191,21 +206,29 @@ public class ShowLocationActivity extends LocationActivity implements LocationLi
}
private void startNavigation() {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(
"google.navigation:q=" +
this.loc.getLatitude() + "," + this.loc.getLongitude()
)));
final Intent intent = getStartNavigationIntent();
startActivity(intent);
}
private Intent getStartNavigationIntent() {
return new Intent(
Intent.ACTION_VIEW,
Uri.parse(
"google.navigation:q="
+ this.loc.getLatitude()
+ ","
+ this.loc.getLongitude()));
}
@Override
protected void updateUi() {
final Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=0,0"));
final ComponentName component = i.resolveActivity(getPackageManager());
this.binding.fab.setVisibility(component == null ? View.GONE : View.VISIBLE);
final Intent intent = getStartNavigationIntent();
final ActivityInfo activityInfo = intent.resolveActivityInfo(getPackageManager(), 0);
this.binding.fab.setVisibility(activityInfo == null ? View.GONE : View.VISIBLE);
}
@Override
public void onLocationChanged(final Location location) {
public void onLocationChanged(@NotNull final Location location) {
if (LocationHelper.isBetterLocation(location, this.myLoc)) {
this.myLoc = location;
updateLocationMarkers();
@ -213,17 +236,11 @@ public class ShowLocationActivity extends LocationActivity implements LocationLi
}
@Override
public void onStatusChanged(final String provider, final int status, final Bundle extras) {
}
public void onStatusChanged(final String provider, final int status, final Bundle extras) {}
@Override
public void onProviderEnabled(final String provider) {
}
public void onProviderEnabled(@NotNull final String provider) {}
@Override
public void onProviderDisabled(final String provider) {
}
public void onProviderDisabled(@NotNull final String provider) {}
}