Fix security exception

This commit is contained in:
Victor Shcherb 2020-05-26 15:15:53 +02:00
parent 1985364a21
commit b48a516dd8
2 changed files with 49 additions and 51 deletions

View file

@ -9,7 +9,6 @@ import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.provider.OpenableColumns; import android.provider.OpenableColumns;
import android.provider.Settings; import android.provider.Settings;
import android.widget.Toast; import android.widget.Toast;
@ -60,7 +59,6 @@ import org.xmlpull.v1.XmlPullParserException;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
@ -165,7 +163,7 @@ public class ImportHelper {
return false; return false;
} }
public void handleFavouritesImport(@NonNull Uri uri) { public void handleGpxOrFavouritesImport(@NonNull Uri uri) {
String scheme = uri.getScheme(); String scheme = uri.getScheme();
boolean isFileIntent = "file".equals(scheme); boolean isFileIntent = "file".equals(scheme);
boolean isContentIntent = "content".equals(scheme); boolean isContentIntent = "content".equals(scheme);
@ -177,7 +175,7 @@ public class ImportHelper {
} else if (isContentIntent) { } else if (isContentIntent) {
fileName = getNameFromContentUri(app, uri); fileName = getNameFromContentUri(app, uri);
} }
handleFavouritesImport(uri, fileName, saveFile, false, true); handleGpxOrFavouritesImport(uri, fileName, saveFile, false, true);
} }
public void handleFileImport(Uri intentUri, String fileName, Bundle extras, boolean useImportDir) { public void handleFileImport(Uri intentUri, String fileName, Bundle extras, boolean useImportDir) {
@ -186,40 +184,48 @@ public class ImportHelper {
final boolean saveFile = !isFileIntent || !isOsmandSubdir; final boolean saveFile = !isFileIntent || !isOsmandSubdir;
if (fileName != null && fileName.endsWith(KML_SUFFIX)) { if (fileName == null) {
handleGpxOrFavouritesImport(intentUri, fileName, saveFile, useImportDir, false);
} else if (fileName.endsWith(KML_SUFFIX)) {
handleKmlImport(intentUri, fileName, saveFile, useImportDir); handleKmlImport(intentUri, fileName, saveFile, useImportDir);
} else if (fileName != null && fileName.endsWith(KMZ_SUFFIX)) { } else if (fileName.endsWith(KMZ_SUFFIX)) {
handleKmzImport(intentUri, fileName, saveFile, useImportDir); handleKmzImport(intentUri, fileName, saveFile, useImportDir);
} else if (fileName != null && fileName.endsWith(IndexConstants.BINARY_MAP_INDEX_EXT)) { } else if (fileName.endsWith(IndexConstants.BINARY_MAP_INDEX_EXT)) {
handleObfImport(intentUri, fileName); handleObfImport(intentUri, fileName);
} else if (fileName != null && fileName.endsWith(IndexConstants.SQLITE_EXT)) { } else if (fileName.endsWith(IndexConstants.SQLITE_EXT)) {
handleSqliteTileImport(intentUri, fileName); handleSqliteTileImport(intentUri, fileName);
} else if (fileName != null && fileName.endsWith(OSMAND_SETTINGS_FILE_EXT)) { } else if (fileName.endsWith(OSMAND_SETTINGS_FILE_EXT)) {
handleOsmAndSettingsImport(intentUri, fileName, extras, null); handleOsmAndSettingsImport(intentUri, fileName, extras, null);
} else if (fileName != null && fileName.endsWith(ROUTING_FILE_EXT)) { } else if (fileName.endsWith(ROUTING_FILE_EXT)) {
handleXmlFileImport(intentUri, fileName, null); handleXmlFileImport(intentUri, fileName, null);
} else { } else {
handleFavouritesImport(intentUri, fileName, saveFile, useImportDir, false); handleGpxOrFavouritesImport(intentUri, fileName, saveFile, useImportDir, false);
} }
} }
public static String getNameFromContentUri(OsmandApplication app, Uri contentUri) { public static String getNameFromContentUri(OsmandApplication app, Uri contentUri) {
final String name; try {
final Cursor returnCursor = app.getContentResolver().query(contentUri, new String[] {OpenableColumns.DISPLAY_NAME}, null, null, null); final String name;
if (returnCursor != null && returnCursor.moveToFirst()) { final Cursor returnCursor = app.getContentResolver().query(contentUri, new String[]{OpenableColumns.DISPLAY_NAME}, null, null, null);
int columnIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); if (returnCursor != null && returnCursor.moveToFirst()) {
if (columnIndex != -1) { int columnIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
name = returnCursor.getString(columnIndex); if (columnIndex != -1) {
name = returnCursor.getString(columnIndex);
} else {
name = contentUri.getLastPathSegment();
}
} else { } else {
name = contentUri.getLastPathSegment(); name = null;
} }
} else { if (returnCursor != null && !returnCursor.isClosed()) {
name = null; returnCursor.close();
}
return name;
} catch (RuntimeException e) {
log.error(e.getMessage(), e);
return null;
} }
if (returnCursor != null && !returnCursor.isClosed()) {
returnCursor.close();
}
return name;
} }
@SuppressLint("StaticFieldLeak") @SuppressLint("StaticFieldLeak")
@ -236,10 +242,8 @@ public class ImportHelper {
protected GPXFile doInBackground(Void... nothing) { protected GPXFile doInBackground(Void... nothing) {
InputStream is = null; InputStream is = null;
try { try {
final ParcelFileDescriptor pFD = app.getContentResolver().openFileDescriptor(gpxFile, "r"); is = app.getContentResolver().openInputStream(gpxFile);
if (is != null) {
if (pFD != null) {
is = new FileInputStream(pFD.getFileDescriptor());
return GPXUtilities.loadGPXFile(is); return GPXUtilities.loadGPXFile(is);
} }
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
@ -264,7 +268,7 @@ public class ImportHelper {
} }
@SuppressLint("StaticFieldLeak") @SuppressLint("StaticFieldLeak")
private void handleFavouritesImport(final Uri fileUri, final String fileName, final boolean save, final boolean useImportDir, final boolean forceImportFavourites) { private void handleGpxOrFavouritesImport(final Uri fileUri, final String fileName, final boolean save, final boolean useImportDir, final boolean forceImportFavourites) {
new AsyncTask<Void, Void, GPXFile>() { new AsyncTask<Void, Void, GPXFile>() {
ProgressDialog progress = null; ProgressDialog progress = null;
@ -278,10 +282,8 @@ public class ImportHelper {
InputStream is = null; InputStream is = null;
ZipInputStream zis = null; ZipInputStream zis = null;
try { try {
final ParcelFileDescriptor pFD = app.getContentResolver().openFileDescriptor(fileUri, "r"); is = app.getContentResolver().openInputStream(fileUri);
if (pFD != null) { if (is != null) {
is = new FileInputStream(pFD.getFileDescriptor());
if (fileName != null && fileName.endsWith(KML_SUFFIX)) { if (fileName != null && fileName.endsWith(KML_SUFFIX)) {
final String result = Kml2Gpx.toGpx(is); final String result = Kml2Gpx.toGpx(is);
if (result != null) { if (result != null) {
@ -331,7 +333,7 @@ public class ImportHelper {
progress.dismiss(); progress.dismiss();
} }
importFavourites(result, fileName, save, useImportDir, forceImportFavourites); importGpxOrFavourites(result, fileName, save, useImportDir, forceImportFavourites);
} }
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} }
@ -394,9 +396,8 @@ public class ImportHelper {
InputStream is = null; InputStream is = null;
ZipInputStream zis = null; ZipInputStream zis = null;
try { try {
final ParcelFileDescriptor pFD = app.getContentResolver().openFileDescriptor(kmzFile, "r"); is = app.getContentResolver().openInputStream(kmzFile);
if (pFD != null) { if (is != null) {
is = new FileInputStream(pFD.getFileDescriptor());
zis = new ZipInputStream(is); zis = new ZipInputStream(is);
zis.getNextEntry(); zis.getNextEntry();
final String result = Kml2Gpx.toGpx(zis); final String result = Kml2Gpx.toGpx(zis);
@ -449,9 +450,8 @@ public class ImportHelper {
protected GPXFile doInBackground(Void... nothing) { protected GPXFile doInBackground(Void... nothing) {
InputStream is = null; InputStream is = null;
try { try {
final ParcelFileDescriptor pFD = app.getContentResolver().openFileDescriptor(kmlFile, "r"); is = app.getContentResolver().openInputStream(kmlFile);
if (pFD != null) { if (is != null) {
is = new FileInputStream(pFD.getFileDescriptor());
final String result = Kml2Gpx.toGpx(is); final String result = Kml2Gpx.toGpx(is);
if (result != null) { if (result != null) {
try { try {
@ -533,13 +533,12 @@ public class ImportHelper {
InputStream in = null; InputStream in = null;
OutputStream out = null; OutputStream out = null;
try { try {
final ParcelFileDescriptor pFD = app.getContentResolver().openFileDescriptor(uri, "r"); in = app.getContentResolver().openInputStream(uri);
if (pFD != null) { if (in != null) {
in = new FileInputStream(pFD.getFileDescriptor());
out = new FileOutputStream(dest); out = new FileOutputStream(dest);
Algorithms.streamCopy(in, out); Algorithms.streamCopy(in, out);
try { try {
pFD.close(); in.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -893,9 +892,8 @@ public class ImportHelper {
private void checkImportType() { private void checkImportType() {
InputStream is = null; InputStream is = null;
try { try {
final ParcelFileDescriptor pFD = app.getContentResolver().openFileDescriptor(intentUri, "r"); is = app.getContentResolver().openInputStream(intentUri);
if (pFD != null) { if (is != null) {
is = new FileInputStream(pFD.getFileDescriptor());
XmlPullParser parser = PlatformUtil.newXMLPullParser(); XmlPullParser parser = PlatformUtil.newXMLPullParser();
parser.setInput(is, "UTF-8"); parser.setInput(is, "UTF-8");
int tok; int tok;
@ -911,7 +909,7 @@ public class ImportHelper {
} }
} }
try { try {
pFD.close(); is.close();
} catch (IOException e) { } catch (IOException e) {
log.error(e); log.error(e);
} }
@ -1101,8 +1099,8 @@ public class ImportHelper {
} }
} }
private void importFavourites(final GPXFile gpxFile, final String fileName, final boolean save, private void importGpxOrFavourites(final GPXFile gpxFile, final String fileName, final boolean save,
final boolean useImportDir, final boolean forceImportFavourites) { final boolean useImportDir, final boolean forceImportFavourites) {
if (gpxFile == null || gpxFile.isPointsEmpty()) { if (gpxFile == null || gpxFile.isPointsEmpty()) {
if (forceImportFavourites) { if (forceImportFavourites) {
final DialogInterface.OnClickListener importAsTrackListener = new DialogInterface.OnClickListener() { final DialogInterface.OnClickListener importAsTrackListener = new DialogInterface.OnClickListener() {

View file

@ -155,7 +155,7 @@ public class FavoritesActivity extends TabActivity {
} }
} else if (requestCode == IMPORT_FAVOURITES_REQUEST && resultCode == Activity.RESULT_OK) { } else if (requestCode == IMPORT_FAVOURITES_REQUEST && resultCode == Activity.RESULT_OK) {
if (data != null && data.getData() != null) { if (data != null && data.getData() != null) {
importHelper.handleFavouritesImport(data.getData()); importHelper.handleGpxOrFavouritesImport(data.getData());
} }
} else { } else {
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);