istereo2

view src/android/MainActivity.java @ 30:900651a2f401

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 08 Oct 2015 06:54:18 +0300
parents c6c45fa9658d
children 48a0660bac82
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 /*
53 @Override
54 public void onAttachedToWindow()
55 {
56 super.onAttachedToWindow();
58 create_ad_popup();
59 }
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 super.onResume();
74 if(ad_view != null) {
75 ad_view.resume();
76 }
77 set_fullscreen();
78 }
80 protected void onPause()
81 {
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, "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, "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);
156 ad_view.setAdListener(new AdListener() {
157 @Override
158 public void onAdLoaded()
159 {
160 super.onAdLoaded();
161 Log.i(tag, "ad loaded");
162 ad_ready = true;
163 waiting_for_ad = false;
164 show_ad();
165 }
166 @Override
167 public void onAdFailedToLoad(int error_code)
168 {
169 super.onAdFailedToLoad(error_code);
170 Log.e(tag, "ad failed to load, error code: " + error_code);
171 ad_ready = false;
172 waiting_for_ad = false;
173 request_ad();
174 }
175 });
177 Log.i(tag, "Done creating ad popup");
178 }
179 });
180 }
182 public void destroy_ad_popup()
183 {
184 Log.i(tag, "destroy_ad_popup called");
186 if(ad_view != null) {
187 ad_view.destroy();
188 ad_view = null;
189 }
190 if(ad_win != null) {
191 ad_win.dismiss();
192 ad_win = null;
193 }
194 }
196 public void request_ad()
197 {
198 Log.i(tag, "requesting ad");
200 AdRequest.Builder reqbuild = new AdRequest.Builder();
201 reqbuild.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
202 reqbuild.addTestDevice(dev_id_gnexus);
203 reqbuild.addTestDevice(dev_id_nexus7);
204 ad_view.loadAd(reqbuild.build());
206 waiting_for_ad = true;
207 }
209 private void show_ad()
210 {
211 Log.i(tag, "show_ad called");
213 if(ad_win == null) {
214 create_ad_popup();
215 }
217 if(ad_ready) {
218 //ad_view.setVisibility(View.VISIBLE);
219 ad_win.showAtLocation(ad_main_layout, Gravity.TOP, 0, 0);
220 ad_win.update();
221 } else {
222 if(!waiting_for_ad) {
223 request_ad();
224 }
225 }
226 }
228 private void hide_ad()
229 {
230 Log.i(tag, "hide_ad called");
231 //ad_view.setVisibility(View.GONE);
232 ad_win.dismiss();
233 //ad_win.update();
234 //destroy_ad_popup();
235 ad_ready = false;
236 waiting_for_ad = false;
237 }
240 /*
241 @Override
242 public void onAdOpened()
243 {
244 // Code to be executed when an ad opens an overlay that
245 // covers the screen.
246 }
248 @Override
249 public void onAdLeftApplication()
250 {
251 // Code to be executed when the user has left the app.
252 }
254 @Override
255 public void onAdClosed()
256 {
257 // Code to be executed when when the user is about to return
258 // to the app after tapping on an ad.
259 }
260 */
261 }