Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/BackgroundService.java
Show First 20 Lines • Show All 54 Lines • ▼ Show 20 Line(s) | 47 | public class BackgroundService extends Service { | |||
---|---|---|---|---|---|
55 | private final ConcurrentHashMap<String, DeviceListChangedCallback> deviceListChangedCallbacks = new ConcurrentHashMap<>(); | 55 | private final ConcurrentHashMap<String, DeviceListChangedCallback> deviceListChangedCallbacks = new ConcurrentHashMap<>(); | ||
56 | 56 | | |||
57 | private final ArrayList<BaseLinkProvider> linkProviders = new ArrayList<>(); | 57 | private final ArrayList<BaseLinkProvider> linkProviders = new ArrayList<>(); | ||
58 | 58 | | |||
59 | private final ConcurrentHashMap<String, Device> devices = new ConcurrentHashMap<>(); | 59 | private final ConcurrentHashMap<String, Device> devices = new ConcurrentHashMap<>(); | ||
60 | 60 | | |||
61 | private final HashSet<Object> discoveryModeAcquisitions = new HashSet<>(); | 61 | private final HashSet<Object> discoveryModeAcquisitions = new HashSet<>(); | ||
62 | 62 | | |||
63 | public static BackgroundService getInstance(){ | 63 | public static BackgroundService getInstance() { | ||
64 | return instance; | 64 | return instance; | ||
65 | } | 65 | } | ||
66 | 66 | | |||
67 | public boolean acquireDiscoveryMode(Object key) { | 67 | public boolean acquireDiscoveryMode(Object key) { | ||
68 | boolean wasEmpty = discoveryModeAcquisitions.isEmpty(); | 68 | boolean wasEmpty = discoveryModeAcquisitions.isEmpty(); | ||
69 | discoveryModeAcquisitions.add(key); | 69 | discoveryModeAcquisitions.add(key); | ||
70 | if (wasEmpty) { | 70 | if (wasEmpty) { | ||
71 | onNetworkChange(); | 71 | onNetworkChange(); | ||
Show All 36 Lines | 101 | public static void removeGuiInUseCounter(final Context activity) { | |||
108 | }); | 108 | }); | ||
109 | } | 109 | } | ||
110 | 110 | | |||
111 | private final Device.PairingCallback devicePairingCallback = new Device.PairingCallback() { | 111 | private final Device.PairingCallback devicePairingCallback = new Device.PairingCallback() { | ||
112 | @Override | 112 | @Override | ||
113 | public void incomingRequest() { | 113 | public void incomingRequest() { | ||
114 | onDeviceListChanged(); | 114 | onDeviceListChanged(); | ||
115 | } | 115 | } | ||
116 | | ||||
116 | @Override | 117 | @Override | ||
117 | public void pairingSuccessful() { | 118 | public void pairingSuccessful() { | ||
118 | onDeviceListChanged(); | 119 | onDeviceListChanged(); | ||
119 | } | 120 | } | ||
121 | | ||||
120 | @Override | 122 | @Override | ||
121 | public void pairingFailed(String error) { | 123 | public void pairingFailed(String error) { | ||
122 | onDeviceListChanged(); | 124 | onDeviceListChanged(); | ||
123 | } | 125 | } | ||
126 | | ||||
124 | @Override | 127 | @Override | ||
125 | public void unpaired() { | 128 | public void unpaired() { | ||
126 | onDeviceListChanged(); | 129 | onDeviceListChanged(); | ||
127 | } | 130 | } | ||
128 | }; | 131 | }; | ||
129 | 132 | | |||
130 | public void onDeviceListChanged() { | 133 | public void onDeviceListChanged() { | ||
131 | for(DeviceListChangedCallback callback : deviceListChangedCallbacks.values()) { | 134 | for (DeviceListChangedCallback callback : deviceListChangedCallbacks.values()) { | ||
132 | callback.onDeviceListChanged(); | 135 | callback.onDeviceListChanged(); | ||
133 | } | 136 | } | ||
134 | } | 137 | } | ||
135 | 138 | | |||
136 | private void loadRememberedDevicesFromSettings() { | 139 | private void loadRememberedDevicesFromSettings() { | ||
137 | //Log.e("BackgroundService", "Loading remembered trusted devices"); | 140 | //Log.e("BackgroundService", "Loading remembered trusted devices"); | ||
138 | SharedPreferences preferences = getSharedPreferences("trusted_devices", Context.MODE_PRIVATE); | 141 | SharedPreferences preferences = getSharedPreferences("trusted_devices", Context.MODE_PRIVATE); | ||
139 | Set<String> trustedDevices = preferences.getAll().keySet(); | 142 | Set<String> trustedDevices = preferences.getAll().keySet(); | ||
140 | for(String deviceId : trustedDevices) { | 143 | for (String deviceId : trustedDevices) { | ||
141 | //Log.e("BackgroundService", "Loading device "+deviceId); | 144 | //Log.e("BackgroundService", "Loading device "+deviceId); | ||
142 | if (preferences.getBoolean(deviceId, false)) { | 145 | if (preferences.getBoolean(deviceId, false)) { | ||
143 | Device device = new Device(this, deviceId); | 146 | Device device = new Device(this, deviceId); | ||
144 | devices.put(deviceId,device); | 147 | devices.put(deviceId, device); | ||
145 | device.addPairingCallback(devicePairingCallback); | 148 | device.addPairingCallback(devicePairingCallback); | ||
146 | } | 149 | } | ||
147 | } | 150 | } | ||
148 | } | 151 | } | ||
149 | 152 | | |||
150 | private void registerLinkProviders() { | 153 | private void registerLinkProviders() { | ||
151 | linkProviders.add(new LanLinkProvider(this)); | 154 | linkProviders.add(new LanLinkProvider(this)); | ||
152 | // linkProviders.add(new LoopbackLinkProvider(this)); | 155 | // linkProviders.add(new LoopbackLinkProvider(this)); | ||
Show All 9 Lines | 157 | // linkProviders.add(new BluetoothLinkProvider(this)); | |||
162 | public Device getDevice(String id) { | 165 | public Device getDevice(String id) { | ||
163 | return devices.get(id); | 166 | return devices.get(id); | ||
164 | } | 167 | } | ||
165 | 168 | | |||
166 | private void cleanDevices() { | 169 | private void cleanDevices() { | ||
167 | new Thread(new Runnable() { | 170 | new Thread(new Runnable() { | ||
168 | @Override | 171 | @Override | ||
169 | public void run() { | 172 | public void run() { | ||
170 | for(Device d : devices.values()) { | 173 | for (Device d : devices.values()) { | ||
171 | if (!d.isPaired() && !d.isPairRequested() && !d.isPairRequestedByPeer() && !d.deviceShouldBeKeptAlive()) { | 174 | if (!d.isPaired() && !d.isPairRequested() && !d.isPairRequestedByPeer() && !d.deviceShouldBeKeptAlive()) { | ||
172 | d.disconnect(); | 175 | d.disconnect(); | ||
173 | } | 176 | } | ||
174 | } | 177 | } | ||
175 | } | 178 | } | ||
176 | }).start(); | 179 | }).start(); | ||
177 | } | 180 | } | ||
178 | 181 | | |||
179 | private final BaseLinkProvider.ConnectionReceiver deviceListener = new BaseLinkProvider.ConnectionReceiver() { | 182 | private final BaseLinkProvider.ConnectionReceiver deviceListener = new BaseLinkProvider.ConnectionReceiver() { | ||
180 | @Override | 183 | @Override | ||
181 | public void onConnectionReceived(final NetworkPackage identityPackage, final BaseLink link) { | 184 | public void onConnectionReceived(final NetworkPackage identityPackage, final BaseLink link) { | ||
182 | 185 | | |||
183 | String deviceId = identityPackage.getString("deviceId"); | 186 | String deviceId = identityPackage.getString("deviceId"); | ||
184 | 187 | | |||
185 | Device device = devices.get(deviceId); | 188 | Device device = devices.get(deviceId); | ||
186 | 189 | | |||
187 | if (device != null) { | 190 | if (device != null) { | ||
188 | Log.i("KDE/BackgroundService", "addLink, known device: " + deviceId); | 191 | Log.i("KDE/BackgroundService", "addLink, known device: " + deviceId); | ||
189 | device.addLink(identityPackage, link); | 192 | device.addLink(identityPackage, link); | ||
190 | } else { | 193 | } else { | ||
191 | Log.i("KDE/BackgroundService", "addLink,unknown device: " + deviceId); | 194 | Log.i("KDE/BackgroundService", "addLink,unknown device: " + deviceId); | ||
192 | device = new Device(BackgroundService.this, identityPackage, link); | 195 | device = new Device(BackgroundService.this, identityPackage, link); | ||
193 | if (device.isPaired() || device.isPairRequested() || device.isPairRequestedByPeer() | 196 | if (device.isPaired() || device.isPairRequested() || device.isPairRequestedByPeer() | ||
194 | || link.linkShouldBeKeptAlive() | 197 | || link.linkShouldBeKeptAlive() | ||
195 | ||!discoveryModeAcquisitions.isEmpty() ) | 198 | || !discoveryModeAcquisitions.isEmpty()) { | ||
196 | { | | |||
197 | devices.put(deviceId, device); | 199 | devices.put(deviceId, device); | ||
198 | device.addPairingCallback(devicePairingCallback); | 200 | device.addPairingCallback(devicePairingCallback); | ||
199 | } else { | 201 | } else { | ||
200 | device.disconnect(); | 202 | device.disconnect(); | ||
201 | } | 203 | } | ||
202 | } | 204 | } | ||
203 | 205 | | |||
204 | onDeviceListChanged(); | 206 | onDeviceListChanged(); | ||
Show All 37 Lines | 243 | public void removeConnectionListener(BaseLinkProvider.ConnectionReceiver cr) { | |||
242 | for (BaseLinkProvider a : linkProviders) { | 244 | for (BaseLinkProvider a : linkProviders) { | ||
243 | a.removeConnectionReceiver(cr); | 245 | a.removeConnectionReceiver(cr); | ||
244 | } | 246 | } | ||
245 | } | 247 | } | ||
246 | 248 | | |||
247 | public void addDeviceListChangedCallback(String key, DeviceListChangedCallback callback) { | 249 | public void addDeviceListChangedCallback(String key, DeviceListChangedCallback callback) { | ||
248 | deviceListChangedCallbacks.put(key, callback); | 250 | deviceListChangedCallbacks.put(key, callback); | ||
249 | } | 251 | } | ||
252 | | ||||
250 | public void removeDeviceListChangedCallback(String key) { | 253 | public void removeDeviceListChangedCallback(String key) { | ||
251 | deviceListChangedCallbacks.remove(key); | 254 | deviceListChangedCallbacks.remove(key); | ||
252 | } | 255 | } | ||
253 | 256 | | |||
254 | //This will called only once, even if we launch the service intent several times | 257 | //This will called only once, even if we launch the service intent several times | ||
255 | @Override | 258 | @Override | ||
256 | public void onCreate() { | 259 | public void onCreate() { | ||
257 | super.onCreate(); | 260 | super.onCreate(); | ||
Show All 27 Lines | |||||
285 | public void onDestroy() { | 288 | public void onDestroy() { | ||
286 | for (BaseLinkProvider a : linkProviders) { | 289 | for (BaseLinkProvider a : linkProviders) { | ||
287 | a.onStop(); | 290 | a.onStop(); | ||
288 | } | 291 | } | ||
289 | super.onDestroy(); | 292 | super.onDestroy(); | ||
290 | } | 293 | } | ||
291 | 294 | | |||
292 | @Override | 295 | @Override | ||
293 | public IBinder onBind (Intent intent) { | 296 | public IBinder onBind(Intent intent) { | ||
294 | return new Binder(); | 297 | return new Binder(); | ||
295 | } | 298 | } | ||
296 | 299 | | |||
297 | 300 | | |||
298 | //To use the service from the gui | 301 | //To use the service from the gui | ||
299 | 302 | | |||
300 | public interface InstanceCallback { | 303 | public interface InstanceCallback { | ||
301 | void onServiceStart(BackgroundService service); | 304 | void onServiceStart(BackgroundService service); | ||
▲ Show 20 Lines • Show All 44 Lines • Show Last 20 Lines |