Improve detection of sdcard write access

This allows reading waypoints from apps on till devices running API 28
if they store their files in a accessible path (not Android/data). It
even works for devices running API 30 if they installed it as an update.
This commit is contained in:
Manuel Fuhr 2021-11-02 17:24:50 +01:00
parent 12148f6a5d
commit dc95984199
2 changed files with 28 additions and 19 deletions

View file

@ -17,13 +17,20 @@ import java.util.List;
import java.util.zip.GZIPOutputStream; import java.util.zip.GZIPOutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import android.Manifest;
import android.app.Service; import android.app.Service;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder; import android.os.IBinder;
import android.os.RemoteException; import android.os.RemoteException;
import android.util.Log; import android.util.Log;
import android.util.Base64; import android.util.Base64;
import androidx.core.content.ContextCompat;
import btools.router.OsmNodeNamed; import btools.router.OsmNodeNamed;
public class BRouterService extends Service public class BRouterService extends Service
@ -203,7 +210,13 @@ public class BRouterService extends Service
// add nogos from waypoint database // add nogos from waypoint database
int deviceLevel = android.os.Build.VERSION.SDK_INT; int deviceLevel = android.os.Build.VERSION.SDK_INT;
int targetSdkVersion = getApplicationInfo().targetSdkVersion; int targetSdkVersion = getApplicationInfo().targetSdkVersion;
boolean canAccessSdCard = deviceLevel < 23 || targetSdkVersion == 19; boolean canAccessSdCard = true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && !Environment.isExternalStorageLegacy()) {
canAccessSdCard = false;
}
if (ContextCompat.checkSelfPermission(BRouterService.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
canAccessSdCard = false;
}
AppLogger.log( "dev/target=" + deviceLevel + "/" + targetSdkVersion + " canAccessSdCard=" + canAccessSdCard ); AppLogger.log( "dev/target=" + deviceLevel + "/" + targetSdkVersion + " canAccessSdCard=" + canAccessSdCard );
if ( canAccessSdCard ) if ( canAccessSdCard )
{ {
@ -211,18 +224,13 @@ public class BRouterService extends Service
worker.nogoList = new ArrayList<OsmNodeNamed>( cor.nogopoints ); worker.nogoList = new ArrayList<OsmNodeNamed>( cor.nogopoints );
worker.nogoPolygonsList = new ArrayList<OsmNodeNamed>(); worker.nogoPolygonsList = new ArrayList<OsmNodeNamed>();
} }
else if (deviceLevel >= android.os.Build.VERSION_CODES.Q) { else {
CoordinateReader cor = new CoordinateReaderInternal( baseDir ); CoordinateReader cor = new CoordinateReaderInternal( baseDir );
cor.readFromTo(); cor.readFromTo();
worker.nogoList = new ArrayList<OsmNodeNamed>( cor.nogopoints ); worker.nogoList = new ArrayList<OsmNodeNamed>( cor.nogopoints );
worker.nogoPolygonsList = new ArrayList<OsmNodeNamed>(); worker.nogoPolygonsList = new ArrayList<OsmNodeNamed>();
} }
else
{
worker.nogoList = new ArrayList<OsmNodeNamed>();
worker.nogoPolygonsList = new ArrayList<OsmNodeNamed>();
}
} }

View file

@ -222,20 +222,21 @@ public class BRouterView extends View
waitingForMigration = false; waitingForMigration = false;
} }
int deviceLevel = android.os.Build.VERSION.SDK_INT; int deviceLevel = Build.VERSION.SDK_INT;
int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion; int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
canAccessSdCard = deviceLevel < 23 || targetSdkVersion == 19; canAccessSdCard = true;
if ( canAccessSdCard ) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && !Environment.isExternalStorageLegacy()) {
{ canAccessSdCard = false;
cor = CoordinateReader.obtainValidReader( basedir, segmentDir );
} }
else if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
{ canAccessSdCard = false;
if (deviceLevel >= android.os.Build.VERSION_CODES.Q) { }
cor = new CoordinateReaderInternal(basedir);
} else { if (canAccessSdCard) {
cor = new CoordinateReaderNone(); cor = CoordinateReader.obtainValidReader(basedir, segmentDir);
} }
else {
cor = new CoordinateReaderInternal(basedir);
cor.readFromTo(); cor.readFromTo();
} }