Add a progress bar.
authorJoshua Wise <jwise@andrew.cmu.edu>
Sat, 7 Aug 2010 07:57:46 +0000 (03:57 -0400)
committerJoshua Wise <jwise@andrew.cmu.edu>
Sat, 7 Aug 2010 07:57:46 +0000 (03:57 -0400)
res/layout/progressnotif.xml [new file with mode: 0644]
res/layout/textnotif.xml [new file with mode: 0644]
src/com/joshuawise/dumload/NotifSlave.java
src/com/joshuawise/dumload/Uploader.java

diff --git a/res/layout/progressnotif.xml b/res/layout/progressnotif.xml
new file mode 100644 (file)
index 0000000..d742e57
--- /dev/null
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:orientation="horizontal"
+    android:layout_width="fill_parent"
+    android:layout_height="fill_parent"
+    android:padding="3dp"
+    >
+
+<ImageView
+    android:id="@+id/image"
+    android:layout_width="wrap_content"
+    android:layout_height="fill_parent"
+    android:layout_marginRight="10dp"
+    />
+
+<LinearLayout
+    android:orientation="vertical"
+    android:layout_width="fill_parent"
+    android:layout_height="wrap_content"
+    >
+<TextView  
+    android:id="@+id/headline"
+    android:layout_width="fill_parent" 
+    android:layout_height="wrap_content" 
+    android:textStyle="bold"
+    />
+<ProgressBar
+    android:id="@+id/status"
+    android:layout_width="fill_parent" 
+    android:layout_height="wrap_content" 
+    style="?android:attr/progressBarStyleHorizontal"
+    />
+
+</LinearLayout>
+</LinearLayout>
diff --git a/res/layout/textnotif.xml b/res/layout/textnotif.xml
new file mode 100644 (file)
index 0000000..1e1060a
--- /dev/null
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:orientation="horizontal"
+    android:layout_width="fill_parent"
+    android:layout_height="fill_parent"
+    android:padding="3dp"
+    >
+
+<ImageView
+    android:id="@+id/image"
+    android:layout_width="wrap_content"
+    android:layout_height="fill_parent"
+    android:layout_marginRight="10dp"
+    />
+
+<LinearLayout
+    android:orientation="vertical"
+    android:layout_width="fill_parent"
+    android:layout_height="wrap_content"
+    >
+<TextView  
+    android:id="@+id/headline"
+    android:layout_width="fill_parent" 
+    android:layout_height="wrap_content" 
+    android:textStyle="bold"
+    />
+<TextView  
+    android:id="@+id/status"
+    android:layout_width="fill_parent" 
+    android:layout_height="wrap_content" 
+    />
+
+</LinearLayout>
+</LinearLayout>
index 21af25e..40db024 100644 (file)
@@ -62,7 +62,12 @@ public class NotifSlave extends Activity {
                final Messenger m = (Messenger)i.getParcelableExtra("com.joshuawise.dumload.returnmessenger");
                String reqtype = i.getStringExtra("com.joshuawise.dumload.reqtype");
                String prompt = i.getStringExtra("com.joshuawise.dumload.prompt");
-               /* If any of these were null, we'll just take the exception. */
+               
+               if (prompt == null || reqtype == null || m == null)     /* i.e., we got called by a dummy notification */
+               {
+                       this.finish();
+                       return;
+               }
        
                if (reqtype.equals("yesno")) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(this);
index 56ac043..763e3f0 100644 (file)
@@ -24,17 +24,42 @@ import android.os.Messenger;
 import android.os.Looper;
 import android.os.Message;
 import android.os.SystemClock;
+import android.widget.RemoteViews;
 
 public class Uploader extends Service implements Runnable, UserInfo, UIKeyboardInteractive {
        private Uri uri;
        private String homedir;
        private Thread me;
        private static final int HELPME_ID = 1;
+       private RemoteViews remote;
+       private int thenotifid;
+       private Notification thenotif;
+       private String headline;
        
        private InputStream is;
        
        public Object _theObject;
 
+       private void sayNullNotification(final String scroller, final String headline, final String description)
+       {
+               int bogon = (int)SystemClock.elapsedRealtime();
+               NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
+               Notification notification = new Notification(R.drawable.icon, scroller, System.currentTimeMillis());
+
+               Intent intent = new Intent(this, NotifSlave.class);
+                                       
+               intent.setAction("com.joshuawise.dumload.NotifSlave");
+               /* no extras to make the notifslave die */
+               intent.setData((Uri.parse("suckit://"+SystemClock.elapsedRealtime())));
+                               
+               PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
+               notification.defaults |= Notification.DEFAULT_VIBRATE;
+               notification.flags |= Notification.FLAG_AUTO_CANCEL;
+               notification.setLatestEventInfo(getApplicationContext(), headline, description, contentIntent);
+                               
+               mNotificationManager.notify(bogon, notification);
+       }
+
        private Object /* pick one type, and fixate on it */ dance(final String type, final String text)        /* for inside the thread */
        {
                final Uploader thisupl = this;
@@ -67,7 +92,7 @@ public class Uploader extends Service implements Runnable, UserInfo, UIKeyboardI
                                
                                PendingIntent contentIntent = PendingIntent.getActivity(thisupl, 0, intent, 0);
                                notification.defaults |= Notification.DEFAULT_VIBRATE;
-                               notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
+                               notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
                                notification.setLatestEventInfo(getApplicationContext(), "I've been had!", "Dumload needs your input.", contentIntent);
                                
                                Log.e("Dumload.Uploader[thread]", "Notifying...");
@@ -148,6 +173,8 @@ public class Uploader extends Service implements Runnable, UserInfo, UIKeyboardI
                for (i = 0; i < prompt.length; i++)
                {
                        responses[i] = (String) dance("password", "[" + dest + "]\n" + prompt[i]);
+                       if (responses[i] == null)
+                               return null;
                }
                return responses;
        }
@@ -173,6 +200,66 @@ public class Uploader extends Service implements Runnable, UserInfo, UIKeyboardI
                }
        }
        
