@Override public void toggleAutoRefresh() { if (getMediaType().equals(MediaType.BANNER) && mAdFetcher.getState() == AdFetcher.STATE.STOPPED) { mAdFetcher.start(); } }
void setPeriod(int period) { boolean periodChanged = this.period != period; this.period = period; if ((periodChanged) && !state.equals(STATE.STOPPED)) { // We should reset. Clog.d(Clog.baseLogTag, "AdFetcher refresh period changed to " + this.period); Clog.d(Clog.baseLogTag, "Resetting AdFetcher"); stop(); start(); } }
/** * Loads a new ad, if the ad space is visible. You should * have called setPlacementID before invoking this method. * * @return true means the ad will begin loading; false otherwise. */ @Override public boolean loadAd() { if (!isReadyToStart()) return false; if (mAdFetcher != null) { // Reload Ad Fetcher to get new ad at user's request mAdFetcher.stop(); mAdFetcher.clearDurations(); mAdFetcher.start(); if(this.getWindowVisibility() != VISIBLE){ loadedOffscreen = true; } return true; } return false; }
/** * Requests a new interstitial ad from the server and stores it in * a local queue. Note that interstitials have a timeout of 60 * seconds; you must show the interstitial (by calling * <code>show()</code>) within 60 seconds of getting a response; * otherwise, the ad will not show. * * @return <code>true</code> if the ad load was successfully * dispatched; <code>false</code> otherwise. */ @Override public boolean loadAd() { Clog.d(Clog.publicFunctionsLogTag, Clog.getString(R.string.load_ad_int)); if (!isReadyToStart()) return false; if (mAdFetcher != null) { // Load an interstitial ad mAdFetcher.stop(); mAdFetcher.start(); return true; } return false; }
void start() { Clog.d("BannerAdView", getAdType().name()); /* * To check if it does not triggers AUTO_REFRESH * for video Ads (rendered using BannerAdView) */ if (getAdType() != AdType.VIDEO) { Clog.d(Clog.publicFunctionsLogTag, Clog.getString(R.string.start)); mAdFetcher.start(); loadAdHasBeenCalled = true; } }
/** * Call this to request a native ad for parameters described by this object. */ @Override public boolean loadAd() { if (listener == null) { // error message Clog.e(Clog.nativeLogTag, "No listener installed for this request, won't load a new ad"); return false; } if (isLoading) { Clog.e(Clog.nativeLogTag, "Still loading last native ad , won't load a new ad"); return false; } if (requestParameters.isReadyForRequest()) { mAdFetcher.stop(); mAdFetcher.clearDurations(); mAdFetcher.start(); isLoading = true; return true; } return false; }
@Test public void testResetRefreshOff() { adFetcher.setPeriod(5000); adFetcher.start(); assertEquals(AdFetcher.STATE.AUTO_REFRESH, adFetcher.getState()); adFetcher.setPeriod(-1); assertEquals(AdFetcher.STATE.SINGLE_REQUEST, adFetcher.getState()); }
@Override protected void setup(Context context, AttributeSet attrs) { period = Settings.DEFAULT_REFRESH; shouldResetContainer = false; expandsToFitScreenWidth = false; resizeToFitContainer = false; measured = false; animator = new Animator(getContext(), TransitionType.NONE, TransitionDirection.UP, 1000); super.setup(context, attrs); onFirstLayout(); requestParameters.setMediaType(MediaType.BANNER); mAdFetcher.setPeriod(period); if (autoRefreshOffInXML) { mAdFetcher.start(); } }
@Test public void testStartWithRefreshOff() { // Default state is auto refresh off, just start the ad fetcher assertEquals("Default state should be stopped", AdFetcher.STATE.STOPPED, adFetcher.getState()); adFetcher.start(); Lock.pause(1000); // added this so jenkins can have enough time to process assertEquals("Single request should be used.", AdFetcher.STATE.SINGLE_REQUEST, adFetcher.getState()); assertExpectedBGTasksAfterOneAdRequest(2); assertEquals("State should not be changed after request.", AdFetcher.STATE.SINGLE_REQUEST, adFetcher.getState()); }
@Test public void testStartWithRefreshOn() { // default state was stopped adFetcher.setPeriod(30000); adFetcher.start(); Lock.pause(1000); // added this so jenkins can have enough time to process // assert 3 here because a AAID async task assertExpectedBGTasksAfterOneAdRequest(2); assertEquals(AdFetcher.STATE.AUTO_REFRESH, adFetcher.getState()); // reset background scheduler, clear tasks for the refresh Lock.pause(30000 + 1000); // We wait for till autorefresh is triggered // in the following method, wait until next ad request is enqueued assertExpectedBGTasksAfterOneAdRequest(3); assertEquals(AdFetcher.STATE.AUTO_REFRESH, adFetcher.getState()); }
@Test public void testStop() { if (adFetcher != null) { // not needed, but in case AdRequest is run server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.blank())); clearAAIDAsyncTasks(); // start an AdFetcher normally, until an AdRequest is queued adFetcher.start(); Lock.pause(1000); // added this so jenkins can have enough time to process assertExpectedBGTasksAfterOneAdRequest(1); assertNotSame(AdFetcher.STATE.STOPPED, adFetcher.getState()); adFetcher.stop(); // pause until a scheduler has a task in queue waitForTasks(); // Run the cancel command on AdRequest Robolectric.flushForegroundThreadScheduler(); // Run the pending AdRequest from start() -- should have been canceled while (Robolectric.getBackgroundThreadScheduler().areAnyRunnable()) { Robolectric.getBackgroundThreadScheduler().runOneTask(); } // A normally executed AdRequest will queue onPostExecute call to the UI thread, // but it should be canceled, and queue nothing int uiTaskCount = Robolectric.getForegroundThreadScheduler().size(); assertEquals(0, uiTaskCount); assertEquals(AdFetcher.STATE.STOPPED, adFetcher.getState()); } }