com.google.android.gms.ads.NativeExpressAdView Java Examples

The following examples show how to use com.google.android.gms.ads.NativeExpressAdView. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AdmobExpressRecyclerAdapterWrapper.java    From admobadapter with Apache License 2.0 6 votes vote down vote up
@Override
public void onAdFailed(int adIdx, int errorCode, Object adPayload) {
    NativeExpressAdView adView = (NativeExpressAdView)adPayload;
    if (adView != null) {
        ViewParent parent = adView.getParent();
        if(parent == null || parent instanceof RecyclerView)
            adView.setVisibility(View.GONE);
        else {
            while (parent.getParent() != null && !(parent.getParent() instanceof RecyclerView))
                parent = parent.getParent();
            ((View) parent).setVisibility(View.GONE);
        }
    }
    int pos = getAdapterCalculator().translateAdToWrapperPosition(Math.max(adIdx,0));
    notifyItemRangeChanged(pos, pos+15);
}
 
Example #2
Source File: AdmobExpressRecyclerAdapterWrapper.java    From admobadapter with Apache License 2.0 6 votes vote down vote up
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
    if (viewHolder == null)
        return;
    if(viewHolder.getItemViewType() == getViewTypeAdExpress()) {
        NativeHolder nativeExpressHolder = (NativeHolder) viewHolder;
        ViewGroup wrapper = nativeExpressHolder.getAdViewWrapper();
        int adPos = AdapterCalculator.getAdIndex(position);
        NativeExpressAdView adView = adFetcher.getAdForIndex(adPos);
        if (adView == null)
            adView = prefetchAds(1);
        AdViewWrappingStrategy.recycleAdViewWrapper(wrapper, adView);
        //make sure the AdView for this position doesn't already have a parent of a different recycled NativeExpressHolder.
        if (adView.getParent() != null)
            ((ViewGroup) adView.getParent()).removeView(adView);
        AdViewWrappingStrategy.addAdViewToWrapper(wrapper, adView);

    } else {
        int origPos = AdapterCalculator.getOriginalContentPosition(position,
                adFetcher.getFetchingAdsCount(), mAdapter.getItemCount());
        mAdapter.onBindViewHolder(viewHolder, origPos);
    }
}
 
Example #3
Source File: AdmobExpressRecyclerAdapterWrapper.java    From admobadapter with Apache License 2.0 6 votes vote down vote up
/**
 * Creates N instances {@link NativeExpressAdView} from the next N taken instances {@link AdPreset}
 * Will start async prefetch of ad blocks to use its further
 * @return last created NativeExpressAdView
 */
private NativeExpressAdView prefetchAds(int cntToPrefetch){
    NativeExpressAdView last = null;
    for (int i = 0; i < cntToPrefetch; i++) {
        final NativeExpressAdView item = AdViewHelper.getExpressAdView(mContext, adFetcher.takeNextAdPreset());
        adFetcher.setupAd(item);
        //50 ms throttling to prevent a high-load of server
        new Handler(mContext.getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                adFetcher.fetchAd(item);
            }
        }, 50 * i);
        last = item;
    }
    return last;
}
 
Example #4
Source File: AdmobExpressAdapterWrapper.java    From admobadapter with Apache License 2.0 6 votes vote down vote up
/**
 * Creates N instances {@link NativeExpressAdView} from the next N taken instances {@link AdPreset}
 * Will start async prefetch of ad blocks to use its further
 * @return last created NativeExpressAdView
 */
private NativeExpressAdView prefetchAds(int cntToPrefetch){
    NativeExpressAdView last = null;
    for (int i = 0; i < cntToPrefetch; i++){
        final NativeExpressAdView item = AdViewHelper.getExpressAdView(mContext, adFetcher.takeNextAdPreset());
        adFetcher.setupAd(item);
        //50 ms throttling to prevent a high-load of server
        new Handler(mContext.getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                adFetcher.fetchAd(item);
            }
        }, 50 * i);
        last = item;
    }
    return last;
}
 
