· 7 years ago · Oct 27, 2018, 02:38 AM
1package com.ict3103team35.sit.doctotrust.Patients;
2
3import android.app.ProgressDialog;
4import android.content.Intent;
5import android.content.SharedPreferences;
6import android.os.Bundle;
7import android.support.design.widget.FloatingActionButton;
8import android.support.v7.app.AppCompatActivity;
9import android.support.v7.widget.RecyclerView;
10import android.util.Log;
11import android.view.View;
12import android.widget.TextView;
13
14import com.ict3103team35.sit.doctotrust.CryptoClass.CryptoActivity;
15import com.ict3103team35.sit.doctotrust.Doctor.Doctor_MedicalHistoryUpdate;
16import com.ict3103team35.sit.doctotrust.Models.DefaultCreateResponse;
17import com.ict3103team35.sit.doctotrust.Models.DefaultGetResponse;
18import com.ict3103team35.sit.doctotrust.Models.MedicalHistory;
19import com.ict3103team35.sit.doctotrust.Models.User;
20import com.ict3103team35.sit.doctotrust.R;
21import com.ict3103team35.sit.doctotrust.Services.RetrofitClient;
22
23import java.util.ArrayList;
24import java.util.Map;
25
26import javax.crypto.SecretKey;
27import javax.crypto.spec.IvParameterSpec;
28
29import retrofit2.Call;
30import retrofit2.Callback;
31import retrofit2.Response;
32
33/*
34A viewer to open and read medical reports
35 */
36public class Patient_MedicalHistoryViewerActivity extends AppCompatActivity {
37
38 private String USER_NRIC_FLAG = "";//user flag to make activity unique to each user NRIC
39 private String USER_ROLE_FLAG = "";//user flag to make activity unique to each user role
40 private String USER_NRIC_INTENT = "";//user bundle to get user nric from precedent activity
41
42 private String TAG = "Class-Patient_MedicalHistoryViewerActivity";
43
44 private TextView tvALLERGIES;
45 private TextView tvMEDICATION;
46 private TextView tvDIAGNOSIS;
47 private TextView tvSURGERIES;
48 FloatingActionButton floatingBtn;
49
50 private User user = new User();
51
52 private CryptoActivity encAnddec = new CryptoActivity();
53 private String Encrypted_PATIENT_AES_KEY;
54 private static final String AES_ALIAS = "PASSWORD_KEY";
55 private SharedPreferences.Editor editor;
56 private SharedPreferences preferences;
57 private static final String mypreference = "StoringofIV";
58 private String MyMedIVValue = "MyMedIVValue";
59 private String MyMedicalkey = "Patient";
60 private String MyPassIVValue = "MyPassIVValue";
61 private IvParameterSpec SharepreferenceIV;
62 private IvParameterSpec OneTimeIV;
63 SecretKey OneTimeKey;
64
65 @Override
66 protected void onCreate(Bundle savedInstanceState) {
67 super.onCreate(savedInstanceState);
68 setContentView(R.layout.activity_patient_medicalhistoryviewer);
69
70 USER_NRIC_FLAG = "S9111111Z";//get user nric flag session
71 Log.d(TAG, "USER_NRIC_FLAG : " + USER_NRIC_FLAG);
72
73 USER_ROLE_FLAG = user.getUserType(USER_NRIC_FLAG);//get user type flag
74 Log.d(TAG, "USER_ROLE_FLAG : " + USER_ROLE_FLAG);
75
76 USER_NRIC_INTENT = getIntent().getStringExtra("user_nric_intent");//get user nric from intent
77 Log.d(TAG, "user_nric_intent : " + USER_NRIC_INTENT);
78
79 tvALLERGIES = (TextView) findViewById(R.id.tvHistory_allergies);
80 tvMEDICATION = (TextView) findViewById(R.id.tvHistory_medication);
81 tvDIAGNOSIS = (TextView) findViewById(R.id.tvHistory_diagnosis);
82 tvSURGERIES = (TextView) findViewById(R.id.tvHistory_surgery);
83
84 floatingBtn = (FloatingActionButton) findViewById(R.id.floatingActionButton);
85
86 OneTimeIV = encAnddec.MedIVs(16);
87 OneTimeKey= encAnddec.generateAESKey(256);
88
89
90 if (USER_ROLE_FLAG.equals("patient")) {
91 getMedicalHistory(USER_NRIC_FLAG);//If patient log in, use their nric to retrieve db, else if doctor, get intent from patient profile list
92 floatingBtn.setVisibility(View.INVISIBLE);//hide transfer button if user is patient, otherwise show for doc to edit medical history
93 } else if (USER_ROLE_FLAG.equals("doctor")) {
94 getMedicalHistory(USER_NRIC_INTENT);
95 floatingBtn.setVisibility(View.VISIBLE);
96 }
97
98 floatingBtn.setOnClickListener(new View.OnClickListener() {//Button to let doc edit medical history
99 @Override
100 public void onClick(View v) {
101 Intent i = new Intent(getApplicationContext(), Doctor_MedicalHistoryUpdate.class);
102 i.putExtra("user_nric_intent",USER_NRIC_INTENT );
103 startActivity(i);
104 }
105 });
106
107 }
108
109 /**
110 * Read medical history by nric
111 * @param nric
112 */
113 public void getMedicalHistory(String nric){
114 Log.d(TAG, "getMedicalHistory");
115
116 try{
117
118 RetrofitClient.getInstance().getApi().getMedicalHistory(nric).enqueue(
119 new Callback<DefaultGetResponse>() {
120 @Override
121 public void onResponse(Call<DefaultGetResponse> call, Response<DefaultGetResponse> response) {
122 if(response!=null){
123 //Successfully insert
124 DefaultGetResponse dgr = response.body();
125 MedicalHistory medicalHistory = dgr.getMedicalHistory();
126 //Log.d(TAG, medicalHistory.toString());
127
128 String allergic = medicalHistory.getAllergic();
129 String currenttaking = medicalHistory.getCurrenttaking();
130 String medicalprob = medicalHistory.getMedicalproblem();
131 String surgeries = medicalHistory.getSurgeries();
132
133 String decAllergy;
134 String decCurrentTaking;
135 String decMedicalProb;
136 String decSurgeries;
137
138
139 Log.d(TAG, "asdasdasd");
140
141// decAllergy = encAnddec.decryptMedicalHistory(allergic, OneTimeKey, OneTimeIV);
142// decCurrentTaking= encAnddec.decryptMedicalHistory(currenttaking, OneTimeKey, OneTimeIV);
143// decMedicalProb = encAnddec.decryptMedicalHistory( medicalprob, OneTimeKey, OneTimeIV);
144// decSurgeries = encAnddec.decryptMedicalHistory( surgeries, OneTimeKey, OneTimeIV);
145// Log.d(TAG, decAllergy);
146//
147// Log.d(TAG, "DECRYPT : >>>>>>>"+ decSurgeries);
148//
149//
150//
151// //set viewa
152// tvALLERGIES.setText(decAllergy);
153// tvMEDICATION.setText(decCurrentTaking);
154// tvDIAGNOSIS.setText(decMedicalProb);
155// tvSURGERIES.setText(decSurgeries);
156
157
158 }
159 else{
160 //User Query Failure
161 String s = response.errorBody().toString();
162 Log.i(TAG,"Message:"+"getMedicalHistory Query Failure");
163 }
164 }
165 @Override
166 public void onFailure(Call<DefaultGetResponse> call, Throwable t) {
167 Log.i(TAG,"on Failure");
168 Log.i(TAG,t.getMessage());
169 }
170 });
171
172 }catch (Exception e){
173 e.printStackTrace();
174 }
175
176
177 }
178
179
180}