From dedfdb582a3ce9dc50b41875b1e42541f07ebd21 Mon Sep 17 00:00:00 2001 From: Jesse Young Date: Sat, 1 Oct 2011 15:45:28 -0700 Subject: [PATCH] improve performance of displaying log in Main activity --- src/org/envaya/sms/App.java | 15 ++++++++- src/org/envaya/sms/Inbox.java | 4 +++ src/org/envaya/sms/Outbox.java | 5 +++ src/org/envaya/sms/ui/Main.java | 37 ++++++++++++++++++---- src/org/envaya/sms/ui/PendingMessages.java | 4 +++ 5 files changed, 57 insertions(+), 8 deletions(-) diff --git a/src/org/envaya/sms/App.java b/src/org/envaya/sms/App.java index 42efde3..3da20a7 100755 --- a/src/org/envaya/sms/App.java +++ b/src/org/envaya/sms/App.java @@ -86,7 +86,7 @@ public final class App extends Application { public static final String STATUS_EXTRA_INDEX = "status"; public static final String STATUS_EXTRA_NUM_PARTS = "num_parts"; - public static final int MAX_DISPLAYED_LOG = 4000; + public static final int MAX_DISPLAYED_LOG = 8000; public static final int LOG_TIMESTAMP_INTERVAL = 60000; // ms public static final int HTTP_CONNECTION_TIMEOUT = 10000; // ms @@ -437,6 +437,8 @@ public final class App extends Application { Log.d(LOG_NAME, msg); } + private int logEpoch = 0; + public synchronized void log(CharSequence msg) { Log.d(LOG_NAME, msg.toString()); @@ -457,6 +459,7 @@ public final class App extends Application { } displayedLog.replace(0, startPos, "[Older log messages not shown]\n"); + logEpoch++; } // display a timestamp in the log occasionally @@ -473,6 +476,16 @@ public final class App extends Application { sendBroadcast(new Intent(App.LOG_CHANGED_INTENT)); } + + /* + * Changes whenever we change the beginning of the displayed log. + * If it doesn't change, the Main activity can update the log view much + * faster by using TextView.append() instead of TextView.setText() + */ + public int getLogEpoch() + { + return logEpoch; + } public synchronized CharSequence getDisplayedLog() { diff --git a/src/org/envaya/sms/Inbox.java b/src/org/envaya/sms/Inbox.java index 1c7cc84..b4d1558 100755 --- a/src/org/envaya/sms/Inbox.java +++ b/src/org/envaya/sms/Inbox.java @@ -79,6 +79,10 @@ public class Inbox { { incomingQueue.remove(message); } + else if (message.getProcessingState() == IncomingMessage.ProcessingState.Forwarding) + { + numForwardingMessages--; + } app.log(message.getDescription() + " deleted"); notifyChanged(); diff --git a/src/org/envaya/sms/Outbox.java b/src/org/envaya/sms/Outbox.java index 4ebdf43..4032b96 100755 --- a/src/org/envaya/sms/Outbox.java +++ b/src/org/envaya/sms/Outbox.java @@ -176,6 +176,11 @@ public class Outbox { { outgoingQueue.remove(message); } + else if (message.getProcessingState() == OutgoingMessage.ProcessingState.Sending) + { + numSendingOutgoingMessages--; + } + notifyMessageStatus(message, App.STATUS_FAILED, "deleted by user"); app.log(message.getDescription() + " deleted"); diff --git a/src/org/envaya/sms/ui/Main.java b/src/org/envaya/sms/ui/Main.java index 75bcba9..87c1db3 100755 --- a/src/org/envaya/sms/ui/Main.java +++ b/src/org/envaya/sms/ui/Main.java @@ -31,6 +31,9 @@ public class Main extends Activity { } }; + private ScrollView scrollView; + private TextView info; + private class TestTask extends HttpTask { public TestTask() { @@ -44,14 +47,32 @@ public class Main extends Activity { app.log("Server connection OK!"); } } + + private int lastLogEpoch = -1; public void updateLogView() - { - final ScrollView scrollView = (ScrollView) this.findViewById(R.id.info_scroll); - TextView info = (TextView) this.findViewById(R.id.info); - - info.setText(app.getDisplayedLog()); + { + int logEpoch = app.getLogEpoch(); + CharSequence displayedLog = app.getDisplayedLog(); + if (lastLogEpoch == logEpoch) + { + int beforeLen = info.getText().length(); + int afterLen = displayedLog.length(); + + if (beforeLen == afterLen) + { + return; + } + + info.append(displayedLog, beforeLen, afterLen); + } + else + { + info.setText(displayedLog); + lastLogEpoch = logEpoch; + } + scrollView.post(new Runnable() { public void run() { scrollView.fullScroll(View.FOCUS_DOWN); } }); @@ -63,11 +84,13 @@ public class Main extends Activity { super.onCreate(savedInstanceState); app = (App) getApplication(); - + setContentView(R.layout.main); PreferenceManager.setDefaultValues(this, R.xml.prefs, false); - TextView info = (TextView) this.findViewById(R.id.info); + scrollView = (ScrollView) this.findViewById(R.id.info_scroll); + info = (TextView) this.findViewById(R.id.info); + info.setMovementMethod(new ScrollingMovementMethod()); updateLogView(); diff --git a/src/org/envaya/sms/ui/PendingMessages.java b/src/org/envaya/sms/ui/PendingMessages.java index 9105340..3dc8082 100755 --- a/src/org/envaya/sms/ui/PendingMessages.java +++ b/src/org/envaya/sms/ui/PendingMessages.java @@ -123,6 +123,8 @@ public class PendingMessages extends ListActivity { displayedMessages = messages; + this.setTitle("EnvayaSMS : Pending Messages ("+messages.size()+")"); + final LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); final DateFormat longFormat = new SimpleDateFormat("dd MMM hh:mm:ss"); final DateFormat shortFormat = new SimpleDateFormat("hh:mm:ss"); @@ -169,6 +171,8 @@ public class PendingMessages extends ListActivity { }; setListAdapter(arrayAdapter); + + } public void deleteMessage(QueuedMessage message)