diff --git a/app/src/main/java/com/xypower/secapp/CertActivity.java b/app/src/main/java/com/xypower/secapp/CertActivity.java index c6293bb..033b7ae 100644 --- a/app/src/main/java/com/xypower/secapp/CertActivity.java +++ b/app/src/main/java/com/xypower/secapp/CertActivity.java @@ -2,7 +2,13 @@ package com.xypower.secapp; import android.app.Activity; +import android.content.BroadcastReceiver; +import android.content.ClipData; +import android.content.ClipboardManager; +import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; +import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; import android.support.v7.app.ActionBar; @@ -11,6 +17,8 @@ import android.text.TextUtils; import android.util.Base64; import android.util.Log; import android.view.View; +import android.widget.EditText; +import android.widget.Spinner; import android.widget.TextView; import com.dowse.devicesdk.DsDeviceSdk; @@ -19,33 +27,46 @@ import java.io.File; import java.io.FileOutputStream; import java.nio.file.Files; import java.nio.file.Path; -import java.text.NumberFormat; import java.text.SimpleDateFormat; import java.util.Date; -import java.util.Locale; public class CertActivity extends AppCompatActivity { private static String TAG = "CERTTAG"; - private final static String ACTION_IMP_PUBKEY = "imp_pubkey"; - private final static String ACTION_EXP_PUBKEY = "exp_pubkey"; - private final static String ACTION_EXP_PRIKEY = "exp_prikey"; - private final static String ACTION_GEN_KEYS = "gen_keys"; - private final static String ACTION_CERT_REQ = "cert_req"; - - private final static String ACTION_CERT_REQ_BATCH = "batch_cert_req"; + private final static String ACTION_IMP_PUBKEY = "com.xypower.secapp.imp_pubkey"; + private final static String ACTION_EXP_PUBKEY = "com.xypower.secapp.exp_pubkey"; + private final static String ACTION_EXP_PRIKEY = "com.xypower.secapp.exp_prikey"; + private final static String ACTION_GEN_KEYS = "com.xypower.secapp.gen_keys"; + private final static String ACTION_CERT_REQ = "com.xypower.secapp.cert_req"; + private final static String ACTION_CERT_REQ_BATCH = "com.xypower.secapp.batch_cert_req"; + private final static String ACTION_EXIT_APP = "com.xypower.secapp.exit"; private static long AUTO_CLOSE_TIMEOUT = 200; private Handler mHandler = null; TextView mTextView = null; + private CommandReceiver mCommandReceiver; + private Thread mCommandThread; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_cert); int res = 0; + boolean bres = false; + + mCommandReceiver = new CommandReceiver(); + IntentFilter intentFilter = new IntentFilter(ACTION_CERT_REQ); + intentFilter.addAction(ACTION_EXIT_APP); + intentFilter.addAction(ACTION_IMP_PUBKEY); + intentFilter.addAction(ACTION_EXP_PUBKEY); + intentFilter.addAction(ACTION_EXP_PRIKEY); + intentFilter.addAction(ACTION_GEN_KEYS); + intentFilter.addAction(ACTION_CERT_REQ_BATCH); + + getApplicationContext().registerReceiver(mCommandReceiver, intentFilter, /* Context.RECEIVER_EXPORTED | */Context.RECEIVER_VISIBLE_TO_INSTANT_APPS); DsDeviceSdk dsSdk = DsDeviceSdk.getInstance(); dsSdk.init(); @@ -53,7 +74,12 @@ public class CertActivity extends AppCompatActivity { res = dsSdk.mcuInit(); res = dsSdk.mcuOpen(); - boolean bres = dsSdk.mcuPowerOffCPR(); + bres = dsSdk.mcuPowerOffCPR(); + try { + Thread.sleep(1000); + } catch (Exception ex) { + ex.printStackTrace(); + } bres = dsSdk.mcuPowerOnCPR(); // String v = dsSdk.nrsecGetVersion(); @@ -77,17 +103,39 @@ public class CertActivity extends AppCompatActivity { mHandler = new Handler(); + Spinner spinnerView = (Spinner) findViewById(R.id.format); + spinnerView.setSelection(1); + + SharedPreferences sharedPreferences = getSharedPreferences("CertReq", MODE_PRIVATE); + + setEditText(R.id.batch_no, sharedPreferences.getString("batchNo", "")); + setEditText(R.id.serial_no, sharedPreferences.getString("serialNo", "")); + setEditText(R.id.uid_start, sharedPreferences.getString("uidStart", "")); + setEditText(R.id.uid_end, sharedPreferences.getString("uidEnd", "")); + setSpinnerPosition(R.id.company, sharedPreferences.getInt("company", 0)); + { final Intent intent = getIntent(); if (intent != null) { - Thread th = new Thread(new Runnable() { + if (mCommandThread != null) { + try { + mCommandThread.interrupt(); + } catch (Exception ex) { + + } + } + mCommandThread = new Thread(new Runnable() { @Override public void run() { - handleCommand(intent); + try { + handleCommand(intent); + } catch (Exception ex) { + ex.printStackTrace(); + } } }); - th.start(); + mCommandThread.start(); } } @@ -129,20 +177,55 @@ public class CertActivity extends AppCompatActivity { } }); + findViewById(R.id.output_dir).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + TextView textView = (TextView)view; + ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); + ClipData clipData = ClipData.newPlainText("label", textView.getText().toString()); + clipboardManager.setPrimaryClip(clipData); + } + }); + findViewById(R.id.btnGenCertReq).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { + + int keyPairIndex = getItemData(R.id.keypair_index); + int uidStart = getIntegerData(R.id.uid_start); + int uidEnd = getIntegerData(R.id.uid_end); + + int type = getItemData(R.id.format); + String sn = getStringData(R.id.serial_no); + String bn = getStringData(R.id.batch_no); + String company = getItemStringData(R.id.company); + + String keyPrefix = getStringData(R.id.key_prefix); + if (keyPrefix == null) { + keyPrefix = ""; + } + + Spinner spinnerView = (Spinner) findViewById(R.id.company); + SharedPreferences sharedPreferences = getSharedPreferences("CertReq", MODE_PRIVATE); + SharedPreferences.Editor editor = sharedPreferences.edit(); + editor.putString("batchNo", bn); + editor.putString("serialNo", sn); + editor.putString("uidStart", Integer.toString(uidStart)); + editor.putString("uidEnd", Integer.toString(uidEnd)); + editor.putInt("company", spinnerView.getSelectedItemPosition()); + editor.commit(); + Intent intent = new Intent(); - intent.putExtra("index", 0); + intent.putExtra("index", keyPairIndex); intent.putExtra("action", ACTION_CERT_REQ_BATCH); - intent.putExtra("start", 701); - intent.putExtra("end", 710); - intent.putExtra("type", 0); - intent.putExtra("sn", "XY2024ICQ111"); - intent.putExtra("subject", "C=CN,ST=SH,L=SH,O=SGCC,CN=123456_%%IDX%%_XY2024ICQ111%%IDX%%,OU=NC"); - - intent.putExtra("pubKeyFile", "device_pub.key"); - intent.putExtra("priKeyFile", "device_pri.key"); + intent.putExtra("start", uidStart); + intent.putExtra("end", uidEnd); + intent.putExtra("type", type); + intent.putExtra("sn", sn); + intent.putExtra("subject", "C=CN,ST=SH,L=SH,O=SGCC,OU=SGEP,OU=" + company + ",CN=" + bn + "_%%IDX%%_" + sn + "%%IDX%%"); + + intent.putExtra("pubKeyFile", keyPrefix + "pub.key"); + intent.putExtra("priKeyFile", keyPrefix + "pri.key"); intent.putExtra("dnFile", "dn.txt"); File file = CertActivity.this.getFilesDir(); @@ -151,12 +234,86 @@ public class CertActivity extends AppCompatActivity { String absPath = file.getAbsolutePath(); intent.putExtra("path", absPath); - handleCommand(intent); + TextView outpathView = (TextView) findViewById(R.id.output_dir); + outpathView.setText(absPath); + + final Intent finalIntent = intent; + if (mCommandThread != null) { + try { + mCommandThread.interrupt(); + } catch (Exception ex) { + + } + } + mCommandThread = new Thread(new Runnable() { + @Override + public void run() { + try { + CertActivity.this.handleCommand(finalIntent); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + }); + + mCommandThread.start(); } }); // } + protected int getItemData(int viewId) { + Spinner spinnerView = (Spinner) findViewById(viewId); + if (spinnerView != null) { + String text = (String)spinnerView.getSelectedItem(); + text = text.substring(0, text.indexOf(" -")); + return Integer.parseInt(text); + } + return -1; + } + + protected String getItemStringData(int viewId) { + Spinner spinnerView = (Spinner) findViewById(viewId); + if (spinnerView != null) { + String text = (String)spinnerView.getSelectedItem(); + text = text.substring(0, text.indexOf(" -")); + return text; + } + return null; + } + + protected void setEditText(int viewId, String text) { + EditText editView = (EditText) findViewById(viewId); + if (editView != null) { + editView.setText(TextUtils.isEmpty(text) ? "" : text); + } + } + + protected void setSpinnerPosition(int viewId, int pos) { + Spinner spinnerView = (Spinner) findViewById(viewId); + if (spinnerView != null) { + spinnerView.setSelection(pos); + } + } + + protected int getIntegerData(int viewId) { + EditText editView = (EditText) findViewById(viewId); + if (editView != null) { + String text = editView.getText().toString(); + return Integer.parseInt(text); + } + return -1; + } + + protected String getStringData(int viewId) { + EditText editView = (EditText) findViewById(viewId); + if (editView != null) { + String text = editView.getText().toString(); + return text; + } + return null; + } + protected static void writeTextFile(String path, String content) { FileOutputStream fileOutputStream = null; try { @@ -340,14 +497,14 @@ public class CertActivity extends AppCompatActivity { f = new File(f, "progress.txt"); writeTextFile(f.getAbsolutePath(), Integer.toString(idx - startIdx + 1)); - final String fullSn = sn + idxStr; + final String finalSn = sn; mHandler.post(new Runnable() { @Override public void run() { SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss"); Date dt = new Date(); - mTextView.setText(format.format(dt) + " " + fullSn + " type=" + Integer.toString(type)); + mTextView.setText(format.format(dt) + " " + finalSn + " type=" + Integer.toString(type)); } }); } @@ -361,16 +518,15 @@ public class CertActivity extends AppCompatActivity { } catch (Exception ex) { } + } else if (TextUtils.equals(ACTION_EXIT_APP, action)) { + final Activity activity = CertActivity.this; + mHandler.post(new Runnable() { + @Override + public void run() { + activity.finish(); + } + }); } - - final Activity activity = this; - mHandler.postDelayed(new Runnable() { - @Override - public void run() { - // activity.finish(); - System.exit(0); - } - }, AUTO_CLOSE_TIMEOUT); } } @@ -382,12 +538,20 @@ public class CertActivity extends AppCompatActivity { file.getParentFile().mkdirs(); } } catch (Exception ex) { - } } @Override protected void onDestroy() { + + if (mCommandThread != null) { + try { + mCommandThread.interrupt(); + } catch (Exception ex) { + + } + } + DsDeviceSdk dsSdk = DsDeviceSdk.getInstance(); int res = 0; @@ -398,6 +562,39 @@ public class CertActivity extends AppCompatActivity { dsSdk.release(); + getApplicationContext().unregisterReceiver(mCommandReceiver); + super.onDestroy(); } + + public class CommandReceiver extends BroadcastReceiver { + + @Override + public void onReceive(Context context, final Intent intent) { + final Intent finalIntent = intent; + + if (intent != null) { + + if (mCommandThread != null) { + try { + mCommandThread.interrupt(); + } catch (Exception ex) { + + } + } + + mCommandThread = new Thread(new Runnable() { + @Override + public void run() { + try { + handleCommand(finalIntent); + } catch (Exception ex) { + } + } + }); + + mCommandThread.start(); + } + } + } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_cert.xml b/app/src/main/res/layout/activity_cert.xml index f0ad08c..3e2636b 100644 --- a/app/src/main/res/layout/activity_cert.xml +++ b/app/src/main/res/layout/activity_cert.xml @@ -4,54 +4,242 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" + android:keepScreenOn="true" tools:context=".CertActivity"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +