2018-06-29 13:51:06 -07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
// Java classes for handling Intents on Android (which in this case means
|
|
|
|
// Subsurface-mobile receiving notifaction that an FTDI dive computer was
|
|
|
|
// plugged in
|
|
|
|
|
|
|
|
// Created while looking at https://github.com/ekke/ekkesSHAREexample
|
|
|
|
|
|
|
|
package org.subsurfacedivelog.mobile;
|
|
|
|
|
|
|
|
import org.qtproject.qt5.android.QtNative;
|
|
|
|
|
|
|
|
import org.qtproject.qt5.android.bindings.QtActivity;
|
|
|
|
import android.os.*;
|
|
|
|
import android.content.*;
|
|
|
|
import android.app.*;
|
|
|
|
|
|
|
|
import java.lang.String;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.hardware.usb.UsbDevice;
|
|
|
|
import android.hardware.usb.UsbManager;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
// this is the main class that will be run at start
|
|
|
|
public class SubsurfaceMobileActivity extends QtActivity
|
|
|
|
{
|
|
|
|
public static boolean isIntentPending;
|
|
|
|
public static boolean isInitialized;
|
|
|
|
private static final String TAG = "subsurfacedivelog.mobile";
|
2020-03-14 17:57:36 -07:00
|
|
|
public static native void setUsbDevice(UsbDevice usbDevice);
|
2020-03-15 12:27:10 -07:00
|
|
|
public static native void restartDownload(UsbDevice usbDevice);
|
2020-03-05 22:38:33 +01:00
|
|
|
private static Context appContext;
|
2018-06-29 13:51:06 -07:00
|
|
|
|
|
|
|
// we need to provide two endpoints:
|
|
|
|
// onNewIntent if we receive an Intent while running
|
|
|
|
// onCreate if we were started by an Intent
|
|
|
|
@Override
|
2020-03-05 22:38:33 +01:00
|
|
|
public void onCreate(Bundle savedInstanceState)
|
|
|
|
{
|
2018-06-29 13:51:06 -07:00
|
|
|
Log.i(TAG + " onCreate", "onCreate SubsurfaceMobileActivity");
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
2020-03-05 22:38:33 +01:00
|
|
|
appContext = getApplicationContext();
|
|
|
|
|
2018-06-29 13:51:06 -07:00
|
|
|
// now we're checking if the App was started from another Android App via Intent
|
|
|
|
Intent theIntent = getIntent();
|
|
|
|
if (theIntent != null) {
|
|
|
|
String theAction = theIntent.getAction();
|
|
|
|
if (theAction != null) {
|
|
|
|
Log.i(TAG + " onCreate", theAction);
|
|
|
|
isIntentPending = true;
|
|
|
|
}
|
|
|
|
}
|
2020-03-14 18:17:05 +01:00
|
|
|
|
|
|
|
// Register the usb permission intent filter.
|
|
|
|
IntentFilter filter = new IntentFilter("org.subsurfacedivelog.mobile.USB_PERMISSION");
|
|
|
|
registerReceiver(usbReceiver, filter);
|
|
|
|
|
2018-06-29 13:51:06 -07:00
|
|
|
} // onCreate
|
|
|
|
|
|
|
|
// if we are opened from other apps:
|
|
|
|
@Override
|
2020-03-05 22:38:33 +01:00
|
|
|
public void onNewIntent(Intent intent)
|
|
|
|
{
|
2018-06-29 13:51:06 -07:00
|
|
|
Log.i(TAG + " onNewIntent", intent.getAction());
|
|
|
|
UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
|
2018-08-10 21:43:50 -07:00
|
|
|
if (device == null) {
|
|
|
|
Log.i(TAG + " onNewIntent", "null device");
|
|
|
|
return;
|
|
|
|
}
|
2018-06-29 13:51:06 -07:00
|
|
|
Log.i(TAG + " onNewIntent toString", device.toString());
|
|
|
|
super.onNewIntent(intent);
|
|
|
|
setIntent(intent);
|
|
|
|
// Intent will be processed, if all is initialized and Qt / QML can handle the event
|
|
|
|
if (isInitialized) {
|
|
|
|
processIntent();
|
|
|
|
} else {
|
|
|
|
isIntentPending = true;
|
|
|
|
}
|
|
|
|
} // onNewIntent
|
|
|
|
|
2020-03-05 22:38:33 +01:00
|
|
|
public void checkPendingIntents()
|
|
|
|
{
|
2018-06-29 13:51:06 -07:00
|
|
|
isInitialized = true;
|
|
|
|
if (isIntentPending) {
|
|
|
|
isIntentPending = false;
|
|
|
|
Log.i(TAG + " checkPendingIntents", "checkPendingIntents: true");
|
|
|
|
processIntent();
|
|
|
|
} else {
|
|
|
|
Log.i(TAG + " checkPendingIntents", "nothingPending");
|
|
|
|
}
|
|
|
|
} // checkPendingIntents
|
|
|
|
|
|
|
|
|
2020-03-05 22:38:33 +01:00
|
|
|
private void processIntent()
|
|
|
|
{
|
2018-06-29 13:51:06 -07:00
|
|
|
Intent intent = getIntent();
|
2018-08-10 21:43:50 -07:00
|
|
|
if (intent == null) {
|
|
|
|
Log.i(TAG + " processIntent", "intent is null");
|
|
|
|
return;
|
|
|
|
}
|
2018-06-29 13:51:06 -07:00
|
|
|
Log.i(TAG + " processIntent", intent.getAction());
|
|
|
|
UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
|
2018-08-10 21:43:50 -07:00
|
|
|
if (device == null) {
|
|
|
|
Log.i(TAG + " processIntent", "null device");
|
|
|
|
return;
|
|
|
|
}
|
2018-06-29 13:51:06 -07:00
|
|
|
Log.i(TAG + " processIntent device name", device.getDeviceName());
|
2020-03-14 17:57:36 -07:00
|
|
|
setUsbDevice(device);
|
2018-06-29 13:51:06 -07:00
|
|
|
} // processIntent
|
2020-03-05 22:38:33 +01:00
|
|
|
|
|
|
|
|
2020-03-14 18:07:24 -07:00
|
|
|
private final BroadcastReceiver usbReceiver = new BroadcastReceiver()
|
|
|
|
{
|
2020-03-14 18:17:05 +01:00
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
String action = intent.getAction();
|
|
|
|
if ("org.subsurfacedivelog.mobile.USB_PERMISSION".equals(action)) {
|
|
|
|
synchronized (this) {
|
|
|
|
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
|
2020-03-15 12:27:10 -07:00
|
|
|
UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
|
|
|
|
if (device == null) {
|
|
|
|
Log.i(TAG, " permission granted but null device");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Log.d(TAG, "USB device permission granted for " + device.getDeviceName());
|
|
|
|
restartDownload(device);
|
2020-03-14 18:07:24 -07:00
|
|
|
} else {
|
|
|
|
Log.d(TAG, "USB device permission denied");
|
2020-03-14 18:17:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-05 22:38:33 +01:00
|
|
|
public static Context getAppContext()
|
|
|
|
{
|
|
|
|
return appContext;
|
|
|
|
}
|
2018-06-29 13:51:06 -07:00
|
|
|
}
|