Android Open Source - snakk-ads-android-sample-app Mraid Command






From Project

Back to project page snakk-ads-android-sample-app.

License

The source code is released under:

Copyright (c) 2012, Snakk! Media Group All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are m...

If you think the Android project snakk-ads-android-sample-app listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.snakk.adview;
// www.  ja  v  a2  s .co  m
import android.content.Intent;
import android.net.Uri;
import android.provider.CalendarContract;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import com.snakk.advertising.internal.AdActivityContentWrapper;
import com.snakk.advertising.internal.SnakkAdActivity;
import com.snakk.adview.Mraid.*;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

enum MraidCommand implements IMraidCommand {
    CLOSE("close", new IMraidCommand() {
        @Override
        public void execute(Map<String, String> params, AdViewCore adView) {
            SnakkAdActivity snakkAdActivity = adView.getMraidExpandedActivity();
            if (snakkAdActivity != null) {
                // closing expanded ad
                snakkAdActivity.close();
            }
            else {
                // closing resized ad
                adView.mraidClose();
            }
        }
    }),

    EXPAND("expand", new IMraidCommand() {
        @Override
        public void execute(Map<String, String> params, final AdViewCore adView) {

            final boolean useCustomClose = "true".equalsIgnoreCase(params.get("useCustomClose"));

            AdActivityContentWrapper wrapper = new AdActivityContentWrapper() {
                private ViewGroup parent = null;

                @Override
                public View getContentView(final SnakkAdActivity activity) {
                    activity.setCloseButtonVisible(!useCustomClose);

                    // transplant view from current location to activity
                    parent  = (ViewGroup)adView.getParent();
                    parent.removeView(adView);
                    adView.setMraidExpandedActivity(activity);
                    return adView;
                }

                @Override
                public ViewGroup.LayoutParams getContentLayoutParams() {
                    // force the webview to take up the entire area
                    return new FrameLayout.LayoutParams(
                                                ViewGroup.LayoutParams.MATCH_PARENT,
                                                ViewGroup.LayoutParams.MATCH_PARENT,
                                                Gravity.CENTER);
                }

                @Override
                public void startContent() {
                    AdViewCore.OnAdDownload listener =  adView.getOnAdDownload();
                    if (listener != null) {
                        listener.willPresentFullScreen(adView);
                    }
                    adView.setMraidState(Mraid.MraidState.EXPANDED);
                    adView.syncMraidState();
                    adView.fireMraidEvent(Mraid.MraidEvent.STATECHANGE, adView.getMraidState().value);
                    adView.fireMraidEvent(Mraid.MraidEvent.SIZECHANGE, "["
                            + adView.pxToDip(adView.getWidth()) + ',' + adView.pxToDip(adView.getHeight()) + ']');
                }

                @Override
                public void done() {
                    // noop
                }

                @Override
                public void stopContent() {
                    // put the view back where we found it
                    //TODO investigate if there could be problems w/ the underlying activity being destroyed and recreated...
                    ViewGroup fosterParent = (ViewGroup)adView.getParent();
                    fosterParent.removeView(adView);
                    parent.addView(adView);
                    adView.mraidClose();
                }
            };

            SnakkAdActivity.startActivity(adView.getContext(), wrapper);
        }
    }),

    RESIZE("resize", new IMraidCommand() {
        @Override
        public void execute(Map<String, String> params, final AdViewCore adView) {
            // tell adview to expand to full screen
            final int height = Integer.parseInt(params.get("height"));
            final int width = Integer.parseInt(params.get("width"));
            int offsetX = Integer.parseInt(params.get("offsetX"));
            int offsetY = Integer.parseInt(params.get("offsetY"));
            MraidCloseRegionPosition customClosePosition = MraidCloseRegionPosition.parse(params.get("customClosePosition"));
            boolean allowOffscreen = Boolean.parseBoolean(params.get("allowOffscreen"));

            boolean success = adView.resize(width, height, offsetX, offsetY, customClosePosition, allowOffscreen);
            if (success) {
                // post later to give the ui a chance to update (mraidSync sometimes gets old vals)
                adView.post(new Runnable() {
                    @Override
                    public void run() {
                        adView.setMraidState(Mraid.MraidState.RESIZED);
                        adView.syncMraidState();
                        adView.fireMraidEvent(Mraid.MraidEvent.STATECHANGE, adView.getMraidState().value);
                        adView.fireMraidEvent(Mraid.MraidEvent.SIZECHANGE, "[" + width + ',' + height + ']');
                        AdViewCore.OnAdDownload listener =  adView.getOnAdDownload();
                        if (listener != null) {
                            listener.didResize(adView);
                        }
                    }
                });
            }
            else {
                adView.fireMraidEvent(Mraid.MraidEvent.ERROR, "['failed to resize','resize']");
            }
        }
    }),

