istereo2

view src/android/MainActivity.java @ 27:f0da8b2b61ec

removed iOS cpu restriction and bumped build number to 3 implemented android JNI calls to show/hide ads (untested)
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 05 Oct 2015 17:15:02 +0300
parents 9d53a4938ce8
children 74b50b538858
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 = "";
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 ad_view.loadAd(reqbuild.build());
206 waiting_for_ad = true;
207 }
209 private void show_ad()
210 {
211 Log.i(tag, "show_ad called");
212 //ad_view.setVisibility(View.VISIBLE);
214 if(ad_ready) {
215 ad_win.showAtLocation(ad_main_layout, Gravity.TOP, 0, 0);
216 ad_win.update();
217 } else {
218 if(!waiting_for_ad) {
219 request_ad();
220 }
221 }
222 }
224 private void hide_ad()
225 {
226 Log.i(tag, "hide_ad called");
227 //ad_view.setVisibility(View.GONE);
228 ad_win.dismiss();
229 ad_ready = false;
230 waiting_for_ad = false;
231 }
234 /*
235 @Override
236 public void onAdOpened()
237 {
238 // Code to be executed when an ad opens an overlay that
239 // covers the screen.
240 }
242 @Override
243 public void onAdLeftApplication()
244 {
245 // Code to be executed when the user has left the app.
246 }
248 @Override
249 public void onAdClosed()
250 {
251 // Code to be executed when when the user is about to return
252 // to the app after tapping on an ad.
253 }
254 */
255 }