|
@@ -0,0 +1,326 @@
|
|
|
+package com.wdkl.app.ncs.callingbed.ecg;
|
|
|
+
|
|
|
+import android.annotation.SuppressLint;
|
|
|
+import android.content.Context;
|
|
|
+import android.content.Intent;
|
|
|
+import android.net.Uri;
|
|
|
+import android.os.AsyncTask;
|
|
|
+import android.os.Build;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.os.Environment;
|
|
|
+import android.text.TextUtils;
|
|
|
+import android.util.Log;
|
|
|
+import android.view.Menu;
|
|
|
+import android.view.MenuItem;
|
|
|
+import android.view.View;
|
|
|
+import android.view.Window;
|
|
|
+import android.view.WindowManager;
|
|
|
+import android.widget.Button;
|
|
|
+
|
|
|
+import androidx.annotation.NonNull;
|
|
|
+import androidx.annotation.Nullable;
|
|
|
+import androidx.appcompat.app.AppCompatActivity;
|
|
|
+import androidx.appcompat.widget.Toolbar;
|
|
|
+import androidx.core.content.FileProvider;
|
|
|
+
|
|
|
+import com.borsam.device.BorsamDevice;
|
|
|
+import com.borsam.network_lib.code.response.BorsamHttpResponse;
|
|
|
+import com.borsam.network_lib.manager.UploadManager;
|
|
|
+import com.borsam.network_lib.upload.UploadTask;
|
|
|
+import com.borsam.network_lib.upload.callback.UploadRecordCallback;
|
|
|
+import com.borsam.pdf.ECGPdfCreator;
|
|
|
+import com.borsam.util.MD5Utils;
|
|
|
+import com.github.barteksc.pdfviewer.PDFView;
|
|
|
+import com.github.barteksc.pdfviewer.util.FitPolicy;
|
|
|
+import com.wdkl.app.ncs.callingbed.R;
|
|
|
+
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author Borsam Medical
|
|
|
+ * @version 1.0
|
|
|
+ **/
|
|
|
+public class ECGPdfHrvUploadViewer extends AppCompatActivity {
|
|
|
+ private static final String TAG="ECGPdfHrvUploadViewer";
|
|
|
+
|
|
|
+ private static final String BORSAM_DEVICE = "borsam_device";
|
|
|
+ private static final String DEVICE_CODE_KEY = "device_code_key";
|
|
|
+ private PDFView mPDFView;
|
|
|
+ private BorsamDevice mBorsamDevice;
|
|
|
+ private String mPdfPath;
|
|
|
+ private String mCodeKey;
|
|
|
+ private AsyncTask<Void, Void, Boolean> mTask;
|
|
|
+
|
|
|
+
|
|
|
+ public static void start(Context context, @NonNull BorsamDevice borsamDevice, String codeKey) {
|
|
|
+ Intent starter = new Intent(context, ECGPdfHrvUploadViewer.class);
|
|
|
+ starter.putExtra(BORSAM_DEVICE, borsamDevice);
|
|
|
+ starter.putExtra(DEVICE_CODE_KEY, codeKey);
|
|
|
+ context.startActivity(starter);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
+ super.onCreate(savedInstanceState);
|
|
|
+
|
|
|
+ //全屏显示
|
|
|
+ requestWindowFeature(Window.FEATURE_NO_TITLE);
|
|
|
+ getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
|
|
|
+ WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
|
|
|
+ WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
|
|
|
+ WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
|
|
|
+
|
|
|
+ setContentView(R.layout.activity_pdf);
|
|
|
+ Toolbar toolbar = findViewById(R.id.toolbar);
|
|
|
+ setSupportActionBar(toolbar);
|
|
|
+ Button back = findViewById(R.id.button_back);
|
|
|
+ back.setOnClickListener(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View view) {
|
|
|
+ finish();
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ mPDFView = findViewById(R.id.pdfView);
|
|
|
+ mBorsamDevice = getIntent().getParcelableExtra(BORSAM_DEVICE);
|
|
|
+ mCodeKey = getIntent().getStringExtra(DEVICE_CODE_KEY);
|
|
|
+ createPdf();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onWindowFocusChanged(boolean hasFocus) {
|
|
|
+ super.onWindowFocusChanged(hasFocus);
|
|
|
+ getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
|
|
|
+ | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
|
|
|
+ | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
|
|
|
+ | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
|
|
|
+ | View.SYSTEM_UI_FLAG_FULLSCREEN);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * createPdf
|
|
|
+ */
|
|
|
+ @SuppressLint("StaticFieldLeak")
|
|
|
+ private void createPdf() {
|
|
|
+ mTask = new AsyncTask<Void, Void, Boolean>() {
|
|
|
+ @Override
|
|
|
+ protected Boolean doInBackground(Void... voids) {
|
|
|
+ File pdfParentFile = getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
|
|
|
+ File pdfFile = new File(pdfParentFile, "test.pdf");
|
|
|
+ mPdfPath = pdfFile.getPath();
|
|
|
+ ECGPdfCreator ecgPdfCreator = new ECGPdfCreator(ECGPdfHrvUploadViewer.this, mPdfPath);
|
|
|
+ //BorsamDevice#getRecordData if you want to upload record data,
|
|
|
+ //use this method to save data as file and upload it
|
|
|
+ //set pdf config params
|
|
|
+ ecgPdfCreator.setConfig(createPdfConfig());
|
|
|
+ byte[] datas = null;
|
|
|
+ try {
|
|
|
+ datas = mBorsamDevice.getRecordData();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ ecgPdfCreator.setData(datas, mBorsamDevice.getSampling(),
|
|
|
+ mBorsamDevice.getADUnit(), mBorsamDevice.getPassageNumbers(),
|
|
|
+ mBorsamDevice.isReverse());
|
|
|
+ return ecgPdfCreator.createPdf();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onPostExecute(Boolean result) {
|
|
|
+ if (result != null && result) {
|
|
|
+ mPDFView.fromFile(new File(mPdfPath))
|
|
|
+ .pageFitPolicy(FitPolicy.BOTH)
|
|
|
+ .load();
|
|
|
+ //load local pdf and upload
|
|
|
+ prepareUpload();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ mTask.execute();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * set pdf config
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private ECGPdfCreator.Config createPdfConfig(){
|
|
|
+ //TODO set config
|
|
|
+ ECGPdfCreator.Config pdfConfig = new ECGPdfCreator.Config();
|
|
|
+ /** method.1 */
|
|
|
+ //You can hide param if you don't need it
|
|
|
+// pdfConfig.hideParams(PdfParamsType.PARAMS_AGE,PdfParamsType.PARAMS_ADDRESS);
|
|
|
+ //hide all params and subTitle
|
|
|
+ pdfConfig.hideAll();
|
|
|
+
|
|
|
+ /** method.2 */
|
|
|
+ // setCustomItem(true) will draw customize text
|
|
|
+ // item is customize text ,column range 0-1
|
|
|
+// pdfConfig.setCustomItem(true).addItem("cusName:borsam demo-1",0,0)
|
|
|
+// .addItem("cusAges:100",1,0)
|
|
|
+// .addItem("cusId:00001",2,0)
|
|
|
+// .addItem("cusPhone:12345678901",2,1);
|
|
|
+ return pdfConfig;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Prepare to upload task
|
|
|
+ */
|
|
|
+ private void prepareUpload(){
|
|
|
+ if(TextUtils.isEmpty(mCodeKey)){
|
|
|
+ Log.e(TAG,"not exists code key can't upload");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ byte[] ecgDatas = mBorsamDevice.getRecordData();
|
|
|
+ //
|
|
|
+ File saveFile = getSaveFile();
|
|
|
+ boolean success = BorsamUtils.saveToFile(ecgDatas,saveFile);
|
|
|
+ if(success){
|
|
|
+ //Important parameters for upload task
|
|
|
+ String deviceMac = mBorsamDevice.getAddress();
|
|
|
+ String deviceName = mBorsamDevice.getDeviceName();
|
|
|
+ int deviceType = BorsamUtils.getDeviceType(deviceName);
|
|
|
+ upload(saveFile,deviceMac,deviceType);
|
|
|
+ }else{
|
|
|
+ Log.e(TAG,"save file failed");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * upload
|
|
|
+ * @param saveFile
|
|
|
+ * @param deviceMac
|
|
|
+ * @param deviceType
|
|
|
+ */
|
|
|
+ private void upload(File saveFile, String deviceMac, int deviceType){
|
|
|
+ //upload task
|
|
|
+ UploadTask uploadTask = new UploadTask();
|
|
|
+ //code Key from BorsamDevice, This mCodeKey is very important
|
|
|
+ uploadTask.setKey(mCodeKey);
|
|
|
+ uploadTask.setFilePath(saveFile.getPath());
|
|
|
+ //BorsamDevice type, One device corresponds to one type, {@link BorsamUtils.getDeviceType}
|
|
|
+ uploadTask.setDeviceType(deviceType);
|
|
|
+ uploadTask.setDeviceMac(deviceMac);
|
|
|
+ UploadManager.getInstance().upload(uploadTask, new UploadRecordCallback() {
|
|
|
+ @Override
|
|
|
+ public void onUploadStart(@NonNull UploadTask uploadTask) {
|
|
|
+ Log.d(TAG,"--------------onUploadStart---------");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onUploadProgress(@NonNull UploadTask uploadTask, int progress) {
|
|
|
+ Log.d(TAG,"--------------onUploadStart---------"+progress);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onUploadSuccess(@NonNull UploadTask uploadTask) {
|
|
|
+ Log.d(TAG,"--------------onUploadSuccess---------");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onUploadFailure(@NonNull UploadTask uploadTask) {
|
|
|
+ Log.d(TAG,"--------------onUploadFailure---------");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onError(@NonNull UploadTask uploadTask, Throwable throwable) {
|
|
|
+ Log.d(TAG,"--------------onError---------");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onUploadTaskCancel(@NonNull UploadTask uploadTask) {
|
|
|
+ Log.d(TAG,"--------------onUploadTaskCancel---------");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onAddRecordSuccess(@NonNull UploadTask uploadTask, BorsamHttpResponse borsamHttpResponse) {
|
|
|
+ Log.d(TAG,"--------------onAddRecordSuccess---------"+borsamHttpResponse);
|
|
|
+ //file_report is remote pdf
|
|
|
+ //ext is analysis data
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * create ECG data files to be uploaded
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private File getSaveFile(){
|
|
|
+ File file = new File(getExternalFilesDir(null).getAbsolutePath() + "/ecg_data/");
|
|
|
+ file.mkdirs();
|
|
|
+ return new File(file.getPath(), MD5Utils
|
|
|
+ .MD5(String.valueOf(System.currentTimeMillis())) + ".data");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onDestroy() {
|
|
|
+ super.onDestroy();
|
|
|
+ if (mTask != null) {
|
|
|
+ mTask.cancel(true);
|
|
|
+ }
|
|
|
+ //IMPORTANT BorsamDevice#close will clear all datas and listeners
|
|
|
+ mBorsamDevice.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * file to bytes
|
|
|
+ */
|
|
|
+ public static byte[] fileToByteArray(String filePath) throws IOException {
|
|
|
+ FileInputStream fis = new FileInputStream(filePath);
|
|
|
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
|
+ byte[] buffer = new byte[1024 * 4];
|
|
|
+ int n;
|
|
|
+ while ((n = fis.read(buffer)) != -1) {
|
|
|
+ out.write(buffer, 0, n);
|
|
|
+ }
|
|
|
+ fis.close();
|
|
|
+ return out.toByteArray();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Share pdf
|
|
|
+ */
|
|
|
+ public void share(String pdfPath){
|
|
|
+ File pdfFile = new File(pdfPath);
|
|
|
+ Uri uri = null;
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
|
+ uri = FileProvider.getUriForFile(this,getPackageName()+".myfileprovider",new File(pdfPath));
|
|
|
+ }else{
|
|
|
+ uri = Uri.fromFile(pdfFile);
|
|
|
+ }
|
|
|
+ Intent intent = Intent.createChooser(getSharePdfIntent(uri,"subject","content"),"share");
|
|
|
+ startActivity(intent);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Create Share pdf Intent
|
|
|
+ */
|
|
|
+ public static Intent getSharePdfIntent(Uri pdfUri, CharSequence subject, CharSequence content) {
|
|
|
+ Intent intent = new Intent(Intent.ACTION_SEND);
|
|
|
+ intent.putExtra(Intent.EXTRA_STREAM, pdfUri);
|
|
|
+ intent.putExtra(Intent.EXTRA_SUBJECT, subject);
|
|
|
+ intent.putExtra(Intent.EXTRA_TEXT, content);
|
|
|
+ intent.setType("application/pdf");
|
|
|
+ return intent;
|
|
|
+ }
|
|
|
+
|
|
|
+/* @Override
|
|
|
+ public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
+ getMenuInflater().inflate(R.menu.pdf_menu, menu);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
+ switch (item.getItemId()) {
|
|
|
+ case R.id.share:
|
|
|
+ share(mPdfPath);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return super.onOptionsItemSelected(item);
|
|
|
+ }*/
|
|
|
+}
|