    OPEN("open", new IMraidCommand() {
        @Override
        public void execute(Map<String, String> params, AdViewCore adView) {
            // tell adview to open url in in-app browser
            String url = params.get("url");
            adView.setOpenInInternalBrowser(true);
            adView.open(url);
        }
    }),

    CUSTOM_CLOSE_BUTTON("useCustomClose", new IMraidCommand() {
        @Override
        public void execute(Map<String, String> params, AdViewCore adView) {
            // tell adview if we should render a close button
            boolean useCustomClose = "true".equalsIgnoreCase(params.get("useCustomClose"));
            adView.useCustomCloseButton(useCustomClose);
        }
    }),

    CREATE_CALENDAR_EVENT("createCalendarEvent", new IMraidCommand() {
        @Override
        public void execute(Map<String, String> params, AdViewCore adView) {
            //{description: "Mayan Apocalypse/End of World",
            //location: "everywhere", start: "2012-12-21T00:00-05:00", end: "2012-12-22T00:00-05:00",
            //recurrence:{frequency: "daily"}}

            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ");

            try {
                Date startDate = formatter.parse(params.get("start"));
                Date endDate = formatter.parse(params.get("end"));

                String location = params.get("location");
                String title = params.get("summary"); // the title comes in as description...
                String description = params.get("description"); // the title comes in as description...

                //TODO handle re-occurrence rules
//                String frequency = params.get("frequency");
//                String recurrence = "FREQ=DAILY";

                Intent intent = new Intent(Intent.ACTION_INSERT)
                        .setData(CalendarContract.Events.CONTENT_URI)
                        .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startDate.getTime())
                        .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endDate.getTime())
                        .putExtra(CalendarContract.Events.TITLE, title)
                        .putExtra(CalendarContract.Events.DESCRIPTION, description)
                        .putExtra(CalendarContract.Events.EVENT_LOCATION, location)
//                        .putExtra(CalendarContract.Events.RRULE, recurrence);
                        ;
                adView.getContext().startActivity(intent);
            } catch (ParseException e) {
                Log.e(TAG, "Failed To parse dates", e);
            }
        }
    }),

    PLAY_VIDEO("playVideo", new IMraidCommand(){

        @Override
        public void execute(Map<String, String> params, AdViewCore adView) {
            String videoUrl = params.get("url");
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse(videoUrl), "video/*");
            adView.getContext().startActivity(intent);
        }
    }),

    /**
     * only applies to expanded banners and interstitials (e.g. ads displayed from within an AdActivity)
     */
    SET_ORIENTATION_PROPERTIES("setOrientationProperties", new IMraidCommand() {
        @Override
        public void execute(Map<String, String> params, AdViewCore adView) {
//            boolean allowOrientationChange = Boolean.parseBoolean(params.get("allowOrientationChange"));
            MraidOrientation forceOrientation = MraidOrientation.parse(params.get("forceOrientation"));

            if (adView.getMraidExpandedActivity() != null) {
                adView.getMraidExpandedActivity().setRequestedOrientation(forceOrientation.orientation);
            }
            else {
                Log.d(TAG, "setOrientationProperties call ignored");
            }
        }
    });

    private static final String TAG = "Snakk";

    public final String command;
    private final IMraidCommand commandListener;

    private static final Pattern QUESTION_MARK_PATTERN = Pattern.compile("\\?");
    private static final Pattern DBL_SLASH_PATTERN = Pattern.compile("//");

    private MraidCommand(String commandName, IMraidCommand commandCode) {
        command = commandName;
        commandListener = commandCode;
    }

    public void execute(Map<String, String> params, AdViewCore adView) {
        commandListener.execute(params, adView);
    }

    public static MraidCommand marshalMraidCommand(String commandName) {
        for (MraidCommand cmd : MraidCommand.values()) {
            if (cmd.command.equalsIgnoreCase(commandName)) {
                return cmd;
            }
        }

        return null;
    }

    public static void routeRequest(String url, AdViewCore adView) {
        // parse string
        String parts[] = QUESTION_MARK_PATTERN.split(url, 2);
        String commandName = parts[0];
        Map<String, String> params = Collections.emptyMap();
        if(parts.length == 2) {
            // has query string
            try {
                params = Utils.parseUrlParams(url);
            } catch (UnsupportedEncodingException e) {
                Log.e(TAG, "Failed to parse native MRAID QS params: " + url);
                return;
            }
        }
        parts = DBL_SLASH_PATTERN.split(commandName);
        if (parts.length != 2) {
            Log.e(TAG, "Failed to parse native MRAID command: " + url);
            return;
        }
        commandName = parts[1];

        // fire off command
        Log.d(TAG, "Command: " + commandName + '(' + params + ')');
        MraidCommand command = marshalMraidCommand(commandName);
        if (command != null) {
            command.execute(params, adView);
        }
    }
}




