通过ContentProvider提供接口

main
Matthew 9 months ago
parent af9c659ea5
commit e5e3f4c57e

@ -51,4 +51,5 @@ dependencies {
androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation files('libs/dsdevicesdk1.3.aar') implementation files('libs/dsdevicesdk1.3.aar')
implementation files('libs/common-release.aar')
} }

Binary file not shown.

@ -11,8 +11,8 @@
"type": "SINGLE", "type": "SINGLE",
"filters": [], "filters": [],
"attributes": [], "attributes": [],
"versionCode": 2, "versionCode": 5,
"versionName": "1.1", "versionName": "1.4",
"outputFile": "app-release.apk" "outputFile": "app-release.apk"
} }
], ],

@ -6,29 +6,32 @@
<application <application
android:allowBackup="true" android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules" android:dataExtractionRules="@xml/data_extraction_rules"
android:extractNativeLibs="true"
android:fullBackupContent="@xml/backup_rules" android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/Theme.SecApp" android:theme="@style/Theme.SecApp"
android:extractNativeLibs="true"
tools:targetApi="28"> tools:targetApi="28">
<provider
android:name=".BridgeProvider"
android:authorities="com.xypower.mpapp.provider"
android:enabled="true"
android:exported="true"></provider>
<activity <activity
android:name=".MainActivity" android:name=".MainActivity"
android:exported="true" > android:exported="true"></activity>
</activity>
<activity <activity
android:name=".CertActivity" android:name=".CertActivity"
android:exported="true"> android:exported="true">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>
</application> </application>
</manifest> </manifest>

