⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ads.java

📁 moblie syncml mail javame
💻 JAVA
字号:
/*
 * Funambol is a mobile platform developed by Funambol, Inc.
 * Copyright (C) 2003 - 2007 Funambol, Inc.
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License version 3 as published by
 * the Free Software Foundation with the addition of the following permission
 * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
 * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE
 * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program; if not, see http://www.gnu.org/licenses or write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA.
 *
 * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite
 * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
 *
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License version 3.
 *
 * In accordance with Section 7(b) of the GNU Affero General Public License
 * version 3, these Appropriate Legal Notices must retain the display of the
 * "Powered by Funambol" logo. If the display of the logo is not reasonably
 * feasible for technical reasons, the Appropriate Legal Notices must display
 * the words "Powered by Funambol".
 */
package com.funambol.mailclient.ui.view;

import com.funambol.mailclient.ui.controller.AppProperties;
import com.funambol.mailclient.ui.controller.Theme;
import com.funambol.mailclient.ui.controller.UIController;
import com.funambol.util.Log;
import java.io.IOException;
import java.util.Random;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *  Ads class manages advertising using banners embedded in jar file
 */
public class Ads {

    /**
     *  ad has call to action set to dial a phone number
     */
    public static final int AD_CALL = 0;
    /**
     *  ad has call to action set to open a website
     */
    public static final int AD_WEBSITE = 1;
    /**
     *  ad has call to action set to show a fullscreen image
     */
    public static final int AD_FULLSCREEN = 2;
    /**
     * number of ads files, the files need to be called
     * something like
     * adsmall0.png
     * adsmall1.png
     * adsmall2.png
     * etc.
     **/
    private static final int AD_NUMBER = 3;
    /**
     *  ad array type, urls and phone number. 
     * in the future this needs to be provided via jad file or remotely
     */
    private static final int[] AD_TYPE = {AD_WEBSITE, //starbucks is web
        AD_FULLSCREEN, //MBank open a fullscreen ad
        AD_CALL //yellowcab dials a phone number
    };
    private static final String[] AD_URL = {"http://www.starbucks.com",
        "http://www.mbank.com",
        null
    };
    private static final String[] AD_PHONE_NUMBER = {null,
        null,
        "7028732000"
    };
    private static final String AD_IMG_SMALL_PATH_PREFIX = Theme.IMAGES_PATH + "adsmall";
    private static final String AD_IMG_LARGE_PATH_PREFIX = Theme.IMAGES_PATH + "adlarge";
    /**
     * the image extension
     */
    private static final String AD_IMG_EXTENSION = ".png";
    /**
     * current small ad image
     */
    private Image small;
    /**
     * current large ad image
     */
    private Image large;
    /**
     * the current index of the ad to display
     */
    private int adIndex = 0;
    /**
     * commands
     */
    private Command actionCommand;
    private Command dismissCommand;
    /**
     * is advertising enabled?
     */
    private boolean enabled = false;
    private String phoneNumber;

    /** Creates a new instance of Ads */
    public Ads() {
        // enable advertising if needed
        //String r = UIController.midlet.getAppProperty("EmbeddedAds");
        String r = UIController.appProperties.get(
                AppProperties.EMBEDDED_ADS);
        if (r != null && (r.toLowerCase().trim().equals("true"))) {
            enabled = true;
        }

        // switch to enable ads on BB: change this to add ads support

        //#ifdef isBlackberry
        //# enabled = false;
        //#endif

        // start from a random ad
        if (enabled) {
            adIndex = Math.abs(new Random().nextInt()) % AD_NUMBER;
        }


    }

    /**
     * @return true if the ad is enabled (i.e. if this is an ad-enabled build)
     */
    public boolean isEnabled() {
        return enabled;
    }

    /**
     * Move to next ad. subsequent calls to paintsmallAd or paintLargeAdd 
     * will display a new ad image
     */
    public void toNextAd() {

        // move to the next ad

        if (adIndex < AD_NUMBER - 1) {
            adIndex = adIndex + 1;
        } else {
            adIndex = 0;
        }

        //adIndex = adIndex < AD_NUMBER ? adIndex++ : 0 ;
        Log.info("tonextad: adindex = " + adIndex);

        initAd(adIndex);
    }

    /**
     * return the actioncommand
     */
    public Command getActionCommand() {

        if (actionCommand == null) {
            actionCommand = new Command("Tell Me More", Command.OK, 0);
        }
        return actionCommand;


    }

    /**
     *@return  the dismisscommand
     */
    public Command getDismissCommand() {
        if (dismissCommand == null) {
            dismissCommand = new Command("Close", Command.BACK, 0);
        }
        return dismissCommand;
    }

    /**
     * paint the small add on the given graphic at 0,0 position
     */
    public int paintSmallAd(Graphics g) {
        if (enabled) {
            try {
                g.drawImage(getSmallImage(), 0, 0, g.TOP | g.LEFT);
                return small.getHeight();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return 0;
    }

    /**
     * paint the large add on the given graphic at 0,0 position
     */
    public int paintLargeAd(Graphics g) {
        if (enabled) {
            try {
                g.drawImage(getLargeImage(), 0, 0, g.TOP | g.LEFT);
                return large.getHeight();
            } catch (IOException ex) {
                Log.error("unamble to open large ad image");
                ex.printStackTrace();
            }
        }
        return 0;
    }

    /**
     * perform the action
     */
    public void doAction() {
        if (isEnabled()) {
            Log.info("DoAction!!");
            switch (AD_TYPE[adIndex]) {

                case AD_CALL:
                    String number = AD_PHONE_NUMBER[adIndex];
                    if (number != null) {
                        UIController.dialNumber(number);
                    }
                    break;


                case AD_WEBSITE:
                    String website = AD_URL[adIndex];
                    if (website != null) {
                        UIController.openUrl(website);
                    }
                    break;

                default:
                    website = AD_URL[adIndex];
                    if (website != null) {
                        UIController.openUrl(website);
                    }
                    ;
            }


        }



    }

    /**
     * free some memory
     */
    public void freeMemory() {
        small = null;
        large = null;
    }

    private Image getLargeImage() throws IOException {
        if (large == null) {
            String path = AD_IMG_LARGE_PATH_PREFIX + adIndex + AD_IMG_EXTENSION;
            large = Image.createImage(path);
        }
        return large;
    }

    private Image getSmallImage() throws IOException {

        if (small == null) {
            String path = AD_IMG_SMALL_PATH_PREFIX + adIndex + AD_IMG_EXTENSION;
            small = Image.createImage(path);
        }
        return small;
    }

    private void initAd(int adIndex) {

        small = null;
        large = null;

    }

    public int getAdType() {
        return AD_TYPE[adIndex];
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -