-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSambaExplorer.java
465 lines (369 loc) · 12.9 KB
/
SambaExplorer.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
package com.shank.SambaExplorer;
import java.io.IOException;
import java.net.InetAddress;
import java.net.MalformedURLException;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.DhcpInfo;
import android.net.NetworkInfo;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import jcifs.smb.SmbAuthException;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
public class SambaExplorer extends android.app.ListActivity {
public ArrayAdapter<String> mList;
public String[] mListStrings;
public String[] mListContents;
public String mHost;
int curListID=0;
public boolean active;
private String IPsubnet;
private static String ipAddressToString(int addr) {
StringBuffer buf = new StringBuffer();
buf.append(addr & 0xff).append('.').
append((addr >>>= 8) & 0xff).append('.').
append((addr >>>= 8) & 0xff).append('.').
append((addr >>>= 8) & 0xff);
return buf.toString();
}
private static String getIPsubnet(int addr) {
StringBuffer buf = new StringBuffer();
buf.append(addr & 0xff).append('.').
append((addr >>>= 8) & 0xff).append('.').
append((addr >>>= 8) & 0xff).append('.');
return buf.toString();
}
Thread m_subnetScanThread;
public int numThreadsRunning;
public int serversScanned;
class SubnetScanThread implements Runnable {
public SambaExplorer mOwner;
SubnetScanThread(SambaExplorer owner) {
mOwner = owner;
}
@Override
public void run() {
int timeout = 1000;
int start = 1;
int end = 10;
mOwner.numThreadsRunning++;
if (mOwner.IPsubnet.endsWith("*")) {
mOwner.IPsubnet = mOwner.IPsubnet.substring(0, mOwner.IPsubnet.length()-1);
}
for (int tries = 0; tries < 1; tries++) {
for (int i=start;i<=end;i++) {
String serverName = new String(mOwner.IPsubnet+String.valueOf(i));
mOwner.serversScanned++;
while (!mOwner.active) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
InetAddress serverAddr = InetAddress.getByName(serverName);
mOwner.getWindow().setFeatureInt(Window.FEATURE_PROGRESS, (int)(9999.0 * ((1.0+mOwner.serversScanned)/256)));
if (serverAddr.isReachable(timeout)) {
mOwner.AddListItem("smb://"+serverAddr.getCanonicalHostName()+"/");
}
} catch (Exception e) {
mOwner.AddListItem(e.getMessage());
}
}
timeout += 500;
}
if (mOwner.numThreadsRunning == 1) {
// if we're the last thread running...
Runnable alertDialog = new Runnable() {
@Override
public void run() {
Toast.makeText(SambaExplorer.this, "Finished scanning "+mOwner.serversScanned+" servers", 0);
}
};
runOnUiThread(alertDialog);
}
mOwner.numThreadsRunning--;
}
};
private String mSubnetOverride;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mListStrings = new String[255];
mListContents = new String[255];
for (int i=0;i<255;i++) {
mListStrings[i] = "";
mListContents[i] = "";
}
ListView view = getListView();
mList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListStrings);
view.setAdapter(mList);
view.setOnCreateContextMenuListener(this);
ConnectivityManager cm = (ConnectivityManager)this.getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni.getType() == ConnectivityManager.TYPE_MOBILE) {
new AlertDialog.Builder(this)
.setMessage("This application is meant for WIFI networks.")
.show();
return;
}
WifiManager wifi = (WifiManager) getSystemService(WIFI_SERVICE);
DhcpInfo info = wifi.getDhcpInfo();
IPsubnet = getIPsubnet(info.ipAddress);
mHost = null;
Intent intent = getIntent();
mHost = intent.getDataString();
// mSubnetOverride = intent.getStringExtra("subnet");
if (mHost == null) {
//m_subnetScanThread = new Thread(new SubnetScanThread(this));
//m_subnetScanThread.start();
//startActivity(new Intent(this,com.shank.SambaExplorer.PickHost.class));
try{
Intent i = new Intent("com.shank.portscan.PortScan.class");
startActivity(i);
} catch (ActivityNotFoundException e) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("market://search?q=NetScan"));
startActivity(i);
}
} else {
SetLastListItem("Connecting to "+mHost);
jcifs.Config.setProperty("jcifs.encoding", "Cp1252");
jcifs.Config.setProperty("jcifs.smb.lmCompatibility", "0");
jcifs.Config.setProperty("jcifs.netbios.hostname", "AndroidPhone");
jcifs.Config.registerSmbURLHandler();
if (!mHost.startsWith("smb:/")) {
if (mHost.startsWith("/")) {
mHost = "smb:/"+mHost+"/";
} else {
mHost = "smb://"+mHost+"/";
}
}
SmbFile f;
try {
if (DownloadService.userAuth != null) {
f = new SmbFile( mHost, DownloadService.userAuth );
} else {
f = new SmbFile( mHost );
}
if (f.canRead()) {
TraverseSMB(f,1);
}
} catch (SmbAuthException e) {
startActivity(new Intent(this, com.shank.SambaExplorer.SambaLogin.class).putExtra("path", mHost));
} catch (MalformedURLException e) {
final MalformedURLException E = e;
Runnable dialogPopup = new Runnable() {
@Override
public void run() {
String StackTrace="";
StackTraceElement[] Stack = E.getStackTrace();
for (int i=0; i<Stack.length; i++) {
StackTrace += Stack[i].toString() + "\n";
}
new AlertDialog.Builder(SambaExplorer.this)
.setMessage(StackTrace)
.setTitle(E.toString())
.show();
}
};
runOnUiThread(dialogPopup);
} catch (SmbException e) {
final SmbException E = e;
Runnable dialogPopup = new Runnable() {
@Override
public void run() {
String StackTrace="";
StackTraceElement[] Stack = E.getStackTrace();
for (int i=0; i<Stack.length; i++) {
StackTrace += Stack[i].toString() + "\n";
}
new AlertDialog.Builder(SambaExplorer.this)
.setMessage(StackTrace)
.setTitle(E.toString())
.show();
}
};
runOnUiThread(dialogPopup);
} catch (IOException e) {
final IOException E = e;
Runnable dialogPopup = new Runnable() {
@Override
public void run() {
String StackTrace="";
StackTraceElement[] Stack = E.getStackTrace();
for (int i=0; i<Stack.length; i++) {
StackTrace += Stack[i].toString() + "\n";
}
new AlertDialog.Builder(SambaExplorer.this)
.setMessage(StackTrace)
.setTitle(E.toString())
.show();
}
};
runOnUiThread(dialogPopup);
}
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.add(0,0,0,"Download");
menu.add(0,1,1,"Rename");
menu.add(0,2,2,"Delete");
}
@Override
public boolean onContextItemSelected(MenuItem menuItem) {
return false;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
String share = mListContents[position];
if (share.startsWith("smb://")) {
if (share.endsWith("/")) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(share));
startActivity(intent);
} else {
// files
DownloadService.QueueDownload(this, share);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, 0, 0, "Download All");
menu.add(0, 1, 1, "Recursive Download All");
menu.add(0, 2, 2, "Download Queue");
menu.add(0, 3, 3, "Options");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
new Thread(new Runnable() {
@Override
public void run() {
try {
DownloadDirectory(new SmbFile(mHost),1);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
break;
case 1:
new Thread(new Runnable() {
@Override
public void run() {
try {
DownloadDirectory(new SmbFile(mHost),255);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
break;
case 2:
Intent intent = new Intent(this, com.shank.SambaExplorer.DownloadQueue.class);
startActivity(intent);
break;
}
return false;
}
private Runnable updateAdapter = new Runnable() {
@Override
public void run() {
mList.notifyDataSetChanged();
}
};
public void ForceUpdate() {
runOnUiThread(updateAdapter);
}
public void SetLastListItem(String str) {
mListStrings[curListID] = str;
ForceUpdate();
}
public void AddListItem(String server) {
if (server.endsWith("/")) {
String temp = server.substring(0,server.lastIndexOf('/'));
mListStrings[curListID] = server.substring(temp.lastIndexOf('/'));
} else {
mListStrings[curListID] = server.substring(server.lastIndexOf('/'));
}
mListContents[curListID] = server;
curListID++;
ForceUpdate();
}
void DownloadDirectory( SmbFile f, int depth ) throws MalformedURLException, IOException {
if( depth == 0 ) {
return;
}
try{
SmbFile[] l;
l = f.listFiles();
for(int i = 0; l != null && i < l.length; i++ ) {
try {
if( l[i].isDirectory() ) {
DownloadDirectory( l[i], depth - 1 );
} else {
DownloadService.QueueDownload(this, l[i].getPath());
}
Thread.sleep(100);
} catch( IOException ioe ) {
}
}
}catch(Exception e){
AddListItem(e.toString());
}
}
void TraverseSMB( SmbFile f, int depth ) throws MalformedURLException, IOException {
if( depth == 0 ) {
return;
}
try{
SmbFile[] l;
l = f.listFiles();
for(int i = 0; l != null && i < l.length; i++ ) {
try {
AddListItem( l[i].getCanonicalPath());//.getDfsPath() );
if( l[i].isDirectory() ) {
TraverseSMB( l[i], depth - 1 );
}
} catch (SmbAuthException e) {
startActivity(new Intent(this, com.shank.SambaExplorer.SambaLogin.class).putExtra("path", l[i].getCanonicalPath()));
} catch( IOException ioe ) {
}
}
} catch (SmbAuthException e) {
startActivity(new Intent(this, com.shank.SambaExplorer.SambaLogin.class).putExtra("path", f.getCanonicalPath()));
}catch(Exception e){
AddListItem(e.toString());
}
}
}