-
Notifications
You must be signed in to change notification settings - Fork 118
/
DeviceOpenActivity.java
277 lines (231 loc) · 8.32 KB
/
DeviceOpenActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* rtl_tcp_andro is a library that uses libusb and librtlsdr to
* turn your Realtek RTL2832 based DVB dongle into a SDR receiver.
* It independently implements the rtl-tcp API protocol for native Android usage.
* Copyright (C) 2022 by Signalware Ltd <[email protected]>
*
* 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, either version 2 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sdrtouch.rtlsdr;
import static com.sdrtouch.rtlsdr.SdrDeviceProviderRegistry.SDR_DEVICE_PROVIDERS;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;
import com.sdrtouch.core.SdrTcpArguments;
import com.sdrtouch.core.devices.SdrDevice;
import com.sdrtouch.core.devices.SdrDeviceProvider;
import com.sdrtouch.core.exceptions.SdrException;
import com.sdrtouch.core.exceptions.SdrException.err_info;
import com.sdrtouch.rtlsdr.driver.RtlSdrDevice;
import com.sdrtouch.tools.DeviceDialog;
import com.sdrtouch.tools.ExceptionTools;
import com.sdrtouch.tools.Log;
import java.util.ArrayList;
import java.util.List;
import marto.rtl_tcp_andro.R;
public class DeviceOpenActivity extends FragmentActivity implements DeviceDialog.OnDeviceDialog {
private volatile SdrTcpArguments sdrTcpArguments;
private volatile UsbDevice usbDevice;
private volatile SdrServiceConnection mConnection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.clear();
if (!RtlSdrApplication.IS_PLATFORM_SUPPORTED) {
finishWithError(new SdrException(SdrException.EXIT_PLATFORM_NOT_SUPPORTED));
return;
}
setContentView(R.layout.progress);
Intent intent = getIntent();
final Uri data = intent.getData();
try {
sdrTcpArguments = SdrTcpArguments.fromString(data.toString().replace("iqsrc://", ""));
if (intent.hasExtra(UsbManager.EXTRA_DEVICE)) {
usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
Log.appendLine("USB device: " + usbDevice.toString());
} else {
usbDevice = null;
}
} catch (IllegalArgumentException e) {
finishWithError(e, err_info.unknown_error, SdrException.EXIT_WRONG_ARGS);
}
}
@Override
protected void onStart() {
super.onStart();
if (usbDevice != null) {
Log.appendLine("onStart with USB device");
startServer(new RtlSdrDevice(getApplicationContext(), usbDevice));
return;
}
Log.appendLine("onStart");
try {
List<SdrDevice> availableSdrDevices;
availableSdrDevices = new ArrayList<>();
for (SdrDeviceProvider sdrDeviceProvider : SDR_DEVICE_PROVIDERS) {
List<SdrDevice> devicesForProvider = sdrDeviceProvider.listDevices(getApplicationContext(), false);
availableSdrDevices.addAll(devicesForProvider);
Log.appendLine("%s: found %d device opening options", sdrDeviceProvider.getName(), devicesForProvider.size());
}
switch (availableSdrDevices.size()) {
case 0:
finishWithError(new SdrException(SdrException.EXIT_NO_DEVICES));
break;
case 1:
Log.appendLine("Only 1 option available, no need to ask user. Opening %s", availableSdrDevices.get(0).getName());
startServer(availableSdrDevices.get(0));
break;
default:
Log.appendLine("%d options available. Asking user to pick.", availableSdrDevices.size());
showDeviceSelectionDialog(availableSdrDevices);
break;
}
} catch (Throwable t) {
finishWithError(t);
}
}
private void showDeviceSelectionDialog(List<SdrDevice> availableSdrDevices) {
// We can serialize the list of SdrDevices here since none of the devices have any callbacks
showDialog(DeviceDialog.invokeDialog(availableSdrDevices));
}
@Override
protected void onStop() {
super.onStop();
if (mConnection != null && mConnection.isBound()) {
unbindService(mConnection);
}
usbDevice = null;
mConnection = null;
}
public void showDialog(final DialogFragment dialog) {
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
final Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
// Create and show the dialog.
try {
dialog.show(ft, "dialog");
} catch (Throwable t) {t.printStackTrace();}
}
/**
* Starts the tcp binary
*/
public void startServer(final SdrDevice sdrDevice) {
try {
//start the service
mConnection = new SdrServiceConnection(sdrDevice, sdrTcpArguments, this::finishWithError);
sdrDevice.addOnStatusListener(onDeviceStatusListener);
Intent serviceIntent = new Intent(this, BinaryRunnerService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(serviceIntent);
} else {
startService(serviceIntent);
}
bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE);
} catch (Exception e) {
finishWithError(e);
}
}
@Override
public void onDeviceDialogDeviceChosen(SdrDevice selected) {
Log.appendLine("User has picked %s", selected.getName());
startServer(selected);
}
@Override
public void onDeviceDialogCanceled() {
Log.appendLine("User has canceled the device selection dialog");
finishWithError(new SdrException(SdrException.ERROR_ACCESS));
}
private final SdrDevice.OnStatusListener onDeviceStatusListener = new SdrDevice.OnStatusListener() {
@Override
public void onOpen(SdrDevice sdrDevice) {
finishWithSuccess(sdrDevice);
}
@Override
public void onClosed(Throwable e) {
finishWithError(e);
}
};
// RETURNING RESULTS BELOW
public void finishWithError(int id, Integer second_id, String msg) {
final Intent data = new Intent();
data.putExtra("marto.rtl_tcp_andro.RtlTcpExceptionId", id);
if (second_id != null) data.putExtra("detailed_exception_code", second_id);
if (msg != null) data.putExtra("detailed_exception_message", msg);
if (getParent() == null) {
setResult(RESULT_CANCELED, data);
} else {
getParent().setResult(RESULT_CANCELED, data);
}
finish();
}
public void finishWithError(final Throwable e) {
if (e == null) {
finishWithError();
return;
}
if (e instanceof SdrException) {
final SdrException rtlexception = (SdrException) e;
finishWithError(rtlexception.getReason(), rtlexception.getId(), rtlexception.getMessage());
} else {
Log.appendLine("Caught exception "+ExceptionTools.getNicelyFormattedTrace(e));
e.printStackTrace();
finishWithError();
}
}
public void finishWithError(final Throwable e, SdrException.err_info err_info, int id) {
if (e == null) {
finishWithError(err_info, id, null);
return;
}
Log.appendLine("Caught exception "+ExceptionTools.getNicelyFormattedTrace(e));
e.printStackTrace();
finishWithError(err_info, id, e.getMessage());
}
public void finishWithError(final err_info info, Integer second_id, String msg) {
if (info != null)
finishWithError(info.ordinal(), second_id, msg);
else
finishWithError(second_id, msg);
}
public void finishWithError() {
finishWithError(null, null);
}
public void finishWithError(Integer second_id, String msg) {
finishWithError(-1, second_id, msg);
}
private void finishWithSuccess(SdrDevice sdrDevice) {
final Intent data = new Intent();
// SDR device
data.putExtra(BinaryRunnerService.EXTRA_SUPPORTED_TCP_CMDS, sdrDevice.getSupportedCommands());
data.putExtra(BinaryRunnerService.EXTRA_DEVICE_NAME, sdrDevice.getName());
if (getParent() == null) {
setResult(RESULT_OK, data);
} else {
getParent().setResult(RESULT_OK, data);
}
Log.appendLine("Device was open. Closing the prompt activity.");
finish();
}
}