android 11 part 3, split api 10/19

This commit is contained in:
afischerdev 2021-08-31 11:29:05 +02:00
parent ae120c9f5a
commit f6e014f61d
17 changed files with 446 additions and 93 deletions

View file

@ -18,7 +18,8 @@
<activity android:name=".BRouterActivity"
android:label="@string/app_name"
android:exported="true"
android:screenOrientation="unspecified" android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
android:screenOrientation="unspecified"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
@ -38,7 +39,7 @@
android:process=":brouter_service" />
<service android:name="btools.routingapp.DownloadService"
android:label="Download Service"
android:icon="@drawable/icon"
android:icon="@mipmap/ic_launcher"
android:enabled="true"
/>

View file

@ -16,9 +16,10 @@ import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.os.StatFs;
import android.util.Log;
public class BInstallerActivity extends Activity implements OnInitListener {
public class BInstallerActivity extends BInstallerMainActivity {
public static final String DOWNLOAD_ACTION = "btools.routingapp.download";
@ -101,11 +102,6 @@ public class BInstallerActivity extends Activity implements OnInitListener {
System.exit(0);
}
@Override
public void onInit(int i)
{
}
@Override
@SuppressWarnings("deprecation")
protected Dialog onCreateDialog( int id )
@ -154,4 +150,5 @@ public class BInstallerActivity extends Activity implements OnInitListener {
showDialog( id );
}
}

View file

@ -269,8 +269,9 @@ public class BInstallerView extends View
availableSize = -1;
try
{
StatFs stat = new StatFs(baseDir.getAbsolutePath ());
availableSize = (long)stat.getAvailableBlocksLong()*stat.getBlockSizeLong();
availableSize = (long)((BInstallerActivity)getContext()).getAvailableSpace(baseDir.getAbsolutePath ());
//StatFs stat = new StatFs(baseDir.getAbsolutePath ());
//availableSize = (long)stat.getAvailableBlocksLong()*stat.getBlockSizeLong();
}
catch (Exception e) { /* ignore */ }
}

View file

@ -1,8 +1,10 @@
package btools.routingapp;
import java.io.File;
import java.lang.reflect.Method;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@ -30,12 +32,11 @@ import android.util.Log;
import android.view.KeyEvent;
import android.widget.EditText;
import androidx.core.app.ActivityCompat;
import btools.router.OsmNodeNamed;
public class BRouterActivity extends Activity implements OnInitListener, ActivityCompat.OnRequestPermissionsResultCallback
{
public class BRouterActivity extends BRouterMainActivity {
private static final int DIALOG_SELECTPROFILE_ID = 1;
private static final int DIALOG_EXCEPTION_ID = 2;
private static final int DIALOG_SHOW_DM_INFO_ID = 3;
@ -138,7 +139,7 @@ public class BRouterActivity extends Activity implements OnInitListener, Activit
{
Intent intent = new Intent( BRouterActivity.this, BInstallerActivity.class );
startActivity( intent );
finish();
// finish();
}
} ).setNegativeButton( "Cancel", new DialogInterface.OnClickListener()
{
@ -666,19 +667,4 @@ public class BRouterActivity extends Activity implements OnInitListener, Activit
mWakeLock.release();
}
@Override
public void onInit( int i )
{
}
@Override
public void onRequestPermissionsResult (int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == 0) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mBRouterView.startSetup(null, true);
} else {
mBRouterView.init();
}
}
}
}

View file

@ -11,6 +11,7 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.zip.GZIPOutputStream;
@ -102,9 +103,9 @@ public class BRouterService extends Service
try
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write( "z64".getBytes(StandardCharsets.UTF_8) ); // marker prefix
baos.write( "z64".getBytes(Charset.forName("UTF-8")) ); // marker prefix
OutputStream os = new GZIPOutputStream( baos );
byte[] ab = gpxMessage.getBytes(StandardCharsets.UTF_8);
byte[] ab = gpxMessage.getBytes(Charset.forName("UTF-8")); //StandardCharsets.UTF_8
gpxMessage = null;
os.write( ab );
ab = null;

View file

