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

@@ -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;