forked from flutter/flutter-intellij
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAndroidEmulatorManager.java
123 lines (101 loc) · 3.68 KB
/
AndroidEmulatorManager.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
/*
* Copyright 2019 The Chromium Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
package io.flutter.sdk;
import com.google.common.collect.ImmutableSet;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.util.concurrency.AppExecutorUtil;
import io.flutter.FlutterUtils;
import io.flutter.android.AndroidEmulator;
import io.flutter.android.AndroidSdk;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;
/**
* This class manages the list of known Android eumlators, and handles refreshing the list as well
* as notifying interested parties when the list changes.
*/
public class AndroidEmulatorManager {
private static final Logger LOG = Logger.getInstance(AndroidEmulatorManager.class);
@NotNull
public static AndroidEmulatorManager getInstance(@NotNull Project project) {
return Objects.requireNonNull(project.getService(AndroidEmulatorManager.class));
}
private final @NotNull Project project;
private final AtomicReference<ImmutableSet<Runnable>> listeners = new AtomicReference<>(ImmutableSet.of());
private List<AndroidEmulator> cachedEmulators = new ArrayList<>();
private AndroidEmulatorManager(@NotNull Project project) {
this.project = project;
}
public void addListener(@NotNull Runnable callback) {
listeners.updateAndGet((old) -> {
final List<Runnable> changed = new ArrayList<>(old);
changed.add(callback);
return ImmutableSet.copyOf(changed);
});
}
public void removeListener(@NotNull Runnable callback) {
listeners.updateAndGet((old) -> {
final List<Runnable> changed = new ArrayList<>(old);
changed.remove(callback);
return ImmutableSet.copyOf(changed);
});
}
private CompletableFuture<List<AndroidEmulator>> inProgressRefresh;
public CompletableFuture<List<AndroidEmulator>> refresh() {
// We don't need to refresh if one is in progress.
synchronized (this) {
if (inProgressRefresh != null) {
return inProgressRefresh;
}
}
final CompletableFuture<List<AndroidEmulator>> future = new CompletableFuture<>();
synchronized (this) {
inProgressRefresh = future;
}
AppExecutorUtil.getAppExecutorService().submit(() -> {
final AndroidSdk sdk = AndroidSdk.createFromProject(project);
if (sdk == null) {
future.complete(Collections.emptyList());
}
else {
final List<AndroidEmulator> emulators = sdk.getEmulators();
emulators.sort((emulator1, emulator2) -> emulator1.getName().compareToIgnoreCase(emulator2.getName()));
future.complete(emulators);
}
});
future.thenAccept(emulators -> {
fireChangeEvent(emulators, cachedEmulators);
synchronized (this) {
inProgressRefresh = null;
}
});
return future;
}
public List<AndroidEmulator> getCachedEmulators() {
return cachedEmulators;
}
private void fireChangeEvent(final List<AndroidEmulator> newEmulators, final List<AndroidEmulator> oldEmulators) {
if (project.isDisposed()) return;
// Don't fire if the list of devices is unchanged.
if (Objects.equals(cachedEmulators, newEmulators)) {
return;
}
cachedEmulators = newEmulators;
for (Runnable listener : listeners.get()) {
try {
listener.run();
}
catch (Exception e) {
FlutterUtils.warn(LOG, "AndroidEmulatorManager listener threw an exception", e);
}
}
}
}