Skip to content

Commit 0f919d7

Browse files
committed
use a service as packet receiver
1 parent c51339c commit 0f919d7

File tree

8 files changed

+107
-77
lines changed

8 files changed

+107
-77
lines changed

AndroidManifest.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
<uses-permission android:name="android.permission.INTERNET"/>
1111

1212
<application
13-
android:name=".AppMain"
1413
android:icon="@drawable/netpowerctrl"
1514
android:label="@string/app_name" android:allowBackup="true">
1615
<activity
@@ -64,6 +63,13 @@
6463
android:name="android.appwidget.provider"
6564
android:resource="@xml/appwidget_provider" />
6665
</receiver>
66+
67+
<service
68+
android:label="@string/app_name"
69+
android:icon="@drawable/netpowerctrl"
70+
android:name=".NetpowerctrlService"
71+
android:permission="android.permission.INTERNET">
72+
</service>
6773

6874
</application>
6975

src/oly/netpowerctrl/AppMain.java

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

src/oly/netpowerctrl/DeviceControl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ public class DeviceControl extends Activity implements OnClickListener {
4343
public void onCreate(Bundle savedInstanceState) {
4444
super.onCreate(savedInstanceState);
4545

46-
((AppMain)getApplicationContext()).maybeStartDiscoveryThreads(this);
46+
Intent it = new Intent(this, NetpowerctrlService.class);
47+
startService(it);
4748

4849
buttons = new ArrayList<CompoundButton>();
4950

5051
device = null;
51-
Intent it = getIntent();
52+
it = getIntent();
5253
if (it != null) {
5354
Bundle extra = it.getExtras();
5455
if (extra != null) {
@@ -98,7 +99,6 @@ public void onCreate(Bundle savedInstanceState) {
9899
ll.addView(cb, lp);
99100
buttons.add(cb);
100101
}
101-
102102
}
103103

104104

src/oly/netpowerctrl/DevicePreferences.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
8383

8484
getPreferenceManager().getSharedPreferences()
8585
.edit().putInt(SharedPrefs.PREF_RECVPORT, port);
86+
87+
DeviceQuery.restartDiscovery(self); // port may have changed
8688
return true;
8789
}
8890
});

src/oly/netpowerctrl/DeviceQuery.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212

1313
import android.app.Activity;
1414
import android.content.Context;
15+
import android.content.Intent;
1516
import android.content.SharedPreferences;
1617
import android.preference.PreferenceManager;
18+
import android.support.v4.content.LocalBroadcastManager;
1719
import android.widget.Toast;
1820

1921
public class DeviceQuery {
@@ -106,4 +108,9 @@ public static ArrayList<Integer> getAllReceivePorts(Context context) {
106108
return new ArrayList<Integer>(ports);
107109
}
108110

111+
public static void restartDiscovery(Context ctx) {
112+
Intent it = new Intent(DiscoveryThread.BROADCAST_RESTART_DISCOVERY);
113+
LocalBroadcastManager.getInstance(ctx).sendBroadcast(it);
114+
}
115+
109116
}

src/oly/netpowerctrl/DiscoveryThread.java

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,24 @@
44
import java.net.DatagramPacket;
55
import java.net.DatagramSocket;
66

7-
import android.app.Activity;
7+
import android.content.Context;
88
import android.content.Intent;
99
import android.support.v4.content.LocalBroadcastManager;
1010
import android.widget.Toast;
1111

1212
public class DiscoveryThread extends Thread {
1313

1414
public static String BROADCAST_DEVICE_DISCOVERED = "com.nittka.netpowerctrl.DEVICE_DISCOVERED";
15+
public static String BROADCAST_RESTART_DISCOVERY = "com.nittka.netpowerctrl.RESTART_DISCOVERY";
1516

1617
int recv_port;
17-
Activity activity;
18+
Context ctx;
1819
boolean keep_running;
1920
DatagramSocket socket;
2021

21-
public DiscoveryThread(int port, Activity act) {
22+
public DiscoveryThread(int port, Context context) {
2223
recv_port = port;
23-
activity = act;
24+
ctx = context;
2425
socket = null;
2526
}
2627

@@ -40,14 +41,10 @@ public void run() {
4041
socket.close();
4142
} catch (final IOException e) {
4243
if (keep_running) { // no message if we were interrupt()ed
43-
activity.runOnUiThread(new Runnable() {
44-
public void run() {
45-
String msg = String.format(activity.getResources().getString(R.string.error_listen_thread_exception), recv_port);
46-
msg += e.getLocalizedMessage();
47-
if (recv_port < 1024) msg += activity.getResources().getString(R.string.error_port_lt_1024);
48-
Toast.makeText(activity, msg, Toast.LENGTH_LONG).show();
49-
}
50-
});
44+
String msg = String.format(ctx.getResources().getString(R.string.error_listen_thread_exception), recv_port);
45+
msg += e.getLocalizedMessage();
46+
if (recv_port < 1024) msg += ctx.getResources().getString(R.string.error_port_lt_1024);
47+
Toast.makeText(ctx, msg, Toast.LENGTH_LONG).show();
5148
}
5249
break;
5350
}
@@ -73,18 +70,14 @@ public void parsePacket(final String message, int recevied_port) {
7370
// error packet received
7471
String desc;
7572
if (msg[2].trim().equals("NoPass"))
76-
desc = activity.getResources().getString(R.string.error_nopass);
73+
desc = ctx.getResources().getString(R.string.error_nopass);
7774
else desc = msg[2];
78-
final String error = activity.getResources().getString(R.string.error_packet_received) + desc;
79-
activity.runOnUiThread(new Runnable() {
80-
public void run() {
81-
Toast.makeText(activity, error, Toast.LENGTH_LONG).show();
82-
}
83-
});
75+
String error = ctx.getResources().getString(R.string.error_packet_received) + desc;
76+
Toast.makeText(ctx, error, Toast.LENGTH_LONG).show();
8477
return;
8578
}
8679

87-
final DeviceInfo di = new DeviceInfo(activity);
80+
final DeviceInfo di = new DeviceInfo(ctx);
8881
di.DeviceName = msg[1].trim();
8982
di.HostName = msg[2];
9083
di.MacAddress = msg[5];
@@ -103,13 +96,8 @@ public void run() {
10396
di.Outlets.add(oi);
10497
}
10598

106-
activity.runOnUiThread(new Runnable() {
107-
public void run() {
108-
Intent it = new Intent(BROADCAST_DEVICE_DISCOVERED);
109-
it.putExtra("device_info", di);
110-
LocalBroadcastManager.getInstance(activity).sendBroadcast(it);
111-
}
112-
});
113-
99+
Intent it = new Intent(BROADCAST_DEVICE_DISCOVERED);
100+
it.putExtra("device_info", di);
101+
LocalBroadcastManager.getInstance(ctx).sendBroadcast(it);
114102
}
115103
}

