istereo2

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