@ -384,6 +384,37 @@ Java_com_xypower_secapp_MicroPhotoService_importPublicKey(
#endif #endif
} }
extern "C" JNIEXPORT jboolean JNICALL
Java_com_xypower_secapp_MicroPhotoService_importPrivateKey(
JNIEnv* env, jclass cls, jint index, jbyteArray cert) {
#ifdef USING_NRSEC
int byteCertLen = env->GetArrayLength(cert);
if (byteCertLen <= 0)
{
return JNI_FALSE;
}
NrsecPort nrsec;
const char *path = NRSEC_PATH;
if (!nrsec.Open(path))
{
return JNI_FALSE;
}
jbyte* byteCert = env->GetByteArrayElements(cert, 0);
bool res = nrsec.SM2ImportPrivateKey(index, (const uint8_t*)byteCert) == 0;
nrsec.Close();
env->ReleaseByteArrayElements(cert, byteCert, JNI_ABORT);
return res ? JNI_TRUE : JNI_FALSE;
#endif
}
extern "C" JNIEXPORT jboolean JNICALL extern "C" JNIEXPORT jboolean JNICALL
Java_com_xypower_secapp_MicroPhotoService_genKeys( Java_com_xypower_secapp_MicroPhotoService_genKeys(
JNIEnv* env, JNIEnv* env,

@ -0,0 +1,205 @@
package com.xypower.secapp;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import com.xypower.common.FilesUtils;
import org.json.JSONObject;
import java.io.File;
public class BridgeProvider extends ContentProvider {
private static String TAG = "CERTTAG";
private final static String AUTHORITY = "com.xypower.mpapp.provider";
private final static String PATH_QUERY_SEC_VERSION = "/querySecVersion";
private final static String PATH_IMP_PRI_KEY = "/importPriKey";
private final static String PATH_IMP_PUB_KEY = "/importPubKey";
private final static String PATH_GEN_KEYS = "/genKeys";
private final static String PATH_GEN_CERT_REQ = "/genCertReq";
public BridgeProvider() {
Log.i(TAG, "BridgeProvider");
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// Implement this to handle requests to delete one or more rows.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public String getType(Uri uri) {
// TODO: Implement this to handle requests for the MIME type of the data
// at the given URI.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public Uri insert(Uri uri, ContentValues values) {
return null;
}
@Override
public boolean onCreate() {
// TODO: Implement this to initialize your content provider on startup.
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
Log.i(TAG, uri.toString());
matcher.addURI(AUTHORITY, PATH_QUERY_SEC_VERSION, 1);
Cursor cursor = null;
int matched = matcher.match(uri);
switch (matched) {
case 1:
cursor = querySecVersion();
break;
default:
break;
}
return cursor;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
matcher.addURI(AUTHORITY, PATH_IMP_PRI_KEY, 1);
matcher.addURI(AUTHORITY, PATH_IMP_PUB_KEY, 2);
matcher.addURI(AUTHORITY, PATH_GEN_KEYS, 3);
matcher.addURI(AUTHORITY, PATH_GEN_CERT_REQ, 4);
int res = 0;
int matched = matcher.match(uri);
switch (matched) {
case 1:
res = importPrivateKey(uri, values);
break;
case 2:
res = importPublicKey(uri, values);
break;
case 3:
res = genKeys(uri, values);
break;
case 4:
res = genCertReq(uri, values);
break;
default:
break;
}
return res;
}
private Cursor querySecVersion() {
String version = MicroPhotoService.querySecVersion();
String[] columns = { "version" };
MatrixCursor matrixCursor = new MatrixCursor(columns, 1);
matrixCursor.addRow(new Object[] { version });
return matrixCursor;
}
private int importPrivateKey(Uri uri, ContentValues values) {
String cert = values.containsKey("cert") ? values.getAsString("cert") : null;
String path = values.containsKey("path") ? values.getAsString("path") : null;
String resultFile = values.containsKey("resultFile") ? values.getAsString("resultFile") : null;
int index = values.containsKey("index") ? values.getAsInteger("index").intValue() : 0;
Log.i(TAG, "Start import private key");
boolean res = false;
if (!TextUtils.isEmpty(cert)) {
// Import
byte[] content = Base64.decode(cert, Base64.DEFAULT);
if (content != null) {
res = MicroPhotoService.importPrivateKey(index, content);
}
} else if (TextUtils.isEmpty(path)) {
String md5 = values.containsKey("md5") ? values.getAsString("md5") : null;
File file = new File(path);
if (file.exists() && file.isFile()) {
res = MicroPhotoService.importPrivateKeyFile(index, path, md5);
}
}
Log.i(TAG, "Finish import private key");
if (!TextUtils.isEmpty(resultFile)) {
FilesUtils.ensureParentDirectoryExisted(resultFile);
FilesUtils.writeTextFile(resultFile, res ? "1" : "0");
}
return res ? 1 : 0;
}
private int importPublicKey(Uri uri, ContentValues values) {
String cert = values.containsKey("cert") ? values.getAsString("cert") : null;
String path = values.containsKey("path") ? values.getAsString("path") : null;
String resultFile = values.containsKey("resultFile") ? values.getAsString("resultFile") : null;
int index = values.containsKey("index") ? values.getAsInteger("index").intValue() : 0;
boolean res = false;
if (!TextUtils.isEmpty(cert)) {
// Import
byte[] content = Base64.decode(cert, Base64.DEFAULT);
if (content != null) {
res = MicroPhotoService.importPublicKey(index, content);
}
} else if (TextUtils.isEmpty(path)) {
String md5 = values.containsKey("md5") ? values.getAsString("md5") : null;
File file = new File(path);
if (file.exists() && file.isFile()) {
res = MicroPhotoService.importPublicKeyFile(index, path, md5);
}
}
if (!TextUtils.isEmpty(resultFile)) {
FilesUtils.ensureParentDirectoryExisted(resultFile);
FilesUtils.writeTextFile(resultFile, res ? "1" : "0");
}
return res ? 1 : 0;
}
private int genKeys(Uri uri, ContentValues values) {
int index = values.containsKey("index") ? values.getAsInteger("index").intValue() : 0;
boolean res = MicroPhotoService.genKeys(index);
return res ? 1 : 0;
}
private int genCertReq(Uri uri, ContentValues values) {
int index = values.containsKey("index") ? values.getAsInteger("index").intValue() : 0;
int type = values.containsKey("type") ? values.getAsInteger("type").intValue() : 0;
String subject = values.containsKey("subject") ? values.getAsString("subject") : null;
String path = values.containsKey("path") ? values.getAsString("path") : null;
if (TextUtils.isEmpty(subject) || TextUtils.isEmpty(path)) {
return 0;
}
boolean res = MicroPhotoService.genCertRequest(index, type, subject, path);
return res ? 1 : 0;
}
}

@ -78,6 +78,7 @@ public class MicroPhotoService extends Service {
public static native String getSerialNumber(); public static native String getSerialNumber();
public static native boolean importPublicKeyFile(int index, String outputPath, String md5); public static native boolean importPublicKeyFile(int index, String outputPath, String md5);
public static native boolean importPublicKey(int index, byte cert[]); public static native boolean importPublicKey(int index, byte cert[]);
public static native boolean importPrivateKey(int index, byte cert[]);
public static native boolean genKeys(int index); public static native boolean genKeys(int index);
public static native String querySecVersion(); public static native String querySecVersion();
@ -87,6 +88,8 @@ public class MicroPhotoService extends Service {
public static native boolean exportPrivateFile(int index, String outputPath); public static native boolean exportPrivateFile(int index, String outputPath);
////////////////////////GPS//////////////////// ////////////////////////GPS////////////////////
// private static final String GPS_LOCATION_NAME = android.location.LocationManager.GPS_PROVIDER; // private static final String GPS_LOCATION_NAME = android.location.LocationManager.GPS_PROVIDER;
private LocationManager mLocationManager; private LocationManager mLocationManager;

Loading…
Cancel
Save