Skip to content

Commit 098a6cb

Browse files
committed
TargetList notification system re-implemented.
do not use intents, use Observer. notify on every interesting event ( target {port , name, connected} changed, target list changed )
1 parent 28e8ecc commit 098a6cb

File tree

5 files changed

+68
-24
lines changed

5 files changed

+68
-24
lines changed

cSploit/src/org/csploit/android/MainActivity.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public boolean onItemLongClick(AdapterView<?> parent, View view, int position, l
204204
mWipeReceiver.register(MainActivity.this);
205205
mMsfReceiver.register(MainActivity.this);
206206

207-
mRadarReceiver.setObserver(mTargetAdapter);
207+
System.setTargetListObserver(mTargetAdapter);
208208

209209
StartRPCServer();
210210

@@ -930,7 +930,30 @@ public int[] getSelectedPositions() {
930930

931931
@Override
932932
public void update(Observable observable, Object data) {
933-
MainActivity.this.runOnUiThread(this);
933+
final Target target = (Target) data;
934+
935+
if(target == null) {
936+
// update the whole list
937+
MainActivity.this.runOnUiThread(this);
938+
return;
939+
}
940+
941+
// update only a row, if it's displayed
942+
MainActivity.this.runOnUiThread(new Runnable() {
943+
@Override
944+
public void run() {
945+
if(lv == null)
946+
return;
947+
int start = lv.getFirstVisiblePosition();
948+
for(int i=start, j=lv.getLastVisiblePosition();i<=j;i++)
949+
if(target==list.get(i)){
950+
View view = lv.getChildAt(i-start);
951+
getView(i, view, lv);
952+
break;
953+
}
954+
}
955+
});
956+
934957
}
935958

936959
@Override

cSploit/src/org/csploit/android/core/System.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
import java.util.LinkedList;
8787
import java.util.List;
8888
import java.util.Map;
89+
import java.util.Observer;
8990
import java.util.regex.Matcher;
9091
import java.util.regex.Pattern;
9192
import java.util.zip.GZIPInputStream;
@@ -143,6 +144,8 @@ public class System
143144

144145
private static KnownIssues mKnownIssues = null;
145146

147+
private static Observer targetListObserver = null;
148+
146149
private final static LinkedList<SettingReceiver> mSettingReceivers = new LinkedList<SettingReceiver>();
147150

148151
public static void init(Context context) throws Exception{
@@ -372,6 +375,33 @@ public static boolean checkNetworking(final Activity current){
372375
return true;
373376
}
374377

378+
public synchronized static void setTargetListObserver(Observer targetListObserver) {
379+
System.targetListObserver = targetListObserver;
380+
}
381+
382+
/**
383+
* notify that a specific target of the list has been changed
384+
* @param target the changed target
385+
*/
386+
public static void notifyTargetListChanged(Target target) {
387+
Observer o;
388+
synchronized (System.class) {
389+
o = targetListObserver;
390+
}
391+
392+
if(o==null)
393+
return;
394+
395+
o.update(null, target);
396+
}
397+
398+
/**
399+
* notify that the targets list has been changed
400+
*/
401+
public static void notifyTargetListChanged() {
402+
notifyTargetListChanged(null);
403+
}
404+
375405
public static void setLastError(String error){
376406
mLastError = error;
377407
}

cSploit/src/org/csploit/android/net/Target.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,7 @@ public String getHostname(){
655655
}
656656

657657
public void addOpenPort(Port port){
658+
boolean notifyList = false;
658659
synchronized (mPorts) {
659660
Port existing = null;
660661
for(Port p : mPorts) {
@@ -668,8 +669,11 @@ public void addOpenPort(Port port){
668669
port.mergeTo(existing);
669670
} else {
670671
mPorts.add(port);
672+
notifyList = true;
671673
}
672674
}
675+
if(notifyList)
676+
System.notifyTargetListChanged(this);
673677
}
674678

675679
public void addOpenPort(int port, Protocol protocol){

cSploit/src/org/csploit/android/services/NetworkRadar.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
* network-radar process manager
2222
*/
2323
public class NetworkRadar extends NativeService implements MenuControllableService {
24-
public static final String NRDR_CHANGED = "NetworkRadar.action.TARGET_CHANGED";
2524
public static final String NRDR_STOPPED = "NetworkRadar.action.STOPPED";
2625
public static final String NRDR_STARTED = "NetworkRadar.action.STARTED";
2726
public static final String NRDR_START_FAILED = "NetworkRadar.action.START_FAILED";
@@ -121,18 +120,23 @@ public void onHostFound(byte[] macAddress, InetAddress ipAddress, String name) {
121120
}
122121
}
123122

124-
if(notify) {
125-
sendIntent(NRDR_CHANGED);
123+
if(!notify)
124+
return;
125+
126+
if(justFound) {
127+
System.notifyTargetListChanged();
128+
} else {
129+
System.notifyTargetListChanged(t);
126130
}
127131
}
128132

129133
@Override
130134
public void onHostLost(InetAddress ipAddress) {
131135
Target t = System.getTargetByAddress(ipAddress);
132136

133-
if(t != null) {
137+
if(t != null && t.isConnected()) {
134138
t.setConneced(false);
135-
sendIntent(NRDR_CHANGED);
139+
System.notifyTargetListChanged(t);
136140
}
137141
}
138142

@@ -162,7 +166,6 @@ public ScanReceiver(Target target) {
162166
@Override
163167
public void onPortFound(int port, String protocol) {
164168
target.addOpenPort(port, Network.Protocol.fromString(protocol));
165-
sendIntent(NRDR_CHANGED);
166169
}
167170
}
168171
}

cSploit/src/org/csploit/android/services/receivers/NetworkRadarReceiver.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,25 @@
44
import android.content.Context;
55
import android.content.Intent;
66
import android.content.IntentFilter;
7-
import android.os.Build;
8-
import android.widget.ArrayAdapter;
97
import android.widget.Toast;
108

11-
import org.csploit.android.MainActivity;
129
import org.csploit.android.R;
1310
import org.csploit.android.core.*;
1411
import org.csploit.android.services.NetworkRadar;
1512

16-
import java.util.Observer;
17-
1813
/**
1914
* receive notifications from NetworkRadar
2015
*/
2116
public class NetworkRadarReceiver extends ManagedReceiver {
2217

2318
private final IntentFilter filter;
24-
private Observer changeObserver;
2519

2620
public NetworkRadarReceiver() {
2721
filter = new IntentFilter();
2822

2923
filter.addAction(NetworkRadar.NRDR_STARTED);
3024
filter.addAction(NetworkRadar.NRDR_START_FAILED);
3125
filter.addAction(NetworkRadar.NRDR_STOPPED);
32-
filter.addAction(NetworkRadar.NRDR_CHANGED);
33-
}
34-
35-
public void setObserver(Observer changeObserver) {
36-
this.changeObserver = changeObserver;
3726
}
3827

3928
@Override
@@ -68,11 +57,6 @@ private void notifyIntent(Context context, Intent intent) {
6857
case NetworkRadar.NRDR_START_FAILED:
6958
Toast.makeText(context, R.string.net_discovery_start_failed, Toast.LENGTH_LONG).show();
7059
break;
71-
case NetworkRadar.NRDR_CHANGED:
72-
if(changeObserver != null) {
73-
changeObserver.update(null, null);
74-
}
75-
break;
7660
}
7761
}
7862
}

0 commit comments

Comments
 (0)