You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
243 lines
8.0 KiB
Java
243 lines
8.0 KiB
Java
package com.xypower.mpres;
|
|
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.res.AssetManager;
|
|
import android.os.Bundle;
|
|
import android.os.Handler;
|
|
import android.text.TextUtils;
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
import androidx.core.graphics.Insets;
|
|
import androidx.core.view.ViewCompat;
|
|
import androidx.core.view.WindowInsetsCompat;
|
|
|
|
import com.dev.devapi.api.SysApi;
|
|
import com.xypower.common.FilesUtils;
|
|
import com.xypower.common.MicroPhotoContext;
|
|
|
|
import java.io.File;
|
|
import java.io.FileOutputStream;
|
|
import java.io.InputStream;
|
|
|
|
public class MainActivity extends AppCompatActivity {
|
|
|
|
private Handler mHandler;
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
setContentView(R.layout.activity_main);
|
|
|
|
Intent intent = getIntent();
|
|
int initres = intent.getIntExtra("initres", 0);
|
|
if (initres != 0) {
|
|
mHandler = new Handler();
|
|
Runnable runnable = new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
notifyMpApp();
|
|
}
|
|
};
|
|
initMpAppConfigurations(getApplicationContext(), mHandler, runnable);
|
|
}
|
|
}
|
|
|
|
private void notifyMpApp() {
|
|
restartMpApp(getApplicationContext(), "FIRST Config Init");
|
|
sleep(100);
|
|
System.exit(0);
|
|
}
|
|
|
|
public static boolean initMpAppConfigurations(final Context context, final Handler handler, final Runnable runnable) {
|
|
|
|
boolean existed = true;
|
|
String destPath = MicroPhotoContext.buildMpAppDir(context);
|
|
File destPathFile = new File(destPath);
|
|
if (destPathFile.exists()) {
|
|
File dataPath = new File(destPathFile, "data");
|
|
if (dataPath.exists()) {
|
|
File file = new File(dataPath, "App.json");
|
|
if (file.exists()) {
|
|
return false;
|
|
} else {
|
|
existed = false;
|
|
}
|
|
} else {
|
|
existed = false;
|
|
|
|
try {
|
|
dataPath.mkdirs();
|
|
} catch (Exception ex) {
|
|
ex.printStackTrace();
|
|
}
|
|
}
|
|
} else {
|
|
existed = false;
|
|
File dataPath = new File(destPathFile, "data");
|
|
try {
|
|
dataPath.mkdirs();
|
|
} catch (Exception ex) {
|
|
ex.printStackTrace();
|
|
}
|
|
}
|
|
|
|
if (existed) {
|
|
return false;
|
|
}
|
|
|
|
Runnable thRunnable = new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
|
|
// sleep(1000);
|
|
|
|
File tmpDestPath = new File(MicroPhotoContext.buildMpResAppDir(context));
|
|
tmpDestPath = new File(tmpDestPath, "mpdata");
|
|
if (tmpDestPath.exists()) {
|
|
try {
|
|
FilesUtils.delete(tmpDestPath);
|
|
tmpDestPath.mkdirs();
|
|
} catch (Exception ex) {
|
|
ex.printStackTrace();
|
|
}
|
|
}
|
|
copyAssetsDir(context, "mpapp/data", tmpDestPath.getAbsolutePath());
|
|
|
|
handler.postDelayed(runnable, 0);
|
|
}
|
|
};
|
|
|
|
Thread th = new Thread(thRunnable);
|
|
th.start();
|
|
|
|
return true;
|
|
}
|
|
|
|
public static boolean initMpMasterConfigurations(final Context context) {
|
|
String destPath = MicroPhotoContext.buildMasterAppDir(context);
|
|
File destPathFile = new File(destPath);
|
|
File dataPath = new File(destPathFile, "data");
|
|
File file = new File(dataPath, "Master.json");
|
|
if (file.exists()) {
|
|
return false;
|
|
}
|
|
|
|
if (!dataPath.exists()) {
|
|
try {
|
|
dataPath.mkdirs();
|
|
} catch (Exception ex) {
|
|
ex.printStackTrace();
|
|
}
|
|
}
|
|
|
|
copyAssetsDir(context, "mpmst", dataPath.getAbsolutePath());
|
|
return true;
|
|
}
|
|
|
|
public static void copyAssetsDir(Context context, String directory, String destPath) {
|
|
try {
|
|
AssetManager assetManager = context.getAssets();
|
|
String[] fileList = assetManager.list(directory);
|
|
if (fileList != null && fileList.length > 0) {
|
|
File file = new File(destPath);
|
|
if (!file.exists()) {
|
|
file.mkdirs();
|
|
}
|
|
|
|
if (!directory.endsWith(File.separator)) {
|
|
directory += File.separator;
|
|
}
|
|
if (!destPath.endsWith(File.separator)) {
|
|
destPath += File.separator;
|
|
}
|
|
|
|
for (String fileName : fileList) {
|
|
copyAssetsDir(context, directory + fileName, destPath + fileName);
|
|
}
|
|
} else {
|
|
// Try to file
|
|
copyAssetsFile(context, directory, destPath);
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
// else {//如果是文件
|
|
// InputStream inputStream=context.getAssets().open(filePath);
|
|
// File file=new File(context.getFilesDir().getAbsolutePath()+ File.separator+filePath);
|
|
// Log.i("copyAssets2Phone","file:"+file);
|
|
// if(!file.exists() || file.length()==0) {
|
|
// FileOutputStream fos=new FileOutputStream(file);
|
|
// int len=-1;
|
|
// byte[] buffer=new byte[1024];
|
|
// while ((len=inputStream.read(buffer))!=-1){
|
|
// fos.write(buffer,0,len);
|
|
// }
|
|
// fos.flush();
|
|
// inputStream.close();
|
|
// fos.close();
|
|
// showToast(context,"模型文件复制完毕");
|
|
// } else {
|
|
// showToast(context,"模型文件已存在,无需复制");
|
|
// }
|
|
// }
|
|
}
|
|
|
|
|
|
public static void copyAssetsFile(Context context, String fileName, String destPath) {
|
|
InputStream inputStream = null;
|
|
FileOutputStream fos = null;
|
|
try {
|
|
inputStream = context.getAssets().open(fileName);
|
|
//getFilesDir() 获得当前APP的安装路径 /data/data/包名/files 目录
|
|
File file = new File(destPath);
|
|
if (file.exists()) {
|
|
file.delete();
|
|
}
|
|
|
|
fos = new FileOutputStream(file);
|
|
int len = -1;
|
|
byte[] buffer = new byte[1024];
|
|
while ((len = inputStream.read(buffer)) != -1) {
|
|
try {
|
|
fos.write(buffer, 0, len);
|
|
} catch (Exception ex) {
|
|
ex.printStackTrace();
|
|
}
|
|
}
|
|
fos.flush();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
} finally {
|
|
FilesUtils.closeFriendly(inputStream);
|
|
FilesUtils.closeFriendly(fos);
|
|
}
|
|
}
|
|
|
|
private static void sleep(long ms) {
|
|
try {
|
|
Thread.sleep(ms);
|
|
} catch (Exception ex) {
|
|
}
|
|
}
|
|
|
|
public static void restartMpApp(Context context, String reason) {
|
|
|
|
SysApi.forceStopApp(context, MicroPhotoContext.PACKAGE_NAME_MPAPP);
|
|
sleep(100);
|
|
|
|
try {
|
|
Intent intent = context.getPackageManager().getLaunchIntentForPackage(MicroPhotoContext.PACKAGE_NAME_MPAPP);
|
|
if (intent != null) {
|
|
intent.putExtra("noDelay", 1);
|
|
if (!TextUtils.isEmpty(reason)) {
|
|
intent.putExtra("reason", reason);
|
|
}
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
context.startActivity(intent);
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
} |