diff --git a/mobile/android/chrome/content/CastingApps.js b/mobile/android/chrome/content/CastingApps.js index 341628290492..4d8177823e36 100644 --- a/mobile/android/chrome/content/CastingApps.js +++ b/mobile/android/chrome/content/CastingApps.js @@ -65,6 +65,7 @@ var CastingApps = { mirrorStopMenuId: -1, _blocked: null, _bound: null, + _interval: 120 * 1000, // 120 seconds init: function ca_init() { if (!this.isCastingEnabled()) { @@ -78,8 +79,8 @@ var CastingApps = { mediaPlayerDevice.init(); SimpleServiceDiscovery.registerDevice(mediaPlayerDevice); - // Search for devices continuously every 120 seconds - SimpleServiceDiscovery.search(120 * 1000); + // Search for devices continuously + SimpleServiceDiscovery.search(this._interval); this._castMenuId = NativeWindow.contextmenus.add( Strings.browser.GetStringFromName("contextmenu.sendToDevice"), @@ -93,6 +94,8 @@ var CastingApps = { Services.obs.addObserver(this, "Casting:Mirror", false); Services.obs.addObserver(this, "ssdp-service-found", false); Services.obs.addObserver(this, "ssdp-service-lost", false); + Services.obs.addObserver(this, "application-background", false); + Services.obs.addObserver(this, "application-foreground", false); BrowserApp.deck.addEventListener("TabSelect", this, true); BrowserApp.deck.addEventListener("pageshow", this, true); @@ -195,15 +198,20 @@ var CastingApps = { } break; case "ssdp-service-found": - { - this.serviceAdded(SimpleServiceDiscovery.findServiceForID(aData)); - break; - } + this.serviceAdded(SimpleServiceDiscovery.findServiceForID(aData)); + break; case "ssdp-service-lost": - { - this.serviceLost(SimpleServiceDiscovery.findServiceForID(aData)); - break; - } + this.serviceLost(SimpleServiceDiscovery.findServiceForID(aData)); + break; + case "application-background": + // Turn off polling while in the background + this._interval = SimpleServiceDiscovery.search(0); + SimpleServiceDiscovery.stopSearch(); + break; + case "application-foreground": + // Turn polling on when app comes back to foreground + SimpleServiceDiscovery.search(this._interval); + break; } },