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

【igl nanovg part-1】Android : add seperate nanovg sample project #218

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,30 @@
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 SampleActivity extends Activity implements View.OnClickListener {
// UI
LinearLayout mMainView;
LinearLayout mTabBar;
FrameLayout mBackendViewFrame;

private static String TAG = "SampleActivity";

// initialize runtime backend configuration / context
private int curConfig = 0;
private SampleLib.RenderSessionConfig[] mConfigs;
Expand Down Expand Up @@ -89,6 +98,16 @@ protected void onCreate(Bundle icicle) {
mMainView.addView(mTabBar);
mMainView.addView(mBackendViewFrame);
setContentView(mMainView);

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();
}
}

@Override
Expand Down Expand Up @@ -136,4 +155,47 @@ protected void onResume() {
((SampleView) mTabViews[curConfig]).onResume();
}
}

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();
}
}

}
Loading