Bug 778247: mAppContext cleanup: TabsPanel doesn't need it. [r=mbrubeck]
authorSriram Ramasubramanian <sriram@mozilla.com>
Fri, 27 Jul 2012 23:31:40 -0700
changeset 100815 985bfd3aea74a596329855231817d9654eb8fd1c
parent 100814 b2a7b60ef3b12c4b8c0187727ab9a80492152b23
child 100816 123e9052f37eeebb061101d36b7d5845a4d97b21
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersmbrubeck
bugs778247
milestone17.0a1
Bug 778247: mAppContext cleanup: TabsPanel doesn't need it. [r=mbrubeck]
mobile/android/base/RemoteTabs.java
mobile/android/base/TabsPanel.java
mobile/android/base/TabsTray.java
--- a/mobile/android/base/RemoteTabs.java
+++ b/mobile/android/base/RemoteTabs.java
@@ -25,16 +25,17 @@ import android.text.TextUtils;
 public class RemoteTabs extends LinearLayout
                         implements TabsPanel.PanelView,
                                    ExpandableListView.OnGroupClickListener,
                                    ExpandableListView.OnChildClickListener, 
                                    TabsAccessor.OnQueryTabsCompleteListener {
     private static final String LOGTAG = "GeckoRemoteTabs";
 
     private Context mContext;
+    private TabsPanel mTabsPanel;
 
     private static ExpandableListView mList;
     
     private static ArrayList <ArrayList <HashMap <String, String>>> mTabsList;
 
     private static final String[] CLIENT_KEY = new String[] { "name" };
     private static final String[] TAB_KEY = new String[] { "title" };
     private static final int[] CLIENT_RESOURCE = new int[] { R.id.client };
@@ -52,63 +53,68 @@ public class RemoteTabs extends LinearLa
     }
 
     @Override
     public ViewGroup getLayout() {
         return this;
     }
 
     @Override
+    public void setTabsPanel(TabsPanel panel) {
+        mTabsPanel = panel;
+    }
+
+    @Override
     public void show() {
         TabsAccessor.getTabs(mContext, this);
     }
 
     @Override
     public void hide() {
     }
 
