istereo2

view src/android/MainActivity.java @ 35:643f4ab609a4

added readme and license
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 31 Oct 2015 05:45:35 +0200
parents 622f61160016
children
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 ad_visible = false;
29 boolean ad_show_pending = false;
30 AdRequest ad_request;
32 @Override
33 protected void onCreate(Bundle saved_inst)
34 {
35 super.onCreate(saved_inst);
37 // go fullscreen
38 int winflags = LayoutParams.FLAG_FULLSCREEN |
39 LayoutParams.FLAG_LAYOUT_NO_LIMITS |
40 LayoutParams.FLAG_LAYOUT_IN_SCREEN |
41 LayoutParams.FLAG_KEEP_SCREEN_ON;
43 Window win = getWindow();
44 win.setFlags(winflags, winflags);
45 }
47 @Override
48 public void onDestroy()
49 {
50 destroy_ad_popup();
51 super.onDestroy();
52 }
54 @Override
55 public void onAttachedToWindow()
56 {
57 super.onAttachedToWindow();
59 create_ad_popup();
60 }
62 @Override
63 public void onWindowFocusChanged(boolean focus)
64 {
65 super.onWindowFocusChanged(focus);
66 if(focus) {
67 set_fullscreen();
68 }
69 }
71 protected void onResume()
72 {
73 Log.i(tag, "[JAVA] onResume()");
74 super.onResume();
75 if(ad_view != null) {
76 ad_view.resume();
77 }
78 set_fullscreen();
79 }
81 protected void onPause()
82 {
83 Log.i(tag, "[JAVA] onPause()");
84 super.onPause();
85 if(ad_view != null) {
86 ad_view.pause();
87 }
88 }
90 /*
91 public void onConfigurationChanged(Configuration config)
92 {
93 super.onConfigurationChanged(config);
95 destroy_ad_popup();
96 create_ad_popup();
97 }
98 */
100 public void set_fullscreen()
101 {
102 int uiflags = View.SYSTEM_UI_FLAG_FULLSCREEN |
103 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
104 View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
105 View.SYSTEM_UI_FLAG_LOW_PROFILE;
107 if(android.os.Build.VERSION.SDK_INT >= 19) {
108 uiflags |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
109 View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
110 View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
111 }
113 View decor = getWindow().getDecorView();
114 decor.setSystemUiVisibility(uiflags);
115 }
117 // ads ...
118 public void create_ad_popup()
119 {
120 Log.i(tag, "[JAVA] create_ad_popup called");
121 if(ad_view != null) return;
123 act = this;
125 /*
126 this.runOnUiThread(new Runnable() {
127 @Override
128 public void run()
129 {*/
130 Log.i(tag, "[JAVA] Creating Ad popup");
132 ad_win = new PopupWindow(act);
133 // set minimum size
134 int xsz = 320;//AdSize.SMART_BANNER.getWidthInPixels(act);
135 int ysz = 50;//AdSize.SMART_BANNER.getHeightInPixels(act);
136 ad_win.setWidth(xsz); // orig:320
137 ad_win.setHeight(ysz); // orig:50
138 ad_win.setWindowLayoutMode(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
139 ad_win.setClippingEnabled(false);
141 ad_main_layout = new LinearLayout(act);
142 MarginLayoutParams params = new MarginLayoutParams(LayoutParams.WRAP_CONTENT,
143 LayoutParams.WRAP_CONTENT);
144 params.setMargins(0, 0, 0, 0);
145 act.setContentView(ad_main_layout, params);
148 ad_view = new AdView(act);
149 ad_view.setAdSize(AdSize.BANNER);
150 ad_view.setAdUnitId(ad_id_test);
152 ad_layout = new LinearLayout(act);
153 ad_layout.setPadding(-5, -5, -5, -5);
154 ad_layout.setOrientation(LinearLayout.VERTICAL);
155 ad_layout.addView(ad_view, params);
156 ad_win.setContentView(ad_layout);
158 ad_view.setAdListener(new AdListener() {
159 @Override
160 public void onAdLoaded()
161 {
162 Log.i(tag, "[JAVA] ad loaded");
163 super.onAdLoaded();
164 ad_ready = true;
165 show_ad();
166 }
167 @Override
168 public void onAdFailedToLoad(int error_code)
169 {
170 Log.e(tag, "[JAVA] ad failed to load, error code: " + error_code);
171 super.onAdFailedToLoad(error_code);
172 ad_ready = false;
173 request_ad();
174 }
175 });
177 /*if(!ad_view.isLoading()) {
178 request_ad();
179 }*/
180 AdRequest.Builder reqbuild = new AdRequest.Builder();
181 reqbuild.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
182 reqbuild.addTestDevice(dev_id_gnexus);
183 reqbuild.addTestDevice(dev_id_nexus7);
184 ad_request = reqbuild.build();
186 ad_win.showAtLocation(ad_main_layout, Gravity.TOP, 0, 0);
187 ad_win.update();
189 show_ad();
191 Log.i(tag, "[JAVA] Done creating ad popup");
192 /*
193 }
194 });*/
195 }
197 public void destroy_ad_popup()
198 {
199 Log.i(tag, "[JAVA] destroy_ad_popup called");
201 if(ad_view != null) {
202 ad_view.destroy();
203 ad_view = null;
204 }
205 if(ad_win != null) {
206 ad_win.dismiss();
207 ad_win = null;
208 }
209 }
211 public void request_ad()
212 {
213 Log.i(tag, "[JAVA] requesting ad");
215 if(ad_view == null) {
216 Log.e(tag, "[JAVA] request_ad called without an ad_view");
217 return;
218 }
220 AdRequest.Builder reqbuild = new AdRequest.Builder();
221 reqbuild.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
222 reqbuild.addTestDevice(dev_id_gnexus);
223 reqbuild.addTestDevice(dev_id_nexus7);
224 ad_view.loadAd(reqbuild.build());
225 }
227 private void show_ad()
228 {
229 Log.i(tag, "[JAVA] show_ad called");
230 if(ad_view == null || ad_request == null) {
231 ad_show_pending = true;
232 return;
233 }
234 /*if(!ad_visible) {
235 ad_view.resume();
236 }*/
237 if(!ad_ready && !ad_view.isLoading()) {
238 ad_view.loadAd(ad_request);
239 return;
240 }
241 ad_view.setVisibility(View.VISIBLE);
242 ad_view.resume();
243 ad_win.update();
244 ad_show_pending = false;
245 /*
246 if(ad_ready) {
247 Log.i(tag, "[JAVA] show_ad called with ad ready");
248 ad_win.showAtLocation(ad_main_layout, Gravity.TOP, 0, 0);
249 ad_win.update();
250 } else {
251 if(!ad_view.isLoading()) {
252 Log.i(tag, "[JAVA] show_ad called with ad neither ready nor pending");
253 request_ad();
254 } else {
255 Log.i(tag, "[JAVA] show_ad called with ad pending: nop");
256 }
257 }
258 */
259 }
261 private void hide_ad()
262 {
263 Log.i(tag, "[JAVA] hide_ad called");
264 ad_view.destroy();
265 ad_view.setVisibility(View.GONE);
266 ad_view.pause();
267 //ad_win.dismiss();
268 ad_win.update();
269 //destroy_ad_popup();
270 ad_ready = false;
271 ad_visible = false;
272 ad_show_pending = false;
273 }
276 /*
277 @Override
278 public void onAdOpened()
279 {
280 // Code to be executed when an ad opens an overlay that
281 // covers the screen.
282 }
284 @Override
285 public void onAdLeftApplication()
286 {
287 // Code to be executed when the user has left the app.
288 }
290 @Override
291 public void onAdClosed()
292 {
293 // Code to be executed when when the user is about to return
294 // to the app after tapping on an ad.
295 }
296 */
297 }