Example #5
Source File: AdmobFetcherExpress.java    From admobadapter with Apache License 2.0 6 votes vote down vote up
/**
 * Fetches a new native ad.
 */
protected synchronized void fetchAd(final NativeExpressAdView adView) {
    if(mFetchFailCount > MAX_FETCH_ATTEMPT)
        return;

    Context context = mContext.get();

    if (context != null) {
        Log.i(TAG, "Fetching Ad now");
        new Handler(context.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                adView.loadAd(getAdRequest()); //Fetching the ads item
            }
        });
    } else {
        mFetchFailCount++;
        Log.i(TAG, "Context is null, not fetching Ad");
    }
}
 
Example #6
Source File: AdmobFetcherExpress.java    From admobadapter with Apache License 2.0 6 votes vote down vote up
/**
 * Subscribing to the native ads events
 * @param adView
 */
protected synchronized void setupAd(final NativeExpressAdView adView) {
    if(mFetchFailCount > MAX_FETCH_ATTEMPT)
        return;

    if(!mPrefetchedAds.contains(adView))
        mPrefetchedAds.add(adView);
    adView.setAdListener(new AdListener() {
        @Override
        public void onAdFailedToLoad(int errorCode) {
            super.onAdFailedToLoad(errorCode);
            // Handle the failure by logging, altering the UI, etc.
            onFailedToLoad(adView, errorCode);
        }
        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
            onFetched(adView);
        }
    });
}
 
Example #7
Source File: AdmobFetcherExpress.java    From admobadapter with Apache License 2.0 5 votes vote down vote up
/**
 * A handler for received native ads
 * @param adView
 */
private synchronized void onFetched(NativeExpressAdView adView) {
    Log.i(TAG, "onAdFetched");
    mFetchFailCount = 0;
    mNoOfFetchedAds++;
    onAdLoaded(mNoOfFetchedAds - 1);
}
 
Example #8
Source File: AdmobExpressAdapterWrapper.java    From admobadapter with Apache License 2.0 5 votes vote down vote up
@Override
public void onAdFailed(int adIdx, int errorCode, Object adPayload) {
    NativeExpressAdView adView = (NativeExpressAdView)adPayload;
    if (adView != null) {
        ViewParent parent = adView.getParent();
        if(parent == null || parent instanceof ListView)
            adView.setVisibility(View.GONE);
        else {
            while (parent.getParent() != null && !(parent.getParent() instanceof ListView))
                parent = parent.getParent();
            ((View) parent).setVisibility(View.GONE);
        }
    }
    notifyDataSetChanged();
}
 
Example #9
Source File: AdViewWrappingStrategy.java    From admobadapter with Apache License 2.0 5 votes vote down vote up
/**
 * This method can be overriden to recycle (remove) {@param ad} from {@param wrapper} view before adding to wrap.
 * By default it will look for {@param ad} in the direct children of {@param wrapper} and remove the first occurence.
 * See the super's implementation for instance.
 * The NativeExpressHolder recycled by the RecyclerView may be a different
 * instance than the one used previously for this position. Clear the
 * wrapper of any subviews in case it has a different
 * AdView associated with it
 */
@Override
protected void recycleAdViewWrapper(@NonNull ViewGroup wrapper, @NonNull NativeExpressAdView ad) {
    for (int i = 0; i < wrapper.getChildCount(); i++) {
        View v = wrapper.getChildAt(i);
        if (v instanceof NativeExpressAdView) {
            wrapper.removeViewAt(i);
            break;
        }
    }
}
 
Example #10
Source File: AdmobFetcherExpress.java    From admobadapter with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized void destroyAllAds() {
    super.destroyAllAds();
    for(NativeExpressAdView ad:mPrefetchedAds){
        ad.destroy();
    }
    mPrefetchedAds.clear();
}
 
