diff --git a/app/android/client/src/main/java/org/stendhalgame/client/ClientView.java b/app/android/client/src/main/java/org/stendhalgame/client/ClientView.java index d857a724cd4..349baffa9d3 100644 --- a/app/android/client/src/main/java/org/stendhalgame/client/ClientView.java +++ b/app/android/client/src/main/java/org/stendhalgame/client/ClientView.java @@ -18,7 +18,6 @@ import android.content.DialogInterface; import android.content.Intent; import android.content.pm.ApplicationInfo; -import android.content.res.Configuration; import android.graphics.Bitmap; import android.graphics.Color; import android.net.Uri; @@ -32,7 +31,6 @@ import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; -import android.widget.ImageView; /** @@ -40,10 +38,6 @@ */ public class ClientView extends WebView { - // TODO: move to MainActivity or its own class - /** Image used as title page background. */ - private ImageView splash; - /** Client URL path. */ private String clientUrlSuffix = "client"; @@ -422,7 +416,7 @@ public void loadUrl(final String url) { public void loadTitleScreen() { reset(); setPage(PageId.TITLE); - onUpdateTitleOrient(MainActivity.get().getOrientation()); + SplashUtil.get().setVisible(true); loadUrl("about:blank"); Menu.get().show(); } @@ -441,47 +435,6 @@ public void loadLogin() { } } - /** - * Sets the background image. - * - * @param resId - * Resource ID. - * @param bgColor - * Coloring to fill behind splash image. - */ - private void setSplashResource(final int resId, final int bgColor) { - if (splash == null) { - splash = (ImageView) MainActivity.get().findViewById(R.id.splash); - } - splash.setBackgroundColor(bgColor); - splash.setImageResource(resId); - } - - /** - * Sets the background image. - * - * @param resId - * Resource ID. - */ - private void setSplashResource(final int resId) { - setSplashResource(resId, Color.TRANSPARENT); - } - - /** - * Sets splash image dependent on device orientation. - * - * @param orient - * Current screen orientation either portrait (1) or landscape (2). - */ - public void onUpdateTitleOrient(final int orient) { - int splash = R.drawable.splash; - if (orient == Configuration.ORIENTATION_PORTRAIT) { - splash = R.drawable.splash_portrait; - } - // light blue background color - setSplashResource(splash, 0xff6c9ed1); - } - /** * Opens a message dialog for user to choose between main & test clients. */ @@ -548,8 +501,8 @@ private void onSelectServer() { // create a unique state stateId = generateRandomString(); seed = generateRandomString(); - // remove splash image & background coloring - setSplashResource(android.R.color.transparent); + // hide splash image + SplashUtil.get().setVisible(false); final String initialPage = UrlHelper.getInitialPageUrl(); Logger.debug("Loading initial page: " + initialPage); diff --git a/app/android/client/src/main/java/org/stendhalgame/client/MainActivity.java b/app/android/client/src/main/java/org/stendhalgame/client/MainActivity.java index 2012a0539fb..cd30145b996 100644 --- a/app/android/client/src/main/java/org/stendhalgame/client/MainActivity.java +++ b/app/android/client/src/main/java/org/stendhalgame/client/MainActivity.java @@ -110,6 +110,8 @@ private void createClientView() { setActiveClientView(clientView); clientList.addView(clientView); clientView.loadTitleScreen(); + // show splash when a new view is created + SplashUtil.get().setVisible(true); } /** @@ -249,7 +251,7 @@ public void onConfigurationChanged(final Configuration config) { super.onConfigurationChanged(config); final ClientView clientView = getActiveClientView(); if (clientView != null && PageId.TITLE.equals(clientView.getCurrentPageId())) { - clientView.onUpdateTitleOrient(config.orientation); + SplashUtil.get().update(); } } diff --git a/app/android/client/src/main/java/org/stendhalgame/client/SplashUtil.java b/app/android/client/src/main/java/org/stendhalgame/client/SplashUtil.java new file mode 100644 index 00000000000..56efcb23163 --- /dev/null +++ b/app/android/client/src/main/java/org/stendhalgame/client/SplashUtil.java @@ -0,0 +1,90 @@ +/*************************************************************************** + * Copyright © 2024 - Faiumoni e. V. * + *************************************************************************** + *************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ +package org.stendhalgame.client; + +import android.content.res.Configuration; +import android.widget.ImageView; + + +public class SplashUtil { + + /** Image used as title page background. */ + private final ImageView splash; + + /** Singleton instance. */ + private static SplashUtil instance; + + + /** + * Retrieves singleton instance. + */ + public static SplashUtil get() { + if (SplashUtil.instance == null) { + SplashUtil.instance = new SplashUtil(); + } + return SplashUtil.instance; + } + + /** + * Hidden singleton constructor. + */ + private SplashUtil() { + splash = (ImageView) MainActivity.get().findViewById(R.id.splash); + setVisible(false); + } + + /** + * Sets the background splash image. + * + * @param resId + * Resource ID. + * @param bgColor + * Coloring to fill behind splash image. + */ + private void setImage(final int resId, final int bgColor) { + splash.setBackgroundColor(bgColor); + splash.setImageResource(resId); + } + + /** + * Sets splash image dependent on device orientation. + */ + public void update() { + if (!isVisible()) { + return; + } + int resId = R.drawable.splash; + if (MainActivity.get().getOrientation() == Configuration.ORIENTATION_PORTRAIT) { + resId = R.drawable.splash_portrait; + } + // light blue background color + setImage(resId, 0xff6c9ed1); + } + + /** + * Shows or hides background splash image. + * + * @param visible + * `true` if image should visible, `false` if not. + */ + public void setVisible(final boolean visible) { + splash.setVisibility(visible ? ImageView.VISIBLE : ImageView.GONE); + update(); + } + + /** + * Checks if splash is visible. + */ + public boolean isVisible() { + return ImageView.VISIBLE == splash.getVisibility(); + } +}