istereo2

annotate src/android/MainActivity.java @ 31:48a0660bac82

fucked up the ads
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 08 Oct 2015 16:23:27 +0300
parents 900651a2f401
children 622f61160016
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@31 71 Log.i(tag, "[JAVA] onResume()");
nuclear@24 72 super.onResume();
nuclear@25 73 if(ad_view != null) {
nuclear@25 74 ad_view.resume();
nuclear@25 75 }
nuclear@24 76 set_fullscreen();
nuclear@24 77 }
nuclear@24 78
nuclear@25 79 protected void onPause()
nuclear@25 80 {
nuclear@31 81 Log.i(tag, "[JAVA] onPause()");
nuclear@25 82 super.onPause();
nuclear@25 83 if(ad_view != null) {
nuclear@25 84 ad_view.pause();
nuclear@25 85 }
nuclear@25 86 }
nuclear@25 87
nuclear@30 88 /*
nuclear@25 89 public void onConfigurationChanged(Configuration config)
nuclear@25 90 {
nuclear@25 91 super.onConfigurationChanged(config);
nuclear@25 92
nuclear@25 93 destroy_ad_popup();
nuclear@25 94 create_ad_popup();
nuclear@25 95 }
nuclear@30 96 */
nuclear@25 97
nuclear@24 98 public void set_fullscreen()
nuclear@24 99 {
nuclear@24 100 int uiflags = View.SYSTEM_UI_FLAG_FULLSCREEN |
nuclear@24 101 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
nuclear@24 102 View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
nuclear@24 103 View.SYSTEM_UI_FLAG_LOW_PROFILE;
nuclear@24 104
nuclear@24 105 if(android.os.Build.VERSION.SDK_INT >= 19) {
nuclear@24 106 uiflags |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
nuclear@24 107 View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
nuclear@24 108 View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
nuclear@24 109 }
nuclear@24 110
nuclear@24 111 View decor = getWindow().getDecorView();
nuclear@24 112 decor.setSystemUiVisibility(uiflags);
nuclear@24 113 }
nuclear@25 114
nuclear@25 115 // ads ...
nuclear@25 116 public void create_ad_popup()
nuclear@25 117 {
nuclear@31 118 Log.i(tag, "[JAVA] create_ad_popup called");
nuclear@25 119 if(ad_view != null) return;
nuclear@25 120
nuclear@25 121 act = this;
nuclear@25 122
nuclear@25 123 this.runOnUiThread(new Runnable() {
nuclear@25 124 @Override
nuclear@25 125 public void run()
nuclear@25 126 {
nuclear@31 127 Log.i(tag, "[JAVA] Creating Ad popup");
nuclear@25 128
nuclear@25 129 ad_win = new PopupWindow(act);
nuclear@25 130 // set minimum size
nuclear@25 131 int xsz = 320;//AdSize.SMART_BANNER.getWidthInPixels(act);
nuclear@25 132 int ysz = 50;//AdSize.SMART_BANNER.getHeightInPixels(act);
nuclear@25 133 ad_win.setWidth(xsz); // orig:320
nuclear@25 134 ad_win.setHeight(ysz); // orig:50
nuclear@25 135 ad_win.setWindowLayoutMode(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
nuclear@25 136 ad_win.setClippingEnabled(false);
nuclear@25 137
nuclear@25 138 ad_main_layout = new LinearLayout(act);
nuclear@25 139 MarginLayoutParams params = new MarginLayoutParams(LayoutParams.WRAP_CONTENT,
nuclear@25 140 LayoutParams.WRAP_CONTENT);
nuclear@25 141 params.setMargins(0, 0, 0, 0);
nuclear@25 142 act.setContentView(ad_main_layout, params);
nuclear@25 143
nuclear@25 144
nuclear@25 145 ad_view = new AdView(act);
nuclear@25 146 ad_view.setAdSize(AdSize.BANNER);
nuclear@25 147 ad_view.setAdUnitId(ad_id_test);
nuclear@25 148
nuclear@25 149 ad_layout = new LinearLayout(act);
nuclear@25 150 ad_layout.setPadding(-5, -5, -5, -5);
nuclear@25 151 ad_layout.setOrientation(LinearLayout.VERTICAL);
nuclear@25 152 ad_layout.addView(ad_view, params);
nuclear@25 153 ad_win.setContentView(ad_layout);
nuclear@25 154
nuclear@25 155 ad_view.setAdListener(new AdListener() {
nuclear@25 156 @Override
nuclear@25 157 public void onAdLoaded()
nuclear@25 158 {
nuclear@31 159 Log.i(tag, "[JAVA] ad loaded");
nuclear@25 160 super.onAdLoaded();
nuclear@25 161 ad_ready = true;
nuclear@25 162 waiting_for_ad = false;
nuclear@25 163 show_ad();
nuclear@25 164 }
nuclear@25 165 @Override
nuclear@25 166 public void onAdFailedToLoad(int error_code)
nuclear@25 167 {
nuclear@31 168 Log.e(tag, "[JAVA] ad failed to load, error code: " + error_code);
nuclear@25 169 super.onAdFailedToLoad(error_code);
nuclear@25 170 ad_ready = false;
nuclear@25 171 waiting_for_ad = false;
nuclear@25 172 request_ad();
nuclear@25 173 }
nuclear@25 174 });
nuclear@25 175
nuclear@31 176 Log.i(tag, "[JAVA] Done creating ad popup");
nuclear@25 177 }
nuclear@25 178 });
nuclear@25 179 }
nuclear@25 180
nuclear@25 181 public void destroy_ad_popup()
nuclear@25 182 {
nuclear@31 183 Log.i(tag, "[JAVA] destroy_ad_popup called");
nuclear@25 184
nuclear@25 185 if(ad_view != null) {
nuclear@25 186 ad_view.destroy();
nuclear@25 187 ad_view = null;
nuclear@25 188 }
nuclear@25 189 if(ad_win != null) {
nuclear@25 190 ad_win.dismiss();
nuclear@25 191 ad_win = null;
nuclear@25 192 }
nuclear@25 193 }
nuclear@25 194
nuclear@25 195 public void request_ad()
nuclear@25 196 {
nuclear@31 197 Log.i(tag, "[JAVA] requesting ad");
nuclear@31 198
nuclear@31 199 if(ad_view == null) {
nuclear@31 200 Log.e(tag, "[JAVA] request_ad called without an ad_view");
nuclear@31 201 return;
nuclear@31 202 }
nuclear@25 203
nuclear@25 204 AdRequest.Builder reqbuild = new AdRequest.Builder();
nuclear@25 205 reqbuild.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
nuclear@25 206 reqbuild.addTestDevice(dev_id_gnexus);
nuclear@28 207 reqbuild.addTestDevice(dev_id_nexus7);
nuclear@25 208 ad_view.loadAd(reqbuild.build());
nuclear@25 209
nuclear@25 210 waiting_for_ad = true;
nuclear@25 211 }
nuclear@25 212
nuclear@25 213 private void show_ad()
nuclear@25 214 {
nuclear@25 215 if(ad_ready) {
nuclear@31 216 Log.i(tag, "[JAVA] show_ad called with ad ready");
nuclear@31 217 ad_view.setVisibility(View.VISIBLE);
nuclear@25 218 ad_win.showAtLocation(ad_main_layout, Gravity.TOP, 0, 0);
nuclear@25 219 ad_win.update();
nuclear@25 220 } else {
nuclear@25 221 if(!waiting_for_ad) {
nuclear@31 222 Log.i(tag, "[JAVA] show_ad called with ad neither ready nor pending");
nuclear@25 223 request_ad();
nuclear@31 224 } else {
nuclear@31 225 Log.i(tag, "[JAVA] show_ad called with ad pending: nop");
nuclear@25 226 }
nuclear@25 227 }
nuclear@25 228 }
nuclear@25 229
nuclear@25 230 private void hide_ad()
nuclear@25 231 {
nuclear@31 232 Log.i(tag, "[JAVA] hide_ad called");
nuclear@25 233 //ad_view.setVisibility(View.GONE);
nuclear@25 234 ad_win.dismiss();
nuclear@29 235 //ad_win.update();
nuclear@29 236 //destroy_ad_popup();
nuclear@25 237 ad_ready = false;
nuclear@25 238 waiting_for_ad = false;
nuclear@25 239 }
nuclear@25 240
nuclear@25 241
nuclear@25 242 /*
nuclear@25 243 @Override
nuclear@25 244 public void onAdOpened()
nuclear@25 245 {
nuclear@25 246 // Code to be executed when an ad opens an overlay that
nuclear@25 247 // covers the screen.
nuclear@25 248 }
nuclear@25 249
nuclear@25 250 @Override
nuclear@25 251 public void onAdLeftApplication()
nuclear@25 252 {
nuclear@25 253 // Code to be executed when the user has left the app.
nuclear@25 254 }
nuclear@25 255
nuclear@25 256 @Override
nuclear@25 257 public void onAdClosed()
nuclear@25 258 {
nuclear@25 259 // Code to be executed when when the user is about to return
nuclear@25 260 // to the app after tapping on an ad.
nuclear@25 261 }
nuclear@25 262 */
nuclear@24 263 }