2017-04-27 18:24:53 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2014-03-26 22:08:57 +00:00
|
|
|
/* implements Android specific functions */
|
|
|
|
#include "dive.h"
|
2022-03-12 09:44:37 +00:00
|
|
|
#include "device.h"
|
|
|
|
#include "libdivecomputer.h"
|
2019-08-05 18:07:10 +00:00
|
|
|
#include "file.h"
|
2018-02-28 22:37:09 +00:00
|
|
|
#include "qthelper.h"
|
2014-03-26 22:08:57 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <fcntl.h>
|
2015-08-20 22:19:40 +00:00
|
|
|
#include <errno.h>
|
2017-07-21 13:33:07 +00:00
|
|
|
#include <unistd.h>
|
2019-08-05 18:07:10 +00:00
|
|
|
#include <zip.h>
|
2023-04-29 09:43:17 +00:00
|
|
|
#include <string>
|
2014-03-26 22:08:57 +00:00
|
|
|
|
|
|
|
#include <QtAndroidExtras/QtAndroidExtras>
|
|
|
|
#include <QtAndroidExtras/QAndroidJniObject>
|
2015-08-20 22:19:39 +00:00
|
|
|
#include <QtAndroid>
|
2018-07-01 00:32:26 +00:00
|
|
|
#include <QDebug>
|
2020-03-15 00:57:36 +00:00
|
|
|
#include <core/serial_usb_android.h>
|
2015-08-20 22:19:39 +00:00
|
|
|
|
2018-06-30 20:40:14 +00:00
|
|
|
#if defined(SUBSURFACE_MOBILE)
|
|
|
|
#include "mobile-widgets/qmlmanager.h"
|
2018-07-01 00:32:26 +00:00
|
|
|
#define LOG(x) QMLManager::instance()->appendTextToLog(x);
|
|
|
|
#else
|
|
|
|
#define LOG(x) qDebug() << x;
|
2018-06-30 20:40:14 +00:00
|
|
|
#endif
|
|
|
|
|
2015-08-20 22:19:39 +00:00
|
|
|
#define USB_SERVICE "usb"
|
2014-03-26 22:08:57 +00:00
|
|
|
|
2023-04-29 09:43:17 +00:00
|
|
|
static std::string system_default_path()
|
|
|
|
{
|
|
|
|
// Qt appears to find a working path for us - let's just go with that
|
|
|
|
// AppDataLocation allows potential sharing of the files we put there
|
|
|
|
return QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).first().toStdString();
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string make_default_filename()
|
|
|
|
{
|
|
|
|
return system_default_path() + "/subsurface.xml";
|
|
|
|
}
|
|
|
|
|
2014-08-24 21:43:42 +00:00
|
|
|
const char android_system_divelist_default_font[] = "Roboto";
|
|
|
|
const char *system_divelist_default_font = android_system_divelist_default_font;
|
2015-11-06 21:04:41 +00:00
|
|
|
double system_divelist_default_font_size = -1;
|
2014-03-26 22:08:57 +00:00
|
|
|
|
2015-08-20 22:19:40 +00:00
|
|
|
int get_usb_fd(uint16_t idVendor, uint16_t idProduct);
|
2024-05-04 16:53:41 +00:00
|
|
|
void subsurface_OS_pref_setup()
|
2014-08-24 21:43:42 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-08-27 13:00:10 +00:00
|
|
|
bool subsurface_ignore_font(const char *font)
|
|
|
|
{
|
|
|
|
// there are no old default fonts that we would want to ignore
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-05-04 16:53:41 +00:00
|
|
|
const char *system_default_directory()
|
2015-10-11 16:55:04 +00:00
|
|
|
{
|
2023-04-29 09:43:17 +00:00
|
|
|
static const std::string path = system_default_path();
|
|
|
|
return path.c_str();
|
2015-10-11 16:55:04 +00:00
|
|
|
}
|
|
|
|
|
2024-05-04 16:53:41 +00:00
|
|
|
const char *system_default_filename()
|
2015-10-11 16:55:04 +00:00
|
|
|
{
|
2023-04-29 09:43:17 +00:00
|
|
|
static const std::string fn = make_default_filename();
|
|
|
|
return fn.c_str();
|
2014-03-26 22:08:57 +00:00
|
|
|
}
|
|
|
|
|
2023-04-29 09:43:17 +00:00
|
|
|
|
2018-08-27 17:32:14 +00:00
|
|
|
int enumerate_devices(device_callback_t callback, void *userdata, unsigned int transport)
|
2014-03-26 22:08:57 +00:00
|
|
|
{
|
|
|
|
/* FIXME: we need to enumerate in some other way on android */
|
|
|
|
/* qtserialport maybee? */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-08-20 22:19:39 +00:00
|
|
|
/**
|
2015-08-20 22:19:40 +00:00
|
|
|
* Get the file descriptor of first available matching device attached to usb in android.
|
2015-08-20 22:19:39 +00:00
|
|
|
*
|
2015-08-20 22:19:40 +00:00
|
|
|
* returns a fd to the device, or -1 and errno is set.
|
|
|
|
*/
|
|
|
|
int get_usb_fd(uint16_t idVendor, uint16_t idProduct)
|
2015-08-20 22:19:39 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
jint fd, vendorid, productid;
|
|
|
|
QAndroidJniObject usbName, usbDevice;
|
|
|
|
|
|
|
|
// Get the current main activity of the application.
|
|
|
|
QAndroidJniObject activity = QtAndroid::androidActivity();
|
|
|
|
|
|
|
|
QAndroidJniObject usb_service = QAndroidJniObject::fromString(USB_SERVICE);
|
|
|
|
|
|
|
|
// Get UsbManager from activity
|
|
|
|
QAndroidJniObject usbManager = activity.callObjectMethod("getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;", usb_service.object());
|
|
|
|
|
|
|
|
// Get a HashMap<Name, UsbDevice> of all USB devices attached to Android
|
|
|
|
QAndroidJniObject deviceMap = usbManager.callObjectMethod("getDeviceList", "()Ljava/util/HashMap;");
|
|
|
|
jint num_devices = deviceMap.callMethod<jint>("size", "()I");
|
|
|
|
if (num_devices == 0) {
|
|
|
|
// No USB device is attached.
|
2018-07-01 00:50:15 +00:00
|
|
|
LOG("usbManager says no devices attached");
|
2015-08-20 22:19:39 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over all the devices and find the first available FTDI device.
|
|
|
|
QAndroidJniObject keySet = deviceMap.callObjectMethod("keySet", "()Ljava/util/Set;");
|
|
|
|
QAndroidJniObject iterator = keySet.callObjectMethod("iterator", "()Ljava/util/Iterator;");
|
|
|
|
|
|
|
|
for (i = 0; i < num_devices; i++) {
|
|
|
|
usbName = iterator.callObjectMethod("next", "()Ljava/lang/Object;");
|
|
|
|
usbDevice = deviceMap.callObjectMethod ("get", "(Ljava/lang/Object;)Ljava/lang/Object;", usbName.object());
|
|
|
|
vendorid = usbDevice.callMethod<jint>("getVendorId", "()I");
|
|
|
|
productid = usbDevice.callMethod<jint>("getProductId", "()I");
|
2018-07-01 00:50:15 +00:00
|
|
|
LOG(QString("Looking at device with VID/PID %1/%2").arg(vendorid).arg(productid));
|
2015-08-20 22:19:40 +00:00
|
|
|
if(vendorid == idVendor && productid == idProduct) // Found the requested device
|
2015-08-20 22:19:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i == num_devices) {
|
2015-08-20 22:19:40 +00:00
|
|
|
// No device found.
|
2018-07-01 00:50:15 +00:00
|
|
|
LOG(QString("Didn't find device matching %1/%2").arg(idVendor).arg(idProduct))
|
2015-08-20 22:19:40 +00:00
|
|
|
errno = ENOENT;
|
|
|
|
return -1;
|
2015-08-20 22:19:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
jboolean hasPermission = usbManager.callMethod<jboolean>("hasPermission", "(Landroid/hardware/usb/UsbDevice;)Z", usbDevice.object());
|
|
|
|
if (!hasPermission) {
|
|
|
|
// You do not have permission to use the usbDevice.
|
|
|
|
// Please remove and reinsert the USB device.
|
|
|
|
// Could also give an dialogbox asking for permission.
|
2018-07-01 00:50:15 +00:00
|
|
|
LOG("usbManager tells us we don't have permission to access this device");
|
2015-08-20 22:19:40 +00:00
|
|
|
errno = EPERM;
|
|
|
|
return -1;
|
2015-08-20 22:19:39 +00:00
|
|
|
}
|
|
|
|
|
2015-08-20 22:19:40 +00:00
|
|
|
// An device is present and we also have permission to use the device.
|
2015-08-20 22:19:39 +00:00
|
|
|
// Open the device and get its file descriptor.
|
|
|
|
QAndroidJniObject usbDeviceConnection = usbManager.callObjectMethod("openDevice", "(Landroid/hardware/usb/UsbDevice;)Landroid/hardware/usb/UsbDeviceConnection;", usbDevice.object());
|
|
|
|
if (usbDeviceConnection.object() == NULL) {
|
|
|
|
// Some error occurred while opening the device. Exit.
|
2018-07-01 00:50:15 +00:00
|
|
|
LOG("usbManager said we had permission to access, but then opening the device failed");
|
2015-08-20 22:19:40 +00:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
2015-08-20 22:19:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finally get the required file descriptor.
|
|
|
|
fd = usbDeviceConnection.callMethod<jint>("getFileDescriptor", "()I");
|
|
|
|
if (fd == -1) {
|
|
|
|
// The device is not opened. Some error.
|
2018-07-01 00:50:15 +00:00
|
|
|
LOG("usbManager said we successfully opened the device, but the fd was -1");
|
2015-08-20 22:19:40 +00:00
|
|
|
errno = ENODEV;
|
|
|
|
return -1;
|
2015-08-20 22:19:39 +00:00
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2018-06-30 20:40:14 +00:00
|
|
|
JNIEXPORT void JNICALL
|
2024-05-04 20:53:05 +00:00
|
|
|
Java_org_subsurfacedivelog_mobile_SubsurfaceMobileActivity_setUsbDevice(JNIEnv *,
|
|
|
|
jobject,
|
2020-03-15 00:57:36 +00:00
|
|
|
jobject javaUsbDevice)
|
2018-06-30 20:40:14 +00:00
|
|
|
{
|
2020-03-15 00:57:36 +00:00
|
|
|
QAndroidJniObject usbDevice(javaUsbDevice);
|
|
|
|
if (usbDevice.isValid()) {
|
|
|
|
android_usb_serial_device_descriptor descriptor = getDescriptor(usbDevice);
|
|
|
|
|
2020-03-15 19:27:10 +00:00
|
|
|
LOG(QString("called by connect intent for device %1").arg(QString::fromStdString(descriptor.uiRepresentation)));
|
2020-03-15 00:57:36 +00:00
|
|
|
}
|
2018-08-07 01:29:27 +00:00
|
|
|
#if defined(SUBSURFACE_MOBILE)
|
2020-03-15 00:57:36 +00:00
|
|
|
QMLManager::instance()->showDownloadPage(usbDevice);
|
2018-08-07 01:29:27 +00:00
|
|
|
#endif
|
2018-06-30 20:40:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-15 19:27:10 +00:00
|
|
|
JNIEXPORT void JNICALL
|
2024-05-04 20:53:05 +00:00
|
|
|
Java_org_subsurfacedivelog_mobile_SubsurfaceMobileActivity_restartDownload(JNIEnv *,
|
|
|
|
jobject,
|
2020-03-15 19:27:10 +00:00
|
|
|
jobject javaUsbDevice)
|
|
|
|
{
|
|
|
|
QAndroidJniObject usbDevice(javaUsbDevice);
|
|
|
|
if (usbDevice.isValid()) {
|
|
|
|
android_usb_serial_device_descriptor descriptor = getDescriptor(usbDevice);
|
|
|
|
|
|
|
|
LOG(QString("called by permission granted intent for device %1").arg(QString::fromStdString(descriptor.uiRepresentation)));
|
|
|
|
}
|
|
|
|
#if defined(SUBSURFACE_MOBILE)
|
|
|
|
QMLManager::instance()->restartDownload(usbDevice);
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-26 22:08:57 +00:00
|
|
|
/* NOP wrappers to comform with windows.c */
|
|
|
|
int subsurface_rename(const char *path, const char *newpath)
|
|
|
|
{
|
|
|
|
return rename(path, newpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
int subsurface_open(const char *path, int oflags, mode_t mode)
|
|
|
|
{
|
|
|
|
return open(path, oflags, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *subsurface_fopen(const char *path, const char *mode)
|
|
|
|
{
|
|
|
|
return fopen(path, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *subsurface_opendir(const char *path)
|
|
|
|
{
|
|
|
|
return (void *)opendir(path);
|
|
|
|
}
|
|
|
|
|
2014-11-21 19:53:31 +00:00
|
|
|
int subsurface_access(const char *path, int mode)
|
|
|
|
{
|
|
|
|
return access(path, mode);
|
|
|
|
}
|
|
|
|
|
2017-02-24 07:06:48 +00:00
|
|
|
int subsurface_stat(const char* path, struct stat* buf)
|
|
|
|
{
|
|
|
|
return stat(path, buf);
|
|
|
|
}
|
|
|
|
|
2014-03-26 22:08:57 +00:00
|
|
|
struct zip *subsurface_zip_open_readonly(const char *path, int flags, int *errorp)
|
|
|
|
{
|
|
|
|
return zip_open(path, flags, errorp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int subsurface_zip_close(struct zip *zip)
|
|
|
|
{
|
|
|
|
return zip_close(zip);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* win32 console */
|
2024-05-04 16:53:41 +00:00
|
|
|
void subsurface_console_init()
|
2014-03-26 22:08:57 +00:00
|
|
|
{
|
|
|
|
/* NOP */
|
|
|
|
}
|
|
|
|
|
2024-05-04 16:53:41 +00:00
|
|
|
void subsurface_console_exit()
|
2014-03-26 22:08:57 +00:00
|
|
|
{
|
|
|
|
/* NOP */
|
|
|
|
}
|
2016-12-28 22:22:33 +00:00
|
|
|
|
|
|
|
bool subsurface_user_is_root()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-09 14:11:24 +00:00
|
|
|
|
|
|
|
/* called from QML manager */
|
|
|
|
void checkPendingIntents()
|
|
|
|
{
|
|
|
|
QAndroidJniObject activity = QtAndroid::androidActivity();
|
2018-08-11 04:43:50 +00:00
|
|
|
if (activity.isValid()) {
|
2018-08-09 14:11:24 +00:00
|
|
|
activity.callMethod<void>("checkPendingIntents");
|
|
|
|
qDebug() << "checkPendingIntents ";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
qDebug() << "checkPendingIntents: Activity not valid";
|
|
|
|
}
|
2019-09-18 20:35:22 +00:00
|
|
|
|
|
|
|
QString getAndroidHWInfo()
|
|
|
|
{
|
|
|
|
return QStringLiteral("%1/%2/%3")
|
|
|
|
.arg(QAndroidJniObject::getStaticObjectField<jstring>("android/os/Build", "MODEL").toString())
|
|
|
|
.arg(QAndroidJniObject::getStaticObjectField<jstring>("android/os/Build", "BRAND").toString())
|
|
|
|
.arg(QAndroidJniObject::getStaticObjectField<jstring>("android/os/Build", "PRODUCT").toString());
|
|
|
|
}
|