-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownloadService.java
More file actions
303 lines (243 loc) · 9.37 KB
/
DownloadService.java
File metadata and controls
303 lines (243 loc) · 9.37 KB
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
package com.shank.SambaExplorer;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbAuthException;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.RemoteViews;
public class DownloadService extends Service {
public static String[] DownloadQueue = new String[2048];
public static int QueueTop=0;
public static boolean serviceStarted = false;
public static boolean servicePaused = false;
public static Activity mOwner;
private static int retryCount = 0;
protected static boolean serviceCancel;
protected static NtlmPasswordAuthentication userAuth;
static void ProvideLoginCredentials(String domain, String username, String password) {
try {
userAuth = new NtlmPasswordAuthentication(null, username, password);
} catch (Exception e) {
showNotification(username + "@"+ domain + " failed to authenticate");
}
}
static void QueueDownload(final Activity owner, String download) {
if (DownloadService.QueueTop >= 2047) {
showNotification("Download queue full! some files not queued");
return;
}
DownloadService.mOwner = owner;
DownloadService.DownloadQueue[DownloadService.QueueTop++] = download;
try{
if ( DownloadService.serviceStarted == false ) {
Intent service = new Intent(owner, DownloadService.class);
owner.startService(service);
}
}catch(Exception e){
final Exception E = e;
Runnable dialogPopup1 = 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(owner)
.setMessage(StackTrace)
.setTitle(E.toString())
.show();
}
};
owner.runOnUiThread(dialogPopup1);
}
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
Thread mDownloadThread = new Thread(new Runnable() {
@Override
public void run() {
DownloadService.serviceStarted = true;
showNotification("Download service started.");
while (QueueTop > 0) {
try {
SmbFile f;
DownloadService.retryCount++;
if (DownloadService.retryCount > 4) {
DownloadService.retryCount = 1;
DownloadService.QueueTop--;
if (DownloadService.QueueTop < 0) {
break;
}
}
int filenameIndex = DownloadService.DownloadQueue[DownloadService.QueueTop-1].lastIndexOf("/")+1;
String fileName = DownloadService.DownloadQueue[DownloadService.QueueTop-1].substring(filenameIndex);
showNotification("Downloading " + fileName);
if (DownloadService.userAuth != null) {
f = new SmbFile( DownloadService.DownloadQueue[DownloadService.QueueTop-1], DownloadService.userAuth );
} else {
f = new SmbFile( DownloadService.DownloadQueue[DownloadService.QueueTop-1] );
}
f = new SmbFile( DownloadService.DownloadQueue[DownloadService.QueueTop-1] );
SmbFileInputStream in = new SmbFileInputStream( f );
FileOutputStream out = new FileOutputStream( "/sdcard/download/"+fileName );
long t0 = System.currentTimeMillis();
byte[] b = new byte[8192];
int n, tot = 0;
while(( n = in.read( b )) > 0 ) {
out.write( b, 0, n );
tot += n;
if (DownloadService.serviceCancel) {
DownloadService.serviceCancel = false;
break;
}
}
long t = System.currentTimeMillis() - t0;
in.close();
out.close();
DownloadService.QueueTop--;
showNotification("Finished downloading " + fileName + "("+(tot/1024)+"kb) in " + (t/1000.0) + "sec");
while (DownloadService.servicePaused) {
Thread.sleep(1000);
}
} catch (SmbAuthException e) {
startActivity(new Intent(mOwner, com.shank.SambaExplorer.SambaLogin.class));
} catch (MalformedURLException e) {
final MalformedURLException E = e;
Runnable dialogPopup1 = 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(mOwner)
.setMessage(StackTrace)
.setTitle(E.toString())
.show();
}
};
mOwner.runOnUiThread(dialogPopup1);
} catch (SmbException e) {
final SmbException E = e;
Runnable dialogPopup1 = 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(mOwner)
.setMessage(StackTrace)
.setTitle(E.toString())
.show();
}
};
mOwner.runOnUiThread(dialogPopup1);
} catch (UnknownHostException e) {
final UnknownHostException E = e;
Runnable dialogPopup1 = 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(mOwner)
.setMessage(StackTrace)
.setTitle(E.toString())
.show();
}
};
mOwner.runOnUiThread(dialogPopup1);
} catch (FileNotFoundException e) {
final FileNotFoundException E = e;
Runnable dialogPopup1 = 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(mOwner)
.setMessage(StackTrace)
.setTitle(E.toString())
.show();
}
};
mOwner.runOnUiThread(dialogPopup1);
} catch (IOException e) {
final IOException E = e;
Runnable dialogPopup1 = 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(mOwner)
.setMessage(StackTrace)
.setTitle(E.toString())
.show();
}
};
mOwner.runOnUiThread(dialogPopup1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DownloadService.retryCount = 0;
}
stopSelf();
}
});
@Override
public void onCreate() {
super.onCreate();
mDownloadThread.start();
}
@Override
public void onDestroy() {
DownloadService.serviceStarted = false;
mDownloadThread.stop();
}
public static void showNotification(String text) {
NotificationManager nm = (NotificationManager) mOwner.getSystemService(Service.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(mOwner, 0,
new Intent(mOwner, DownloadQueue.class), 0);
// construct the Notification object.
Notification notif = new Notification();
notif.tickerText = text;
notif.icon = R.drawable.icon;
RemoteViews nmView = new RemoteViews(mOwner.getPackageName(), R.layout.notify);
nmView.setTextViewText(R.id.TextView01, text);
// nmView.setProgressBar(R.id.ProgressBar01, max, tot, false);
// nmView.setProgressBar(R.id.ProgressBar02, files, file, false);
notif.contentView = nmView;
notif.contentIntent = contentIntent;
nm.notify(R.layout.notify, notif);
}
public static String GetDownloadQueue(int i) {
return DownloadQueue[i];
}
}