+       private void set_up_notif(final String _headline)
+       {
+               NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
+               thenotif = new Notification(R.drawable.icon, headline, System.currentTimeMillis());
+               thenotifid = (int)SystemClock.elapsedRealtime();
+
+               Intent intent = new Intent(this, NotifSlave.class);
+               
+               headline = _headline;
+                                       
+               intent.setAction("com.joshuawise.dumload.NotifSlave");
+               /* no extras to make the notifslave die */
+               intent.setData((Uri.parse("suckit://"+SystemClock.elapsedRealtime())));
+                               
+               PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
+               thenotif.defaults |= 0;
+               thenotif.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
+               
+               remote = new RemoteViews(getPackageName(), R.layout.textnotif);
+               remote.setImageViewResource(R.id.image, R.drawable.icon);
+               remote.setTextViewText(R.id.headline, headline);
+               remote.setTextViewText(R.id.status, "Beginning upload...");
+               thenotif.contentView = remote;
+               thenotif.contentIntent = contentIntent;
+                               
+               mNotificationManager.notify(thenotifid, thenotif);
+       }
+       
+       private void destroy_notif()
+       {
+               NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
+               mNotificationManager.cancel(thenotifid);
+       }
+       
+       private void update_notif(String text)
+       {
+               NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
+               
+               remote = new RemoteViews(getPackageName(), R.layout.textnotif);
+               remote.setImageViewResource(R.id.image, R.drawable.icon);
+               remote.setTextViewText(R.id.headline, headline);
+               remote.setTextViewText(R.id.status, text);
+               thenotif.contentView = remote;
+               
+               mNotificationManager.notify(thenotifid, thenotif);
+       }
+       
+       private void update_notif(int n, int total)
+       {
+               NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
+               
+               remote = new RemoteViews(getPackageName(), R.layout.progressnotif);
+               remote.setImageViewResource(R.id.image, R.drawable.icon);
+               remote.setTextViewText(R.id.headline, headline);
+               remote.setProgressBar(R.id.status, total, n, false);
+               thenotif.contentView = remote;
+               
+               mNotificationManager.notify(thenotifid, thenotif);
+       }
+       
        @Override
        public void run()
        {
@@ -180,9 +267,13 @@ public class Uploader extends Service implements Runnable, UserInfo, UIKeyboardI
                
                Log.e("Dumload.Uploader[thread]", "This brought to you from the new thread.");
                
+               set_up_notif("Dumload upload in progress");
+               
                try {
                        say("Uploading "+(Integer.toString(is.available()))+" bytes");
                
+                       update_notif("Connecting...");
+               
                        JSch jsch = new JSch();
                        jsch.setKnownHosts(homedir + "/known_hosts");
                        Session s = jsch.getSession("joshua", "nyus.joshuawise.com", 22);
@@ -196,6 +287,8 @@ public class Uploader extends Service implements Runnable, UserInfo, UIKeyboardI
                        OutputStream scp_out = channel.getOutputStream();
                        InputStream scp_in = channel.getInputStream();
                        
+                       update_notif("Starting send...");
+                       
                        /* Okay, BS out of the way.  Now go send the file. */
                        expect_ack(scp_in);
                        
@@ -204,26 +297,38 @@ public class Uploader extends Service implements Runnable, UserInfo, UIKeyboardI
                        
                        expect_ack(scp_in);
                        
+                       int total, nbytes;
+                       total = is.available();
+                       nbytes = 0;
                        int len;
                        byte[] buf = new byte[4096];
                        while ((len = is.read(buf, 0, buf.length)) > 0)
+                       {
                                scp_out.write(buf, 0, len);
+                               nbytes += len;
+                               update_notif(nbytes, total);
+                       }
                        
                        is.close();
                        
+                       update_notif("Disconnecting...");
+                       
                        scp_out.write("\0".getBytes());
                        scp_out.flush();
                        
                        expect_ack(scp_in);
                        
-                       dance("message", "done");
+                       sayNullNotification("Dumload upload complete", "Upload complete", "Dumload has finished uploading your file.");
 
                        channel.disconnect();
                        s.disconnect();
                } catch (Exception e) {
                        Log.e("Dumload.uploader[thread]", "JSchException: "+(e.toString()));
+                       sayNullNotification("Dumload upload failed", "Upload failed", e.toString());
                }
                
+               destroy_notif();
+               
                Log.e("Dumload.uploader[thread]", "And now I'm back to life!");
        }
        
This page took 0.02401 seconds and 4 git commands to generate.