altosdroid: Deal with Google requiring API version 31
authorKeith Packard <keithp@keithp.com>
Fri, 28 Apr 2023 05:11:30 +0000 (22:11 -0700)
committerKeith Packard <keithp@keithp.com>
Fri, 28 Apr 2023 05:48:24 +0000 (22:48 -0700)
Need to add some magic constants to the build.
Need to support BLUETOOTH_SCAN permission.

Signed-off-by: Keith Packard <keithp@keithp.com>
altosdroid/app/build.gradle.in
altosdroid/app/src/main/AndroidManifest.xml.in
altosdroid/app/src/main/java/org/altusmetrum/AltosDroid/AltosDroid.java
altosdroid/app/src/main/java/org/altusmetrum/AltosDroid/TelemetryService.java

index dd1a7d97375fa7323c2d4c5686a33863f563db26..e386ad8cf244b2e6bc94e3d68b252635284350fe 100644 (file)
@@ -18,7 +18,7 @@ android {
     defaultConfig {
         applicationId "org.altusmetrum.AltosDroid"
         minSdkVersion 21
-        targetSdkVersion 30
+        targetSdkVersion 31
         versionCode @ANDROID_VERSION@
         versionName "@VERSION@"
     }
index ec84b457fae07a0c8b2f2b2d841e88000508ff64..d723432758cede166dfd9094e2d81d9da3982551 100644 (file)
@@ -29,6 +29,7 @@
     <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
     <uses-permission android:name="android.permission.BLUETOOTH" />
     <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
+    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
     <!-- Permissions needed to save Telemetry logs to SD card -->
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
     <!-- Permissions needed for GoogleMaps -->
@@ -52,7 +53,8 @@
         <activity android:name="org.altusmetrum.AltosDroid.AltosDroid"
                   android:label="@string/app_name"
                   android:configChanges="orientation|keyboardHidden"
-                  android:launchMode="singleTop">
+                  android:launchMode="singleTop"
+                 android:exported="true">
             <intent-filter>
                 <category android:name="android.intent.category.LAUNCHER" />
                 <action android:name="android.intent.action.MAIN" />
index cf54aad059de146b74e9aecb92d33b051cbf88eb..6d70872123954b1a88ba3fc3b897529e9b4da9f8 100644 (file)
@@ -674,7 +674,13 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener,
 
                if (device != null) {
                        Intent          i = new Intent(this, AltosDroid.class);
-                       PendingIntent   pi = PendingIntent.getActivity(this, 0, new Intent("hello world", null, this, AltosDroid.class), 0);
+                       int             flag;
+
+                       if (android.os.Build.VERSION.SDK_INT >= 31) // android.os.Build.VERSION_CODES.S
+                               flag = 33554432; // PendingIntent.FLAG_MUTABLE
+                       else
+                               flag = 0;
+                       PendingIntent   pi = PendingIntent.getActivity(this, 0, new Intent("hello world", null, this, AltosDroid.class), flag);
 
                        if (AltosUsb.request_permission(this, device, pi)) {
                                connectUsb(device);
@@ -795,9 +801,11 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener,
        public boolean have_storage_permission = false;
        public boolean have_bluetooth_permission = false;
        public boolean have_bluetooth_connect_permission = false;
+       public boolean have_bluetooth_scan_permission = false;
        public boolean asked_permission = false;
 
        static final String BLUETOOTH_CONNECT = "android.permission.BLUETOOTH_CONNECT";
+       static final String BLUETOOTH_SCAN = "android.permission.BLUETOOTH_SCAN";
 
        AltosMapOnline map_online;
 
@@ -827,6 +835,9 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener,
                                        if (permissions[i].equals(BLUETOOTH_CONNECT)) {
                                                have_bluetooth_connect_permission = true;
                                        }
+                                       if (permissions[i].equals(BLUETOOTH_SCAN)) {
+                                               have_bluetooth_scan_permission = true;
+                                       }
                                }
                        }
                }
@@ -864,6 +875,12 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener,
                        {
                                have_bluetooth_connect_permission = true;
                        }
+                       if (ActivityCompat.checkSelfPermission(this,
+                                                              BLUETOOTH_SCAN)
+                           == PackageManager.PERMISSION_GRANTED)
+                       {
+                               have_bluetooth_scan_permission = true;
+                       }
                        int count = 0;
                        if (!have_location_permission)
                                count += 1;
@@ -873,6 +890,8 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener,
                                count += 1;
                        if (!have_bluetooth_connect_permission)
                                count += 1;
+                       if (!have_bluetooth_scan_permission)
+                               count += 1;
                        if (count > 0)
                        {
                                String[] permissions = new String[count];
@@ -885,6 +904,8 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener,
                                        permissions[i++] = Manifest.permission.BLUETOOTH;
                                if (!have_bluetooth_connect_permission)
                                        permissions[i++] = BLUETOOTH_CONNECT;
+                               if (!have_bluetooth_scan_permission)
+                                       permissions[i++] = BLUETOOTH_SCAN;
                                ActivityCompat.requestPermissions(this, permissions, MY_PERMISSION_REQUEST);
                        }
                }
index b0b82ab9c9673643f8fead134a8a9e9445a8a1d8..e58906ea425d952fee6804fc5f32846c3ae92788 100644 (file)
@@ -31,6 +31,7 @@ import android.hardware.usb.*;
 import android.content.Intent;
 import android.content.Context;
 import android.os.*;
+import android.os.Build.*;
 import android.widget.Toast;
 import androidx.core.app.NotificationCompat;
 
@@ -662,10 +663,16 @@ public class TelemetryService extends Service implements AltosIdleMonitorListene
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
                AltosDebug.debug("Received start id %d: %s", startId, intent);
+               int             flag;
+
+               if (android.os.Build.VERSION.SDK_INT >= 31) // android.os.Build.VERSION_CODES.S
+                       flag = 33554432; // PendingIntent.FLAG_MUTABLE
+               else
+                       flag = 0;
 
                // The PendingIntent to launch our activity if the user selects this notification
                PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
-                               new Intent(this, AltosDroid.class), 0);
+                               new Intent(this, AltosDroid.class), flag);
 
                String channelId =
                                (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)