Java Source Code List

com.snakk.advertising.Config.java
com.snakk.advertising.SnakkAdPrompt.java
com.snakk.advertising.SnakkAdRequest.java
com.snakk.advertising.SnakkAdvertising.java
com.snakk.advertising.SnakkBannerAdView.java
com.snakk.advertising.SnakkInterstitialAd.java
com.snakk.advertising.SnakkVideoInterstitialAd.java
com.snakk.advertising.internal.AbstractStatefulAd.java
com.snakk.advertising.internal.AdActivityContentWrapper.java
com.snakk.advertising.internal.AdPromptImpl.java
com.snakk.advertising.internal.AdRequestImpl.java
com.snakk.advertising.internal.BasicWebView.java
com.snakk.advertising.internal.DeviceCapabilities.java
com.snakk.advertising.internal.InterstitialAdImpl.java
com.snakk.advertising.internal.InterstitialBaseView.java
com.snakk.advertising.internal.Sharable.java
com.snakk.advertising.internal.SnakkAdActivity.java
com.snakk.advertising.internal.VastPlayerView.java
com.snakk.advertising.internal.VideoInterstitialAdImpl.java
com.snakk.advertising.package-info.java
com.snakk.adview.AdFullscreenView.java
com.snakk.adview.AdInterstitialBaseView.java
com.snakk.adview.AdInterstitialView.java
com.snakk.adview.AdLog.java
com.snakk.adview.AdOfferWallView.java
com.snakk.adview.AdPrompt.java
com.snakk.adview.AdRequest.java
com.snakk.adview.AdVideoUnitView.java
com.snakk.adview.AdViewCore.java
com.snakk.adview.AdView.java
com.snakk.adview.AutoDetectedParametersSet.java
com.snakk.adview.Base64.java
com.snakk.adview.DeprecatedInterstitialBaseView.java
com.snakk.adview.IMraidCommand.java
com.snakk.adview.LockedOrientationDialog.java
com.snakk.adview.MraidCommand.java
com.snakk.adview.Mraid.java
com.snakk.adview.OldAdActivity.java
com.snakk.adview.Rotate3dAnimation.java
com.snakk.adview.Utils.java
com.snakk.adview.track.EventTracker.java
com.snakk.adview.track.InstallTracker.java
com.snakk.adview.package-info.java
com.snakk.core.SnakkLog.java
com.snakk.vastsdk.TVASTAdErrorEvent.java
com.snakk.vastsdk.TVASTAdErrorListener.java
com.snakk.vastsdk.TVASTAdError.java
com.snakk.vastsdk.TVASTAdType.java
com.snakk.vastsdk.TVASTAdView.java
com.snakk.vastsdk.TVASTAd.java
com.snakk.vastsdk.TVASTAdsLoader.java
com.snakk.vastsdk.TVASTAdsRequest.java
com.snakk.vastsdk.TVASTCompanionAdSlot.java
com.snakk.vastsdk.TVASTCompanionAd.java
com.snakk.vastsdk.TVASTCreative.java
com.snakk.vastsdk.TVASTLinearAd.java
com.snakk.vastsdk.TVASTLinearIcon.java
com.snakk.vastsdk.TVASTMediaFile.java
com.snakk.vastsdk.TVASTNonlinearAd.java
com.snakk.vastsdk.TVASTPostbackTask.java
com.snakk.vastsdk.TVASTUtils.java
com.snakk.vastsdk.TVASTVideoAdsManager.java
com.snakk.vastsdk.player.TVASTPlayer.java
com.snakk.vastsdk.player.TVASTSharable.java
com.snakk.vastsdk.player.TVASTTrackingVideoView.java
com.snakk.vastsdk.player.TVASTVideoProgressThread.java
com.snakkads.tabsswipe.BannerAdPromptFragment.java
com.snakkads.tabsswipe.BannerInterstitialFragment.java
com.snakkads.tabsswipe.BannerRichMediaFragment.java
com.snakkads.tabsswipe.BannerStandardMediaFragment.java
com.snakkads.tabsswipe.BannerVideoFragment.java
com.snakkads.tabsswipe.MainActivity.java
com.snakkads.tabsswipe.SplashScreen.java
com.snakkads.tabsswipe.adapter.TabsPagerAdapter.java
com.yourcompany.AdMobActivity.java
com.yourcompany.SampleActivity.java