@ -37,9 +37,6 @@ import android.util.Log;
import android.view.View;
import android.widget.Toast;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.os.EnvironmentCompat;
import btools.expressions.BExpressionContextWay;
import btools.expressions.BExpressionMetaData;
@ -131,7 +128,7 @@ public class BRouterView extends View
if (brd.getAbsolutePath().contains("/Android/data/")) {
String message = "(previous basedir " + baseDir + " has to migrate )" ;
( (BRouterActivity) getContext() ).selectBasedir( getStorageDirectories(), guessBaseDir(), message );
( (BRouterActivity) getContext() ).selectBasedir( ( (BRouterActivity) getContext() ).getStorageDirectories(), guessBaseDir(), message );
waitingForSelection = true;
waitingForMigration = true;
oldMigrationPath = brd.getAbsolutePath();
@ -145,7 +142,7 @@ public class BRouterView extends View
String message = baseDir == null ? "(no basedir configured previously)" : "(previous basedir " + baseDir
+ ( bdValid ? " does not contain 'brouter' subfolder)" : " is not valid)" );
( (BRouterActivity) getContext() ).selectBasedir( getStorageDirectories(), guessBaseDir(), message );
( (BRouterActivity) getContext() ).selectBasedir( ( (BRouterActivity) getContext() ).getStorageDirectories(), guessBaseDir(), message );
waitingForSelection = true;
}
catch (Exception e)
@ -179,11 +176,11 @@ public class BRouterView extends View
}
if ( !td.isDirectory() ) {
if (ContextCompat.checkSelfPermission (getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
if ( ( (BRouterActivity) getContext() ).checkSelfPermission (getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// if (ContextCompat.checkSelfPermission (getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
retryBaseDir = baseDir;
ActivityCompat.requestPermissions ((BRouterActivity) getContext(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
} else {
( (BRouterActivity) getContext() ).selectBasedir( getStorageDirectories (), guessBaseDir(), "Cannot access " + baseDir.getAbsolutePath () + "; select another");
( (BRouterActivity) getContext() ).selectBasedir( ( (BRouterActivity) getContext() ).getStorageDirectories (), guessBaseDir(), "Cannot access " + baseDir.getAbsolutePath () + "; select another");
}
return;
}
@ -1175,23 +1172,4 @@ public class BRouterView extends View
}
}
private ArrayList<File> getStorageDirectories () {
ArrayList<File> list = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
list = new ArrayList<File>(Arrays.asList(getContext().getExternalMediaDirs()));
} else {
list = new ArrayList<File>(Arrays.asList(getContext().getExternalFilesDirs(null)));
}
ArrayList<File> res = new ArrayList<File>();
for (File f : list) {
if (f != null) {
if (EnvironmentCompat.getStorageState(f).equals(Environment.MEDIA_MOUNTED))
res.add (f);
}
}
// res.add(getContext().getFilesDir());
return res;
}
}

View file

@ -409,7 +409,7 @@ public class BRouterWorker
private List<OsmNodeNamed> readPoisList(Bundle params )
{
// lon,lat,name|...
String pois = params.getString( "pois", null );
String pois = params.getString( "pois" );
if ( pois == null ) return null;
String[] lonLatNameList = pois.split("\\|");

View file

@ -4,6 +4,7 @@ import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.net.TrafficStats;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
@ -88,8 +89,9 @@ public class DownloadService extends Service implements ProgressListener {
availableSize = -1;
try
{
StatFs stat = new StatFs(baseDir);
availableSize = (long)stat.getAvailableBlocksLong()*stat.getBlockSizeLong();
availableSize = BInstallerActivity.getAvailableSpace(baseDir);
//StatFs stat = new StatFs(baseDir);
//availableSize = (long)stat.getAvailableBlocksLong()*stat.getBlockSizeLong();
}
catch (Exception e) { /* ignore */ }
@ -228,7 +230,9 @@ public class DownloadService extends Service implements ProgressListener {
{
try
{
TrafficStats.setThreadStatsTag(1);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
TrafficStats.setThreadStatsTag(1);
}
int slidx = surl.lastIndexOf( "segments4/" );
String name = surl.substring( slidx+10 );
@ -249,6 +253,7 @@ public class DownloadService extends Service implements ProgressListener {
URL urlDelta = new URL(surlDelta);
connection = (HttpURLConnection) urlDelta.openConnection();
connection.setConnectTimeout(5000);
connection.connect();
// 404 kind of expected here, means there's no delta file
@ -262,11 +267,12 @@ public class DownloadService extends Service implements ProgressListener {
if ( connection == null )
{
updateProgress( "Connecting.." + surl );
updateProgress( "Connecting.." + name );
delta = false;
URL url = new URL(surl);
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.connect();
}
@ -333,7 +339,7 @@ public class DownloadService extends Service implements ProgressListener {
tmp_file = new File( fname + "_tmp" );
Rd5DiffTool.recoverFromDelta( fname, diffFile, tmp_file, this );
diffFile.delete();
}
}
if (isCanceled())
{
return "Canceled!";
@ -348,7 +354,7 @@ public class DownloadService extends Service implements ProgressListener {
}
return check_result;
}
if (fname.exists()) fname.delete();
if ( !tmp_file.renameTo( fname ) )
{
return "Could not rename to " + fname.getAbsolutePath();
@ -429,12 +435,15 @@ public class DownloadService extends Service implements ProgressListener {
{
try
{
TrafficStats.setThreadStatsTag(1);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
TrafficStats.setThreadStatsTag(1);
}
if ( connection == null )
{
URL url = new URL(surl);
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.connect();
}
// expect HTTP 200 OK, so we don't mistakenly save error report

View file

@ -1,135 +0,0 @@
package btools.routingapp;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.AudioAttributes;
import android.os.Build;
import android.util.Log;
import androidx.core.app.NotificationCompat;
import static android.content.Context.NOTIFICATION_SERVICE;
public class NotificationHelper {
private static final boolean DEBUG = false;
public static String BRouterNotificationChannel1 = "brouter_channel_01";
private Context mContext;
private int NOTIFICATION_ID = 111;
private Notification mNotification;
private NotificationManager mNotificationManager;
private PendingIntent mContentIntent;
private CharSequence mContentTitle;
public NotificationHelper(Context context)
{
if (DEBUG) Log.d("NH", "init " );
mContext = context;
createNotificationChannels();
}
public void startNotification(Service service) {
if (DEBUG) Log.d("NH", "startNotification " );
mNotification = createNotification("BRouter Download", "Download some files");
if (service != null) service.startForeground(NOTIFICATION_ID, mNotification);
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
}
public void progressUpdate(String text) {
mNotification = createNotification("BRouter Download", text);
mNotification.flags = Notification.FLAG_NO_CLEAR |
Notification.FLAG_ONGOING_EVENT;
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
}
public Notification createNotification(String title, String desc) {
Intent resultIntent = new Intent(mContext, BInstallerActivity.class);
Intent notificationIntent = new Intent();
mContentIntent = PendingIntent.getActivity(mContext, 0, resultIntent, PendingIntent.FLAG_IMMUTABLE);
mNotificationManager = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
final NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, BRouterNotificationChannel1);
builder.setSmallIcon(android.R.drawable.stat_sys_download)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentTitle(title)
.setContentText(desc)
.setTicker(desc)
.setOngoing(true)
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setContentIntent(mContentIntent);
return builder.build();
} else {
final NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);
builder.setSmallIcon(android.R.drawable.stat_sys_download)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentTitle(title)
.setContentText(desc)
.setOnlyAlertOnce(true)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setContentIntent(mContentIntent);
return builder.build();
}
}
/**
* create notification channels
*/
public void createNotificationChannels() {
if (DEBUG) Log.d("NH", "createNotificationChannels " );
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager sNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
// Sound channel
CharSequence name = "BRouter Download";
// The user-visible description of the channel.
String description = "BRouter Download Channel"; //getString(R.string.channel_description);
NotificationChannel channel = new NotificationChannel(BRouterNotificationChannel1, name, NotificationManager.IMPORTANCE_LOW);
channel.setDescription(description);
AudioAttributes att = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_UNKNOWN)
.setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
.build();
channel.setSound(null, null);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
sNotificationManager.createNotificationChannel(channel);
}
}
public void stopNotification() {
if (DEBUG) Log.d("NH", "stopNotification " );
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mNotificationManager.deleteNotificationChannel(BRouterNotificationChannel1);
}
mNotificationManager.cancel(NOTIFICATION_ID);
}
}