/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2014 Chukong Technologies Inc.
Copyright (c) 2014 Adrian Dawid
http://www.cocos2d-x.org
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 org.cocos2dx.cpp;
import input.your.game.R;
//해당 게임의 패키지로 변경
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.annotation.TargetApi;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Build;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.games.Games;
public class AppActivity extends BaseGameActivity {
/////////////////////////////////이하 AdMob관련 부분////////////////////////////////////
private static AppActivity _appActiviy;
private AdView adView;
private static final String AD_UNIT_ID = "ca-app-pub-0000000000000000/0000000000";
//admob에서 얻은 코드 삽입
// Helper get display screen to avoid deprecated function use
private Point getDisplaySize(Display d)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
return getDisplaySizeGE11(d);
}
return getDisplaySizeLT11(d);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
private Point getDisplaySizeGE11(Display d)
{
Point p = new Point(0, 0);
d.getSize(p);
return p;
}
private Point getDisplaySizeLT11(Display d)
{
try
{
Method getWidth = Display.class.getMethod("getWidth", new Class[] {});
Method getHeight = Display.class.getMethod("getHeight", new Class[] {});
return new Point(((Integer) getWidth.invoke(d, (Object[]) null)).intValue(), ((Integer) getHeight.invoke(d, (Object[]) null)).intValue());
}
catch (NoSuchMethodException e2) // None of these exceptions should ever occur.
{
return new Point(-1, -1);
}
catch (IllegalArgumentException e2)
{
return new Point(-2, -2);
}
catch (IllegalAccessException e2)
{
return new Point(-3, -3);
}
catch (InvocationTargetException e2)
{
return new Point(-4, -4);
}
}
public static void hideAd()
{
_appActiviy.runOnUiThread(new Runnable()
{
@Override
public void run()
{
if (_appActiviy.adView.isEnabled())
_appActiviy.adView.setEnabled(false);
if (_appActiviy.adView.getVisibility() != 4 )
_appActiviy.adView.setVisibility(View.INVISIBLE);
}
});
}
public static void showAd()
{
_appActiviy.runOnUiThread(new Runnable()
{
@Override
public void run()
{
if (!_appActiviy.adView.isEnabled())
_appActiviy.adView.setEnabled(true);
if (_appActiviy.adView.getVisibility() == 4 )
_appActiviy.adView.setVisibility(View.VISIBLE);
}
});
}
@Override
protected void onResume() {
super.onResume();
if (adView != null) {
adView.resume();
}
}
@Override
protected void onPause() {
if (adView != null) {
adView.pause();
}
super.onPause();
}
@Override
protected void onDestroy() {
adView.destroy();
super.onDestroy();
}
/////////////////////////////////이상 AdMob 관련 부분////////////////////////////////////
@Override
protected void onCreate(Bundle savedInstanceState) {
String leaderboardIdsRaw = getString(R.string.leaderboards);
String achievementIdsRaw = getString(R.string.achievements);
leaderboardIDs = leaderboardIdsRaw.split(";");
achievementIDs = achievementIdsRaw.split(";");
currentContext = this;
super.onCreate(savedInstanceState);
////여기서부터 admob관련 추가 부분////
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
int width = getDisplaySize(getWindowManager().getDefaultDisplay()).x;
LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams(
width,
LinearLayout.LayoutParams.WRAP_CONTENT);
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("HASH_DEVICE_ID")
.build();
adView.loadAd(adRequest);
adView.setBackgroundColor(Color.BLACK);
adView.setBackgroundColor(0);
addContentView(adView,adParams);
_appActiviy = this;
////여기까지 admob관련 추가 부분////
}
/////////////////////////////////이하 Google Play 관련 부분////////////////////////////////////
static int currentID;
static int currentAchievementID;
static boolean gpgAvailable;
static String[] leaderboardIDs;
static String[] achievementIDs;
static Context currentContext;
@Override
public void onSignInSucceeded(){
gpgAvailable = true;
}
@Override
public void onSignInFailed(){
gpgAvailable = false;
}
/*@brief Changes the actvie leaderboard
@param The index of the leaderboard
*/
static public void openLeaderboard(int leaderboardID){
currentID = leaderboardID;
}
/*@brief This function opens the leaderboards ui for an leaderboard id*/
static public void openLeaderboardUI(){
if(gpgAvailable){
((AppActivity)currentContext).runOnUiThread(new Runnable() {
public void run() {
((AppActivity)currentContext).startActivityForResult(Games.Leaderboards.getLeaderboardIntent(((AppActivity)currentContext).getGameHelper().getApiClient(), leaderboardIDs[currentID]),2);
}
});
}
}
static public boolean isGPGSupported(){
return gpgAvailable;
}
/*@brief Submits a score to the leaderboard that is currently actvie*/
static public void submitScoreToLeaderboard(int score)
{
if(gpgAvailable){
Games.Leaderboards.submitScore(((AppActivity)currentContext).getGameHelper().getApiClient(),leaderboardIDs[currentID],score);
updateAchievement(100);
}
}
/*@brief Shows the achievements ui*/
static public void showAchievements() {
if(gpgAvailable){
((AppActivity)currentContext).runOnUiThread(new Runnable() {
public void run() {
((AppActivity)currentContext).startActivityForResult(Games.Achievements.getAchievementsIntent(((AppActivity)currentContext).getGameHelper().getApiClient()), 5);
}
});
}
}
/*@brief Changes the actvie Achievement
@param The index of the achievement in the list*/
static public void openAchievement(int achievementID){
currentAchievementID = achievementID;
}
static public void updateAchievement(int percentage){
if(gpgAvailable){
Games.Achievements.unlock(((AppActivity)currentContext).getGameHelper().getApiClient(), achievementIDs[currentAchievementID]);
}
}
static public void exitGame()
{
Intent intent = new Intent(currentContext, MainActivity.class);
MainActivity.exiting=true;
currentContext.startActivity(intent);
}
/////////////////////////////////이상 Google Play 관련 부분////////////////////////////////////
}