Bug 1266383 - Do not show HomeScreenPrompt if tab is not in foreground. r=ahunt
authorSebastian Kaspari <s.kaspari@gmail.com>
Thu, 21 Apr 2016 19:36:22 +0200
changeset 294745 b3c8e62a256c810a20cfee8dd127d00b7602af8c
parent 294744 be988dc7bd2063d7ad0c75e9244fe6aa1a998b84
child 294746 79de998e7307650fec99ea6188ae60ac64ebeedb
child 294824 14952428292c90aa00c28fdfb1f1e79bd8257ce2
push id30210
push userkwierso@gmail.com
push dateMon, 25 Apr 2016 22:25:12 +0000
treeherdermozilla-central@79de998e7307 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersahunt
bugs1266383
milestone48.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1266383 - Do not show HomeScreenPrompt if tab is not in foreground. r=ahunt MozReview-Commit-ID: 1j7FKOfVt0X
mobile/android/base/java/org/mozilla/gecko/promotion/AddToHomeScreenPromotion.java
--- a/mobile/android/base/java/org/mozilla/gecko/promotion/AddToHomeScreenPromotion.java
+++ b/mobile/android/base/java/org/mozilla/gecko/promotion/AddToHomeScreenPromotion.java
@@ -19,16 +19,17 @@ import org.mozilla.gecko.AboutPages;
 import org.mozilla.gecko.BrowserApp;
 import org.mozilla.gecko.BrowserAppDelegate;
 import org.mozilla.gecko.GeckoProfile;
 import org.mozilla.gecko.Tab;
 import org.mozilla.gecko.Tabs;
 import org.mozilla.gecko.db.BrowserContract;
 import org.mozilla.gecko.db.BrowserDB;
 import org.mozilla.gecko.db.UrlAnnotations;
+import org.mozilla.gecko.tabs.TabsPanel;
 import org.mozilla.gecko.util.Experiments;
 import org.mozilla.gecko.util.ThreadUtils;
 
 import java.lang.ref.WeakReference;
 
 import ch.boye.httpclientandroidlib.util.TextUtils;
 
 /**
@@ -48,37 +49,49 @@ public class AddToHomeScreenPromotion ex
     private static final String LOGTAG = "GeckoPromoteShortcut";
 
     private static final String EXPERIMENT_MINIMUM_TOTAL_VISITS = "minimumTotalVisits";
     private static final String EXPERIMENT_LAST_VISIT_MINIMUM_AGE = "lastVisitMinimumAgeMs";
     private static final String EXPERIMENT_LAST_VISIT_MAXIMUM_AGE = "lastVisitMaximumAgeMs";
 
     private WeakReference<Activity> activityReference;
     private boolean isEnabled;
+    private boolean isInForeground;
     private int minimumVisits;
     private int lastVisitMinimumAgeMs;
     private int lastVisitMaximumAgeMs;
 
     @Override
     public void onCreate(BrowserApp browserApp, Bundle savedInstanceState) {
         activityReference = new WeakReference<Activity>(browserApp);
+        isInForeground = true;
 
         initializeExperiment(browserApp);
     }
 
     @Override
     public void onResume(BrowserApp browserApp) {
         Tabs.registerOnTabsChangedListener(this);
     }
 
     @Override
     public void onPause(BrowserApp browserApp) {
         Tabs.unregisterOnTabsChangedListener(this);
     }
 
+    @Override
+    public void onTabsTrayShown(BrowserApp browserApp, TabsPanel tabsPanel) {
+        isInForeground = false;
+    }
+
+    @Override
+    public void onTabsTrayHidden(BrowserApp browserApp, TabsPanel tabsPanel) {
+        isInForeground = true;
+    }
+
     private void initializeExperiment(Context context) {
         if (!SwitchBoard.isInExperiment(context, Experiments.PROMOTE_ADD_TO_HOMESCREEN)) {
             Log.v(LOGTAG, "Experiment not enabled");
             // Experiment is not enabled. No need to try to read values.
             return;
         }
 
         JSONObject values = SwitchBoard.getExperimentValuesFromJson(context, Experiments.PROMOTE_ADD_TO_HOMESCREEN);
@@ -112,24 +125,32 @@ public class AddToHomeScreenPromotion ex
             return;
         }
 
         if (!Tabs.getInstance().isSelectedTab(tab)) {
             // We only ever want to show this promotion for the current tab.
             return;
         }
 
-        if (Tabs.TabEvents.LOADED == msg) {
-            ThreadUtils.postToBackgroundThread(new Runnable() {
-                @Override
-                public void run() {
-                    maybeShowPromotionForUrl(tab.getURL(), tab.getTitle());
-                }
-            });
+        if (Tabs.TabEvents.LOADED != msg) {
+            return;
         }
+
+        if (!isInForeground) {
+            // We only want to show this prompt if this tab is in the foreground and not on top
+            // of the tabs tray.
+            return;
+        }
+
+        ThreadUtils.postToBackgroundThread(new Runnable() {
+            @Override
+            public void run() {
+                maybeShowPromotionForUrl(tab.getURL(), tab.getTitle());
+            }
+        });
     }
 
     private void maybeShowPromotionForUrl(String url, String title) {
         if (!isEnabled) {
             return;
         }
 
         final Context context = activityReference.get();