AdMob support added, you cannot yet hide or reposition the ad from the C code. Also did some cleanup on include files.

This commit is contained in:
pelya
2012-09-05 21:23:36 +03:00
parent 5b21ee268c
commit 96e640f78e
20 changed files with 1251 additions and 258 deletions

View File

@@ -37,7 +37,8 @@ import android.hardware.SensorEvent;
import android.widget.TextView;
class AccelerometerReader implements SensorEventListener {
class AccelerometerReader implements SensorEventListener
{
private SensorManager _manager = null;

View File

@@ -0,0 +1,46 @@
/*
Simple DirectMedia Layer
Java source code (C) 2009-2012 Sergii Pylypenko
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
package net.sourceforge.clonekeenplus;
import android.app.Activity;
import android.content.Context;
import android.view.MotionEvent;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.view.View;
class Advertisement
{
MainActivity parent;
public Advertisement(MainActivity p)
{
parent = p;
}
public View getView()
{
return null;
}
}

View File

@@ -37,7 +37,8 @@ import android.util.Log;
import java.lang.Thread;
class AudioThread {
class AudioThread
{
private MainActivity mParent;
private AudioTrack mAudio;

View File

@@ -59,7 +59,8 @@ import java.lang.String;
import android.text.SpannedString;
class CountingInputStream extends BufferedInputStream {
class CountingInputStream extends BufferedInputStream
{
private long bytesReadMark = 0;
private long bytesRead = 0;

View File

@@ -26,7 +26,8 @@ import android.content.Context;
import java.util.Vector;
import android.view.KeyEvent;
class Globals {
class Globals
{
// These config options are modified by ChangeAppsettings.sh script - see the detailed descriptions there
public static String ApplicationName = "CommanderGenius";
public static String AppLibraries[] = { "sdl-1.2", };
@@ -61,6 +62,9 @@ class Globals {
public static int StartupMenuButtonTimeout = 3000;
public static Settings.Menu HiddenMenuOptions [] = {};
public static Settings.Menu FirstStartMenuOptions [] = { (AppUsesMouse && ! ForceRelativeMouseMode ? new Settings.DisplaySizeConfig(true) : new Settings.DummyMenu()), new Settings.OptionalDownloadConfig(true) };
public static String AdmobPublisherId = "";
public static String AdmobTestDeviceId = "";
public static String AdmobBannerSize = "";
// Phone-specific config, modified by user in "Change phone config" startup dialog, TODO: move this to settings
public static boolean DownloadToSdcard = true;

View File

@@ -29,8 +29,8 @@ import java.lang.reflect.Field;
// Autogenerated by hand with a command:
// grep 'SDLK_' SDL_keysym.h | sed 's/SDLK_\([a-zA-Z0-9_]\+\).*[=] \([0-9]\+\).*/public static final int SDLK_\1 = \2;/' >> Keycodes.java
class SDL_1_2_Keycodes {
class SDL_1_2_Keycodes
{
public static final int SDLK_UNKNOWN = 0;
public static final int SDLK_BACKSPACE = 8;
public static final int SDLK_TAB = 9;
@@ -269,8 +269,8 @@ class SDL_1_2_Keycodes {
// Autogenerated by hand with a command:
// grep 'SDL_SCANCODE_' SDL_scancode.h | sed 's/SDL_SCANCODE_\([a-zA-Z0-9_]\+\).*[=] \([0-9]\+\).*/public static final int SDLK_\1 = \2;/' >> Keycodes.java
class SDL_1_3_Keycodes {
class SDL_1_3_Keycodes
{
public static final int SDLK_UNKNOWN = 0;
public static final int SDLK_A = 4;
public static final int SDLK_B = 5;

View File

@@ -46,6 +46,7 @@ import android.content.Intent;
import android.view.View.OnKeyListener;
import android.view.MenuItem;
import android.view.Menu;
import android.view.Gravity;
import android.text.method.TextKeyListener;
import java.util.LinkedList;
import java.io.SequenceInputStream;
@@ -71,9 +72,11 @@ import android.os.Message;
import java.util.concurrent.Semaphore;
import android.content.pm.ActivityInfo;
public class MainActivity extends Activity {
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setRequestedOrientation(Globals.HorizontalOrientation ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE : ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
@@ -90,12 +93,12 @@ public class MainActivity extends Activity {
System.out.println("libSDL: Creating startup screen");
_layout = new LinearLayout(this);
_layout.setOrientation(LinearLayout.VERTICAL);
_layout.setLayoutParams(new LinearLayout.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
_layout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
_layout2 = new LinearLayout(this);
_layout2.setLayoutParams(new LinearLayout.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
_layout2.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
_btn = new Button(this);
_btn.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
_btn.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
_btn.setText(getResources().getString(R.string.device_change_cfg));
class onClickListener implements View.OnClickListener
{
@@ -125,11 +128,20 @@ public class MainActivity extends Activity {
{
img.setImageResource(R.drawable.publisherlogo);
}
img.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
img.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
_layout.addView(img);
_videoLayout = new FrameLayout(this);
_videoLayout.addView(_layout);
_ad = new Advertisement(this);
if( _ad.getView() != null )
{
_videoLayout.addView(_ad.getView());
_ad.getView().setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM | Gravity.RIGHT));
_ad.getView().setFocusable(true);
_ad.getView().setFocusableInTouchMode(true);
}
setContentView(_videoLayout);
@@ -232,6 +244,8 @@ public class MainActivity extends Activity {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
_videoLayout.removeView(_layout);
if( _ad.getView() != null )
_videoLayout.removeView(_ad.getView());
_layout = null;
_layout2 = null;
_btn = null;
@@ -241,10 +255,18 @@ public class MainActivity extends Activity {
setContentView(_videoLayout);
mGLView = new DemoGLSurfaceView(this);
_videoLayout.addView(mGLView);
// Receive keyboard events
mGLView.setFocusableInTouchMode(true);
mGLView.setFocusable(true);
mGLView.requestFocus();
if( _ad.getView() != null )
{
_videoLayout.addView(_ad.getView());
_ad.getView().setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.TOP | Gravity.RIGHT));
_ad.getView().setFocusable(true);
_ad.getView().setFocusableInTouchMode(true);
_ad.getView().requestFocus();
}
// Receive keyboard events
DimSystemStatusBar.get().dim(_videoLayout);
DimSystemStatusBar.get().dim(mGLView);
}
@@ -261,6 +283,8 @@ public class MainActivity extends Activity {
_isPaused = true;
if( mGLView != null )
mGLView.onPause();
//if( _ad.getView() != null )
// _ad.getView().onPause();
super.onPause();
}
@@ -283,6 +307,8 @@ public class MainActivity extends Activity {
initSDL();
}
}
//if( _ad.getView() != null )
// _ad.getView().onResume();
_isPaused = false;
}
@@ -396,6 +422,41 @@ public class MainActivity extends Activity {
mGLView.requestFocus();
};
public void setAdvertisementParams(int visible, int left, int top)
{
if( _ad.getView() != null )
{
if( visible == 0 )
_ad.getView().setVisibility(View.GONE);
else
{
FrameLayout.LayoutParams layout = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.TOP | Gravity.LEFT);
layout.leftMargin = left;
layout.topMargin = top;
_ad.getView().setLayoutParams(layout);
_ad.getView().setVisibility(View.VISIBLE);
}
}
}
public void getAdvertisementParams(int size[])
{
size[0] = 0;
size[1] = 0;
size[2] = 0;
size[3] = 0;
size[4] = 0;
if( _ad.getView() != null )
{
size[0] = _ad.getView().getMeasuredWidth();
size[1] = _ad.getView().getMeasuredHeight();
size[2] = (_ad.getView().getVisibility() == View.VISIBLE) ? 1 : 0;
FrameLayout.LayoutParams layout = (FrameLayout.LayoutParams) _ad.getView().getLayoutParams();
size[3] = layout.leftMargin;
size[4] = layout.topMargin;
}
}
@Override
public boolean onKeyDown(int keyCode, final KeyEvent event)
{
@@ -442,45 +503,6 @@ public class MainActivity extends Activity {
return true;
}
// Action bar support for Android 3+, which replaces the Menu button, however in emulator everything works, because it's broken
// Also this does not work, because that action bar thingie is NOT shown for fullscreen apps, so we're targetting the legacy compatibility mode here
/*
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if( mGLView != null )
{
if(item.getItemId() == 2)
{
mGLView.nativeKey( KeyEvent.KEYCODE_SEARCH, 1 );
mGLView.nativeKey( KeyEvent.KEYCODE_SEARCH, 0 );
}
else
{
mGLView.nativeKey( KeyEvent.KEYCODE_MENU, 1 );
mGLView.nativeKey( KeyEvent.KEYCODE_MENU, 0 );
}
}
else
if( keyListener != null )
{
if(item.getItemId() == 2)
keyListener.onKeyEvent(KeyEvent.KEYCODE_SEARCH);
else
keyListener.onKeyEvent(KeyEvent.KEYCODE_MENU);
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, 1, 0, "Menu");
menu.add(0, 2, 0, "Search");
return true;
}
*/
@Override
public boolean dispatchTouchEvent(final MotionEvent ev)
{
@@ -488,6 +510,15 @@ public class MainActivity extends Activity {
if(_screenKeyboard != null)
_screenKeyboard.dispatchTouchEvent(ev);
else
if( _ad.getView() != null && // User clicked the advertisement, ignore when user moved finger from game screen to advertisement or touches screen with several fingers
((ev.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN ||
(ev.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP) &&
_ad.getView().getLeft() <= (int)ev.getX() &&
_ad.getView().getRight() > (int)ev.getX() &&
_ad.getView().getTop() <= (int)ev.getY() &&
_ad.getView().getBottom() > (int)ev.getY() )
return super.dispatchTouchEvent(ev);
else
if(mGLView != null)
mGLView.onTouchEvent(ev);
else
@@ -832,6 +863,7 @@ public class MainActivity extends Activity {
private Button _btn = null;
private LinearLayout _layout = null;
private LinearLayout _layout2 = null;
private Advertisement _ad = null;
private FrameLayout _videoLayout = null;
private EditText _screenKeyboard = null;

View File

@@ -60,7 +60,6 @@ import android.content.Intent;
import android.app.PendingIntent;
import android.app.AlarmManager;
// TODO: too much code here, split into multiple files, possibly auto-generated menus?
class Settings
{

View File

@@ -0,0 +1,67 @@
/*
Simple DirectMedia Layer
Java source code (C) 2009-2012 Sergii Pylypenko
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
package net.sourceforge.clonekeenplus;
import android.app.Activity;
import android.content.Context;
import android.view.MotionEvent;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.view.View;
import com.google.ads.*; // Copy GoogleAdMobAdsSdk.jar to the directory project/libs
class Advertisement
{
private AdView ad;
MainActivity parent;
public Advertisement(MainActivity p)
{
parent = p;
AdSize adSize = AdSize.BANNER;
if( Globals.AdmobBannerSize.equals("BANNER") )
adSize = AdSize.BANNER;
else if( Globals.AdmobBannerSize.equals("IAB_BANNER") )
adSize = AdSize.IAB_BANNER;
else if( Globals.AdmobBannerSize.equals("IAB_LEADERBOARD") )
adSize = AdSize.IAB_LEADERBOARD;
else if( Globals.AdmobBannerSize.equals("IAB_MRECT") )
adSize = AdSize.IAB_MRECT;
else if( Globals.AdmobBannerSize.equals("IAB_WIDE_SKYSCRAPER") )
adSize = AdSize.IAB_WIDE_SKYSCRAPER;
else if( Globals.AdmobBannerSize.equals("SMART_BANNER") )
adSize = AdSize.SMART_BANNER;
ad = new AdView(parent, adSize, Globals.AdmobPublisherId);
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(AdRequest.TEST_EMULATOR); // Copy GoogleAdMobAdsSdk.jar to the directory project/libs
adRequest.addTestDevice(Globals.AdmobTestDeviceId);
ad.loadAd(adRequest);
}
public View getView()
{
return ad;
}
}