Android Open Source - android-sdk Ad Alert Reporter






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./*  w  w w .  j  a va  2 s  .  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.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import com.adcash.mobileads.util.Base64;
import com.adcash.mobileads.util.DateAndTime;
import com.adcash.mobileads.util.Streams;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

public class AdAlertReporter {
    private static final String EMAIL_RECIPIENT = "creative-review@adcash.com";
    private static final String EMAIL_SCHEME = "mailto:";
    private static final String SCREEN_SHOT_FILENAME = "mp_adalert_screenshot.png";
    private static final String PARAMETERS_FILENAME = "mp_adalert_parameters.txt";
    private static final String MARKUP_FILENAME = "mp_adalert_markup.html";
    private static final String DATE_FORMAT_PATTERN = "M/d/yy hh:mm:ss a z";
    private static final int IMAGE_QUALITY = 25;
    private static final String BODY_SEPARATOR = "\n=================\n";

    private final String mDateString;

    private final View mView;
    private final Context mContext;
    private final AdConfiguration mAdConfiguration;
    private Intent mEmailIntent;
    private ArrayList<Uri> mEmailAttachments;
    private String mParameters;
    private String mResponse;

    public AdAlertReporter(final Context context, final View view, final AdConfiguration adConfiguration) {
        mView = view;
        mContext = context;
        mAdConfiguration = adConfiguration;

        mEmailAttachments = new ArrayList<Uri>();

        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_PATTERN);
        mDateString = dateFormat.format(DateAndTime.now());

        initEmailIntent();
        Bitmap screenShot = takeScreenShot();
        String screenShotString = convertBitmapInWEBPToBase64EncodedString(screenShot);
        mParameters = formParameters();
        mResponse = getResponseString();

        addEmailSubject();
        addEmailBody( new String[]{ mParameters, mResponse, screenShotString });
        addTextAttachment(PARAMETERS_FILENAME, mParameters);
        addTextAttachment(MARKUP_FILENAME, mResponse);
        addImageAttachment(SCREEN_SHOT_FILENAME, screenShot);
    }

    public void send() {
        mEmailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, mEmailAttachments);

        Intent chooserIntent = Intent.createChooser(mEmailIntent, "Send Email...");
        chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(chooserIntent);
    }

    private void initEmailIntent() {
        Uri emailScheme = Uri.parse(EMAIL_SCHEME);
        mEmailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE, emailScheme);
        mEmailIntent.setType("plain/text");
        mEmailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{EMAIL_RECIPIENT});
    }

    private Bitmap takeScreenShot() {
        if (mView == null || mView.getRootView() == null) {
            return null;
        }

        View rootView = mView.getRootView();
        boolean wasDrawingCacheEnabled = rootView.isDrawingCacheEnabled();
        rootView.setDrawingCacheEnabled(true);

        Bitmap drawingCache = rootView.getDrawingCache();
        if (drawingCache == null) {
            return null;
        }

        Bitmap bitmap = Bitmap.createBitmap(drawingCache);
        rootView.setDrawingCacheEnabled(wasDrawingCacheEnabled);

        return bitmap;
    }

    private String convertBitmapInWEBPToBase64EncodedString(Bitmap bitmap) {
        String result = null;
        if (bitmap != null) {
            try {
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, IMAGE_QUALITY, byteArrayOutputStream);
                byte[] bytes = byteArrayOutputStream.toByteArray();
                result = Base64.encodeToString(bytes, Base64.DEFAULT);
            } catch (Exception e) {
                // should we log something here?
            }
        }
        return result;
    }

    private String formParameters() {
        StringBuilder parameters = new StringBuilder();

        if (mAdConfiguration != null) {
            appendKeyValue(parameters, "sdk_version", mAdConfiguration.getSdkVersion());
            appendKeyValue(parameters, "creative_id", mAdConfiguration.getDspCreativeId());
            appendKeyValue(parameters, "platform_version", Integer.toString(mAdConfiguration.getPlatformVersion()));
            appendKeyValue(parameters, "device_model", mAdConfiguration.getDeviceModel());
            appendKeyValue(parameters, "ad_unit_id", mAdConfiguration.getAdUnitId());
            appendKeyValue(parameters, "device_locale", mAdConfiguration.getDeviceLocale());
            appendKeyValue(parameters, "device_id", mAdConfiguration.getHashedUdid());
            appendKeyValue(parameters, "network_type", mAdConfiguration.getNetworkType());
            appendKeyValue(parameters, "platform", mAdConfiguration.getPlatform());
            appendKeyValue(parameters, "timestamp", getFormattedTimeStamp(mAdConfiguration.getTimeStamp()));
            appendKeyValue(parameters, "ad_type", mAdConfiguration.getAdType());
            appendKeyValue(parameters, "ad_size", "{" + mAdConfiguration.getWidth() + ", " + mAdConfiguration.getHeight() + "}");
        }

        return parameters.toString();
    }

    private String getResponseString() {
        return (mAdConfiguration != null) ? mAdConfiguration.getResponseString() : "";
    }

    private void appendKeyValue(StringBuilder parameters, String key, String value) {
        parameters.append(key);
        parameters.append(" : ");
        parameters.append(value);
        parameters.append("\n");
    }

    private void addEmailSubject() {
        mEmailIntent.putExtra(Intent.EXTRA_SUBJECT, "New creative violation report - " + mDateString);
    }

    private void addEmailBody(String... data) {
        StringBuilder body = new StringBuilder();
        int i = 0;
        while (i<data.length) {
            body.append(data[i]);
            if (i!=data.length-1) {
                body.append(BODY_SEPARATOR);
            }
            i++;
        }
        mEmailIntent.putExtra(Intent.EXTRA_TEXT, body.toString());
    }

    private void addImageAttachment(String fileName, Bitmap bitmap) {
        FileOutputStream fileOutputStream = null;

        if (fileName == null || bitmap == null) {
            return;
        }

        try {
            fileOutputStream = mContext.openFileOutput(fileName, Context.MODE_WORLD_READABLE);
            // image quality is okay to be 0 here, since PNG is lossless and will ignore compression quality
            bitmap.compress(Bitmap.CompressFormat.PNG, IMAGE_QUALITY, fileOutputStream);

            Uri fileUri = Uri.fromFile(new File(mContext.getFilesDir() + File.separator + fileName));
            mEmailAttachments.add(fileUri);
        } catch (Exception exception) {
            Log.d("Adcash", "Unable to write text attachment to file: " + fileName);
        } finally {
            Streams.closeStream(fileOutputStream);
        }
    }

    private void addTextAttachment(String fileName, String body) {
        FileOutputStream fileOutputStream = null;

        if (fileName == null || body == null) {
            return;
        }

        try {
            fileOutputStream = mContext.openFileOutput(fileName, Context.MODE_WORLD_READABLE);
            fileOutputStream.write(body.getBytes());

            Uri fileUri = Uri.fromFile(new File(mContext.getFilesDir() + File.separator + fileName));
            mEmailAttachments.add(fileUri);
        } catch (Exception exception) {
            Log.d("Adcash", "Unable to write text attachment to file: " + fileName);
        } finally {
            Streams.closeStream(fileOutputStream);
        }
    }

    private String getFormattedTimeStamp(long timeStamp) {
        if (timeStamp != -1) {
            SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_PATTERN);
            return dateFormat.format(new Date(timeStamp));
        } else {
            return null;
        }
    }

    @Deprecated // for testing
    Intent getEmailIntent() {
        return mEmailIntent;
    }

    @Deprecated // for testing
    ArrayList<Uri> getEmailAttachments() {
        return mEmailAttachments;
    }

    @Deprecated // for testing
    String getParameters() {
        return mParameters;
    }

    @Deprecated
    String getResponse(){
        return mResponse;
    }
}




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