src/oly/netpowerctrl/NetpowerctrlActivity.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ public void onCreate(Bundle savedInstanceState) {
5959
adpDiscoveredDevices = new DeviceListAdapter(this, alDiscoveredDevices);
6060
lvDiscoveredDevices.setAdapter(adpDiscoveredDevices);
6161

62-
ReadConfiguredDevices();
63-
6462
lvConfiguredDevices.setOnItemClickListener(this);
6563
lvDiscoveredDevices.setOnItemClickListener(this);
6664

@@ -69,7 +67,6 @@ public void onCreate(Bundle savedInstanceState) {
6967

7068
adpConfiguredDevices.setDeviceConfigureEvent(this);
7169
adpDiscoveredDevices.setDeviceConfigureEvent(this);
72-
7370
}
7471

7572

@@ -83,6 +80,9 @@ protected void onResume() {
8380
IntentFilter itf= new IntentFilter(DiscoveryThread.BROADCAST_DEVICE_DISCOVERED);
8481
LocalBroadcastManager.getInstance(this).registerReceiver(onDeviceDiscovered, itf);
8582

83+
Intent it = new Intent(this, NetpowerctrlService.class);
84+
startService(it);
85+
8686
DeviceQuery.sendBroadcastQuery(this);
8787
}
8888

@@ -161,12 +161,11 @@ public void ReadConfiguredDevices() {
161161
alConfiguredDevices = SharedPrefs.ReadConfiguredDevices(this);
162162
adpConfiguredDevices.setDevices(alConfiguredDevices);
163163
adpConfiguredDevices.getFilter().filter("");
164-
((AppMain)getApplicationContext()).restartDiscoveryThreads(this);
165164
}
166165

167166
public void SaveConfiguredDevices() {
168167
SharedPrefs.SaveConfiguredDevices(alConfiguredDevices, this);
169-
((AppMain)getApplicationContext()).restartDiscoveryThreads(this);
168+
DeviceQuery.restartDiscovery(this); // ports may have changed
170169
}
171170

172171
@Override
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package oly.netpowerctrl;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import android.app.Service;
7+
import android.content.BroadcastReceiver;
8+
import android.content.Context;
9+
import android.content.Intent;
10+
import android.content.IntentFilter;
11+
import android.os.IBinder;
12+
import android.support.v4.content.LocalBroadcastManager;
13+
14+
public class NetpowerctrlService extends Service {
15+
16+
public static String BROADCAST_PORTS_CHANGED = "com.nittka.netpowerctrl.DEVICE_DISCOVERED";
17+
List<DiscoveryThread> discoveryThreads;
18+
19+
@Override
20+
public void onCreate() {
21+
//android.os.Debug.waitForDebugger();
22+
discoveryThreads = new ArrayList<DiscoveryThread>();
23+
IntentFilter itf= new IntentFilter(DiscoveryThread.BROADCAST_RESTART_DISCOVERY);
24+
LocalBroadcastManager.getInstance(this).registerReceiver(onConfigChanged, itf);
25+
}
26+
27+
@Override
28+
public IBinder onBind(Intent intent) {
29+
// not needed, we use broadcasts
30+
return null;
31+
}
32+
33+
@Override
34+
public int onStartCommand(Intent intent, int flags, int startId) {
35+
startDiscoveryThreads();
36+
return START_STICKY;
37+
}
38+
39+
public void startDiscoveryThreads() {
40+
// only start if not yet running
41+
if (discoveryThreads.size() == 0) {
42+
for (int port: DeviceQuery.getAllReceivePorts(this)) {
43+
DiscoveryThread thr = new DiscoveryThread(port, this);
44+
thr.start();
45+
discoveryThreads.add(thr);
46+
}
47+
}
48+
// give the threads a chance to start
49+
try {Thread.sleep(100);} catch (InterruptedException e) {}
50+
}
51+
52+
public void restartDiscoveryThreads() {
53+
for (DiscoveryThread thr: discoveryThreads)
54+
thr.interrupt();
55+
discoveryThreads.clear();
56+
// socket needs minimal time to really go away
57+
try {Thread.sleep(100);} catch (InterruptedException e) {}
58+
startDiscoveryThreads();
59+
}
60+
61+
private BroadcastReceiver onConfigChanged= new BroadcastReceiver() {
62+
@Override
63+
public void onReceive(Context context, Intent intent) {
64+
restartDiscoveryThreads();
65+
}
66+
};
67+
68+
}

0 commit comments

Comments
 (0)