diff --git a/android/qt6/AndroidManifest.xml b/android/qt6/AndroidManifest.xml new file mode 100644 index 000000000..e7bd96b12 --- /dev/null +++ b/android/qt6/AndroidManifest.xml @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/android/qt6/res/mipmap-anydpi-v26/ic_launcher.xml b/android/qt6/res/mipmap-anydpi-v26/ic_launcher.xml new file mode 100644 index 000000000..fcd3a1e20 --- /dev/null +++ b/android/qt6/res/mipmap-anydpi-v26/ic_launcher.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/android/qt6/res/mipmap-hdpi/ic_launcher.png b/android/qt6/res/mipmap-hdpi/ic_launcher.png new file mode 100644 index 000000000..807ed1db6 Binary files /dev/null and b/android/qt6/res/mipmap-hdpi/ic_launcher.png differ diff --git a/android/qt6/res/mipmap-hdpi/ic_launcher_background.png b/android/qt6/res/mipmap-hdpi/ic_launcher_background.png new file mode 100644 index 000000000..19669488f Binary files /dev/null and b/android/qt6/res/mipmap-hdpi/ic_launcher_background.png differ diff --git a/android/qt6/res/mipmap-hdpi/ic_launcher_foreground.png b/android/qt6/res/mipmap-hdpi/ic_launcher_foreground.png new file mode 100644 index 000000000..a5ed8a85d Binary files /dev/null and b/android/qt6/res/mipmap-hdpi/ic_launcher_foreground.png differ diff --git a/android/qt6/res/mipmap-mdpi/ic_launcher.png b/android/qt6/res/mipmap-mdpi/ic_launcher.png new file mode 100644 index 000000000..20a05fa43 Binary files /dev/null and b/android/qt6/res/mipmap-mdpi/ic_launcher.png differ diff --git a/android/qt6/res/mipmap-mdpi/ic_launcher_background.png b/android/qt6/res/mipmap-mdpi/ic_launcher_background.png new file mode 100644 index 000000000..75025cfd5 Binary files /dev/null and b/android/qt6/res/mipmap-mdpi/ic_launcher_background.png differ diff --git a/android/qt6/res/mipmap-mdpi/ic_launcher_foreground.png b/android/qt6/res/mipmap-mdpi/ic_launcher_foreground.png new file mode 100644 index 000000000..156a55fab Binary files /dev/null and b/android/qt6/res/mipmap-mdpi/ic_launcher_foreground.png differ diff --git a/android/qt6/res/mipmap-xhdpi/ic_launcher.png b/android/qt6/res/mipmap-xhdpi/ic_launcher.png new file mode 100644 index 000000000..019aa2bef Binary files /dev/null and b/android/qt6/res/mipmap-xhdpi/ic_launcher.png differ diff --git a/android/qt6/res/mipmap-xhdpi/ic_launcher_background.png b/android/qt6/res/mipmap-xhdpi/ic_launcher_background.png new file mode 100644 index 000000000..9784f16c8 Binary files /dev/null and b/android/qt6/res/mipmap-xhdpi/ic_launcher_background.png differ diff --git a/android/qt6/res/mipmap-xhdpi/ic_launcher_foreground.png b/android/qt6/res/mipmap-xhdpi/ic_launcher_foreground.png new file mode 100644 index 000000000..21c2582d2 Binary files /dev/null and b/android/qt6/res/mipmap-xhdpi/ic_launcher_foreground.png differ diff --git a/android/qt6/res/mipmap-xxhdpi/ic_launcher.png b/android/qt6/res/mipmap-xxhdpi/ic_launcher.png new file mode 100644 index 000000000..83a848c36 Binary files /dev/null and b/android/qt6/res/mipmap-xxhdpi/ic_launcher.png differ diff --git a/android/qt6/res/mipmap-xxhdpi/ic_launcher_background.png b/android/qt6/res/mipmap-xxhdpi/ic_launcher_background.png new file mode 100644 index 000000000..04ef206c8 Binary files /dev/null and b/android/qt6/res/mipmap-xxhdpi/ic_launcher_background.png differ diff --git a/android/qt6/res/mipmap-xxhdpi/ic_launcher_foreground.png b/android/qt6/res/mipmap-xxhdpi/ic_launcher_foreground.png new file mode 100644 index 000000000..19f3c22c8 Binary files /dev/null and b/android/qt6/res/mipmap-xxhdpi/ic_launcher_foreground.png differ diff --git a/android/qt6/res/mipmap-xxxhdpi/ic_launcher.png b/android/qt6/res/mipmap-xxxhdpi/ic_launcher.png new file mode 100644 index 000000000..fbdf9144a Binary files /dev/null and b/android/qt6/res/mipmap-xxxhdpi/ic_launcher.png differ diff --git a/android/qt6/res/mipmap-xxxhdpi/ic_launcher_background.png b/android/qt6/res/mipmap-xxxhdpi/ic_launcher_background.png new file mode 100644 index 000000000..66a5487a2 Binary files /dev/null and b/android/qt6/res/mipmap-xxxhdpi/ic_launcher_background.png differ diff --git a/android/qt6/res/mipmap-xxxhdpi/ic_launcher_foreground.png b/android/qt6/res/mipmap-xxxhdpi/ic_launcher_foreground.png new file mode 100644 index 000000000..8c7090469 Binary files /dev/null and b/android/qt6/res/mipmap-xxxhdpi/ic_launcher_foreground.png differ diff --git a/android/qt6/src/org/sdrangel/SDRangelActivity.java b/android/qt6/src/org/sdrangel/SDRangelActivity.java new file mode 100644 index 000000000..7cf9074ed --- /dev/null +++ b/android/qt6/src/org/sdrangel/SDRangelActivity.java @@ -0,0 +1,338 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2022 Jon Beniston, M7RCE // +// // +// This program is free software; you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation as version 3 of the License, or // +// (at your option) any later version. // +// // +// This program is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY; without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// + +package org.sdrangel; + +import org.qtproject.qt.android.bindings.QtActivity; + +import android.hardware.usb.*; +import android.app.*; +import android.content.*; +import android.os.*; +import android.os.PowerManager.WakeLock; +import android.view.WindowManager; +import android.util.Log; + +import java.util.HashMap; +import java.util.Vector; +import java.util.Iterator; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +public class SDRangelActivity extends QtActivity +{ + private String TAG = "sdrangel"; + + private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION"; + private static final long USB_PERMISSION_TIMEOUT_SECONDS = 30; + private PendingIntent m_permissionIntent; + private UsbManager m_manager; + private HandlerThread m_usbReceiverThread; + private Handler m_usbReceiverHandler; + + private HashMap m_connectedDevices; // fd to connection + private HashMap m_devicesBySerial; + + private final Object m_lock = new Object(); + + CountDownLatch m_doneSignal; + boolean m_permissionGranted; + int m_fd; + + private WakeLock m_wakeLock; + + public SDRangelActivity() + { + super(); + Log.e(TAG, "SDRangelActivity"); + m_connectedDevices = new HashMap(); + m_devicesBySerial = new HashMap(); + } + + @Override + public void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + try { + m_manager = (UsbManager) getSystemService(Context.USB_SERVICE); + Intent permissionIntent = new Intent(ACTION_USB_PERMISSION); + permissionIntent.setPackage(getPackageName()); + m_permissionIntent = PendingIntent.getBroadcast(this, 0, permissionIntent, PendingIntent.FLAG_MUTABLE); + m_usbReceiverThread = new HandlerThread("SDRangelUsbReceiver"); + m_usbReceiverThread.start(); + m_usbReceiverHandler = new Handler(m_usbReceiverThread.getLooper()); + + IntentFilter permissionFilter = new IntentFilter(ACTION_USB_PERMISSION); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + registerReceiver(usbManagerBroadcastReceiver, permissionFilter, null, m_usbReceiverHandler, Context.RECEIVER_NOT_EXPORTED); + } else { + registerReceiver(usbManagerBroadcastReceiver, permissionFilter, null, m_usbReceiverHandler); + } + } catch (Exception e) { + Log.e(TAG, "SDRangelActivity::onCreate: Exception " + e.toString()); + } + } + + @Override + public void onDestroy() + { + try { + unregisterReceiver(usbManagerBroadcastReceiver); + } catch (Exception e) { + Log.d(TAG, "SDRangelActivity::onDestroy: unregisterReceiver: " + e.toString()); + } + if (m_usbReceiverThread != null) { + m_usbReceiverThread.quitSafely(); + m_usbReceiverThread = null; + m_usbReceiverHandler = null; + } + super.onDestroy(); + } + + // Prevent app from being put too sleep (which can stop acquisition) + public void acquireWakeLock() + { + if (m_wakeLock == null) + { + PowerManager powerManager = (PowerManager)getSystemService(POWER_SERVICE); + m_wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SDRangel::WakeLockTag"); + } + Log.e(TAG, "Acquiring wake lock"); + m_wakeLock.acquire(); + } + + public void releaseWakeLock() + { + if (m_wakeLock != null) + { + Log.e(TAG, "Releasing wake lock"); + m_wakeLock.release(); + } + } + + public void acquireScreenLock() + { + Log.e(TAG, "Acquiring screen lock"); + runOnUiThread(new Runnable() { + @Override + public void run() { + getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + } + }); + + } + + public void releaseScreenLock() + { + Log.e(TAG, "Releasing screen lock"); + runOnUiThread(new Runnable() { + @Override + public void run() { + getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + } + }); + } + + public String[] listUSBDeviceSerials(int vid, int pid) + { + Vector serials = new Vector(); + try + { + synchronized(m_lock) + { + HashMap deviceList = m_manager.getDeviceList(); + Iterator deviceIterator = deviceList.values().iterator(); + + while(deviceIterator.hasNext()) + { + UsbDevice device = deviceIterator.next(); + if ((device.getVendorId() == vid) && (device.getProductId() == pid)) + { + // From Android Q, we need permission to read serial + if (requestUSBPermission(device)) + { + String serial = device.getSerialNumber(); + if (serial != null) + { + Log.d(TAG, "Found a device: ID=" + device.getDeviceId() + " Name=" + device.getDeviceName() + " Serial=" + serial); + m_devicesBySerial.put(serial, device); + serials.add(serial); + } + else + { + Log.d(TAG, "Found a device with no serial: ID=" + device.getDeviceId() + " Name=" + device.getDeviceName()); + } + } + } + } + } + } + catch (Exception e) + { + Log.e(TAG, "SDRangelActivity::listUSBDeviceSerials: Exception " + e.toString()); + } + return serials.toArray(new String[serials.size()]); + } + + public int openUSBDevice(String serial) + { + Log.e(TAG, "SDRangelActivity::openUSBDevice serial=" + serial); + try + { + synchronized(m_lock) + { + m_fd = -1; + + UsbDevice device = m_devicesBySerial.get(serial); + if (device != null) + { + if (requestUSBPermission(device)) + { + String deviceSerial = device.getSerialNumber(); + if ((deviceSerial != null) && (deviceSerial.compareToIgnoreCase(serial) == 0)) + { + m_fd = connectToUSBDevice(device); + return m_fd; + } + } + } + + HashMap deviceList = m_manager.getDeviceList(); + Iterator deviceIterator = deviceList.values().iterator(); + + while(deviceIterator.hasNext()) + { + device = deviceIterator.next(); + if (m_manager.hasPermission(device)) + { + String deviceSerial = device.getSerialNumber(); + if ((deviceSerial != null) && (deviceSerial.compareToIgnoreCase(serial) == 0)) + { + m_fd = connectToUSBDevice(device); + break; + } + } + } + + return m_fd; + } + } + catch (Exception e) + { + Log.e(TAG, "SDRangelActivity::openUSBDevice: Exception " + e.toString()); + return -1; + } + } + + public void closeUSBDevice(int fd) + { + Log.e(TAG, "SDRangelActivity::closeUSBDevice: fd=" + fd); + UsbDeviceConnection connection = m_connectedDevices.get(fd); + if (connection != null) + { + connection.close(); + m_connectedDevices.remove(fd); + } + } + + private boolean requestUSBPermission(UsbDevice device) throws InterruptedException + { + if (m_manager == null) + { + Log.e(TAG, "SDRangelActivity::requestUSBPermission: UsbManager is not available"); + return false; + } + if (m_manager.hasPermission(device)) { + return true; + } + if (m_permissionIntent == null) + { + Log.e(TAG, "SDRangelActivity::requestUSBPermission: permission intent is not available"); + return false; + } + + m_permissionGranted = false; + m_doneSignal = new CountDownLatch(1); + m_manager.requestPermission(device, m_permissionIntent); + if (!m_doneSignal.await(USB_PERMISSION_TIMEOUT_SECONDS, TimeUnit.SECONDS)) + { + Log.e(TAG, "SDRangelActivity::requestUSBPermission: timed out waiting for permission for device " + device); + return false; + } + + return m_permissionGranted; + } + + private int connectToUSBDevice(UsbDevice device) + { + UsbDeviceConnection connection = m_manager.openDevice(device); + if (connection != null) + { + // Can disconnect kernel driver with: + //connection.claimInterface(device.getInterface(0), true); + // However, this results in airspyhf.c:libusb_set_configuration returning BUSY + + int fd = connection.getFileDescriptor(); + if (fd >= 0) + { + m_connectedDevices.put(fd, connection); + return fd; + } + connection.close(); + } + return -1; + } + + private final BroadcastReceiver usbManagerBroadcastReceiver = new BroadcastReceiver() + { + public void onReceive(Context context, Intent intent) + { + try + { + String action = intent.getAction(); + Log.d(TAG, "INTENT ACTION: " + action); + if (ACTION_USB_PERMISSION.equals(action)) + { + synchronized (this) + { + UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); + if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) + { + if(device != null) + { + m_permissionGranted = true; + Log.d(TAG, "Granted permission for device " + device.getDeviceId()); + } + } + else + { + Log.d(TAG, "Permission denied for device " + device); + } + if (m_doneSignal != null) { + m_doneSignal.countDown(); + } + } + } + } + catch(Exception e) + { + Log.d(TAG, "Exception: " + e); + } + } + }; + +}