Fix BadTokenException

This commit is contained in:
Vitaliy 2021-02-04 14:47:49 +02:00
parent ddaaaa6d43
commit 4d518acf23

View file

@ -1,6 +1,7 @@
package net.osmand.plus.importfiles; package net.osmand.plus.importfiles;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent; import android.content.Intent;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
@ -9,13 +10,13 @@ import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.provider.OpenableColumns; import android.provider.OpenableColumns;
import android.provider.Settings; import android.provider.Settings;
import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import net.osmand.AndroidUtils;
import net.osmand.CallbackWithObject; import net.osmand.CallbackWithObject;
import net.osmand.GPXUtilities; import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile; import net.osmand.GPXUtilities.GPXFile;
@ -418,7 +419,7 @@ public class ImportHelper {
final boolean useImportDir, boolean forceImportFavourites, boolean showInDetailsActivity) { final boolean useImportDir, boolean forceImportFavourites, boolean showInDetailsActivity) {
if (result != null) { if (result != null) {
if (result.error != null) { if (result.error != null) {
Toast.makeText(activity, result.error.getMessage(), Toast.LENGTH_LONG).show(); app.showToastMessage(result.error.getMessage());
if (gpxImportCompleteListener != null) { if (gpxImportCompleteListener != null) {
gpxImportCompleteListener.onImportComplete(false); gpxImportCompleteListener.onImportComplete(false);
} }
@ -439,34 +440,36 @@ public class ImportHelper {
} }
} }
} else { } else {
new AlertDialog.Builder(activity) if (AndroidUtils.isActivityNotDestroyed(activity)) {
.setTitle(R.string.shared_string_import2osmand) new AlertDialog.Builder(activity)
.setMessage(R.string.import_gpx_failed_descr) .setTitle(R.string.shared_string_import2osmand)
.setNeutralButton(R.string.shared_string_permissions, new DialogInterface.OnClickListener() { .setMessage(R.string.import_gpx_failed_descr)
@Override .setNeutralButton(R.string.shared_string_permissions, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) { @Override
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); public void onClick(DialogInterface dialog, int which) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", app.getPackageName(), null); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(uri); Uri uri = Uri.fromParts("package", app.getPackageName(), null);
app.startActivity(intent); intent.setData(uri);
if (gpxImportCompleteListener != null) { app.startActivity(intent);
gpxImportCompleteListener.onImportComplete(false); if (gpxImportCompleteListener != null) {
gpxImportCompleteListener.onImportComplete(false);
}
} }
} })
}) .setNegativeButton(R.string.shared_string_cancel, new DialogInterface.OnClickListener() {
.setNegativeButton(R.string.shared_string_cancel, new DialogInterface.OnClickListener() { @Override
@Override public void onClick(DialogInterface dialog, int which) {
public void onClick(DialogInterface dialog, int which) { if (gpxImportCompleteListener != null) {
if (gpxImportCompleteListener != null) { gpxImportCompleteListener.onImportComplete(false);
gpxImportCompleteListener.onImportComplete(false); }
} }
} })
}) .show();
.show(); }
} }
if (forceImportFavourites) { if (forceImportFavourites) {
final Intent newIntent = new Intent(activity, app.getAppCustomization().getFavoritesActivity()); Intent newIntent = new Intent(activity, app.getAppCustomization().getFavoritesActivity());
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
newIntent.putExtra(TAB_ID, GPX_TAB); newIntent.putExtra(TAB_ID, GPX_TAB);
activity.startActivity(newIntent); activity.startActivity(newIntent);
@ -577,7 +580,7 @@ public class ImportHelper {
showPlanRouteFragment(); showPlanRouteFragment();
} }
} else { } else {
Toast.makeText(activity, warning, Toast.LENGTH_LONG).show(); app.showToastMessage(warning);
} }
} }
@ -619,26 +622,27 @@ public class ImportHelper {
final boolean forceImportFavourites, final boolean forceImportGpx) { final boolean forceImportFavourites, final boolean forceImportGpx) {
if (gpxFile == null || gpxFile.isPointsEmpty()) { if (gpxFile == null || gpxFile.isPointsEmpty()) {
if (forceImportFavourites) { if (forceImportFavourites) {
final DialogInterface.OnClickListener importAsTrackListener = new DialogInterface.OnClickListener() { if (AndroidUtils.isActivityNotDestroyed(activity)) {
@Override OnClickListener importAsTrackListener = new OnClickListener() {
public void onClick(DialogInterface dialog, int which) { @Override
switch (which) { public void onClick(DialogInterface dialog, int which) {
case DialogInterface.BUTTON_POSITIVE: switch (which) {
handleResult(gpxFile, fileName, fileSize, save, useImportDir, true); case DialogInterface.BUTTON_POSITIVE:
break; handleResult(gpxFile, fileName, fileSize, save, useImportDir, true);
case DialogInterface.BUTTON_NEGATIVE: break;
dialog.dismiss(); case DialogInterface.BUTTON_NEGATIVE:
break; dialog.dismiss();
break;
}
} }
} };
}; new AlertDialog.Builder(activity)
.setTitle(R.string.import_track)
new AlertDialog.Builder(activity) .setMessage(activity.getString(R.string.import_track_desc, fileName))
.setTitle(R.string.import_track) .setPositiveButton(R.string.shared_string_import, importAsTrackListener)
.setMessage(activity.getString(R.string.import_track_desc, fileName)) .setNegativeButton(R.string.shared_string_cancel, importAsTrackListener)
.setPositiveButton(R.string.shared_string_import, importAsTrackListener) .show();
.setNegativeButton(R.string.shared_string_cancel, importAsTrackListener) }
.show();
} else { } else {
handleResult(gpxFile, fileName, fileSize, save, useImportDir, false); handleResult(gpxFile, fileName, fileSize, save, useImportDir, false);
} }