package com.xypower.mpapp; import androidx.appcompat.app.AppCompatActivity; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.text.TextUtils; import android.util.Base64; import com.xypower.common.FileUtils; import com.xypower.mpapp.v2.Camera2VideoActivity; import java.io.File; import java.nio.charset.StandardCharsets; public class BridgeActivity extends AppCompatActivity { private final static String ACTION_IMP_PUBKEY = "imp_pubkey"; private final static String ACTION_GEN_KEYS = "gen_keys"; private final static String ACTION_CERT_REQ = "cert_req"; private final static String ACTION_BATTERY_VOLTAGE = "query_bv"; private final static String ACTION_RECORDING = "recording"; private final static String ACTION_TAKE_PHOTO = "take_photo"; private final static int REQUEST_CODE_RECORDING = Camera2VideoActivity.REQUEST_CODE_RECORDING; private Handler mHandler = null; private boolean m3V3TurnedOn = false; private boolean mAutoClose = true; private String mVideoFilePath = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_cert); mHandler = new Handler(); Intent intent = getIntent(); final String action = intent.getStringExtra("action"); if (!TextUtils.isEmpty(action)) { if (TextUtils.equals(action, ACTION_IMP_PUBKEY)) { String cert = intent.getStringExtra("cert"); String path = intent.getStringExtra("path"); int index = intent.getIntExtra("index", 1); if (!TextUtils.isEmpty(cert)) { // Import // String cert = intent.getStringExtra("md5"); byte[] content = Base64.decode(cert, Base64.DEFAULT); if (content != null) { MicroPhotoService.importPublicKey(index, content); } } else if (TextUtils.isEmpty(path)) { String md5 = intent.getStringExtra("md5"); File file = new File(path); if (file.exists() && file.isFile()) { MicroPhotoService.importPublicKeyFile(index, path, md5); } } } else if (TextUtils.equals(action, ACTION_GEN_KEYS)) { int index = intent.getIntExtra("index", 0); boolean res = MicroPhotoService.genKeys(index); String path = intent.getStringExtra("path"); if (!TextUtils.isEmpty(path)) { FileUtils.ensureParentDirectoryExisted(path); FileUtils.writeTextFile(path, res ? "1" : "0"); } } else if (TextUtils.equals(action, ACTION_CERT_REQ)) { int index = intent.getIntExtra("index", 0); int type = intent.getIntExtra("type", 0); String subject = intent.getStringExtra("subject"); String path = intent.getStringExtra("path"); MicroPhotoService.genCertRequest(index, type, subject, path); } else if (TextUtils.equals(action, ACTION_BATTERY_VOLTAGE)) { String path = intent.getStringExtra("path"); // #define CMD_GET_CHARGING_BUS_VOLTAGE_STATE 112 // #define CMD_GET_BAT_VOL_STATE 115 // #define CMD_GET_BAT_BUS_VOLTAGE_STATE 117 int bv = MicroPhotoService.getGpioInt(117); int bcv = MicroPhotoService.getGpioInt(112); if (!TextUtils.isEmpty(path)) { FileUtils.ensureParentDirectoryExisted(path); FileUtils.writeTextFile(path + ".tmp", Integer.toString(bv) + " " + Integer.toString(bcv)); File file = new File(path + ".tmp"); file.renameTo(new File(path)); } } else if (TextUtils.equals(action, ACTION_TAKE_PHOTO)) { String path = intent.getStringExtra("path"); int channel = intent.getIntExtra("channel", 1); int preset = intent.getIntExtra("preset", 0xFF); boolean usb = intent.getBooleanExtra("usb", false); int cameraId = intent.getIntExtra("cameraId", -1); String leftTopOsd = intent.getStringExtra("leftTopOsd"); String rightTopOsd = intent.getStringExtra("rightTopOsd"); String rightBottomOsd = intent.getStringExtra("rightBottomOsd"); String leftBottomOsd = intent.getStringExtra("leftBottomOsd"); File file = new File(path); if (file.exists()) { file.delete(); } else { FileUtils.ensureParentDirectoryExisted(path); } MicroPhotoService.takePhoto(channel, preset, cameraId, usb, path, leftTopOsd, rightTopOsd, rightBottomOsd, leftBottomOsd); } else if (TextUtils.equals(action, ACTION_RECORDING)) { String path = intent.getStringExtra("path"); int channel = intent.getIntExtra("channel", 1); int cameraId = intent.getIntExtra("cameraId", -1); int quality = intent.getIntExtra("quality", 0); int width = intent.getIntExtra("width", 1280); int height = intent.getIntExtra("height", 720); int duration = intent.getIntExtra("duration", 15); int orientation = intent.getIntExtra("orientation", 0); long videoId = System.currentTimeMillis() / 1000; String leftTopOsd = intent.getStringExtra("leftTopOsd"); String rightTopOsd = intent.getStringExtra("rightTopOsd"); String rightBottomOsd = intent.getStringExtra("rightBottomOsd"); String leftBottomOsd = intent.getStringExtra("leftBottomOsd"); if (cameraId == -1) { cameraId = channel - 1; } Intent recordingIntent = MicroPhotoService.makeRecordingIntent(getApplicationContext(), cameraId, videoId, duration, width, height, quality, orientation, leftTopOsd, rightTopOsd, rightBottomOsd, leftBottomOsd); mVideoFilePath = path; mAutoClose = false; recordingIntent.putExtra("ActivityResult", true); startActivityForResult(recordingIntent, REQUEST_CODE_RECORDING); } } if (mAutoClose) { final Activity activity = this; mHandler.postDelayed(new Runnable() { @Override public void run() { activity.finish(); } }, 2000); } } @Override protected void onDestroy() { super.onDestroy(); if (m3V3TurnedOn) { MicroPhotoService.setCam3V3Enable(false); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CODE_RECORDING) { if (mVideoFilePath != null && data != null) { String path = data.getStringExtra("path"); if (path != null) { File file = new File(path); file.renameTo(new File(mVideoFilePath)); } } this.finish(); } } }