Skip to content

Commit

Permalink
feat(android): Splash loader
Browse files Browse the repository at this point in the history
  • Loading branch information
breautek committed Oct 8, 2024
1 parent 2a96e46 commit bb1a2fe
Show file tree
Hide file tree
Showing 30 changed files with 641 additions and 194 deletions.
117 changes: 117 additions & 0 deletions android/.idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions android/.idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions android/.idea/deploymentTargetSelector.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions android/.idea/dictionaries/norman.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions android/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 15 additions & 4 deletions android/fuse/src/main/java/com/breautek/fuse/FuseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,20 @@

package com.breautek.fuse;

import android.os.Build;
import android.os.Bundle;

import androidx.annotation.ContentView;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.view.View;
import androidx.core.view.WindowCompat;
import androidx.core.view.WindowInsetsControllerCompat;

import android.view.Window;
import android.view.WindowInsetsController;

import com.breautek.fuse.plugins.IFusePluginRegistrar;

Expand Down Expand Up @@ -82,8 +86,15 @@ public FuseContext getFuseContext() {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Window window = getWindow();
WindowCompat.setDecorFitsSystemWindows(window, false);
$fuseContext.onCreate(savedInstanceState);
setContentView($fuseContext.getLayout());
View contentView = $fuseContext.getLayout();
setContentView(contentView);

WindowInsetsControllerCompat controller = WindowCompat.getInsetsController(window, contentView);
controller.setSystemBarsBehavior(WindowInsetsControllerCompat.BEHAVIOR_DEFAULT);
}

@Override
Expand Down
84 changes: 52 additions & 32 deletions android/fuse/src/main/java/com/breautek/fuse/FuseContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.SslErrorHandler;
Expand All @@ -52,8 +53,9 @@

import com.breautek.fuse.plugins.FuseRuntime;
import com.breautek.fuse.utils.IProgressContext;
import com.breautek.fuse.utils.IProgressContextListener;
import com.breautek.fuse.utils.ProgressContext;
import com.breautek.fuse.views.LoaderSplash;
import com.breautek.fuse.views.SplashLoaderView;

import org.bouncycastle.operator.OperatorCreationException;

Expand All @@ -62,14 +64,14 @@

import javax.net.ssl.SSLContext;

