plugin.chartboost.hasCachedInterstitial.java Source code

Java tutorial

Introduction

Here is the source code for plugin.chartboost.hasCachedInterstitial.java

Source

//
//  hasCachedInterstitial().java
//  Chartboost Plugin
//
/*
The MIT License (MIT)
    
Copyright (c) 2014 Gremlin Interactive Limited
    
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
    
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
    
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
// ----------------------------------------------------------------------------
// Package name
package plugin.chartboost;

// Android Imports
import android.content.Context;

// JNLua imports
import com.naef.jnlua.LuaState;
import com.naef.jnlua.LuaType;

// Corona Imports
import com.ansca.corona.CoronaActivity;
import com.ansca.corona.CoronaEnvironment;
import com.ansca.corona.CoronaLua;
import com.ansca.corona.CoronaRuntime;
import com.ansca.corona.CoronaRuntimeTask;
import com.ansca.corona.CoronaRuntimeTaskDispatcher;
import com.ansca.corona.storage.FileContentProvider;

// Java/Misc Imports
import java.math.BigDecimal;
import org.json.JSONException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.io.File;
import java.util.concurrent.FutureTask;
import java.util.concurrent.Callable;

// Android Imports
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
import android.R;
import android.R.drawable;

// Chartboost Imports
import com.chartboost.sdk.*;

/**
 * Implements the hasCachedInterstitial() function in Lua.
 * <p>
 * Used for starting a Chartboost session.
 */
public class hasCachedInterstitial implements com.naef.jnlua.NamedJavaFunction {
    /**
     * Gets the name of the Lua function as it would appear in the Lua script.
     * @return Returns the name of the custom Lua function.
     */
    @Override
    public String getName() {
        return "hasCachedInterstitial";
    }

    /**
     * This method is called when the Lua function is called.
     * <p>
     * Warning! This method is not called on the main UI thread.
     * @param luaState Reference to the Lua state.
     *                 Needed to retrieve the Lua function's parameters and to return values back to Lua.
     * @return Returns the number of values to be returned by the Lua function.
     */
    @Override
    public int invoke(LuaState luaState) {
        try {
            // The named location
            final String namedLocation = luaState.checkString(1);

            // Corona Activity
            CoronaActivity coronaActivity = null;
            if (CoronaEnvironment.getCoronaActivity() != null) {
                coronaActivity = CoronaEnvironment.getCoronaActivity();
            }

            // The lua state
            final LuaState L = luaState;

            // See if the more apps page is cached
            FutureTask<Boolean> isCachedResult = new FutureTask<Boolean>(new Callable<Boolean>() {
                @Override
                public Boolean call() throws Exception {
                    // Is more apps cached?
                    boolean result = false;

                    // If the chartboost instance is valid - could be invalid by calling this method before init invokes
                    if (chartboostHelper.chartboostInstance != null) {
                        // Check the result
                        if (namedLocation != null) {
                            result = chartboostHelper.chartboostInstance.hasCachedInterstitial(namedLocation);
                        } else {
                            result = chartboostHelper.chartboostInstance
                                    .hasCachedInterstitial("DefaultInterstitial");
                        }
                    }
                    // Push the result
                    L.pushBoolean(result);
                    // Return the result
                    return result;
                }
            });

            // Run the activity on the uiThread
            if (coronaActivity != null) {
                coronaActivity.runOnUiThread(isCachedResult);

                // Get the value of isCached
                boolean returnValue = isCachedResult.get();
            }
        } catch (Exception ex) {
            // An exception will occur if given an invalid argument or no argument. Print the error.
            ex.printStackTrace();
        }

        return 1;
    }
}