-    void autoHideTabs() {
-        GeckoApp.mAppContext.autoHideTabs();
+    void autoHidePanel() {
+        mTabsPanel.autoHidePanel();
     }
 
     @Override
     public boolean onGroupClick(ExpandableListView parent, View view, int position, long id) {
         // By default, the group collapses/expands. Consume the event.
         return true;
     }
 
     @Override
     public boolean onChildClick(ExpandableListView parent, View view, int groupPosition, int childPosition, long id) {
         HashMap <String, String> tab = mTabsList.get(groupPosition).get(childPosition);
         if (tab == null) {
-            autoHideTabs();
+            autoHidePanel();
             return true;
         }
 
         String url = tab.get("url");
         JSONObject args = new JSONObject();
         try {
             args.put("url", url);
             args.put("engine", null);
             args.put("userEntered", false);
         } catch (Exception e) {
             Log.e(LOGTAG, "error building JSON arguments");
         }
 
         Log.d(LOGTAG, "Sending message to Gecko: " + SystemClock.uptimeMillis() + " - Tab:Add");
         GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Tab:Add", args.toString()));
-        autoHideTabs();
+        autoHidePanel();
         return true;
     }
 
     @Override
     public void onQueryTabsComplete(List<TabsAccessor.RemoteTab> remoteTabsList) {
         ArrayList<TabsAccessor.RemoteTab> remoteTabs = new ArrayList<TabsAccessor.RemoteTab> (remoteTabsList);
         if (remoteTabs == null || remoteTabs.size() == 0) {
-            autoHideTabs();
+            autoHidePanel();
             return;
         }
         
         ArrayList <HashMap <String, String>> clients = new ArrayList <HashMap <String, String>>();
 
         mTabsList = new ArrayList <ArrayList <HashMap <String, String>>>();
 
         String oldGuid = null;
--- a/mobile/android/base/TabsPanel.java
+++ b/mobile/android/base/TabsPanel.java
@@ -3,17 +3,16 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko;
 
 import android.content.Context;
 import android.content.res.TypedArray;
 import android.util.AttributeSet;
-import android.util.DisplayMetrics;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.Button;
 import android.widget.ImageButton;
 import android.widget.LinearLayout;
 import android.widget.RelativeLayout;
 import android.widget.TextView;
@@ -25,25 +24,27 @@ public class TabsPanel extends LinearLay
 
     public static enum Panel {
         LOCAL_TABS,
         REMOTE_TABS
     }
 
     public static interface PanelView {
         public ViewGroup getLayout();
+        public void setTabsPanel(TabsPanel panel);
         public void show();
         public void hide();
     }
 
     public static interface TabsLayoutChangeListener {
         public void onTabsLayoutChange(int width, int height);
     }
 
     private Context mContext;
+    private GeckoApp mActivity;
     private PanelView mPanel;
     private TabsPanelToolbar mToolbar;
     private TabsListContainer mListContainer;
     private TabsLayoutChangeListener mLayoutChangeListener;
 
     private static ImageButton mRemoteTabs;
     private TextView mTitle;
 
@@ -52,16 +53,17 @@ public class TabsPanel extends LinearLay
     private boolean mVisible;
 
     private static final int REMOTE_TABS_HIDDEN = 1;
     private static final int REMOTE_TABS_SHOWN = 2;
 
     public TabsPanel(Context context, AttributeSet attrs) {
         super(context, attrs);
         mContext = context;
+        mActivity = (GeckoApp) context;
 
         setOrientation(LinearLayout.VERTICAL);
         LayoutInflater.from(context).inflate(R.layout.tabs_panel, this);
 
         mCurrentPanel = Panel.LOCAL_TABS;
         mVisible = false;
 
         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TabsPanel);
@@ -74,46 +76,45 @@ public class TabsPanel extends LinearLay
         initToolbar();
     }
 
     void initToolbar() {
         mTitle = (TextView) mToolbar.findViewById(R.id.title);
         ImageButton addTab = (ImageButton) mToolbar.findViewById(R.id.add_tab);
         addTab.setOnClickListener(new Button.OnClickListener() {
             public void onClick(View v) {
-                GeckoApp.mAppContext.addTab();
-                GeckoApp.mAppContext.autoHideTabs();
+                mActivity.addTab();
+                mActivity.autoHideTabs();
             }
         });
 
         mRemoteTabs = (ImageButton) mToolbar.findViewById(R.id.remote_tabs);
         mRemoteTabs.setOnClickListener(new Button.OnClickListener() {
             public void onClick(View v) {
                 if (mRemoteTabs.getDrawable().getLevel() == REMOTE_TABS_SHOWN)
-                    GeckoApp.mAppContext.showLocalTabs();
+                    mActivity.showLocalTabs();
                 else
-                    GeckoApp.mAppContext.showRemoteTabs();
+                    mActivity.showRemoteTabs();
             }
         });
     }
 
     // Tabs List Container holds the ListView
     public static class TabsListContainer extends LinearLayout {
+        private Context mContext;
+
         public TabsListContainer(Context context, AttributeSet attrs) {
             super(context, attrs);
+            mContext = context;
         }
 
         @Override
         protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
             if (!GeckoApp.mAppContext.hasTabsSideBar()) {
-                DisplayMetrics metrics = new DisplayMetrics();
-                GeckoApp.mAppContext.getWindowManager().getDefaultDisplay().getMetrics(metrics);
-
-                int height = (int) (0.5 * metrics.heightPixels);
-                int heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
+                int heightSpec = MeasureSpec.makeMeasureSpec(mContext.getResources().getDisplayMetrics().heightPixels, MeasureSpec.EXACTLY);
                 super.onMeasure(widthMeasureSpec, heightSpec);
             } else {
                 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
             }
         }
     }
 
     // Tabs Panel Toolbar contains the Buttons
@@ -151,29 +152,28 @@ public class TabsPanel extends LinearLay
             mRemoteTabs.setImageLevel(REMOTE_TABS_HIDDEN);
         } else {
             mPanel = new RemoteTabs(mContext, null);
             mTitle.setText(R.string.remote_tabs);
             mRemoteTabs.setVisibility(View.VISIBLE);
             mRemoteTabs.setImageLevel(REMOTE_TABS_SHOWN);
         }
 
