Skip to content

Commit

Permalink
Create class to handle splash image
Browse files Browse the repository at this point in the history
  • Loading branch information
AntumDeluge committed Apr 4, 2024
1 parent d88ee79 commit 4b415d2
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,18 +31,13 @@
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;


/**
* Interface to handle web content.
*/
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";

Expand Down Expand Up @@ -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();
}
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 4b415d2

Please sign in to comment.