From 8e85c44ce0a6b669808b368805a39a78e36b90c3 Mon Sep 17 00:00:00 2001 From: vinsentli Date: Sun, 22 Dec 2024 21:19:18 +0800 Subject: [PATCH] android : Add NanovgSampleActivity. --- .../igl/sample/NanovgSampleActivity.java | 91 +++++++++++++++++++ .../facebook/igl/sample/SampleActivity.java | 4 +- build/android/nanovg/AndroidManifest.xml | 27 ++++++ build/android/nanovg/build.gradle | 2 +- 4 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 build/android/app/src/main/java/com/facebook/igl/sample/NanovgSampleActivity.java create mode 100644 build/android/nanovg/AndroidManifest.xml diff --git a/build/android/app/src/main/java/com/facebook/igl/sample/NanovgSampleActivity.java b/build/android/app/src/main/java/com/facebook/igl/sample/NanovgSampleActivity.java new file mode 100644 index 0000000000..5cb48990d0 --- /dev/null +++ b/build/android/app/src/main/java/com/facebook/igl/sample/NanovgSampleActivity.java @@ -0,0 +1,91 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +// @fb-only + +package com.facebook.igl.shell; + +import android.app.Activity; +import android.content.Context; +import android.content.SharedPreferences; +import android.graphics.Color; +import android.os.Bundle; +import android.util.Log; +import android.view.Gravity; +import android.view.SurfaceView; +import android.view.View; +import android.widget.FrameLayout; +import android.widget.LinearLayout; +import android.widget.TextView; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; + +public class NanovgSampleActivity extends SampleActivity implements View.OnClickListener { + private static String TAG = "NanovgSampleActivity"; + + @Override + protected void onCreate(Bundle icicle) { + mEnableStencilBuffer = true; + + super.onCreate(icicle); + + boolean hasCopy = getSharedPreferences("data", Context.MODE_PRIVATE).getBoolean("HasCopyAssets", false); + + if (!hasCopy) { + copyAssetsDirToSDCard(this, "", getFilesDir().getAbsolutePath()); + + SharedPreferences.Editor editor = getSharedPreferences("data", Context.MODE_PRIVATE).edit(); + editor.putBoolean("HasCopyAssets",true); + editor.commit(); + } + } + + public static void copyAssetsDirToSDCard(Context context, String assetsDirName, String sdCardPath) { + Log.d(TAG, "copyAssetsDirToSDCard() called with: context = [" + context + "], assetsDirName = [" + assetsDirName + "], sdCardPath = [" + sdCardPath + "]"); + try { + String list[] = context.getAssets().list(assetsDirName); + if (list.length == 0) { + InputStream inputStream = context.getAssets().open(assetsDirName); + byte[] mByte = new byte[1024]; + int bt = 0; + File file = new File(sdCardPath + File.separator + + assetsDirName); + if (!file.exists()) { + file.createNewFile(); + } else { + return; + } + FileOutputStream fos = new FileOutputStream(file); + while ((bt = inputStream.read(mByte)) != -1) { + fos.write(mByte, 0, bt); + } + fos.flush(); + inputStream.close(); + fos.close(); + } else { + String subDirName = assetsDirName; + if (assetsDirName.contains("/")) { + subDirName = assetsDirName.substring(assetsDirName.lastIndexOf('/') + 1); + } + sdCardPath = sdCardPath + File.separator + subDirName; + File file = new File(sdCardPath); + if (!file.exists()) + file.mkdirs(); + for (String s : list) { + String fileName = assetsDirName.length() > 0 ? assetsDirName + File.separator + s : s; + copyAssetsDirToSDCard(context, fileName , sdCardPath); + } + } + } catch ( + Exception e) { + e.printStackTrace(); + } + } + +} diff --git a/build/android/app/src/main/java/com/facebook/igl/sample/SampleActivity.java b/build/android/app/src/main/java/com/facebook/igl/sample/SampleActivity.java index 822ed8fede..d1ae9a651e 100644 --- a/build/android/app/src/main/java/com/facebook/igl/sample/SampleActivity.java +++ b/build/android/app/src/main/java/com/facebook/igl/sample/SampleActivity.java @@ -33,6 +33,8 @@ public class SampleActivity extends Activity implements View.OnClickListener { private final int selectedTabColor = Color.BLUE; private final int unSelectedTabColor = Color.GRAY; + protected boolean mEnableStencilBuffer = false; + @Override protected void onCreate(Bundle icicle) { super.onCreate(icicle); @@ -69,7 +71,7 @@ protected void onCreate(Bundle icicle) { } else if (mConfigs[i].version.flavor == SampleLib.BackendFlavor.OpenGL_ES) { backendView = new SampleView( - getApplication(), mConfigs[i].version, mConfigs[i].swapchainColorTextureFormat); + getApplication(), mConfigs[i].version, mConfigs[i].swapchainColorTextureFormat, mEnableStencilBuffer); ((SampleView) backendView).onPause(); } diff --git a/build/android/nanovg/AndroidManifest.xml b/build/android/nanovg/AndroidManifest.xml new file mode 100644 index 0000000000..79af625c7b --- /dev/null +++ b/build/android/nanovg/AndroidManifest.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + diff --git a/build/android/nanovg/build.gradle b/build/android/nanovg/build.gradle index 45b123ffdb..15ff7af7f3 100644 --- a/build/android/nanovg/build.gradle +++ b/build/android/nanovg/build.gradle @@ -51,7 +51,7 @@ android { sourceSets { main { - manifest.srcFile '../app/src/main/AndroidManifest.xml' + manifest.srcFile 'AndroidManifest.xml' res.srcDir '../app/src/main/res/' java.srcDir 'java'