+        mPanel.setTabsPanel(this);
         mPanel.show();
         mListContainer.addView(mPanel.getLayout());
 
-        if (GeckoApp.mAppContext.hasTabsSideBar()) {
+        if (isSideBar()) {
             dispatchLayoutChange(getWidth(), getHeight());
         } else {
             int actionBarHeight = (int) (mContext.getResources().getDimension(R.dimen.browser_toolbar_height));
 
             // TabsListContainer takes time to resize on rotation.
             // It's better to add 50% of the screen-size and dispatch it as height.
-            DisplayMetrics metrics = new DisplayMetrics();
-            GeckoApp.mAppContext.getWindowManager().getDefaultDisplay().getMetrics(metrics);
-            int listHeight = (int) (0.5 * metrics.heightPixels);
+            int listHeight = (int) (0.5 * mContext.getResources().getDisplayMetrics().heightPixels);
 
             int height = actionBarHeight + listHeight; 
             dispatchLayoutChange(getWidth(), height);
         }
 
         // If Sync is set up, query the database for remote clients.
         final Context context = mContext;
         new SyncAccounts.AccountsExistTask() {
@@ -209,16 +209,20 @@ public class TabsPanel extends LinearLay
         mToolbar = new TabsPanelToolbar(mContext, null);
         addView(mToolbar, index);
         initToolbar();
 
         if (mVisible)
             show(mCurrentPanel);
     }
 
+    public void autoHidePanel() {
+        mActivity.autoHideTabs();
+    }
+
     @Override
     public boolean isShown() {
         return mVisible;
     }
 
     public boolean isSideBar() {
         return mIsSideBar;
     }
--- a/mobile/android/base/TabsTray.java
+++ b/mobile/android/base/TabsTray.java
@@ -29,16 +29,17 @@ import android.widget.TextView;
 
 import org.mozilla.gecko.PropertyAnimator.Property;
 
 public class TabsTray extends LinearLayout 
                       implements TabsPanel.PanelView {
     private static final String LOGTAG = "GeckoTabsTray";
 
     private Context mContext;
+    private TabsPanel mTabsPanel;
 
     private static ListView mList;
     private TabsAdapter mTabsAdapter;
     private boolean mWaitingForClose;
 
     private GestureDetector mGestureDetector;
     private TabSwipeGestureListener mListener;
     // Minimum velocity swipe that will close a tab, in inches/sec
@@ -100,32 +101,37 @@ public class TabsTray extends LinearLayo
     }
 
     @Override
     public ViewGroup getLayout() {
         return this;
     }
 
     @Override
+    public void setTabsPanel(TabsPanel panel) {
+        mTabsPanel = panel;
+    }
+
+    @Override
     public void show() {
         mWaitingForClose = false;
         Tabs.getInstance().refreshThumbnails();
         Tabs.registerOnTabsChangedListener(mTabsAdapter);
         mTabsAdapter.refreshTabsData();
     }
 
     @Override
     public void hide() {
         Tabs.unregisterOnTabsChangedListener(mTabsAdapter);
         GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Tab:Screenshot:Cancel",""));
         mTabsAdapter.clear();
     }
 
-    void autoHideTabs() {
-        GeckoApp.mAppContext.autoHideTabs();
+    void autoHidePanel() {
+        mTabsPanel.autoHidePanel();
     }
 
     // ViewHolder for a row in the list
     private class TabRow {
         int id;
         TextView title;
         ImageView thumbnail;
         ImageButton close;
@@ -355,17 +361,17 @@ public class TabsTray extends LinearLayo
                     }
     
                     animateTo(mView, finalPos, MAX_ANIMATION_TIME);
                 } else if (mView != null && dir == DragDirection.UNKNOWN) {
                     // the user didn't attempt to scroll the view, so select the row
                     TabRow tab = (TabRow)mView.getTag();
                     int tabId = tab.id;
                     Tabs.getInstance().selectTab(tabId);
-                    autoHideTabs();
+                    autoHidePanel();
                 }
             }
 
             mView = null;
             start = null;
             dir = DragDirection.UNKNOWN;
             return false;
         }