Increase minSdkVersion to 14 and merge implementations

AndroidX needs at least API level 14 (Ice Cream Sandwich) which was
released 10 years ago and should not exclude many devices. Having a
merged tree simplifies the development.
This commit is contained in:
Manuel Fuhr 2021-10-21 15:56:47 +02:00
parent cf4a188e40
commit 0e04c1a849
9 changed files with 78 additions and 368 deletions

View file

@ -12,6 +12,7 @@ import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
@ -19,7 +20,7 @@ import android.speech.tts.TextToSpeech.OnInitListener;
import android.os.StatFs;
import android.util.Log;
public class BInstallerActivity extends BInstallerMainActivity {
public class BInstallerActivity extends Activity {
public static final String DOWNLOAD_ACTION = "btools.routingapp.download";
@ -151,4 +152,14 @@ public class BInstallerActivity extends BInstallerMainActivity {
}
static public long getAvailableSpace (String baseDir) {
StatFs stat = new StatFs(baseDir);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
return stat.getAvailableBlocksLong()*stat.getBlockSizeLong();
}
else {
return stat.getAvailableBlocks()*stat.getBlockSize();
}
}
}

View file

@ -33,9 +33,12 @@ import android.view.KeyEvent;
import android.widget.EditText;
import androidx.core.app.ActivityCompat;
import androidx.core.os.EnvironmentCompat;
import btools.router.OsmNodeNamed;
public class BRouterActivity extends BRouterMainActivity {
public class BRouterActivity extends Activity {
private static final int DIALOG_SELECTPROFILE_ID = 1;
private static final int DIALOG_EXCEPTION_ID = 2;
@ -54,7 +57,7 @@ public class BRouterActivity extends BRouterMainActivity {
private static final int DIALOG_SHOW_WP_SCANRESULT_ID = 15;
private static final int DIALOG_SHOW_REPEAT_TIMEOUT_HELP_ID = 16;
private static final int DIALOG_SHOW_API23_HELP_ID = 17;
private BRouterView mBRouterView;
private PowerManager mPowerManager;
@ -667,4 +670,62 @@ public class BRouterActivity extends BRouterMainActivity {
mWakeLock.release();
}
public boolean checkSelfPermission (Context context, String perm ) {
boolean b = checkSelfPermission(context, perm);
if (b) {
ActivityCompat.requestPermissions (this, new String[]{perm}, 0);
}
return b;
}
private String getStorageState(File f) {
return EnvironmentCompat.getStorageState(f); //Environment.MEDIA_MOUNTED
}
public ArrayList<File> getStorageDirectories () {
ArrayList<File> list = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
list = new ArrayList<File>(Arrays.asList(getExternalMediaDirs()));
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
list = new ArrayList<File>(Arrays.asList(getExternalFilesDirs(null)));
}
ArrayList<File> res = new ArrayList<File>();
for (File f : list) {
if (f != null) {
if (getStorageState(f).equals(Environment.MEDIA_MOUNTED))
res.add (f);
}
}
if (checkExternalStorageWritable()) {
res.add(Environment.getExternalStorageDirectory());
}
return res;
}
private boolean checkExternalStorageWritable() {
boolean isWritable = false;
try {
File sd = Environment.getExternalStorageDirectory();
File testDir = new File( sd, "brouter" );
boolean didExist = testDir.isDirectory();
if ( !didExist )
{
testDir.mkdir();
}
File testFile = new File( testDir, "test" + System.currentTimeMillis() );
testFile.createNewFile();
if ( testFile.exists() ) {
testFile.delete();
isWritable = true;
}
}
catch (Throwable t) {
// ignore
}
return isWritable;
}
}

View file

@ -0,0 +1,135 @@
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);
}
}