From 159cb8195d9c1bbef85a51d7fcd544ab08a362d1 Mon Sep 17 00:00:00 2001 From: Steven Luo Date: Sun, 29 May 2011 17:49:46 -0700 Subject: [PATCH] Remember switched conversations across screen orientation changes As of now, the activity does not remember whether a conversation is switched across configuration changes (such as screen rotations). Fix this by adding onSaveInstanceState() and onRestoreInstanceState() callbacks in the activity to pass this information to the new instance. To make the implementation of this simpler, all code to configure the MessageListView, which was duplicated in several places in the codebase, has been moved to the MessageListView's constructor. While we're at it, make the padding setting independent of screen density instead of specifying in fixed pixels (equivalent to specifying the value in dp instead of px), and increase the padding for switched views. This ensures that message text isn't obscured by the gradient at the edges of the ConversationGallery, which started happening when we began caching MessageListViews in the DeckAdapter. --- .../yaaic/activity/ConversationActivity.java | 31 ++++++++- .../src/org/yaaic/adapter/DeckAdapter.java | 20 ++---- .../listener/ConversationClickListener.java | 5 +- .../src/org/yaaic/view/MessageListView.java | 66 ++++++++++++++----- 4 files changed, 84 insertions(+), 38 deletions(-) diff --git a/application/src/org/yaaic/activity/ConversationActivity.java b/application/src/org/yaaic/activity/ConversationActivity.java index 5405b4c..5238187 100644 --- a/application/src/org/yaaic/activity/ConversationActivity.java +++ b/application/src/org/yaaic/activity/ConversationActivity.java @@ -273,6 +273,33 @@ public class ConversationActivity extends Activity implements ServiceConnection, unregisterReceiver(serverReceiver); } + /** + * On save instance state (e.g. before a configuration change) + */ + @Override + protected void onSaveInstanceState(Bundle outState) + { + super.onSaveInstanceState(outState); + + if (deckAdapter.isSwitched()) { + outState.putBoolean("isSwitched", deckAdapter.isSwitched()); + outState.putString("switchedName", deckAdapter.getSwitchedName()); + } + } + + /** + * On restore instance state (e.g. after a configuration change) + */ + @Override + protected void onRestoreInstanceState(Bundle inState) + { + super.onRestoreInstanceState(inState); + + if (inState.getBoolean("isSwitched")) { + deckAdapter.setSwitched(inState.getString("switchedName"), null); + } + } + /** * On service connected */ @@ -528,9 +555,7 @@ public class ConversationActivity extends Activity implements ServiceConnection, if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { if (deckAdapter.isSwitched()) { MessageListView canvas = (MessageListView) deckAdapter.getView(deckAdapter.getPositionByName(deckAdapter.getSwitchedName()), null, switcher); - canvas.setLayoutParams(new Gallery.LayoutParams(switcher.getWidth()*85/100, switcher.getHeight())); - canvas.setTranscriptMode(MessageListView.TRANSCRIPT_MODE_ALWAYS_SCROLL); - canvas.setDelegateTouchEvents(true); + canvas.setSwitched(false); deckAdapter.setSwitched(null, null); return true; } diff --git a/application/src/org/yaaic/adapter/DeckAdapter.java b/application/src/org/yaaic/adapter/DeckAdapter.java index b6edd24..387c4cc 100644 --- a/application/src/org/yaaic/adapter/DeckAdapter.java +++ b/application/src/org/yaaic/adapter/DeckAdapter.java @@ -225,7 +225,7 @@ public class DeckAdapter extends BaseAdapter */ public boolean isSwitched() { - return currentView != null; + return currentChannel != null; } /** @@ -272,20 +272,12 @@ public class DeckAdapter extends BaseAdapter } list.setAdapter(adapter); + list.setSelection(adapter.getCount() - 1); // scroll to bottom - list.setDivider(null); - list.setLayoutParams(new Gallery.LayoutParams( - parent.getWidth()*85/100, - parent.getHeight() - )); - - list.setBackgroundResource(R.layout.rounded); - list.setCacheColorHint(0xee000000); - list.setPadding(5, 5, 5, 5); - list.setVerticalFadingEdgeEnabled(false); - list.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_INSET); - list.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL); - list.setSelection(list.getAdapter().getCount() - 1); // scroll to bottom + if (convInfo.conv.getName().equals(currentChannel)) { + list.setSwitched(true); + currentView = list; + } return list; } diff --git a/application/src/org/yaaic/listener/ConversationClickListener.java b/application/src/org/yaaic/listener/ConversationClickListener.java index a3d8b91..57abe92 100644 --- a/application/src/org/yaaic/listener/ConversationClickListener.java +++ b/application/src/org/yaaic/listener/ConversationClickListener.java @@ -62,10 +62,7 @@ public class ConversationClickListener implements OnItemClickListener Conversation conversation = adapter.getItem(position); MessageListView canvas = (MessageListView) adapter.getView(position, null, switcher); - canvas.setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL); - canvas.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); - canvas.setDelegateTouchEvents(false); // Do not delegate - + canvas.setSwitched(true); adapter.setSwitched(conversation.getName(), canvas); } } diff --git a/application/src/org/yaaic/view/MessageListView.java b/application/src/org/yaaic/view/MessageListView.java index cf2f28d..ff1f6d4 100644 --- a/application/src/org/yaaic/view/MessageListView.java +++ b/application/src/org/yaaic/view/MessageListView.java @@ -20,6 +20,7 @@ along with Yaaic. If not, see . */ package org.yaaic.view; +import org.yaaic.R; import org.yaaic.adapter.MessageListAdapter; import org.yaaic.listener.MessageClickListener; @@ -37,10 +38,12 @@ import android.widget.ListView; */ public class MessageListView extends ListView { - private boolean delegate = true; + private boolean switched = false; private final View parent; private int parentWidth; private int parentHeight; + private int padding; + private int paddingWide; /** * Create a new MessageListView @@ -52,20 +55,28 @@ public class MessageListView extends ListView super(context); this.parent = parent; - this.setOnItemClickListener(MessageClickListener.getInstance()); + setOnItemClickListener(MessageClickListener.getInstance()); parentWidth = parent.getWidth(); parentHeight = parent.getHeight(); - } - /** - * Should all touch events delegated? - * - * @param delegate If true all touch events will be delegated, otherwise the listview will handle them - */ - public void setDelegateTouchEvents(boolean delegate) - { - this.delegate = delegate; + setDivider(null); + setLayoutParams(new Gallery.LayoutParams( + parentWidth*85/100, + parentHeight + )); + + setBackgroundResource(R.layout.rounded); + setCacheColorHint(0xee000000); + setVerticalFadingEdgeEnabled(false); + setScrollBarStyle(SCROLLBARS_OUTSIDE_INSET); + setTranscriptMode(TRANSCRIPT_MODE_ALWAYS_SCROLL); + + // Scale padding by screen density + float density = context.getResources().getDisplayMetrics().density; + padding = (int)(5 * density); + paddingWide = (int)(12 * density); + setPadding(padding, padding, padding, padding); } /** @@ -74,7 +85,7 @@ public class MessageListView extends ListView @Override public boolean onTouchEvent(MotionEvent event) { - if (delegate) { + if (!switched) { // We delegate the touch events to the underlying view return false; } else { @@ -88,16 +99,18 @@ public class MessageListView extends ListView @Override protected void onDraw(Canvas canvas) { - if (delegate && (parent.getWidth() != parentWidth || parent.getHeight() != parentHeight)) { + if (parent.getWidth() != parentWidth || parent.getHeight() != parentHeight) { // parent size changed, resizing this child too parentWidth = parent.getWidth(); parentHeight = parent.getHeight(); - this.setLayoutParams(new Gallery.LayoutParams( - parentWidth*85/100, - parentHeight - )); + if (!switched) { + setLayoutParams(new Gallery.LayoutParams( + parentWidth*85/100, + parentHeight + )); + } } super.onDraw(canvas); @@ -114,4 +127,23 @@ public class MessageListView extends ListView { return (MessageListAdapter) super.getAdapter(); } + + /** + * Set whether this conversation is switched (taking up all of deck's space + * and handling touch events itself) + */ + public void setSwitched(boolean switched) + { + this.switched = switched; + + if (switched) { + setLayoutParams(new Gallery.LayoutParams(Gallery.LayoutParams.FILL_PARENT, Gallery.LayoutParams.FILL_PARENT)); + setTranscriptMode(TRANSCRIPT_MODE_NORMAL); + setPadding(paddingWide, padding, paddingWide, padding); + } else { + setLayoutParams(new Gallery.LayoutParams(parentWidth*85/100, parentHeight)); + setTranscriptMode(TRANSCRIPT_MODE_ALWAYS_SCROLL); + setPadding(padding, padding, padding, padding); + } + } }