add try-catch for location activitys

This commit is contained in:
Christian Schneppe 2021-04-10 21:24:49 +02:00
parent fac47b3924
commit 6c299307bd
2 changed files with 36 additions and 26 deletions

View file

@ -29,6 +29,7 @@ import java.util.Locale;
import eu.siacs.conversations.R;
import eu.siacs.conversations.utils.LocationHelper;
import eu.siacs.conversations.utils.ThemeHelper;
import me.drakeet.support.toast.ToastCompat;
public class ShareLocationActivity extends LocationActivity implements LocationListener {
@ -211,20 +212,25 @@ public class ShareLocationActivity extends LocationActivity implements LocationL
}
private void showLocation(@Nullable Location location, 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("javascript:toCoordinates(" + mLastLocation.getLatitude() + "," + mLastLocation.getLongitude() + "," + "'" + 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("javascript:toCoordinates(" + mLastLocation.getLatitude() + "," + mLastLocation.getLongitude() + "," + "'" + LocationName + "'" +");");
try {
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("javascript:toCoordinates(" + mLastLocation.getLatitude() + "," + mLastLocation.getLongitude() + "," + "'" + 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("javascript:toCoordinates(" + mLastLocation.getLatitude() + "," + mLastLocation.getLongitude() + "," + "'" + LocationName + "'" + ");");
}
} catch (Exception e) {
e.printStackTrace();
ToastCompat.makeText(this, R.string.error, ToastCompat.LENGTH_LONG);
}
}

View file

@ -17,7 +17,6 @@ import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.Toast;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
@ -137,16 +136,21 @@ public class ShowLocationActivity extends XmppActivity {
}
private void showLocation(Location location, String address) {
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("javascript:toCoordinates(" + location.getLatitude() + "," + location.getLongitude() + "," + "'" + LocationName + "'" +");");
try {
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("javascript:toCoordinates(" + location.getLatitude() + "," + location.getLongitude() + "," + "'" + LocationName + "'" + ");");
}
} catch (Exception e) {
e.printStackTrace();
ToastCompat.makeText(this, R.string.error, ToastCompat.LENGTH_LONG);
}
}
@ -162,7 +166,7 @@ public class ShowLocationActivity extends XmppActivity {
startActivity(intent);
overridePendingTransition(R.animator.fade_in, R.animator.fade_out);
} catch (ActivityNotFoundException e) {
ToastCompat.makeText(this, R.string.no_application_found_to_display_location, Toast.LENGTH_SHORT).show();
ToastCompat.makeText(this, R.string.no_application_found_to_display_location, ToastCompat.LENGTH_SHORT).show();
}
}