diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 22b221ac..40112dff 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -81,6 +81,9 @@ android:supportsRtl="true" android:theme="@style/Theme.MicroPhoto" tools:targetApi="28"> + - GetStringUTFChars(path, 0); const char *md5Str = env->GetStringUTFChars(md5, 0); + bool res = false; std::vector data; if (readFile(pathStr, data) && !data.empty()) { - nrsec.SM2InportPublicKey(1, &data[0]); + res = nrsec.SM2ImportPublicKey(1, &data[0]); } + nrsec.Close(); + env->ReleaseStringUTFChars(path, pathStr); env->ReleaseStringUTFChars(md5, md5Str); + + return res ? JNI_TRUE : JNI_FALSE; +#endif +} + +extern "C" JNIEXPORT jboolean JNICALL +Java_com_xypower_mpapp_MicroPhotoService_importPublicKey( + JNIEnv* env, jclass cls, jint index, jbyteArray cert) { + +#ifdef USING_NRSEC + + NrsecPort nrsec; + // const char *port = "/dev/mtkgpioctrl"; + const char *port = "/dev/spidevSE"; + int byteCertLen = env->GetArrayLength(cert); + if (byteCertLen <= 0) + { + return JNI_FALSE; + } + + if (!nrsec.Open(port)) + { + return JNI_FALSE; + } + + jbyte* byteCert = env->GetByteArrayElements(cert, 0); + + bool res = nrsec.SM2ImportPublicKey(index, (const uint8_t*)byteCert); + + nrsec.Close(); + + env->ReleaseByteArrayElements(cert, byteCert, JNI_ABORT); + + return res ? JNI_TRUE : JNI_FALSE; +#endif +} + +extern "C" JNIEXPORT jboolean JNICALL +Java_com_xypower_mpapp_MicroPhotoService_genKeys( + JNIEnv* env, + jclass cls, jint index) { + +#ifdef USING_NRSEC + + GpioControl::setRS485Enable(true); + + // const char *port = "/dev/mtkgpioctrl"; + const char *port = "/dev/spidevSE"; + + NrsecPort nrsec; + if (!nrsec.Open(port)) + { + return JNI_FALSE; + } + + // std::string version = nrsec.Version(); + + bool res = nrsec.SM2keypair(index) == 0; + + nrsec.Close(); + + return res ? JNI_TRUE : JNI_FALSE; #endif } \ No newline at end of file diff --git a/app/src/main/java/com/xypower/mpapp/CertActivity.java b/app/src/main/java/com/xypower/mpapp/CertActivity.java new file mode 100644 index 00000000..4fb29c26 --- /dev/null +++ b/app/src/main/java/com/xypower/mpapp/CertActivity.java @@ -0,0 +1,65 @@ +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 java.io.File; +import java.nio.charset.StandardCharsets; + +public class CertActivity extends AppCompatActivity { + + private final static String ACTION_IMP_PUBKEY = "imp_pubkey"; + private final static String ACTION_GEN_KEYS = "gen_keys"; + + private Handler mHandler = 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); + MicroPhotoService.genKeys(index); + } + } + + final Activity activity = this; + mHandler.postDelayed(new Runnable() { + @Override + public void run() { + activity.finish(); + } + }, 2000); + } +} \ No newline at end of file diff --git a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java index 87bcf303..efe9ea2d 100644 --- a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java +++ b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java @@ -146,6 +146,14 @@ public class MicroPhotoService extends Service { public static boolean isRunning = false; + private Runnable delayedSleep = new Runnable() { + @Override + public void run() { + Log.i(TAG, "Device Sleep"); + // SysApi.sleep(getApplicationContext()); + } + }; + public MicroPhotoService() { } @Override @@ -725,6 +733,12 @@ public class MicroPhotoService extends Service { synchronized (mWakeLocks) { wl2 = mWakeLocks.get(name); mWakeLocks.put(name, wl); + + } + try { + mHander.removeCallbacks(delayedSleep); + } catch (Exception ex) { + // ex.printStackTrace(); } if (wl2 != null) { Log.i(TAG, "Release same name wakelock:" + name); @@ -742,6 +756,10 @@ public class MicroPhotoService extends Service { try { wl = mWakeLocks.get(name); mWakeLocks.remove(name); + + if (mWakeLocks.isEmpty()) { + mHander.postDelayed(delayedSleep, 2000); + } } catch (Exception ex) { ex.printStackTrace(); } @@ -866,7 +884,7 @@ public class MicroPhotoService extends Service { // Set Listener } try { - mLocationManager.requestLocationUpdates(mLocateType, 100, 1, mLocationListener); + mLocationManager.requestLocationUpdates(mLocateType, 30000, 1, mLocationListener); } catch (Exception ex) { ex.printStackTrace(); } @@ -1074,7 +1092,9 @@ cellSignalStrengthGsm.getDbm(); public static native void setOtgState(boolean enabled); public static native void setCam3V3Enable(boolean enabled); public static native String getSerialNumber(); - public static native void importPublicKey(String path, String md5); + public static native boolean importPublicKeyFile(int index, String path, String md5); + public static native boolean importPublicKey(int index, byte cert[]); + public static native boolean genKeys(int index); ////////////////////////GPS//////////////////// // private static final String GPS_LOCATION_NAME = android.location.LocationManager.GPS_PROVIDER; diff --git a/app/src/main/res/layout/activity_cert.xml b/app/src/main/res/layout/activity_cert.xml new file mode 100644 index 00000000..52f1a7d9 --- /dev/null +++ b/app/src/main/res/layout/activity_cert.xml @@ -0,0 +1,9 @@ + + + + \ No newline at end of file