Android Open Source - android-sdk Mraid Browser






From Project

Back to project page android-sdk.

License

The source code is released under:

Copyright (c) 2013 Adcash OU. All rights reserved under Creative Commons Attribution 3.0 Unported http://creativecommons.org/licenses/by/3.0/ Redistribution and use in source and binary forms, with or...

If you think the Android project android-sdk 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

/*
 * Copyright (c) 2010-2013, Adcash OU.//from  w w w . j a va 2s  .co  m
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *  Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 *  Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 *  Neither the name of 'MoPub Inc.' nor the names of its contributors
 *   may be used to endorse or promote products derived from this software
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.adcash.mobileads;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.webkit.CookieSyncManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import static android.view.ViewGroup.LayoutParams.FILL_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
import static com.adcash.mobileads.resource.Drawables.BACKGROUND;
import static com.adcash.mobileads.resource.Drawables.CLOSE;
import static com.adcash.mobileads.resource.Drawables.LEFT_ARROW;
import static com.adcash.mobileads.resource.Drawables.REFRESH;
import static com.adcash.mobileads.resource.Drawables.RIGHT_ARROW;
import static com.adcash.mobileads.resource.Drawables.UNLEFT_ARROW;
import static com.adcash.mobileads.resource.Drawables.UNRIGHT_ARROW;

public class MraidBrowser extends Activity {
    
    public static final String URL_EXTRA = "extra_url";
    public static final int INNER_LAYOUT_ID = 1;
    private WebView mWebView;
    private ImageButton mBackButton;
    private ImageButton mForwardButton;
    private ImageButton mRefreshButton;
    private ImageButton mCloseButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        getWindow().requestFeature(Window.FEATURE_PROGRESS);
        getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
        
        setContentView(getMraidBrowserView());
        
        Intent intent = getIntent();
        initializeWebView(intent);
        initializeButtons();
        enableCookies();
    }

    private void initializeWebView(Intent intent) {
        WebSettings webSettings = mWebView.getSettings();
        
        webSettings.setJavaScriptEnabled(true);
        
        /* Pinch to zoom is apparently not enabled by default on all devices, so
         * declare zoom support explicitly.
         * http://stackoverflow.com/questions/5125851/enable-disable-zoom-in-android-webview
         */
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setUseWideViewPort(true);
        
        mWebView.loadUrl(intent.getStringExtra(URL_EXTRA));
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description,
                                        String failingUrl) {
                Activity a = (Activity) view.getContext();
                Toast.makeText(a, "MRAID error: " + description, Toast.LENGTH_SHORT).show();
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url == null) return false;

                Uri uri = Uri.parse(url);
                String host = uri.getHost();

                if ((url.startsWith("http:") || url.startsWith("https:"))
                        && !"play.google.com".equals(host)
                        && !"market.android.com".equals(host)) {
                    return false;
                }

                try {
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                } catch (ActivityNotFoundException exception) {
                    Log.w("Adcash", "Unable to start activity for " + url + ". " +
                            "Ensure that your phone can handle this intent.");
                }

                finish();
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                mForwardButton.setImageDrawable(UNRIGHT_ARROW.decodeImage(MraidBrowser.this));
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

                Drawable backImageDrawable = view.canGoBack()
                        ? LEFT_ARROW.decodeImage(MraidBrowser.this)
                        : UNLEFT_ARROW.decodeImage(MraidBrowser.this);
                mBackButton.setImageDrawable(backImageDrawable);

                Drawable forwardImageDrawable = view.canGoForward()
                        ? RIGHT_ARROW.decodeImage(MraidBrowser.this)
                        : UNRIGHT_ARROW.decodeImage(MraidBrowser.this);
                mForwardButton.setImageDrawable(forwardImageDrawable);
            }
        });
        
        mWebView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                Activity a = (Activity) view.getContext();
                a.setTitle("Loading...");
                a.setProgress(progress * 100);
                if (progress == 100) a.setTitle(view.getUrl());
            }
        });
    }
    
    private void initializeButtons() {
        mBackButton.setBackgroundColor(Color.TRANSPARENT);
        mBackButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (mWebView.canGoBack()) mWebView.goBack();
            }
        });
        
        mForwardButton.setBackgroundColor(Color.TRANSPARENT);
        mForwardButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (mWebView.canGoForward()) mWebView.goForward();
            }
        });
        
        mRefreshButton.setBackgroundColor(Color.TRANSPARENT);
        mRefreshButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                mWebView.reload();
            }
        });
        
        mCloseButton.setBackgroundColor(Color.TRANSPARENT);
        mCloseButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                MraidBrowser.this.finish();
            }
        });
    }
    
    private void enableCookies() {
        CookieSyncManager.createInstance(this);
        CookieSyncManager.getInstance().startSync();
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        CookieSyncManager.getInstance().stopSync();
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        CookieSyncManager.getInstance().startSync();
    }

    private View getMraidBrowserView() {
        LinearLayout mraidBrowserView = new LinearLayout(this);
        LinearLayout.LayoutParams browserLayoutParams = new LinearLayout.LayoutParams(FILL_PARENT, FILL_PARENT);
        mraidBrowserView.setLayoutParams(browserLayoutParams);
        mraidBrowserView.setOrientation(LinearLayout.VERTICAL);

        RelativeLayout outerLayout = new RelativeLayout(this);
        LinearLayout.LayoutParams outerLayoutParams = new LinearLayout.LayoutParams(FILL_PARENT, WRAP_CONTENT);
        outerLayout.setLayoutParams(outerLayoutParams);
        mraidBrowserView.addView(outerLayout);

        LinearLayout innerLayout = new LinearLayout(this);
        innerLayout.setId(INNER_LAYOUT_ID);
        RelativeLayout.LayoutParams innerLayoutParams = new RelativeLayout.LayoutParams(FILL_PARENT, WRAP_CONTENT);
        innerLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        innerLayout.setLayoutParams(innerLayoutParams);
        innerLayout.setBackgroundDrawable(BACKGROUND.decodeImage(MraidBrowser.this));
        outerLayout.addView(innerLayout);

        mBackButton = getButton(LEFT_ARROW.decodeImage(MraidBrowser.this));
        mForwardButton = getButton(RIGHT_ARROW.decodeImage(MraidBrowser.this));
        mRefreshButton = getButton(REFRESH.decodeImage(MraidBrowser.this));
        mCloseButton = getButton(CLOSE.decodeImage(MraidBrowser.this));

        innerLayout.addView(mBackButton);
        innerLayout.addView(mForwardButton);
        innerLayout.addView(mRefreshButton);
        innerLayout.addView(mCloseButton);

        mWebView = new WebView(this);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(FILL_PARENT, FILL_PARENT);
        layoutParams.addRule(RelativeLayout.ABOVE, INNER_LAYOUT_ID);
        mWebView.setLayoutParams(layoutParams);
        outerLayout.addView(mWebView);

        return mraidBrowserView;
    }

    private ImageButton getButton(Drawable drawable) {
        ImageButton imageButton = new ImageButton(this);

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT, 1f);
        layoutParams.gravity = Gravity.CENTER_VERTICAL;
        imageButton.setLayoutParams(layoutParams);

        imageButton.setImageDrawable(drawable);

        return imageButton;
    }
}




