istereo2

view src/android/MainActivity.java @ 28:74b50b538858

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