Example #11
Source File: AdmobFetcherExpress.java    From admobadapter with Apache License 2.0 5 votes vote down vote up
/**
 * A handler for failed native ads
 * @param adView
 */
private synchronized void onFailedToLoad(NativeExpressAdView adView, int errorCode) {
    Log.i(TAG, "onAdFailedToLoad " + errorCode);
    mFetchFailCount++;
    mNoOfFetchedAds = Math.max(mNoOfFetchedAds - 1, 0);
    //Since Fetch Ad is only called once without retries
    //hide ad row / rollback its count if still not added to list
    mPrefetchedAds.remove(adView);
    onAdFailed(mNoOfFetchedAds - 1, errorCode, adView);
}
 
Example #12
Source File: StoriesFragment.java    From Instagram-Profile-Downloader with MIT License 5 votes vote down vote up
private void loadNativeExpressAd(final int index) {

        if (index >= userObjectList.size()) {
            return;
        }

        Object item = userObjectList.get(index);
        if (!(item instanceof NativeExpressAdView)) {
            throw new ClassCastException("Expected item at index " + index + " to be a Native"
                    + " Express ad.");
        }

        final NativeExpressAdView adView = (NativeExpressAdView) item;

        // Set an AdListener on the NativeExpressAdView to wait for the previous Native Express ad
        // to finish loading before loading the next ad in the items list.
        adView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                super.onAdLoaded();
                // The previous Native Express ad loaded successfully, call this method again to
                // load the next ad in the items list.
                loadNativeExpressAd(index + spaceBetweenAds + 1);
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                // The previous Native Express ad failed to load. Call this method again to load
                // the next ad in the items list.
                Log.e("AdmobMainActivity", "The previous Native Express ad failed to load. Attempting to"
                        + " load the next Native Express ad in the items list.");
                loadNativeExpressAd(index + spaceBetweenAds + 1);
            }
        });

        // Load the Native Express ad.
        //We also registering our device as Test Device with addTestDevic("ID") method
        adView.loadAd(new AdRequest.Builder().addTestDevice("ca-app-pub-3940256099942544/2247696110").build());
    }
 
Example #13
Source File: AdViewHelper.java    From admobadapter with Apache License 2.0 5 votes vote down vote up
public static NativeExpressAdView getExpressAdView(Context context, AdPreset adPreset) {
    NativeExpressAdView adView = new NativeExpressAdView(context);
    AdSize adSize = adPreset.getAdSize();
    adView.setAdSize(adSize);
    adView.setAdUnitId(adPreset.getAdUnitId());
    adView.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
            adSize.getHeightInPixels(context)));

    //set video options
    if(adPreset.getVideoOptions() != null)
    adView.setVideoOptions(adPreset.getVideoOptions());

    return adView;
}
 
Example #14
Source File: AdViewWrappingStrategy.java    From admobadapter with Apache License 2.0 4 votes vote down vote up
/**
 * Add the Native Express {@param ad} to {@param wrapper}.
 * See the super's implementation for instance.
 */
@Override
protected void addAdViewToWrapper(@NonNull ViewGroup wrapper, @NonNull NativeExpressAdView ad) {
    wrapper.addView(ad);
}
 
