istereo2

annotate src/android/MainActivity.java @ 29:c6c45fa9658d

fixed jni calls for ads
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 08 Oct 2015 04:55:45 +0300
parents 74b50b538858
children 900651a2f401
rev   line source
nuclear@24 1 package com.mutantstargoat.stereotunnel;
nuclear@24 2
nuclear@24 3 import android.os.Bundle;
nuclear@24 4 import android.app.NativeActivity;
nuclear@24 5 import android.view.*;
nuclear@24 6 import android.view.WindowManager.LayoutParams;
nuclear@25 7 import android.view.ViewGroup.MarginLayoutParams;
nuclear@25 8 import android.widget.*;
nuclear@25 9 import com.google.android.gms.ads.*;
nuclear@25 10 import android.util.Log;
nuclear@25 11 import android.content.res.Configuration;
nuclear@24 12
nuclear@24 13 public class MainActivity extends NativeActivity
nuclear@24 14 {
nuclear@25 15 public static String tag = "stereotunnel";
nuclear@25 16 public static String ad_id_test = "ca-app-pub-3940256099942544/6300978111";
nuclear@25 17 public static String ad_id_prod = "ca-app-pub-3466324067850759/4861471428";
nuclear@25 18
nuclear@25 19 public static String dev_id_gnexus = "B6FBA93004067A8DA892B85127D9454C";
nuclear@28 20 public static String dev_id_nexus7 = "EC3418DBCF3628D89527BCE612BBA299";
nuclear@25 21
nuclear@25 22 MainActivity act;
nuclear@25 23
nuclear@25 24 AdView ad_view;
nuclear@25 25 PopupWindow ad_win;
nuclear@25 26 LinearLayout ad_layout, ad_main_layout;
nuclear@25 27 boolean ad_ready = false;
nuclear@25 28 boolean waiting_for_ad = false;
nuclear@24 29
nuclear@24 30 @Override
nuclear@24 31 protected void onCreate(Bundle saved_inst)
nuclear@24 32 {
nuclear@24 33 super.onCreate(saved_inst);
nuclear@24 34
nuclear@24 35 // go fullscreen
nuclear@24 36 int winflags = LayoutParams.FLAG_FULLSCREEN |
nuclear@25 37 LayoutParams.FLAG_LAYOUT_NO_LIMITS |
nuclear@25 38 LayoutParams.FLAG_LAYOUT_IN_SCREEN |
nuclear@25 39 LayoutParams.FLAG_KEEP_SCREEN_ON;
nuclear@25 40
nuclear@24 41 Window win = getWindow();
nuclear@24 42 win.setFlags(winflags, winflags);
nuclear@24 43 }
nuclear@24 44
nuclear@24 45 @Override
nuclear@25 46 public void onDestroy()
nuclear@25 47 {
nuclear@25 48 destroy_ad_popup();
nuclear@25 49 super.onDestroy();
nuclear@25 50 }
nuclear@25 51
nuclear@25 52 @Override
nuclear@25 53 public void onAttachedToWindow()
nuclear@25 54 {
nuclear@25 55 super.onAttachedToWindow();
nuclear@25 56
nuclear@25 57 create_ad_popup();
nuclear@25 58 }
nuclear@25 59
nuclear@25 60 @Override
nuclear@24 61 public void onWindowFocusChanged(boolean focus)
nuclear@24 62 {
nuclear@24 63 super.onWindowFocusChanged(focus);
nuclear@24 64 if(focus) {
nuclear@24 65 set_fullscreen();
nuclear@24 66 }
nuclear@24 67 }
nuclear@24 68
nuclear@24 69 protected void onResume()
nuclear@24 70 {
nuclear@24 71 super.onResume();
nuclear@25 72 if(ad_view != null) {
nuclear@25 73 ad_view.resume();
nuclear@25 74 }
nuclear@24 75 set_fullscreen();
nuclear@24 76 }
nuclear@24 77
nuclear@25 78 protected void onPause()
nuclear@25 79 {
nuclear@25 80 super.onPause();
nuclear@25 81 if(ad_view != null) {
nuclear@25 82 ad_view.pause();
nuclear@25 83 }
nuclear@25 84 }
nuclear@25 85
nuclear@25 86 public void onConfigurationChanged(Configuration config)
nuclear@25 87 {
nuclear@25 88 super.onConfigurationChanged(config);
nuclear@25 89
nuclear@25 90 destroy_ad_popup();
nuclear@25 91 create_ad_popup();
nuclear@25 92 }
nuclear@25 93
nuclear@24 94 public void set_fullscreen()
nuclear@24 95 {
nuclear@24 96 int uiflags = View.SYSTEM_UI_FLAG_FULLSCREEN |
nuclear@24 97 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
nuclear@24 98 View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
nuclear@24 99 View.SYSTEM_UI_FLAG_LOW_PROFILE;
nuclear@24 100
nuclear@24 101 if(android.os.Build.VERSION.SDK_INT >= 19) {
nuclear@24 102 uiflags |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
nuclear@24 103 View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
nuclear@24 104 View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
nuclear@24 105 }
nuclear@24 106
nuclear@24 107 View decor = getWindow().getDecorView();
nuclear@24 108 decor.setSystemUiVisibility(uiflags);
nuclear@24 109 }
nuclear@25 110
nuclear@25 111 // ads ...
nuclear@25 112 public void create_ad_popup()
nuclear@25 113 {
nuclear@25 114 Log.i(tag, "create_ad_popup called");
nuclear@25 115 if(ad_view != null) return;
nuclear@25 116
nuclear@25 117 act = this;
nuclear@25 118
nuclear@25 119 this.runOnUiThread(new Runnable() {
nuclear@25 120 @Override
nuclear@25 121 public void run()
nuclear@25 122 {
nuclear@25 123 Log.i(tag, "Creating Ad popup");
nuclear@25 124
nuclear@25 125 ad_win = new PopupWindow(act);
nuclear@25 126 // set minimum size
nuclear@25 127 int xsz = 320;//AdSize.SMART_BANNER.getWidthInPixels(act);
nuclear@25 128 int ysz = 50;//AdSize.SMART_BANNER.getHeightInPixels(act);
nuclear@25 129 ad_win.setWidth(xsz); // orig:320
nuclear@25 130 ad_win.setHeight(ysz); // orig:50
nuclear@25 131 ad_win.setWindowLayoutMode(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
nuclear@25 132 ad_win.setClippingEnabled(false);
nuclear@25 133
nuclear@25 134 ad_main_layout = new LinearLayout(act);
nuclear@25 135 MarginLayoutParams params = new MarginLayoutParams(LayoutParams.WRAP_CONTENT,
nuclear@25 136 LayoutParams.WRAP_CONTENT);
nuclear@25 137 params.setMargins(0, 0, 0, 0);
nuclear@25 138 act.setContentView(ad_main_layout, params);
nuclear@25 139
nuclear@25 140
nuclear@25 141
nuclear@25 142 ad_view = new AdView(act);
nuclear@25 143 ad_view.setAdSize(AdSize.BANNER);
nuclear@25 144 ad_view.setAdUnitId(ad_id_test);
nuclear@25 145
nuclear@25 146 ad_layout = new LinearLayout(act);
nuclear@25 147 ad_layout.setPadding(-5, -5, -5, -5);
nuclear@25 148 ad_layout.setOrientation(LinearLayout.VERTICAL);
nuclear@25 149 ad_layout.addView(ad_view, params);
nuclear@25 150 ad_win.setContentView(ad_layout);
nuclear@25 151
nuclear@25 152
nuclear@25 153 ad_view.setAdListener(new AdListener() {
nuclear@25 154 @Override
nuclear@25 155 public void onAdLoaded()
nuclear@25 156 {
nuclear@25 157 super.onAdLoaded();
nuclear@25 158 Log.i(tag, "ad loaded");
nuclear@25 159 ad_ready = true;
nuclear@25 160 waiting_for_ad = false;
nuclear@25 161 show_ad();
nuclear@25 162 }
nuclear@25 163 @Override
nuclear@25 164 public void onAdFailedToLoad(int error_code)
nuclear@25 165 {
nuclear@25 166 super.onAdFailedToLoad(error_code);
nuclear@25 167 Log.e(tag, "ad failed to load, error code: " + error_code);
nuclear@25 168 ad_ready = false;
nuclear@25 169 waiting_for_ad = false;
nuclear@25 170
nuclear@25 171 request_ad();
nuclear@25 172 }
nuclear@25 173 });
nuclear@25 174
nuclear@25 175
nuclear@29 176 if(!waiting_for_ad) {
nuclear@29 177 request_ad();
nuclear@29 178 }
nuclear@25 179
nuclear@25 180 Log.i(tag, "Done creating ad popup");
nuclear@25 181 }
nuclear@25 182 });
nuclear@25 183 }
nuclear@25 184
nuclear@25 185 public void destroy_ad_popup()
nuclear@25 186 {
nuclear@25 187 Log.i(tag, "destroy_ad_popup called");
nuclear@25 188
nuclear@25 189 if(ad_view != null) {
nuclear@25 190 ad_view.destroy();
nuclear@25 191 ad_view = null;
nuclear@25 192 }
nuclear@25 193 if(ad_win != null) {
nuclear@25 194 ad_win.dismiss();
nuclear@25 195 ad_win = null;
nuclear@25 196 }
nuclear@25 197 }
nuclear@25 198
nuclear@25 199 public void request_ad()
nuclear@25 200 {
nuclear@25 201 Log.i(tag, "requesting ad");
nuclear@25 202
nuclear@25 203 AdRequest.Builder reqbuild = new AdRequest.Builder();
nuclear@25 204 reqbuild.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
nuclear@25 205 reqbuild.addTestDevice(dev_id_gnexus);
nuclear@28 206 reqbuild.addTestDevice(dev_id_nexus7);
nuclear@25 207 ad_view.loadAd(reqbuild.build());
nuclear@25 208
nuclear@25 209 waiting_for_ad = true;
nuclear@25 210 }
nuclear@25 211
nuclear@25 212 private void show_ad()
nuclear@25 213 {
nuclear@25 214 Log.i(tag, "show_ad called");
nuclear@25 215
nuclear@25 216 if(ad_ready) {
nuclear@29 217 if(ad_win == null) {
nuclear@29 218 create_ad_popup();
nuclear@29 219 }
nuclear@29 220 ad_view.setVisibility(View.VISIBLE);
nuclear@25 221 ad_win.showAtLocation(ad_main_layout, Gravity.TOP, 0, 0);
nuclear@25 222 ad_win.update();
nuclear@29 223 Log.i(tag, "showing ad window: " + ad_win);
nuclear@25 224 } else {
nuclear@25 225 if(!waiting_for_ad) {
nuclear@25 226 request_ad();
nuclear@25 227 }
nuclear@25 228 }
nuclear@25 229 }
nuclear@25 230
nuclear@25 231 private void hide_ad()
nuclear@25 232 {
nuclear@25 233 Log.i(tag, "hide_ad called");
nuclear@25 234 //ad_view.setVisibility(View.GONE);
nuclear@29 235 Log.i(tag, "hiding ad window: " + ad_win);
nuclear@25 236 ad_win.dismiss();
nuclear@29 237 ad_win.update();
nuclear@29 238 //ad_win.update();
nuclear@29 239 //destroy_ad_popup();
nuclear@25 240 ad_ready = false;
nuclear@25 241 waiting_for_ad = false;
nuclear@25 242 }
nuclear@25 243
nuclear@25 244
nuclear@25 245 /*
nuclear@25 246 @Override
nuclear@25 247 public void onAdOpened()
nuclear@25 248 {
nuclear@25 249 // Code to be executed when an ad opens an overlay that
nuclear@25 250 // covers the screen.
nuclear@25 251 }
nuclear@25 252
nuclear@25 253 @Override
nuclear@25 254 public void onAdLeftApplication()
nuclear@25 255 {
nuclear@25 256 // Code to be executed when the user has left the app.
nuclear@25 257 }
nuclear@25 258
nuclear@25 259 @Override
nuclear@25 260 public void onAdClosed()
nuclear@25 261 {
nuclear@25 262 // Code to be executed when when the user is about to return
nuclear@25 263 // to the app after tapping on an ad.
nuclear@25 264 }
nuclear@25 265 */
nuclear@24 266 }