public class FuseContext {
public class FuseContext implements IProgressContextListener {
private static final String TAG = "FuseContext";

private final AppCompatActivity $context;

private WebView $webview;
private IProgressContext $loadProgress;
private LoaderSplash $splash;
private final IProgressContext $loadProgress;
private SplashLoaderView $splash;

/*package private*/ final ReadWriteLock $pluginMapLock;
private final Map<String, FusePlugin> $pluginMap;
Expand All @@ -81,17 +83,17 @@ public class FuseContext {

private final Handler $mainThread;

private final FuseAPIServer $apiServer;
private FuseAPIServer $apiServer;

private final PermissionRequestHandler $permissionRequestHandler;

private FuseAPIResponseFactory $responseFactory;

private FuseLogger $logger;
private final FuseLogger $logger;

private ViewGroup $container;

private FuseScreenUtils $screenUtils;
private final FuseScreenUtils $screenUtils;

private static final String LOAD_CONTEXT_CORE = "FuseContext_core";
private static final String LOAD_CONTEXT_API_SERVER = "FuseContext_apiServer";
Expand All @@ -112,6 +114,8 @@ public FuseContext(AppCompatActivity context) {
$loadProgress.setMax(LOAD_CONTEXT_CORE_PLUGINS, 1);
$loadProgress.setMax(LOAD_CONTEXT_WEBVIEW, 1);

$loadProgress.addListener(this);

$screenUtils = new FuseScreenUtils(context);
$logger = new FuseLogger(this);
$responseFactory = new FuseAPIResponseFactory();
Expand All @@ -120,24 +124,8 @@ public FuseContext(AppCompatActivity context) {
$pluginMapLock = new ReentrantReadWriteLock();
$pluginMap = new HashMap<String, FusePlugin>();
$apiRouter = new FuseAPIRouter(this);

$loadProgress.update(LOAD_CONTEXT_CORE, 1);

try {
$apiServer = new FuseAPIServer(this);
} catch (
IOException | UnrecoverableKeyException | CertificateException |
NoSuchAlgorithmException | KeyStoreException | OperatorCreationException |
KeyManagementException
e
) {
throw new RuntimeException(e);
}

$loadProgress.update(LOAD_CONTEXT_API_SERVER, 1);

Log.i(TAG, "API Server Port: " + $apiServer.getPort());

registerPlugin(new FuseRuntime(this));

$loadProgress.update(LOAD_CONTEXT_CORE_PLUGINS, 1);
Expand Down Expand Up @@ -215,7 +203,7 @@ public void onCreate(Bundle bundle) {
$container = _createLayout($context);
$webview = new WebView($context);

$splash = new LoaderSplash($context);
$splash = new SplashLoaderView($context);
$loadProgress.addListener($splash);

$container.addView($webview);
Expand Down Expand Up @@ -252,14 +240,32 @@ public void onReceivedSslError(WebView webview, SslErrorHandler handler, SslErro
}
});

WebSettings settings = $webview.getSettings();
settings.setAllowFileAccess(false);
settings.setAllowContentAccess(false);
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
$webview.setWebChromeClient(new WebChromeClient());
$webview.addJavascriptInterface(this, "BTFuseNative");
$webview.loadUrl("https://localhost/assets/index.html");
final FuseContext self = this;
new Thread(() -> {
try {
$apiServer = new FuseAPIServer(self);
} catch (
IOException | UnrecoverableKeyException | CertificateException |
NoSuchAlgorithmException | KeyStoreException | OperatorCreationException |
KeyManagementException
e
) {
throw new RuntimeException(e);
}
$loadProgress.update(LOAD_CONTEXT_API_SERVER, 1);
Log.i(TAG, "API Server Port: " + $apiServer.getPort());

self.runOnMainThread(() -> {
WebSettings settings = $webview.getSettings();
settings.setAllowFileAccess(false);
settings.setAllowContentAccess(false);
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
$webview.setWebChromeClient(new WebChromeClient());
$webview.addJavascriptInterface(this, "BTFuseNative");
$webview.loadUrl("https://localhost/assets/index.html");
});
}).start();
}

public void onLowMemory() {
Expand Down Expand Up @@ -396,6 +402,11 @@ public void log(int level, String message) {
}
}

@JavascriptInterface
public void onWebviewReady() {
$loadProgress.update(LOAD_CONTEXT_WEBVIEW, 1);
}

public void execCallback(String callbackID, String payload) {
$mainThread.post(() -> {
$webview.evaluateJavascript(String.format("window.__btfuse_doCallback(\"%s\",\"%s\");", callbackID, payload.replace("\"", "\\\"")), null);
Expand All @@ -411,4 +422,13 @@ public void execCallback(String callbackID) {
public void runOnMainThread(Runnable runnable) {
$mainThread.post(runnable);
}

@Override
public void onProgressUpdate(IProgressContext context) {
if (context.isComplete()) {
$mainThread.postDelayed(() -> {
$splash.setVisibility(View.GONE);
}, 300);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public interface IProgress {
void setMax(int max);
void setValue(int value);

boolean isComplete();

void reset();

float getNormalizedValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public interface IProgressContext {
void setMax(String id, int max);
void setValue(String id, int value);

boolean isComplete();
boolean isComplete(String id);

void reset();

float getNormalizedValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,14 @@ public float getNormalizedValue() {

public void addListener(IProgressListener listener) {
$listeners.add(listener);
listener.onProgressUpdate(this);
}

public void removeListener(IProgressListener listener) {
$listeners.remove(listener);
}

public boolean isComplete() {
return getValue() == getMax();
}
}
Loading

0 comments on commit bb1a2fe

Please sign in to comment.