Skip to content

Commit dfbb540

Browse files
author
davidgraeff
committed
Reduce update queries, no widget flickering
1 parent 496e7bd commit dfbb540

22 files changed

+794
-650
lines changed

.idea/libraries/support_v4_19_0_0.xml

Lines changed: 0 additions & 9 deletions
This file was deleted.

.idea/workspace.xml

Lines changed: 375 additions & 369 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/app.iml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
<orderEntry type="jdk" jdkName="Android API 19 Platform" jdkType="Android SDK" />
7272
<orderEntry type="sourceFolder" forTests="false" />
7373
<orderEntry type="library" exported="" name="support-v4-19.0.1" level="project" />
74+
<orderEntry type="library" exported="" name="acra-4.5.0" level="project" />
7475
</component>
7576
</module>
7677

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ android {
2020

2121
dependencies {
2222
compile 'com.android.support:support-v4:19.0.+'
23+
compile 'ch.acra:acra:4.5.0@jar'
2324
}

app/src/main/AndroidManifest.xml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="oly.netpowerctrl"
4-
android:versionCode="17"
5-
android:versionName="1.9c">
4+
android:versionCode="19"
5+
android:versionName="2.1">
66

7-
<!-- Api level 14: Android 4.0 15. Dezember 2011 -->
7+
<!-- Api level 14: Android 4.0 15. Dezember 2011 -->
88
<!-- Api level 16: Android 4.1 27. Juni 2012 -->
99
<!--<uses-sdk
1010
android:minSdkVersion="14"
@@ -32,9 +32,8 @@
3232
android:name="oly.netpowerctrl.main.NetpowerctrlActivity"
3333
android:label="@string/app_name"
3434
android:launchMode="singleTask"
35-
android:configChanges="orientation"
36-
android:theme="@style/Theme.Transparent">
37-
<intent-filter>
35+
android:configChanges="orientation|screenSize|keyboardHidden|layoutDirection">
36+
<intent-filter>
3837
<action android:name="android.intent.action.MAIN" />
3938
<category android:name="android.intent.category.LAUNCHER" />
4039
</intent-filter>
@@ -74,8 +73,7 @@
7473
android:icon="@drawable/netpowerctrl"
7574
android:label="@string/app_name_shortcut"
7675
android:description="@string/app_name_shortcut_desc"
77-
android:configChanges="orientation"
78-
android:theme="@style/Theme.Transparent"
76+
android:configChanges="orientation|screenSize|keyboardHidden|layoutDirection"
7977
android:name="oly.netpowerctrl.shortcut.ShortcutCreatorActivity">
8078
<intent-filter>
8179
<action android:name="android.intent.action.CREATE_SHORTCUT" />

app/src/main/java/oly/netpowerctrl/anelservice/DeviceQuery.java

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package oly.netpowerctrl.anelservice;
22

3-
import android.content.Context;
43
import android.os.Handler;
54

65
import java.util.ArrayList;
@@ -20,33 +19,52 @@ public class DeviceQuery {
2019
private Collection<DeviceInfo> devices_to_observe;
2120
private DeviceUpdateStateOrTimeout target;
2221
private Handler timeoutHandler = new Handler();
22+
private boolean foundBroudcastQueries = false;
23+
2324
private Runnable timeoutRunnable = new Runnable() {
2425
@Override
2526
public void run() {
26-
for (DeviceInfo di : devices_to_observe)
27-
target.onDeviceTimeout(di);
28-
target.onDeviceQueryFinished(devices_to_observe.size());
2927
NetpowerctrlApplication.instance.removeUpdateDeviceState(DeviceQuery.this);
28+
if (target == null)
29+
return;
30+
31+
if (devices_to_observe.isEmpty()) {
32+
target.onDeviceQueryFinished(devices_to_observe.size());
33+
return;
34+
}
35+
36+
// Special case: We are able to send broadcasts, but nevertheless not all
37+
// configured devices responded, we will send specific queries now
38+
if (foundBroudcastQueries) {
39+
foundBroudcastQueries = false;
40+
for (DeviceInfo di : devices_to_observe) {
41+
DeviceSend.instance().sendQuery(di.HostName, di.SendPort);
42+
}
43+
// New timeout
44+
timeoutHandler.postDelayed(timeoutRunnable, 1200);
45+
} else {
46+
for (DeviceInfo di : devices_to_observe) {
47+
di.reachable = false;
48+
target.onDeviceTimeout(di);
49+
}
50+
target.onDeviceQueryFinished(devices_to_observe.size());
51+
}
3052
}
3153
};
3254

33-
public DeviceQuery(Context context, DeviceUpdateStateOrTimeout target, DeviceInfo device_to_observe, boolean rangeCheck) {
55+
public DeviceQuery(DeviceUpdateStateOrTimeout target, DeviceInfo device_to_observe) {
3456
this.target = target;
3557
this.devices_to_observe = new ArrayList<DeviceInfo>();
3658
devices_to_observe.add(device_to_observe);
3759

3860
// Register on main application object to receive device updates
3961
NetpowerctrlApplication.instance.addUpdateDeviceState(this);
4062

41-
if (!DeviceSend.instance().sendQuery(device_to_observe.HostName, device_to_observe.SendPort, rangeCheck)) {
42-
// Device not in range, immediately timeout
43-
timeoutHandler.postDelayed(timeoutRunnable, 0);
44-
} else
45-
timeoutHandler.postDelayed(timeoutRunnable, 1200);
63+
DeviceSend.instance().sendQuery(device_to_observe.HostName, device_to_observe.SendPort);
64+
timeoutHandler.postDelayed(timeoutRunnable, 1200);
4665
}
4766

48-
public DeviceQuery(Context context, DeviceUpdateStateOrTimeout target,
49-
Collection<DeviceInfo> devices_to_observe, boolean queryForNewDevices, boolean rangeCheck) {
67+
public DeviceQuery(DeviceUpdateStateOrTimeout target, Collection<DeviceInfo> devices_to_observe) {
5068
this.target = target;
5169
this.devices_to_observe = new ArrayList<DeviceInfo>(devices_to_observe);
5270

@@ -56,11 +74,25 @@ public DeviceQuery(Context context, DeviceUpdateStateOrTimeout target,
5674
timeoutHandler.postDelayed(timeoutRunnable, 1200);
5775

5876
// Send out broadcast
59-
if (queryForNewDevices)
60-
DeviceSend.instance().sendBroadcastQuery();
61-
else
62-
for (DeviceInfo di : devices_to_observe)
63-
DeviceSend.instance().sendQuery(di.HostName, di.SendPort, rangeCheck);
77+
for (DeviceInfo di : devices_to_observe)
78+
DeviceSend.instance().sendQuery(di.HostName, di.SendPort);
79+
}
80+
81+
/**
82+
* Issues a broadcast query. If there is no response to that for all
83+
* configured devices, we will also do a device specific query.
84+
*
85+
* @param target
86+
*/
87+
public DeviceQuery(DeviceUpdateStateOrTimeout target) {
88+
this.target = target;
89+
this.devices_to_observe = new ArrayList<DeviceInfo>(NetpowerctrlApplication.instance.configuredDevices);
90+
91+
// Register on main application object to receive device updates
92+
NetpowerctrlApplication.instance.addUpdateDeviceState(this);
93+
94+
timeoutHandler.postDelayed(timeoutRunnable, 1200);
95+
foundBroudcastQueries = DeviceSend.instance().sendBroadcastQuery();
6496
}
6597

6698
/**
@@ -75,12 +107,14 @@ public boolean notifyObservers(DeviceInfo received_data) {
75107
DeviceInfo device_to_observe = it.next();
76108
if (device_to_observe.equals(received_data)) {
77109
it.remove();
78-
target.onDeviceUpdated(received_data);
110+
if (target != null)
111+
target.onDeviceUpdated(received_data);
79112
}
80113
}
81114
if (devices_to_observe.isEmpty()) {
82115
timeoutHandler.removeCallbacks(timeoutRunnable);
83-
target.onDeviceQueryFinished(devices_to_observe.size());
116+
if (target != null)
117+
target.onDeviceQueryFinished(devices_to_observe.size());
84118
return true;
85119
}
86120
return false;

app/src/main/java/oly/netpowerctrl/anelservice/DeviceSend.java

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import java.util.ArrayList;
1515
import java.util.Collection;
1616
import java.util.Enumeration;
17-
import java.util.Iterator;
1817
import java.util.Locale;
1918
import java.util.Set;
2019
import java.util.concurrent.LinkedBlockingQueue;
@@ -45,11 +44,10 @@ public static DeviceSend instance() {
4544
/**
4645
* Call this to stop the send thread.
4746
*/
48-
public void interrupt() {
49-
if (sendThread.isAlive())
50-
q.add(new KillJob());
51-
}
52-
47+
// public void interrupt() {
48+
// if (sendThread.isAlive())
49+
// q.add(new KillJob());
50+
// }
5351
public void addSendJob(String hostname, int port, byte[] message, boolean broadcast, int errorID) throws UnknownHostException {
5452
if (!sendThread.isAlive())
5553
sendThread.start();
@@ -192,7 +190,7 @@ private byte[] generateAllOutletsDatagramBytes(final DeviceCommand device) {
192190
return data;
193191
}
194192

195-
void sendBroadcastQuery() {
193+
boolean sendBroadcastQuery() {
196194
Set<Integer> ports = NetpowerctrlApplication.instance.getAllSendPorts();
197195
boolean foundBroadcastAddresses = false;
198196

@@ -206,10 +204,7 @@ void sendBroadcastQuery() {
206204
if (iface == null) continue;
207205

208206
if (!iface.isLoopback() && iface.isUp()) {
209-
210-
Iterator it = iface.getInterfaceAddresses().iterator();
211-
while (it.hasNext()) {
212-
InterfaceAddress address = (InterfaceAddress) it.next();
207+
for (InterfaceAddress address : iface.getInterfaceAddresses()) {
213208
//System.out.println("Found address: " + address);
214209
if (address == null) continue;
215210
InetAddress broadcast = address.getBroadcast();
@@ -234,29 +229,18 @@ void sendBroadcastQuery() {
234229
// Query all existing devices directly
235230
ArrayList<DeviceInfo> devices = NetpowerctrlApplication.instance.configuredDevices;
236231
for (DeviceInfo di : devices) {
237-
sendQuery(di.HostName, di.SendPort, false);
232+
sendQuery(di.HostName, di.SendPort);
238233
}
239234
}
235+
return foundBroadcastAddresses;
240236
}
241237

242-
boolean sendQuery(final String hostname, final int port, boolean rangeCheck) {
243-
if (rangeCheck) {
244-
// try {
245-
// if (!isIPinNetworkAddressPool(InetAddress.getByName(hostname))) {
246-
// ShowToast.FromOtherThread(context, context.getResources().getString(R.string.error_not_in_range) + ": " + hostname);
247-
// return false;
248-
// }
249-
// } catch (final SocketException e) {
250-
// ShowToast.FromOtherThread(context, context.getResources().getString(R.string.error_not_in_range) + ": " + hostname+" "+e.getMessage());
251-
// return false;
252-
// }
253-
}
238+
void sendQuery(final String hostname, final int port) {
254239
try {
255240
addSendJob(hostname, port, "wer da?\r\n".getBytes(), true, INQUERY_REQUEST);
256241
} catch (UnknownHostException e) {
257242
e.printStackTrace();
258243
}
259-
return true;
260244
}
261245

262246
public void sendOutlet(final OutletInfo oi, boolean new_state) {

app/src/main/java/oly/netpowerctrl/datastructure/DeviceInfo.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ public class DeviceInfo implements Parcelable {
3535
public List<OutletInfo> IOs;
3636
public String Temperature;
3737
public String FirmwareVersion;
38-
public boolean reachable;
38+
39+
// Temporary state variables
40+
public boolean reachable = false;
41+
public boolean updated = false;
3942

4043
private static String uuidToString(UUID uuid) {
4144
return uuid.toString().replace(":", "-");
@@ -63,7 +66,6 @@ private DeviceInfo() {
6366
HttpPort = 80;
6467
Temperature = "";
6568
FirmwareVersion = "";
66-
reachable = false;
6769
Outlets = new ArrayList<OutletInfo>();
6870
IOs = new ArrayList<OutletInfo>();
6971
}
@@ -125,15 +127,19 @@ public void copyFreshValues(DeviceInfo di) {
125127
}
126128

127129
/**
128-
* Return true if both DeviceInfo objects refer to the same device.
130+
* Return true if both DeviceInfo objects refer to the same device, preferable if both
131+
* have a mac address set.
129132
*
130133
* @param other Compare to other DeviceInfo
131134
* @return
132135
*/
133136
@SuppressWarnings("unused")
134137
public boolean equalsFunctional(DeviceInfo other) {
135-
return HostName.equals(other.HostName) && ReceivePort == other.ReceivePort &&
136-
Outlets.size() == other.Outlets.size();
138+
if (MacAddress.isEmpty() || other.MacAddress.isEmpty())
139+
return HostName.equals(other.HostName) && ReceivePort == other.ReceivePort &&
140+
Outlets.size() == other.Outlets.size();
141+
else
142+
return MacAddress.equals(other.MacAddress);
137143
}
138144

139145
/**

app/src/main/java/oly/netpowerctrl/main/ConfigureDeviceFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private void testDevice() {
4949
return;
5050

5151
test_state = TestStates.TEST_REACHABLE;
52-
DeviceQuery dc = new DeviceQuery(getActivity(), this, device, true);
52+
new DeviceQuery(this, device);
5353
}
5454

5555
private void saveDevice() {

app/src/main/java/oly/netpowerctrl/main/NetpowerctrlActivity.java

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,8 @@
2121
import android.app.Fragment;
2222
import android.app.FragmentManager;
2323
import android.app.FragmentTransaction;
24-
import android.content.BroadcastReceiver;
25-
import android.content.Context;
2624
import android.content.Intent;
27-
import android.content.IntentFilter;
2825
import android.content.pm.PackageManager;
29-
import android.content.res.Configuration;
3026
import android.nfc.NdefMessage;
3127
import android.nfc.NdefRecord;
3228
import android.nfc.NfcAdapter;
@@ -90,21 +86,6 @@ public PluginController getPluginController() {
9086
return pluginController;
9187
}
9288

93-
BroadcastReceiver wifiChangedListener = new BroadcastReceiver() {
94-
@Override
95-
public void onReceive(Context context, Intent intent) {
96-
//WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
97-
Handler handler = new Handler();
98-
handler.postDelayed(new Runnable() {
99-
@Override
100-
public void run() {
101-
if (NetpowerctrlApplication.instance != null)
102-
NetpowerctrlApplication.instance.detectNewDevicesAndReachability(true);
103-
}
104-
}, 1500);
105-
}
106-
};
107-
10889
@Override
10990
protected void onDestroy() {
11091
super.onDestroy();
@@ -262,8 +243,7 @@ protected void onPause() {
262243
}
263244

264245
// Stop listener
265-
NetpowerctrlApplication.instance.stopListener();
266-
unregisterReceiver(wifiChangedListener);
246+
NetpowerctrlApplication.instance.stopUseListener();
267247
}
268248

269249
@Override
@@ -311,22 +291,7 @@ public void onResume() {
311291
NFC.parseNFC(this, new String(msg.getRecords()[0].getPayload()));
312292
}
313293

314-
// Listen for wifi changes
315-
IntentFilter filter = new IntentFilter();
316-
filter.addAction("android.net.wifi.STATE_CHANGE");
317-
filter.addAction("android.net.wifi.supplicant.CONNECTION_CHANGE");
318-
registerReceiver(wifiChangedListener, filter);
319-
320-
// Start listener and request new device states after around 800ms
321-
// Its better to request device state after the gui has established itself
322-
// even on slower devices before starting the listener service.
323-
Handler handler = new Handler();
324-
handler.postDelayed(new Runnable() {
325-
@Override
326-
public void run() {
327-
NetpowerctrlApplication.instance.startListener(true);
328-
}
329-
}, 800);
294+
NetpowerctrlApplication.instance.useListener();
330295
}
331296

332297
@Override
@@ -411,12 +376,12 @@ protected void onPostCreate(Bundle savedInstanceState) {
411376
mDrawerToggle.syncState();
412377
}
413378

414-
@Override
415-
public void onConfigurationChanged(Configuration newConfig) {
416-
super.onConfigurationChanged(newConfig);
417-
// Pass any configuration change to the drawer toggle
418-
mDrawerToggle.onConfigurationChanged(newConfig);
419-
}
379+
// @Override
380+
// public void onConfigurationChanged(Configuration newConfig) {
381+
// super.onConfigurationChanged(newConfig);
382+
// // Pass any configuration change to the drawer toggle
383+
// mDrawerToggle.onConfigurationChanged(newConfig);
384+
// }
420385

421386
public DeviceListAdapter getConfiguredDevicesAdapter() {
422387
return adpConfiguredDevices;

0 commit comments

Comments
 (0)