Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(splash-screen): Add layoutName configuration option for Android #521

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion splash-screen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ These config values are available:
| **`spinnerColor`** | <code>string</code> | Color of the spinner in hex format, #RRGGBB or #RRGGBBAA. | | 1.0.0 |
| **`splashFullScreen`** | <code>boolean</code> | Hide the status bar on the Splash Screen. Only available on Android. | | 1.0.0 |
| **`splashImmersive`** | <code>boolean</code> | Hide the status bar and the software navigation buttons on the Splash Screen. Only available on Android. | | 1.0.0 |
| **`layoutName`** | <code>string</code> | Allows to specify a layout xml file that would be used as the splash layout instead of the default ImageView. Only available on Android. | | 1.1.0 |

### Examples

Expand All @@ -103,7 +104,8 @@ In `capacitor.config.json`:
"iosSpinnerStyle": "small",
"spinnerColor": "#999999",
"splashFullScreen": true,
"splashImmersive": true
"splashImmersive": true,
"layoutName": "launch_screen"
}
}
}
Expand All @@ -130,6 +132,7 @@ const config: CapacitorConfig = {
spinnerColor: "#999999",
splashFullScreen: true,
splashImmersive: true,
layoutName: "launch_screen",
},
},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.capacitorjs.plugins.splashscreen;

import android.animation.Animator;
import android.app.Activity;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
Expand All @@ -9,10 +10,9 @@
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.*;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import androidx.appcompat.app.AppCompatActivity;
Expand All @@ -23,7 +23,7 @@
*/
public class SplashScreen {

private ImageView splashImage;
private View splashImage;
private ProgressBar spinnerBar;
private WindowManager windowManager;
private boolean isVisible = false;
Expand Down Expand Up @@ -83,33 +83,56 @@ public void onDestroy() {

private void buildViews() {
if (splashImage == null) {
int splashId = context.getResources().getIdentifier(config.getResourceName(), "drawable", context.getPackageName());
int splashId = 0;

Drawable splash;
try {
splash = context.getResources().getDrawable(splashId, context.getTheme());
} catch (Resources.NotFoundException ex) {
Logger.warn("No splash screen found, not displaying");
return;
if (config.getLayoutName() != null) {
splashId = context.getResources().getIdentifier(config.getLayoutName(), "layout", context.getPackageName());
if (splashId == 0) {
Logger.warn("Layout not found, defaulting to ImageView");
}
}
if (splashId != 0) {
Activity activity = (Activity) context;
LayoutInflater inflator = activity.getLayoutInflater();
ViewGroup root = new FrameLayout(context);
root.setLayoutParams(
new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
);
splashImage = inflator.inflate(splashId, root, false);
} else {
splashId = context.getResources().getIdentifier(config.getResourceName(), "drawable", context.getPackageName());
try {
splash = context.getResources().getDrawable(splashId, context.getTheme());
} catch (Resources.NotFoundException ex) {
Logger.warn("No splash screen found, not displaying");
return;
}

if (splash instanceof Animatable) {
((Animatable) splash).start();
}
if (splash instanceof Animatable) {
((Animatable) splash).start();
}

if (splash instanceof LayerDrawable) {
LayerDrawable layeredSplash = (LayerDrawable) splash;
if (splash instanceof LayerDrawable) {
LayerDrawable layeredSplash = (LayerDrawable) splash;

for (int i = 0; i < layeredSplash.getNumberOfLayers(); i++) {
Drawable layerDrawable = layeredSplash.getDrawable(i);
for (int i = 0; i < layeredSplash.getNumberOfLayers(); i++) {
Drawable layerDrawable = layeredSplash.getDrawable(i);

if (layerDrawable instanceof Animatable) {
((Animatable) layerDrawable).start();
if (layerDrawable instanceof Animatable) {
((Animatable) layerDrawable).start();
}
}
}
}

splashImage = new ImageView(context);
splashImage = new ImageView(context);
// Stops flickers dead in their tracks
// https://stackoverflow.com/a/21847579/32140
ImageView imageView = (ImageView) splashImage;
imageView.setDrawingCacheEnabled(true);
imageView.setScaleType(config.getScaleType());
imageView.setImageDrawable(splash);
}

splashImage.setFitsSystemWindows(true);

Expand All @@ -126,16 +149,9 @@ private void buildViews() {
splashImage.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
}

// Stops flickers dead in their tracks
// https://stackoverflow.com/a/21847579/32140
splashImage.setDrawingCacheEnabled(true);

if (config.getBackgroundColor() != null) {
splashImage.setBackgroundColor(config.getBackgroundColor());
}

splashImage.setScaleType(config.getScaleType());
splashImage.setImageDrawable(splash);
}

if (spinnerBar == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class SplashScreenConfig {
private boolean immersive = false;
private boolean fullScreen = false;
private ScaleType scaleType = ScaleType.FIT_XY;
private String layoutName;

public Integer getBackgroundColor() {
return backgroundColor;
Expand Down Expand Up @@ -99,4 +100,12 @@ public ScaleType getScaleType() {
public void setScaleType(ScaleType scaleType) {
this.scaleType = scaleType;
}

public String getLayoutName() {
return layoutName;
}

public void setLayoutName(String layoutName) {
this.layoutName = layoutName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ private SplashScreenConfig getSplashScreenConfig() {

Boolean showSpinner = getConfig().getBoolean("showSpinner", config.isShowSpinner());
config.setShowSpinner(showSpinner);
if (getConfig().getString("layoutName") != null) {
config.setLayoutName(getConfig().getString("layoutName"));
}

return config;
}
Expand Down
11 changes: 11 additions & 0 deletions splash-screen/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,17 @@ declare module '@capacitor/cli' {
* @example true
*/
splashImmersive?: boolean;

/**
* Allows to specify a layout xml file that would be used
* as the splash layout instead of the default ImageView.
*
* Only available on Android.
*
* @since 1.1.0
* @example "launch_screen"
*/
layoutName?: string;
};
}
}
Expand Down