Java Source Code List

com.adcash.mobileads.AdAlertGestureListener.java
com.adcash.mobileads.AdAlertReporter.java
com.adcash.mobileads.AdConfiguration.java
com.adcash.mobileads.AdFetchTask.java
com.adcash.mobileads.AdFetcher.java
com.adcash.mobileads.AdLoadTask.java
com.adcash.mobileads.AdTypeTranslator.java
com.adcash.mobileads.AdUrlGenerator.java
com.adcash.mobileads.AdViewController.java
com.adcash.mobileads.AdcashActivity.java
com.adcash.mobileads.AdcashConversionTracker.java
com.adcash.mobileads.AdcashErrorCode.java
com.adcash.mobileads.AdcashInterstitial.java
com.adcash.mobileads.AdcashReferrerReceiver.java
com.adcash.mobileads.AdcashView.java
com.adcash.mobileads.Adcash.java
com.adcash.mobileads.BaseHtmlWebView.java
com.adcash.mobileads.BaseInterstitialActivity.java
com.adcash.mobileads.BaseUrlGenerator.java
com.adcash.mobileads.BaseVideoView.java
com.adcash.mobileads.BaseWebView.java
com.adcash.mobileads.CustomEventBannerAdapter.java
com.adcash.mobileads.CustomEventBanner.java
com.adcash.mobileads.CustomEventInterstitialAdapter.java
com.adcash.mobileads.CustomEventInterstitial.java
com.adcash.mobileads.DefaultBannerAdListener.java
com.adcash.mobileads.DefaultInterstitialAdListener.java
com.adcash.mobileads.DiskLruCache.java
com.adcash.mobileads.EventForwardingBroadcastReceiver.java
com.adcash.mobileads.FacebookKeywordProvider.java
com.adcash.mobileads.GpsHelper.java
com.adcash.mobileads.HtmlBannerWebView.java
com.adcash.mobileads.HtmlBanner.java
com.adcash.mobileads.HtmlInterstitialWebView.java
com.adcash.mobileads.HtmlInterstitial.java
com.adcash.mobileads.HtmlWebViewClient.java
com.adcash.mobileads.HtmlWebViewListener.java
com.adcash.mobileads.Log.java
com.adcash.mobileads.MraidAbstractController.java
com.adcash.mobileads.MraidActivity.java
com.adcash.mobileads.MraidBanner.java
com.adcash.mobileads.MraidBrowserController.java
com.adcash.mobileads.MraidBrowser.java
com.adcash.mobileads.MraidCommandFactory.java
com.adcash.mobileads.MraidCommandRegistry.java
com.adcash.mobileads.MraidCommand.java
com.adcash.mobileads.MraidDisplayController.java
com.adcash.mobileads.MraidInterstitial.java
com.adcash.mobileads.MraidProperty.java
com.adcash.mobileads.MraidVideoPlayerActivity.java
com.adcash.mobileads.MraidVideoView.java
com.adcash.mobileads.MraidView.java
com.adcash.mobileads.ResponseBodyInterstitial.java
com.adcash.mobileads.SharedPreferencesHelper.java
com.adcash.mobileads.TaskTracker.java
com.adcash.mobileads.Utils.java
com.adcash.mobileads.VastVideoDownloadTask.java
com.adcash.mobileads.VastVideoInterstitial.java
com.adcash.mobileads.VastVideoView.java
com.adcash.mobileads.ViewGestureDetector.java
com.adcash.mobileads.factories.AdFetchTaskFactory.java
com.adcash.mobileads.factories.AdFetcherFactory.java
com.adcash.mobileads.factories.AdViewControllerFactory.java
com.adcash.mobileads.factories.AdcashViewFactory.java
com.adcash.mobileads.factories.CustomEventBannerAdapterFactory.java
com.adcash.mobileads.factories.CustomEventBannerFactory.java
com.adcash.mobileads.factories.CustomEventInterstitialAdapterFactory.java
com.adcash.mobileads.factories.CustomEventInterstitialFactory.java
com.adcash.mobileads.factories.HtmlBannerWebViewFactory.java
com.adcash.mobileads.factories.HtmlInterstitialWebViewFactory.java
com.adcash.mobileads.factories.HttpClientFactory.java
com.adcash.mobileads.factories.MraidViewFactory.java
com.adcash.mobileads.factories.VastManagerFactory.java
com.adcash.mobileads.factories.VastVideoDownloadTaskFactory.java
com.adcash.mobileads.factories.ViewGestureDetectorFactory.java
com.adcash.mobileads.resource.Drawables.java
com.adcash.mobileads.resource.MraidJavascript.java
com.adcash.mobileads.util.AsyncTasks.java
com.adcash.mobileads.util.Base64.java
com.adcash.mobileads.util.DateAndTime.java
com.adcash.mobileads.util.Dips.java
com.adcash.mobileads.util.Files.java
com.adcash.mobileads.util.HttpClients.java
com.adcash.mobileads.util.HttpResponses.java
com.adcash.mobileads.util.HttpUtils.java
com.adcash.mobileads.util.Json.java
com.adcash.mobileads.util.Lists.java
com.adcash.mobileads.util.Mraids.java
com.adcash.mobileads.util.Reflection.java
com.adcash.mobileads.util.ResponseHeader.java
com.adcash.mobileads.util.Streams.java
com.adcash.mobileads.util.Strings.java
com.adcash.mobileads.util.VersionCode.java
com.adcash.mobileads.util.Views.java
com.adcash.mobileads.util.WebViews.java
com.adcash.mobileads.util.vast.VastManager.java
com.adcash.mobileads.util.vast.VastXmlManager.java