22
33import android .annotation .SuppressLint ;
44import android .app .Activity ;
5- import android .content .Context ;
65import android .content .Intent ;
76import android .content .res .Configuration ;
87import android .graphics .Color ;
98import android .net .Uri ;
109import android .os .Bundle ;
1110import android .util .DisplayMetrics ;
1211import android .util .Log ;
13- import android .view .Display ;
1412import android .view .Gravity ;
1513import android .view .View ;
1614import android .view .ViewGroup ;
@@ -47,7 +45,6 @@ protected void onCreate(Bundle savedInstanceState) {
4745
4846 // there is a stripe at the top of the screen for contents
4947 // we eliminate it with hiding the system ui
50- getWindow ().getDecorView ().setSystemUiVisibility (View .SYSTEM_UI_FLAG_LAYOUT_STABLE );
5148 super .onCreate (savedInstanceState );
5249 overridePendingTransition (0 , 0 );
5350
@@ -72,7 +69,7 @@ protected void onCreate(Bundle savedInstanceState) {
7269
7370 // Configure window layout parameters
7471 WindowManager .LayoutParams params = new WindowManager .LayoutParams ();
75- params .gravity = Gravity .TOP | Gravity .LEFT ; // try out START
72+ params .gravity = Gravity .TOP | Gravity .START ; // try out START
7673 params .x = config .x ;
7774 params .y = config .y ;
7875 params .height = config .height ;
@@ -94,10 +91,7 @@ protected void onCreate(Bundle savedInstanceState) {
9491 }
9592
9693 private TransparentActivityConfig setupConfig (@ Nullable TransparentActivityConfig config ) {
97- final WindowManager wm = (WindowManager ) getSystemService (Context .WINDOW_SERVICE );
98- final Display display = wm .getDefaultDisplay ();
99- final DisplayMetrics metrics = new DisplayMetrics (); // this gets all
100- display .getMetrics (metrics );
94+ final DisplayMetrics metrics = UtilsDevice .getDisplayMetrics (this );
10195
10296 if (config == null ) {
10397 Log .w (Countly .TAG , "[TransparentActivity] setupConfig, Config is null, using default values with full screen size" );
@@ -148,19 +142,39 @@ public void onConfigurationChanged(android.content.res.Configuration newConfig)
148142 currentOrientation = newConfig .orientation ;
149143 }
150144
151- // CHANGE SCREEN SIZE
152- final WindowManager wm = (WindowManager ) getSystemService (Context .WINDOW_SERVICE );
153- final Display display = wm .getDefaultDisplay ();
154- final DisplayMetrics metrics = new DisplayMetrics ();
155- display .getMetrics (metrics );
145+ resizeContent ();
146+ }
156147
148+ private void resizeContent () {
149+ // CHANGE SCREEN SIZE
150+ final DisplayMetrics metrics = UtilsDevice .getDisplayMetrics (this );
157151 int scaledWidth = (int ) Math .ceil (metrics .widthPixels / metrics .density );
158152 int scaledHeight = (int ) Math .ceil (metrics .heightPixels / metrics .density );
159153
160154 // refactor in the future to use the resize_me action
161155 webView .loadUrl ("javascript:window.postMessage({type: 'resize', width: " + scaledWidth + ", height: " + scaledHeight + "}, '*');" );
162156 }
163157
158+ @ Override
159+ public void onDestroy () {
160+ close (new HashMap <>());
161+
162+ if (Countly .sharedInstance ().isInitialized ()) {
163+ Countly .sharedInstance ().moduleContent .notifyAfterContentIsClosed ();
164+ }
165+ super .onDestroy ();
166+ }
167+
168+ private void hideSystemUI () {
169+ getWindow ().getDecorView ().setSystemUiVisibility (
170+ View .SYSTEM_UI_FLAG_LAYOUT_STABLE |
171+ View .SYSTEM_UI_FLAG_IMMERSIVE_STICKY
172+ | View .SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
173+ | View .SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
174+ | View .SYSTEM_UI_FLAG_HIDE_NAVIGATION
175+ | View .SYSTEM_UI_FLAG_FULLSCREEN );
176+ }
177+
164178 private void resizeContentInternal () {
165179 switch (currentOrientation ) {
166180 case Configuration .ORIENTATION_LANDSCAPE :
@@ -274,11 +288,7 @@ private void resizeMeAction(Map<String, Object> query) {
274288 return ;
275289 }
276290 try {
277- final WindowManager wm = (WindowManager ) getSystemService (Context .WINDOW_SERVICE );
278- final Display display = wm .getDefaultDisplay ();
279- final DisplayMetrics metrics = new DisplayMetrics ();
280- display .getMetrics (metrics );
281-
291+ final DisplayMetrics metrics = UtilsDevice .getDisplayMetrics (this );
282292 float density = metrics .density ;
283293
284294 JSONObject resizeMeJson = (JSONObject ) resizeMe ;
@@ -414,15 +424,18 @@ private WebView createWebView(TransparentActivityConfig config) {
414424 return false ;
415425 }
416426 });
417- client .afterPageFinished = (closeIt ) -> {
418- if (closeIt ) {
419- close (new HashMap <>());
427+ client .afterPageFinished = new WebViewPageLoadedListener () {
428+ @ Override public void onPageLoaded (boolean timedOut ) {
429+ if (timedOut ) {
430+ close (new HashMap <>());
420431
421- if (Countly .sharedInstance ().isInitialized ()) {
422- Countly .sharedInstance ().moduleContent .notifyAfterContentIsClosed ();
432+ if (Countly .sharedInstance ().isInitialized ()) {
433+ Countly .sharedInstance ().moduleContent .notifyAfterContentIsClosed ();
434+ }
435+ } else {
436+ hideSystemUI ();
437+ webView .setVisibility (View .VISIBLE );
423438 }
424- } else {
425- webView .setVisibility (View .VISIBLE );
426439 }
427440 };
428441 webView .setWebViewClient (client );
0 commit comments