public NativeAdRequest(Context context, String placementID) { AdvertisingIDUtil.retrieveAndSetAAID(context); requestParameters = new UTRequestParameters(context); requestParameters.setPlacementID(placementID); requestParameters.setMediaType(MediaType.NATIVE); this.setAllowedSizes(); mAdFetcher = new AdFetcher(this); mAdFetcher.setPeriod(-1); dispatcher = new NativeAdDispatcher(); Clog.setErrorContext(context.getApplicationContext()); }
/** * 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; }
@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()); } }
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(); } }
@Override public void tearDown() { super.tearDown(); if (adFetcher != null) { adFetcher.stop(); adFetcher.clearDurations(); } adFetcher = null; }
@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 testToggleAutoRefresh() throws Exception { server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.rtbVASTVideo())); ShadowCustomVideoWebView.simulateDelayedVideoError = true; executeBannerRequest(); assertCallbacks(true); assertTrue(bannerAdView.mAdFetcher.getState() == AdFetcher.STATE.STOPPED); waitForTasks(); Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); assertTrue(bannerAdView.mAdFetcher.getState() == AdFetcher.STATE.AUTO_REFRESH); }
void stop() { Clog.d(Clog.publicFunctionsLogTag, Clog.getString(R.string.stop)); mAdFetcher.stop(); loadAdHasBeenCalled = false; }
@Override public void setup() { super.setup(); // Since ad type is not a key factor that affects ad fetcher // Using BannerAdView as the owner ad of AdFetcher here MockAdOwner owner = new MockAdOwner(activity); owner.setPlacementID("0"); owner.setAdSize(320, 50); adFetcher = new AdFetcher(owner); }
/** * Set the auto-refresh interval. This is the interval, in * milliseconds, at which the BannerAdView will request new ads, * if auto-refresh is enabled. The default period is 30 seconds; * the minimum is 15. You can enable or disable auto-refresh * using the setAutoRefresh method. * * @param period The auto-refresh interval, in milliseconds. */ public void setAutoRefreshInterval(int period) { if (period > 0) { this.period = Math.max(Settings.MIN_REFRESH_MILLISECONDS, period); } else { this.period = period; } Clog.d(Clog.publicFunctionsLogTag, Clog.getString(R.string.set_period, this.period)); if (mAdFetcher != null) mAdFetcher.setPeriod(this.period); }
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; } }
void stop() { if (requestManager != null) { requestManager.cancel(); requestManager = null; } clearTasker(); Clog.d(Clog.baseLogTag, Clog.getString(R.string.stop)); timePausedAt = System.currentTimeMillis(); state = STATE.STOPPED; }
void start() { Clog.d(Clog.baseLogTag, Clog.getString(R.string.start)); createTasker(); switch (state) { case STOPPED:
/** * 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; }
@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()); }