Bug 1202861 - 2. Refresh tabs panel when compact tabs configuration changes. r=sebastian
authorTom Klein <twointofive@gmail.com>
Mon, 28 Nov 2016 09:27:24 -0600
changeset 325434 88e0fb654a104419dd66774d422b20692639919e
parent 325433 4de97a01678bc55bf39daea37fd487ec52e5714c
child 325483 44ab7f53ead5b57ef0d2e4de911567e4b5886d0a
child 325493 57cb50f186b450c6aa25155e1f66b3f29998e8cf
push id24
push usermaklebus@msu.edu
push dateTue, 20 Dec 2016 03:11:33 +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();
+    }
 }