Bug 1202861 - 2. Refresh tabs panel when compact tabs configuration changes. r?sebastian draft
authorTom Klein <twointofive@gmail.com>
Mon, 28 Nov 2016 09:27:24 -0600
changeset 447849 847cf328453abf32ade70743ad4facb0651152cb
parent 447848 83b0c2665191f34f2300c15e733dd696ece95963
child 539150 8661d5d90ab5d873234fedbf5aea018168e1177e
push id38192
push userbmo:twointofive@gmail.com
push dateThu, 08 Dec 2016 03:19:38 +0000
reviewerssebastian
bugs1202861
milestone53.0a1
Bug 1202861 - 2. Refresh tabs panel when compact tabs configuration changes. r?sebastian MozReview-Commit-ID: HV9EQcLtCrE
mobile/android/base/java/org/mozilla/gecko/tabs/TabsPanel.java
--- a/mobile/android/base/java/org/mozilla/gecko/tabs/TabsPanel.java
+++ b/mobile/android/base/java/org/mozilla/gecko/tabs/TabsPanel.java
@@ -20,35 +20,39 @@ import org.mozilla.gecko.lwt.Lightweight
 import org.mozilla.gecko.preferences.GeckoPreferences;
 import org.mozilla.gecko.restrictions.Restrictable;
 import org.mozilla.gecko.restrictions.Restrictions;
 import org.mozilla.gecko.util.HardwareUtils;
 import org.mozilla.gecko.widget.GeckoPopupMenu;
 import org.mozilla.gecko.widget.IconTabWidget;
 
 import android.content.Context;
+import android.content.SharedPreferences;
 import android.content.res.Configuration;
 import android.content.res.Resources;
+import android.support.annotation.UiThread;
+import android.text.TextUtils;
 import android.util.AttributeSet;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
 import android.widget.Button;
 import android.widget.FrameLayout;
 import android.widget.ImageButton;
 import android.widget.LinearLayout;
 import android.widget.RelativeLayout;
 import org.mozilla.gecko.widget.themed.ThemedImageButton;
 
 public class TabsPanel extends LinearLayout
                        implements GeckoPopupMenu.OnMenuItemClickListener,
                                   LightweightTheme.OnChangeListener,
-                                  IconTabWidget.OnTabChangedListener {
+                                  IconTabWidget.OnTabChangedListener,
+                                  SharedPreferences.OnSharedPreferenceChangeListener {
     private static final String LOGTAG = "Gecko" + TabsPanel.class.getSimpleName();
 
     public enum Panel {
         NORMAL_TABS,
         PRIVATE_TABS,
     }
 
     public interface PanelView {
@@ -65,20 +69,23 @@ public class TabsPanel extends LinearLay
     public interface TabsLayout extends CloseAllPanelView {
         void setEmptyView(View view);
     }
 
     public interface TabsLayoutChangeListener {
         void onTabsLayoutChange(int width, int height);
     }
 
+    private static boolean tabletOrLandscapeMode(Context context) {
+        return HardwareUtils.isTablet() ||
+                context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
+    }
+
     public static View createTabsLayout(final Context context, final AttributeSet attrs) {
-        final boolean isLandscape = context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
-
-        if (HardwareUtils.isTablet() || isLandscape) {
+        if (tabletOrLandscapeMode(context)) {
             return new AutoFitTabsGridLayout(context, attrs);
         } else {
             // Phone in portrait mode.
             if (GeckoSharedPrefs.forApp(context).getBoolean(GeckoPreferences.PREFS_COMPACT_TABS, false)) {
                 return new CompactTabsGridLayout(context, attrs);
             } else {
                 return new TabsListLayout(context, attrs);
             }
@@ -254,22 +261,28 @@ public class TabsPanel extends LinearLay
 
         return screenHeight - actionBarHeight;
     }
 
     @Override
     public void onAttachedToWindow() {
         super.onAttachedToWindow();
         mTheme.addListener(this);
+        if (!HardwareUtils.isTablet()) {
+            GeckoSharedPrefs.forApp(getContext()).registerOnSharedPreferenceChangeListener(this);
+        }
     }
 
     @Override
     public void onDetachedFromWindow() {
         super.onDetachedFromWindow();
         mTheme.removeListener(this);
+        if (!HardwareUtils.isTablet()) {
+            GeckoSharedPrefs.forApp(getContext()).unregisterOnSharedPreferenceChangeListener(this);
+        }
     }
 
     @Override
     @SuppressWarnings("deprecation") // setBackgroundDrawable deprecated by API level 16
     public void onLightweightThemeChanged() {
         final int background = ContextCompat.getColor(getContext(), R.color.text_and_tabs_tray_grey);
         final LightweightThemeDrawable drawable = mTheme.getColorDrawable(this, background, true);
         if (drawable == null)
@@ -455,9 +468,19 @@ public class TabsPanel extends LinearLay
     public void setTabsLayoutChangeListener(TabsLayoutChangeListener listener) {
         mLayoutChangeListener = listener;
     }
 
     private void dispatchLayoutChange(int width, int height) {
         if (mLayoutChangeListener != null)
             mLayoutChangeListener.onTabsLayoutChange(width, height);
     }
+
+    @UiThread // according to the docs.
+    @Override
+    public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key) {
+        if (!TextUtils.equals(GeckoPreferences.PREFS_COMPACT_TABS, key)) {
+            return;
+        }
+
+        refresh();
+    }
 }