Skip to content

Commit 17be90a

Browse files
authored
Merge pull request #42 from jpage4500/feature/07-17
- refactor device details to use a separate thread pool from other features like screen mirroring
2 parents df26462 + 8e838ec commit 17be90a

File tree

4 files changed

+78
-32
lines changed

4 files changed

+78
-32
lines changed

src/main/java/com/jpage4500/devicemanager/manager/DeviceManager.java

Lines changed: 61 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import com.jpage4500.devicemanager.data.LogEntry;
66
import com.jpage4500.devicemanager.ui.dialog.ConnectDialog;
77
import com.jpage4500.devicemanager.ui.dialog.SettingsDialog;
8-
import com.jpage4500.devicemanager.utils.Timer;
98
import com.jpage4500.devicemanager.utils.*;
9+
import com.jpage4500.devicemanager.utils.Timer;
1010
import se.vidstige.jadb.*;
1111
import se.vidstige.jadb.managers.PackageManager;
1212
import se.vidstige.jadb.managers.PropertyManager;
@@ -61,6 +61,8 @@ public class DeviceManager {
6161

6262
// how frequently to update logs
6363
public static final int LOG_INTERVAL_MS = 100;
64+
// how frequently to refresh device list
65+
public static final int DEVICE_REFRESH_MINS = 60;
6466

6567
public static final String CUSTOM_KEY_VERSION = "VER";
6668
public static final String CUSTOM_KEY_PROP = "PROP";
@@ -69,11 +71,14 @@ public class DeviceManager {
6971

7072
private static volatile DeviceManager instance;
7173

74+
private DeviceListener deviceListener;
7275
private final List<Device> deviceList;
7376
private final String tempFolder;
7477
private final List<Process> processList;
7578

79+
// thread pool for running commands
7680
private final ExecutorService commandExecutorService;
81+
// thread pool for fetching device details
7782
private final ScheduledExecutorService scheduledExecutorService;
7883
private ScheduledFuture<?> deviceRefreshRuture;
7984

@@ -97,12 +102,16 @@ private DeviceManager() {
97102
processList = new ArrayList<>();
98103

99104
commandExecutorService = Executors.newFixedThreadPool(10);
100-
scheduledExecutorService = Executors.newScheduledThreadPool(3);
105+
scheduledExecutorService = Executors.newScheduledThreadPool(1);
101106

102107
tempFolder = Utils.getTempFolder();
103108
copyResourcesToFiles();
104109
}
105110

111+
public void setDeviceListener(DeviceListener listener) {
112+
this.deviceListener = listener;
113+
}
114+
106115
public interface DeviceListener {
107116
// device list was refreshed
108117
void handleDevicesUpdated(List<Device> deviceList);
@@ -116,7 +125,7 @@ public interface DeviceListener {
116125
void handleException(Exception e);
117126
}
118127

119-
public void connectAdbServer(boolean allowRetry, DeviceManager.DeviceListener listener) {
128+
public void connectAdbServer(boolean allowRetry) {
120129
connection = new JadbConnection();
121130
commandExecutorService.submit(() -> {
122131
try {
@@ -125,26 +134,26 @@ public void connectAdbServer(boolean allowRetry, DeviceManager.DeviceListener li
125134
connection.createDeviceWatcher(new DeviceDetectionListener() {
126135
@Override
127136
public void onDetect(List<JadbDevice> devices) {
128-
handleDeviceUpdate(devices, listener);
137+
handleDeviceUpdate(devices);
129138
}
130139

131140
@Override
132141
public void onException(Exception e) {
133142
log.error("connectAdbServer: onException: {}", e.getMessage());
134143
// change all devices to offline
135144
for (Device device : deviceList) device.isOnline = false;
136-
listener.handleException(e);
145+
if (deviceListener != null) deviceListener.handleException(e);
137146
}
138147
}).run();
139148
} catch (Exception e) {
140149
log.error("connectAdbServer: Exception: {}", e.getMessage());
141150
// likley because adb server isn't running.. try to start it now
142151
startServer((isSuccess, error) -> {
143-
if (isSuccess && allowRetry) connectAdbServer(false, listener);
152+
if (isSuccess && allowRetry) connectAdbServer(false);
144153
else {
145154
// change all devices to offline
146155
for (Device device : deviceList) device.isOnline = false;
147-
listener.handleException(e);
156+
if (deviceListener != null) deviceListener.handleException(e);
148157
}
149158
});
150159
}
@@ -155,7 +164,7 @@ public void onException(Exception e) {
155164
* called when a device is added/updated/removed
156165
* NOTE: run on background thread
157166
*/
158-
private void handleDeviceUpdate(List<JadbDevice> devices, DeviceListener listener) {
167+
private void handleDeviceUpdate(List<JadbDevice> devices) {
159168
//log.debug("onDetect: GOT:{}, {}", devices.size(), GsonHelper.toJson(devices));
160169
List<Device> addedDeviceList = new ArrayList<>();
161170

@@ -195,13 +204,13 @@ private void handleDeviceUpdate(List<JadbDevice> devices, DeviceListener listene
195204
// -- DEVICE REMOVED --
196205
device.isOnline = false;
197206
device.lastUpdateMs = System.currentTimeMillis();
198-
listener.handleDeviceRemoved(device);
207+
if (deviceListener != null) deviceListener.handleDeviceRemoved(device);
199208
}
200209
}
201210

202211
if (!addedDeviceList.isEmpty()) {
203212
// notify listener that device list changed
204-
listener.handleDevicesUpdated(deviceList);
213+
if (deviceListener != null) deviceListener.handleDevicesUpdated(deviceList);
205214

206215
for (Device addedDevice : addedDeviceList) {
207216
// fetch more details for these devices
@@ -212,12 +221,12 @@ private void handleDeviceUpdate(List<JadbDevice> devices, DeviceListener listene
212221
addedDevice.isOnline = true;
213222
addedDevice.status = null;
214223
addedDevice.lastUpdateMs = System.currentTimeMillis();
215-
listener.handleDeviceUpdated(addedDevice);
216-
fetchDeviceDetails(addedDevice, true, listener);
224+
notifyDeviceUpdated(addedDevice);
225+
fetchDeviceDetails(addedDevice, true);
217226
} else {
218227
log.debug("handleDeviceUpdate: NOT_READY: {} -> {}", addedDevice.serial, state);
219228
addedDevice.status = state.name();
220-
listener.handleDeviceUpdated(addedDevice);
229+
notifyDeviceUpdated(addedDevice);
221230
}
222231
} catch (Exception e) {
223232
String errMsg = e.getMessage();
@@ -231,26 +240,35 @@ private void handleDeviceUpdate(List<JadbDevice> devices, DeviceListener listene
231240
addedDevice.status = errMsg;
232241
// TODO: check error message before setting device to offline?
233242
addedDevice.isOnline = false;
234-
listener.handleDeviceUpdated(addedDevice);
243+
notifyDeviceUpdated(addedDevice);
235244
}
236245
}
237246

238-
// run periodic task to update device state
239247
if (deviceRefreshRuture == null) {
240-
deviceRefreshRuture = scheduledExecutorService.scheduleWithFixedDelay(() -> {
241-
//log.trace("handleDeviceUpdate: REFRESH");
242-
for (Device device : deviceList) {
243-
fetchDeviceDetails(device, false, listener);
244-
}
245-
}, 5, 5, TimeUnit.MINUTES);
248+
updateRefreshTime();
246249
}
247250
}
248251
}
249252

250-
public void refreshDevices(DeviceListener listener) {
253+
public void updateRefreshTime() {
254+
if (deviceRefreshRuture != null) {
255+
deviceRefreshRuture.cancel(true);
256+
}
257+
int refreshTimeMins = PreferenceUtils.getPreference(PreferenceUtils.PrefInt.PREF_REFRESH_TIME_MINS, DeviceManager.DEVICE_REFRESH_MINS);
258+
// run periodic task to update device state
259+
log.debug("updateRefreshTime: schedule refresh every {} mins", refreshTimeMins);
260+
deviceRefreshRuture = scheduledExecutorService.scheduleWithFixedDelay(() -> {
261+
log.trace("handleDeviceUpdate: REFRESH");
262+
for (Device device : deviceList) {
263+
fetchDeviceDetails(device, false);
264+
}
265+
}, refreshTimeMins, refreshTimeMins, TimeUnit.MINUTES);
266+
}
267+
268+
public void refreshDevices() {
251269
synchronized (deviceList) {
252270
for (Device device : deviceList) {
253-
fetchDeviceDetails(device, true, listener);
271+
fetchDeviceDetails(device, true);
254272
}
255273
}
256274
}
@@ -260,24 +278,24 @@ public void refreshDevices(DeviceListener listener) {
260278
*
261279
* @param fullRefresh - true to fetch everythign; false to only fetch values that would change often (battery, disk)
262280
*/
263-
private void fetchDeviceDetails(Device device, boolean fullRefresh, DeviceListener listener) {
281+
private void fetchDeviceDetails(Device device, boolean fullRefresh) {
264282
if (!device.isOnline) return;
265-
commandExecutorService.submit(() -> {
283+
scheduledExecutorService.submit(() -> {
266284
Timer timer = new Timer();
267285
// show device as 'busy'
268286
device.setBusy(true);
269-
listener.handleDeviceUpdated(device);
287+
notifyDeviceUpdated(device);
270288

271289
// check if device is fully booted
272290
fetchDeviceBooted(device);
273291
if (!device.isBooted) {
274292
boolean isBusy = device.setBusy(false);
275-
if (!isBusy) listener.handleDeviceUpdated(device);
293+
if (!isBusy) notifyDeviceUpdated(device);
276294

277295
// if device isn't fully booted yet, schedule another refresh
278296
scheduledExecutorService.schedule(() -> {
279297
log.trace("fetchDeviceDetails: try again for {}", device.getDisplayName());
280-
fetchDeviceDetails(device, true, listener);
298+
fetchDeviceDetails(device, true);
281299
}, 5, TimeUnit.SECONDS);
282300
return;
283301
}
@@ -309,7 +327,10 @@ private void fetchDeviceDetails(Device device, boolean fullRefresh, DeviceListen
309327

310328
// -- IMEI --
311329
String imei = runShellServiceCall(device, COMMAND_SERVICE_IMEI);
312-
if (TextUtils.notEmpty(imei)) device.imei = imei;
330+
if (TextUtils.notEmpty(imei)) {
331+
device.imei = imei;
332+
notifyDeviceUpdated(device);
333+
}
313334
} catch (Exception e) {
314335
// not a phone (tablet, TV, etc)
315336
}
@@ -337,10 +358,16 @@ private void fetchDeviceDetails(Device device, boolean fullRefresh, DeviceListen
337358
if (log.isTraceEnabled()) log.trace("fetchDeviceDetails: REFRESH:{}: {}", timer, GsonHelper.toJson(device));
338359
}
339360
boolean isBusy = device.setBusy(false);
340-
if (!isBusy) listener.handleDeviceUpdated(device);
361+
if (!isBusy) notifyDeviceUpdated(device);
341362
});
342363
}
343364

365+
private void notifyDeviceUpdated(Device device) {
366+
if (deviceListener != null) {
367+
deviceListener.handleDeviceUpdated(device);
368+
}
369+
}
370+
344371
/**
345372
* check if device is fully booted
346373
*/
@@ -386,6 +413,7 @@ private void fetchBatteryInfo(Device device) {
386413
break;
387414
}
388415
}
416+
notifyDeviceUpdated(device);
389417
}
390418

391419
private void fetchCustomColumns(Device device) {
@@ -438,6 +466,7 @@ private void fetchCustomColumns(Device device) {
438466
if (value != null) {
439467
if (device.customAppVersionList == null) device.customAppVersionList = new HashMap<>();
440468
device.customAppVersionList.put(label, value);
469+
notifyDeviceUpdated(device);
441470
}
442471
}
443472
int afterSize = device.customAppVersionList != null ? device.customAppVersionList.size() : 0;
@@ -457,6 +486,7 @@ private void fetchFreeDiskSpace(Device device) {
457486
try {
458487
// size is in 1k blocks
459488
device.freeSpace = Long.parseLong(size) * 1000L;
489+
notifyDeviceUpdated(device);
460490
return;
461491
} catch (Exception e) {
462492
log.trace("fetchDeviceDetails: FREE_SPACE Exception:{}", e.getMessage());
@@ -500,6 +530,7 @@ private void fetchNickname(Device device) {
500530
return;
501531
}
502532
device.nickname = nickname;
533+
notifyDeviceUpdated(device);
503534
}
504535
}
505536

src/main/java/com/jpage4500/devicemanager/ui/DeviceScreen.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,8 @@ private void showSystemTray(MouseEvent e) {
499499
}
500500

501501
private void connectAdbServer() {
502-
DeviceManager.getInstance().connectAdbServer(true, this);
502+
DeviceManager.getInstance().setDeviceListener(this);
503+
DeviceManager.getInstance().connectAdbServer(true);
503504
}
504505

505506
@Override
@@ -1273,7 +1274,7 @@ private void handleCustomScriptClicked(File script, String name) {
12731274
}
12741275

12751276
private void refreshDevices() {
1276-
DeviceManager.getInstance().refreshDevices(this);
1277+
DeviceManager.getInstance().refreshDevices();
12771278
}
12781279

12791280
private void handleRunCustomCommand() {

src/main/java/com/jpage4500/devicemanager/ui/dialog/SettingsDialog.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ private SettingsDialog(DeviceScreen deviceScreen) {
4040

4141
private void initalizeUi() {
4242
JPanel devicePanel = UiUtils.createPanel("Device Settings");
43+
UiUtils.addSettingButton(devicePanel, "Refresh Time", "EDIT", () -> showRefreshTime());
4344
UiUtils.addSettingButton(devicePanel, "Manage Columns", "EDIT", () -> showManageDeviceColumnsDialog(deviceScreen, this));
4445
UiUtils.addSettingButton(devicePanel, "Custom Columns", "EDIT", this::showAppsSettings);
4546
UiUtils.addSettingButton(devicePanel, "Customize Toolbar", "EDIT", () -> showManageToolbar(deviceScreen, this));
@@ -156,6 +157,18 @@ public static List<String> getHiddenColumnList() {
156157
return GsonHelper.stringToList(hiddenColsStr, String.class);
157158
}
158159

160+
public void showRefreshTime() {
161+
int refreshTimeMins = PreferenceUtils.getPreference(PreferenceUtils.PrefInt.PREF_REFRESH_TIME_MINS, DeviceManager.DEVICE_REFRESH_MINS);
162+
String result = DialogHelper.showInputDialog(this, "Refresh Time", "Enter Refresh Time (in mins, between 5 and 600)", String.valueOf(refreshTimeMins));
163+
if (TextUtils.isEmpty(result)) return;
164+
165+
int newValue = TextUtils.getNumber(result, DeviceManager.DEVICE_REFRESH_MINS);
166+
if (newValue > 600) newValue = 600;
167+
else if (newValue < 5) newValue = 5;
168+
PreferenceUtils.setPreference(PreferenceUtils.PrefInt.PREF_REFRESH_TIME_MINS, newValue);
169+
DeviceManager.getInstance().updateRefreshTime();
170+
}
171+
159172
public static void showManageDeviceColumnsDialog(DeviceScreen deviceScreen, Component component) {
160173
JPanel panel = new JPanel(new MigLayout("fillx"));
161174
panel.add(new JLabel("Select columns to SHOW"), "span");

src/main/java/com/jpage4500/devicemanager/utils/PreferenceUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public enum PrefInt {
5454
PREF_LOGS_FONT_SIZE,
5555
PREF_LOGS_FONT_STYLE,
5656
PREF_LOGS_MAX_LINES,
57+
PREF_REFRESH_TIME_MINS,
5758
}
5859

5960
public static String getPreference(Pref pref) {

0 commit comments

Comments
 (0)