Example #15
Source File: DownloadStoriesoverViewAdapter.java    From Instagram-Profile-Downloader with MIT License 4 votes vote down vote up
@Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder1, final int position) {


        int viewType = getItemViewType(position);

        // Binding data based on View Type
        switch (viewType) {
            case GlobalConstant.CONTENT_TYPE:
                StoriesOverViewHolder holder = (StoriesOverViewHolder) holder1;


                holder.setIsRecyclable(false);
                final String model = modelListCopy.get(position);

                Glide.with(context).load(model).thumbnail(0.2f).into(holder.imageView);
                holder.layout.setVisibility(View.VISIBLE);
                if (!model.endsWith(".jpg"))
                    holder.isVideo.setVisibility(View.VISIBLE);
                else holder.isVideo.setVisibility(View.GONE);


                for (StoryModel storyModel : selected_usersList) {
                    if (storyModel.getFilePath().equalsIgnoreCase(model)) {
                        holder.checked.setVisibility(View.VISIBLE);
                        break;
                    } else {

                        holder.checked.setVisibility(View.GONE);
                    }
                }
                holder.layout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
//                Intent intent = new Intent(context, ViewStoryActivity.class);
//                DataHolder.setData(storyModels);
//                intent.putExtra("isLarge", true);
//                intent.putExtra("pos", position);
//                context.startActivity(intent);
                        count = ZoomstaUtil.getIntegerPreference(context, "itemCount");


                        Intent intent = new Intent(context, ViewStoryActivity.class);
                        intent.putExtra("isFromNet", false);
                        intent.putParcelableArrayListExtra("storylist", (ArrayList<StoryModel>) storyModels);
                        intent.putExtra("pos", position);
                        intent.putExtra("isFromDownloadScreen", true);
                        context.startActivity(intent);
                        ((Activity) context).overridePendingTransition(R.anim.enter_main, R.anim.exit_splash);
                    }
                });


                break;
            case GlobalConstant.AD_TYPE:
                // fall through
            default:
                UnifiedNativeAdViewHolder nativeExpressHolder = (UnifiedNativeAdViewHolder) holder1;
                NativeExpressAdView adView = (NativeExpressAdView) modelList.get(position);
                ViewGroup adCardView = (ViewGroup) nativeExpressHolder.itemView;

                if (adCardView.getChildCount() > 0) {
                    adCardView.removeAllViews();
                }
                if (adView.getParent() != null) {
                    ((ViewGroup) adView.getParent()).removeView(adView);
                }
                adCardView.addView(adView);
        }


    }
 
Example #16
Source File: StoriesWithNativeAd.java    From Instagram-Profile-Downloader with MIT License 4 votes vote down vote up
public void setNativeExpressAdView(NativeExpressAdView nativeExpressAdView) {
    this.nativeExpressAdView = nativeExpressAdView;
}
 
Example #17
Source File: StoriesWithNativeAd.java    From Instagram-Profile-Downloader with MIT License 4 votes vote down vote up
public NativeExpressAdView getNativeExpressAdView() {
    return nativeExpressAdView;
}
 
Example #18
Source File: AdmobFetcherExpress.java    From admobadapter with Apache License 2.0 2 votes vote down vote up
/**
 * Gets native ad at a particular index in the fetched ads list.
 *
 * @param adPos the index of ad in the fetched ads list
 * @return the native ad in the list
 * @see #getFetchedAdsCount()
 */
public synchronized NativeExpressAdView getAdForIndex(int adPos) {
    if(adPos >= 0 && mPrefetchedAds.size() > adPos)
        return mPrefetchedAds.get(adPos);
    return null;
}
 
Example #19
Source File: AdViewWrappingStrategyBase.java    From admobadapter with Apache License 2.0 2 votes vote down vote up
/**
 * This method can be overriden to recycle (remove) {@param ad} from {@param wrapper} view before adding to wrap.
 * By default it will look for {@param ad} in the direct children of {@param wrapper} and remove the first occurence.
 * See the super's implementation for instance.
 * The NativeExpressHolder recycled by the RecyclerView may be a different
 * instance than the one used previously for this position. Clear the
 * wrapper of any subviews in case it has a different
 * AdView associated with it
 */
protected abstract  void recycleAdViewWrapper(@NonNull ViewGroup wrapper, @NonNull NativeExpressAdView ad);
 
Example #20
Source File: AdViewWrappingStrategyBase.java    From admobadapter with Apache License 2.0 2 votes vote down vote up
/**
 * Add the Native Express {@param ad} to {@param wrapper}.
 * See the super's implementation for instance.
 */
protected abstract void addAdViewToWrapper(@NonNull ViewGroup wrapper, @NonNull NativeExpressAdView ad);