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;
28 public class Uploader extends Service implements Runnable, UserInfo, UIKeyboardInteractive {
30 private String homedir;
32 private static final int HELPME_ID = 1;
34 private InputStream is;
36 public Object _theObject;
38 private Object /* pick one type, and fixate on it */ dance(final String type, final String text) /* for inside the thread */
40 final Uploader thisupl = this;
41 final Message msg = Message.obtain();
44 Thread t = new Thread() {
47 int bogon = (int)SystemClock.elapsedRealtime();
49 NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
50 Notification notification = new Notification(R.drawable.icon, "Dumload prompt", System.currentTimeMillis());
52 Handler h = new Handler() {
53 public void handleMessage(Message M) {
55 Looper.myLooper().quit();
58 Messenger m = new Messenger(h);
60 Intent intent = new Intent(thisupl, NotifSlave.class);
62 intent.setAction("com.joshuawise.dumload.NotifSlave");
63 intent.putExtra("com.joshuawise.dumload.returnmessenger", m);
64 intent.putExtra("com.joshuawise.dumload.reqtype", type);
65 intent.putExtra("com.joshuawise.dumload.prompt", text);
66 intent.setData((Uri.parse("suckit://"+SystemClock.elapsedRealtime())));
68 PendingIntent contentIntent = PendingIntent.getActivity(thisupl, 0, intent, 0);
69 notification.defaults |= Notification.DEFAULT_VIBRATE;
70 notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
71 notification.setLatestEventInfo(getApplicationContext(), "I've been had!", "Dumload needs your input.", contentIntent);
73 Log.e("Dumload.Uploader[thread]", "Notifying...");
75 mNotificationManager.notify(bogon, notification);
77 Log.e("Dumload.Uploader[thread]", "About to go to 'sleep'...");
79 Log.e("Dumload.Uploader[thread]", "And we're alive!");
81 Log.e("Dumload.Uploader[thread]", "result was: "+(Integer.toString(msg.arg1)));
83 mNotificationManager.cancel(bogon);
90 } catch (Exception e) {
94 if (type.equals("yesno"))
95 return new Boolean(msg.arg1 == 1);
96 else if (type.equals("message"))
98 else if (type.equals("password")) {
101 Bundle b = msg.getData();
102 return b.getString("response");
108 String _password = null;
109 public String getPassword()
113 public boolean promptPassword(String message)
115 _password = (String)dance("password", message);
116 return (_password != null);
119 String _passphrase = null;
120 public String getPassphrase()
124 public boolean promptPassphrase(String message)
126 _passphrase = (String)dance("password", message);
127 return (_passphrase != null);
130 public boolean promptYesNo(String str)
132 return ((Boolean)dance("yesno", str)).booleanValue();
135 public void showMessage(String str)
137 dance("message", str);
140 public String[] promptKeyboardInteractive(String dest, String name, String instr, String[] prompt, boolean[] echo)
143 String [] responses = new String[prompt.length];
145 Log.e("Dumload.Uploader", "dest: "+dest);
146 Log.e("Dumload.Uploader", "name: "+name);
147 Log.e("Dumload.Uploader", "instr: "+instr);
148 for (i = 0; i < prompt.length; i++)
150 responses[i] = (String) dance("password", "[" + dest + "]\n" + prompt[i]);
155 private void expect_ack(InputStream in) throws Exception, java.io.IOException
161 throw new Exception("unexpected EOF from remote end");
164 if (b == 1 /* error */ || b == 2 /* fatal error */)
166 StringBuffer sb = new StringBuffer();
169 while ((c = in.read()) != '\n')
172 throw new Exception("error from remote end: " + sb.toString());
181 Log.e("Dumload.Uploader[thread]", "This brought to you from the new thread.");
184 say("Uploading "+(Integer.toString(is.available()))+" bytes");
186 JSch jsch = new JSch();
187 jsch.setKnownHosts(homedir + "/known_hosts");
188 Session s = jsch.getSession("joshua", "nyus.joshuawise.com", 22);
192 Channel channel = s.openChannel("exec");
193 ((ChannelExec)channel).setCommand("scp -t /tmp/lol");
196 OutputStream scp_out = channel.getOutputStream();
197 InputStream scp_in = channel.getInputStream();
199 /* Okay, BS out of the way. Now go send the file. */
202 scp_out.write(("C0644 " + (Integer.toString(is.available())) + " lol\n").getBytes());
208 byte[] buf = new byte[4096];
209 while ((len = is.read(buf, 0, buf.length)) > 0)
210 scp_out.write(buf, 0, len);
214 scp_out.write("\0".getBytes());
219 dance("message", "done");
221 channel.disconnect();
223 } catch (Exception e) {
224 Log.e("Dumload.uploader[thread]", "JSchException: "+(e.toString()));
227 Log.e("Dumload.uploader[thread]", "And now I'm back to life!");
230 private void say(String s) {
231 Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
235 public void onStart(Intent i, int startId)
238 homedir = getApplicationContext().getFilesDir().getAbsolutePath();
241 super.onStart(i, startId);
243 Log.e("Dumload.Uploader", "Started.");
244 Log.e("Dumload.Uploader", "My path is "+homedir);
247 is = getContentResolver().openInputStream(uri);
248 } catch (Exception e) {
249 say("Failed to open input file.");
254 me = new Thread(this, "Uploader thread");
259 public IBinder onBind(Intent i) {
260 Log.e("Dumload.Uploader", "bound");