istereo2

view src/android/MainActivity.java @ 32:622f61160016

ok this fixes it but why?
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 09 Oct 2015 05:17:11 +0300
parents 48a0660bac82
children 3784a2d4bed5
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 = null;
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 if(!waiting_for_ad) {
177 request_ad();
178 }
180 Log.i(tag, "[JAVA] Done creating ad popup");
181 }
182 });
183 }
185 public void destroy_ad_popup()
186 {
187 Log.i(tag, "[JAVA] destroy_ad_popup called");
189 if(ad_view != null) {
190 ad_view.destroy();
191 ad_view = null;
192 }
193 if(ad_win != null) {
194 ad_win.dismiss();
195 ad_win = null;
196 }
197 }
199 public void request_ad()
200 {
201 Log.i(tag, "[JAVA] requesting ad");
203 if(ad_view == null) {
204 Log.e(tag, "[JAVA] request_ad called without an ad_view");
205 return;
206 }
208 AdRequest.Builder reqbuild = new AdRequest.Builder();
209 reqbuild.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
210 reqbuild.addTestDevice(dev_id_gnexus);
211 reqbuild.addTestDevice(dev_id_nexus7);
212 ad_view.loadAd(reqbuild.build());
214 waiting_for_ad = true;
215 }
217 private void show_ad()
218 {
219 if(ad_ready) {
220 Log.i(tag, "[JAVA] show_ad called with ad ready");
221 ad_view.setVisibility(View.VISIBLE);
222 ad_win.showAtLocation(ad_main_layout, Gravity.TOP, 0, 0);
223 ad_win.update();
224 } else {
225 if(!waiting_for_ad) {
226 Log.i(tag, "[JAVA] show_ad called with ad neither ready nor pending");
227 request_ad();
228 } else {
229 Log.i(tag, "[JAVA] show_ad called with ad pending: nop");
230 }
231 }
232 }
234 private void hide_ad()
235 {
236 Log.i(tag, "[JAVA] hide_ad called");
237 //ad_view.setVisibility(View.GONE);
238 ad_win.dismiss();
239 //ad_win.update();
240 //destroy_ad_popup();
241 ad_ready = false;
242 waiting_for_ad = false;
243 }
246 /*
247 @Override
248 public void onAdOpened()
249 {
250 // Code to be executed when an ad opens an overlay that
251 // covers the screen.
252 }
254 @Override
255 public void onAdLeftApplication()
256 {
257 // Code to be executed when the user has left the app.
258 }
260 @Override
261 public void onAdClosed()
262 {
263 // Code to be executed when when the user is about to return
264 // to the app after tapping on an ad.
265 }
266 */
267 }