1 package com.joshuawise.dumload;
3 import java.io.InputStream;
4 import java.io.OutputStream;
6 import com.jcraft.jsch.*;
7 import java.lang.Boolean;
9 import android.app.Activity;
10 import android.app.Service;
11 import android.content.Intent;
12 import android.app.PendingIntent;
13 import android.content.Context;
14 import android.net.Uri;
15 import android.os.Bundle;
16 import android.os.IBinder;
17 import android.widget.TextView;
18 import android.widget.Toast;
19 import android.util.Log;
20 import android.app.NotificationManager;
21 import android.app.Notification;
22 import android.os.Handler;
23 import android.os.Messenger;
24 import android.os.Looper;
25 import android.os.Message;
26 import android.os.SystemClock;
27 import android.widget.RemoteViews;
29 public class Uploader extends Service implements Runnable, UserInfo, UIKeyboardInteractive {
31 private String homedir;
33 private static final int HELPME_ID = 1;
34 private RemoteViews remote;
35 private int thenotifid;
36 private Notification thenotif;
37 private String headline;
40 private InputStream is;
42 public Object _theObject;
44 private void sayNullNotification(final String scroller, final String headline, final String description)
46 int bogon = (int)SystemClock.elapsedRealtime();
47 NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
48 Notification notification = new Notification(R.drawable.icon, scroller, System.currentTimeMillis());
50 Intent intent = new Intent(this, NotifSlave.class);
52 intent.setAction("com.joshuawise.dumload.NotifSlave");
53 /* no extras to make the notifslave die */
54 intent.setData((Uri.parse("suckit://"+SystemClock.elapsedRealtime())));
56 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
57 notification.defaults |= Notification.DEFAULT_VIBRATE;
58 notification.flags |= Notification.FLAG_AUTO_CANCEL;
59 notification.setLatestEventInfo(getApplicationContext(), headline, description, contentIntent);
61 mNotificationManager.notify(bogon, notification);
64 private Object /* pick one type, and fixate on it */ dance(final String type, final String text) /* for inside the thread */
66 final Uploader thisupl = this;
67 final Message msg = Message.obtain();
70 Thread t = new Thread() {
73 int bogon = (int)SystemClock.elapsedRealtime();
75 NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
76 Notification notification = new Notification(R.drawable.icon, "Dumload prompt", System.currentTimeMillis());
78 Handler h = new Handler() {
79 public void handleMessage(Message M) {
81 Looper.myLooper().quit();
84 Messenger m = new Messenger(h);
86 Intent intent = new Intent(thisupl, NotifSlave.class);
88 intent.setAction("com.joshuawise.dumload.NotifSlave");
89 intent.putExtra("com.joshuawise.dumload.returnmessenger", m);
90 intent.putExtra("com.joshuawise.dumload.reqtype", type);
91 intent.putExtra("com.joshuawise.dumload.prompt", text);
92 intent.setData((Uri.parse("suckit://"+SystemClock.elapsedRealtime())));
94 PendingIntent contentIntent = PendingIntent.getActivity(thisupl, 0, intent, 0);
95 notification.defaults |= Notification.DEFAULT_VIBRATE;
96 notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
97 notification.setLatestEventInfo(getApplicationContext(), "I've been had!", "Dumload needs your input.", contentIntent);
99 Log.e("Dumload.Uploader[thread]", "Notifying...");
101 mNotificationManager.notify(bogon, notification);
103 Log.e("Dumload.Uploader[thread]", "About to go to 'sleep'...");
105 Log.e("Dumload.Uploader[thread]", "And we're alive!");
107 Log.e("Dumload.Uploader[thread]", "result was: "+(Integer.toString(msg.arg1)));
109 mNotificationManager.cancel(bogon);
116 } catch (Exception e) {
120 if (type.equals("yesno"))
121 return new Boolean(msg.arg1 == 1);
122 else if (type.equals("message"))
124 else if (type.equals("password")) {
127 Bundle b = msg.getData();
128 return b.getString("response");
134 String _password = null;
135 public String getPassword()
139 public boolean promptPassword(String message)
141 _password = (String)dance("password", message);
142 return (_password != null);
145 String _passphrase = null;
146 public String getPassphrase()
150 public boolean promptPassphrase(String message)
152 _passphrase = (String)dance("password", message);
153 return (_passphrase != null);
156 public boolean promptYesNo(String str)
158 return ((Boolean)dance("yesno", str)).booleanValue();
161 public void showMessage(String str)
163 dance("message", str);
166 public String[] promptKeyboardInteractive(String dest, String name, String instr, String[] prompt, boolean[] echo)
169 String [] responses = new String[prompt.length];
171 Log.e("Dumload.Uploader", "dest: "+dest);
172 Log.e("Dumload.Uploader", "name: "+name);
173 Log.e("Dumload.Uploader", "instr: "+instr);
174 for (i = 0; i < prompt.length; i++)
176 responses[i] = (String) dance("password", "[" + dest + "]\n" + prompt[i]);
177 if (responses[i] == null)
183 private void expect_ack(InputStream in) throws Exception, java.io.IOException
189 throw new Exception("unexpected EOF from remote end");
192 if (b == 1 /* error */ || b == 2 /* fatal error */)
194 StringBuffer sb = new StringBuffer();
197 while ((c = in.read()) != '\n')
200 throw new Exception("error from remote end: " + sb.toString());
204 private void set_up_notif(final String _headline)
206 NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
207 thenotif = new Notification(R.drawable.icon, headline, System.currentTimeMillis());
208 thenotifid = (int)SystemClock.elapsedRealtime();
210 Intent intent = new Intent(this, NotifSlave.class);
212 headline = _headline;
214 intent.setAction("com.joshuawise.dumload.NotifSlave");
215 /* no extras to make the notifslave die */
216 intent.setData((Uri.parse("suckit://"+SystemClock.elapsedRealtime())));
218 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
219 thenotif.defaults |= 0;
220 thenotif.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
222 remote = new RemoteViews(getPackageName(), R.layout.textnotif);
223 remote.setImageViewResource(R.id.image, R.drawable.icon);
224 remote.setTextViewText(R.id.headline, headline);
225 remote.setTextViewText(R.id.status, "Beginning upload...");
226 thenotif.contentView = remote;
227 thenotif.contentIntent = contentIntent;
229 mNotificationManager.notify(thenotifid, thenotif);
232 private void destroy_notif()
234 NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
235 mNotificationManager.cancel(thenotifid);
238 private void update_notif(String text)
240 NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
242 remote = new RemoteViews(getPackageName(), R.layout.textnotif);
243 remote.setImageViewResource(R.id.image, R.drawable.icon);
244 remote.setTextViewText(R.id.headline, headline);
245 remote.setTextViewText(R.id.status, text);
246 thenotif.contentView = remote;
248 mNotificationManager.notify(thenotifid, thenotif);
251 private void update_notif(int n, int total)
253 NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
255 remote = new RemoteViews(getPackageName(), R.layout.progressnotif);
256 remote.setImageViewResource(R.id.image, R.drawable.icon);
257 remote.setTextViewText(R.id.headline, headline);
258 remote.setProgressBar(R.id.status, total, n, false);
259 thenotif.contentView = remote;
261 mNotificationManager.notify(thenotifid, thenotif);
269 Log.e("Dumload.Uploader[thread]", "This brought to you from the new thread.");
271 set_up_notif("Dumload upload: " + dest);
274 say("Uploading "+(Integer.toString(is.available()))+" bytes");
276 update_notif("Connecting...");
278 JSch jsch = new JSch();
279 jsch.setKnownHosts(homedir + "/known_hosts");
281 jsch.addIdentity(homedir + "/id_dsa");
282 } catch (java.lang.Exception e) {
284 Session s = jsch.getSession("joshua", "nyus.joshuawise.com", 22);
288 Channel channel = s.openChannel("exec");
289 ((ChannelExec)channel).setCommand("scp -t "+dest);
292 OutputStream scp_out = channel.getOutputStream();
293 InputStream scp_in = channel.getInputStream();
295 update_notif("Starting send...");
297 /* Okay, BS out of the way. Now go send the file. */
301 if (dest.lastIndexOf("/") > 0)
302 stfu = dest.substring(dest.lastIndexOf("/") + 1);
306 scp_out.write(("C0644 " + (Integer.toString(is.available())) + " "+stfu+"\n").getBytes());
312 total = is.available();
315 byte[] buf = new byte[4096];
316 while ((len = is.read(buf, 0, buf.length)) > 0)
318 scp_out.write(buf, 0, len);
320 update_notif(nbytes, total);
325 update_notif("Finishing file transfer...");
327 scp_out.write("\0".getBytes());
332 channel.disconnect();
334 update_notif("Preparing to resize image...");
336 channel = s.openChannel("exec");
337 ((ChannelExec)channel).setCommand("pscale "+dest);
340 scp_in = channel.getInputStream();
342 update_notif("Resizing image...");
343 while ((len = scp_in.read(buf, 0, buf.length)) > 0)
346 channel.disconnect();
347 update_notif("Upload complete.");
349 sayNullNotification("Dumload upload complete: " + dest, "Upload complete", "Uploaded: " + dest);
352 } catch (Exception e) {
353 Log.e("Dumload.uploader[thread]", "JSchException: "+(e.toString()));
354 sayNullNotification("Dumload upload failed", "Upload failed", e.toString());
359 Log.e("Dumload.uploader[thread]", "And now I'm back to life!");
362 private void say(String s) {
363 Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
367 public void onStart(Intent i, int startId)
370 dest = i.getStringExtra("com.joshuawise.dumload.dest");
371 homedir = getApplicationContext().getFilesDir().getAbsolutePath();
374 super.onStart(i, startId);
376 Log.e("Dumload.Uploader", "Started.");
377 Log.e("Dumload.Uploader", "My path is "+homedir);
380 is = getContentResolver().openInputStream(uri);
381 } catch (Exception e) {
382 say("Failed to open input file.");
387 me = new Thread(this, "Uploader thread");
392 public IBinder onBind(Intent i) {
393 Log.e("Dumload.Uploader", "bound");