Bug 697098: Do not scroll tab list on onTabsChanged() [r=mfinkle]
authorSriram Ramasubramanian <sriram@mozilla.com>
Thu, 17 Nov 2011 15:31:08 -0800
changeset 81897 d4982d5a23cfffb1db1bb48c398d3d3dff303b2d
parent 81896 a5032397850bf2718d2ffd63ef991264a6473425
child 81898 fa4ef7d24a262ff7cdfd2a67b713b9088687c5f6
push idunknown
push userunknown
push dateunknown
reviewersmfinkle
bugs697098
milestone11.0a1
Bug 697098: Do not scroll tab list on onTabsChanged() [r=mfinkle]
embedding/android/GeckoApp.java
embedding/android/TabsTray.java
--- a/embedding/android/GeckoApp.java
+++ b/embedding/android/GeckoApp.java
@@ -117,17 +117,17 @@ abstract public class GeckoApp
     private static LayerController mLayerController;
     private static PlaceholderLayerClient mPlaceholderLayerClient;
     private static GeckoSoftwareLayerClient mSoftwareLayerClient;
     boolean mUserDefinedProfile = false;
 
     private Vector<View> mPluginViews = new Vector<View>();
 
     public interface OnTabsChangedListener {
-        public void onTabsChanged();
+        public void onTabsChanged(Tab tab);
     }
     
     private static ArrayList<OnTabsChangedListener> mTabsChangedListeners;
 
     static class ExtraMenuItem implements MenuItem.OnMenuItemClickListener {
         String label;
         String icon;
         int id;
@@ -569,17 +569,17 @@ abstract public class GeckoApp
 
                 Log.i(LOGTAG, "Favicon is for current URL = " + pageUrl);
 
                 tab.updateFavicon(favicon);
 
                 if (Tabs.getInstance().isSelectedTab(tab))
                     mBrowserToolbar.setFavicon(tab.getFavicon());
 
-                onTabsChanged();
+                onTabsChanged(tab);
             }
         });
     }
 
     void handleLocationChange(final int tabId, final String uri) {
         final Tab tab = Tabs.getInstance().getTab(tabId);
         if (tab == null)
             return;
@@ -652,23 +652,23 @@ abstract public class GeckoApp
 
     public static void unregisterOnTabsChangedListener(OnTabsChangedListener listener) {
         if (mTabsChangedListeners == null)
             return;
         
         mTabsChangedListeners.remove(listener);
     }
 
-    public void onTabsChanged() {
+    public void onTabsChanged(Tab tab) {
         if (mTabsChangedListeners == null)
             return;
 
         Iterator items = mTabsChangedListeners.iterator();
         while (items.hasNext()) {
-            ((OnTabsChangedListener) items.next()).onTabsChanged();
+            ((OnTabsChangedListener) items.next()).onTabsChanged(tab);
         }
     }
 
     public void handleMessage(String event, JSONObject message) {
         Log.i(LOGTAG, "Got message: " + event);
         try {
             if (event.equals("Menu:Add")) {
                 String name = message.getString("name");
@@ -865,32 +865,32 @@ abstract public class GeckoApp
         final Tab tab = Tabs.getInstance().addTab(tabId, uri);
         if (selected) {
             Tabs.getInstance().selectTab(tabId);
         }
 
         mMainHandler.post(new Runnable() { 
             public void run() {
                 if (selected && Tabs.getInstance().isSelectedTab(tab)) {
-                    onTabsChanged();
+                    onTabsChanged(tab);
                     mDoorHangerPopup.updatePopup();
                 }
                 mBrowserToolbar.updateTabs(Tabs.getInstance().getCount());
             }
         });
     }
 
     void handleCloseTab(final int tabId) {
         final Tab tab = Tabs.getInstance().getTab(tabId);
         Tabs.getInstance().removeTab(tabId);
         tab.removeAllDoorHangers();
 
         mMainHandler.post(new Runnable() { 
             public void run() {
-                onTabsChanged();
+                onTabsChanged(tab);
                 mBrowserToolbar.updateTabs(Tabs.getInstance().getCount());
                 mDoorHangerPopup.updatePopup();
             }
         });
     }
 
     void handleSelectTab(int tabId) {
         final Tab tab = Tabs.getInstance().selectTab(tabId);
@@ -919,33 +919,33 @@ abstract public class GeckoApp
         tab.updateSecurityMode("unknown");
 
         mMainHandler.post(new Runnable() {
             public void run() {
                 if (Tabs.getInstance().isSelectedTab(tab)) {
                     mBrowserToolbar.setSecurityMode(tab.getSecurityMode());
                     mBrowserToolbar.setProgressVisibility(true);
                 }
-                onTabsChanged();
+                onTabsChanged(tab);
             }
         });
     }
 
     void handleDocumentStop(int tabId) {
         final Tab tab = Tabs.getInstance().getTab(tabId);
         if (tab == null)
             return;
 
         tab.setLoading(false);
 
         mMainHandler.post(new Runnable() {
             public void run() {
                 if (Tabs.getInstance().isSelectedTab(tab))
                     mBrowserToolbar.setProgressVisibility(false);
-                onTabsChanged();
+                onTabsChanged(tab);
             }
         });
     }
 
     void handleShowToast(final String message, final String duration) {
         mMainHandler.post(new Runnable() {
             public void run() {
                 Toast toast;
@@ -965,33 +965,33 @@ abstract public class GeckoApp
 
         tab.updateTitle(title);
         loadFavicon(tab);
 
         mMainHandler.post(new Runnable() {
             public void run() {
                 if (Tabs.getInstance().isSelectedTab(tab))
                     mBrowserToolbar.setTitle(tab.getDisplayTitle());
-                onTabsChanged();
+                onTabsChanged(tab);
             }
         });
     }
 
     void handleTitleChanged(int tabId, String title) {
         final Tab tab = Tabs.getInstance().getTab(tabId);
         if (tab == null)
             return;
 
         tab.updateTitle(title);
 
         mMainHandler.post(new Runnable() {
             public void run() {
                 if (Tabs.getInstance().isSelectedTab(tab))
                     mBrowserToolbar.setTitle(tab.getDisplayTitle());
-                onTabsChanged();
+                onTabsChanged(tab);
             }
         });
     }
 
     void handleLinkAdded(final int tabId, String rel, final String href) {
         if (rel.indexOf("icon") != -1) {
             Tab tab = Tabs.getInstance().getTab(tabId);
             if (tab != null) {
--- a/embedding/android/TabsTray.java
+++ b/embedding/android/TabsTray.java
@@ -92,17 +92,17 @@ public class TabsTray extends Activity i
         LinearLayout container = (LinearLayout) findViewById(R.id.container);
         container.setOnClickListener(new Button.OnClickListener() {
             public void onClick(View v) {
                 finishActivity();
             }
         });
 
         GeckoApp.registerOnTabsChangedListener(this);
-        onTabsChanged();
+        onTabsChanged(null);
     }
 
     @Override
     public void onDestroy() {
         super.onDestroy();
         GeckoApp.unregisterOnTabsChangedListener(this);
     }
 
@@ -112,22 +112,37 @@ public class TabsTray extends Activity i
         // Scrolling to the selected tab can happen here
         if (hasFocus) {
             int position = mTabsAdapter.getPositionForTab(Tabs.getInstance().getSelectedTab());
             if (position != -1) 
                 mList.smoothScrollToPosition(position);
         }
     } 
    
-    public void onTabsChanged() {
+    public void onTabsChanged(Tab tab) {
         if (Tabs.getInstance().getCount() == 1)
             finishActivity();
 
-        mTabsAdapter = new TabsAdapter(this, Tabs.getInstance().getTabsInOrder());
-        mList.setAdapter(mTabsAdapter);
+        if (mTabsAdapter == null) {
+            mTabsAdapter = new TabsAdapter(this, Tabs.getInstance().getTabsInOrder());
+            mList.setAdapter(mTabsAdapter);
+            return;
+        }
+        
+        int position = mTabsAdapter.getPositionForTab(tab);
+        if (position == -1)
+            return;
+
+        if (Tabs.getInstance().getIndexOf(tab) == -1) {
+            mTabsAdapter = new TabsAdapter(this, Tabs.getInstance().getTabsInOrder());
+            mList.setAdapter(mTabsAdapter);
+        } else {
+            View view = mList.getChildAt(position - mList.getFirstVisiblePosition());
+            mTabsAdapter.assignValues(view, tab);
+        }
     }
 
     void finishActivity() {
         finish();
         overridePendingTransition(0, R.anim.shrink_fade_out);
     }
 
     // Adapter to bind tabs into a list 
@@ -159,45 +174,52 @@ public class TabsTray extends Activity i
 
         public int getPositionForTab(Tab tab) {
             if (mTabs == null || tab == null)
                 return -1;
 
             return mTabs.indexOf(tab);
         }
 
+        public void assignValues(View view, Tab tab) {
+            if (view == null || tab == null)
+                return;
+
+            ImageView favicon = (ImageView) view.findViewById(R.id.favicon);
+
+            Drawable faviconImage = tab.getFavicon();
+            if (faviconImage != null)
+                favicon.setImageDrawable(faviconImage);
+            else
+                favicon.setImageResource(R.drawable.favicon);
+
+            TextView title = (TextView) view.findViewById(R.id.title);
+            title.setText(tab.getDisplayTitle());
+
+            if (Tabs.getInstance().isSelectedTab(tab))
+                title.setTypeface(title.getTypeface(), Typeface.BOLD);
+        }
+
         @Override    
         public View getView(int position, View convertView, ViewGroup parent) {
 
             convertView = mInflater.inflate(R.layout.tabs_row, null);
             
             Tab tab = mTabs.get(position);
 
             RelativeLayout info = (RelativeLayout) convertView.findViewById(R.id.info);
             info.setTag("" + tab.getId());
             info.setOnClickListener(new View.OnClickListener() {
                 public void onClick(View v) {
                     GeckoAppShell.sendEventToGecko(new GeckoEvent("Tab:Select", "" + v.getTag()));
                     finishActivity();
                 }
             });
 
-            ImageView favicon = (ImageView) convertView.findViewById(R.id.favicon);
-
-            Drawable faviconImage = tab.getFavicon();
-            if (faviconImage != null)
-                favicon.setImageDrawable(faviconImage);
-            else
-                favicon.setImageResource(R.drawable.favicon);
-
-            TextView title = (TextView) convertView.findViewById(R.id.title);
-            title.setText(tab.getDisplayTitle());
-
-            if (Tabs.getInstance().isSelectedTab(tab))
-                title.setTypeface(title.getTypeface(), Typeface.BOLD);
+            assignValues(convertView, tab);
             
             ImageButton close = (ImageButton) convertView.findViewById(R.id.close);
             if (mTabs.size() > 1) {
                 close.setTag("" + tab.getId());
                 close.setOnClickListener(new Button.OnClickListener() {
                     public void onClick(View v) {
                         int tabId = Integer.parseInt("" + v.getTag());
                         Tabs tabs = Tabs.getInstance();