diff --git a/app/src/main/java/com/example/hpostesting/presentation/deviceprovision/DeviceProvisionFragment.kt b/app/src/main/java/com/example/hpostesting/presentation/deviceprovision/DeviceProvisionFragment.kt index 8100091..cd70ad2 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/deviceprovision/DeviceProvisionFragment.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/deviceprovision/DeviceProvisionFragment.kt @@ -4,6 +4,8 @@ import android.content.Context import android.content.Intent import android.content.SharedPreferences import android.os.Bundle +import android.os.Environment +import android.provider.Settings import android.util.Log import android.view.LayoutInflater import android.view.View @@ -17,11 +19,15 @@ import com.example.hpostesting.data.constant.Constants import com.example.hpostesting.data.constant.HemoCubeCommands import com.example.hpostesting.data.model.deviceprovision.DeviceProvisionRequest import com.example.hpostesting.data.model.patient.DeviceData +import com.example.hpostesting.encryption.Encryption import com.example.hpostesting.presentation.UsbServiceListener import com.example.hpostesting.presentation.dashboard.DashboardActivity +import com.google.android.gms.ads.identifier.AdvertisingIdClient import com.google.firebase.crashlytics.ktx.crashlytics import com.google.firebase.ktx.Firebase import `in`.sminnovations.hpostesting.databinding.FragmentDeviceProvisionBinding +import java.io.File +import java.io.FileOutputStream class DeviceProvisionFragment : Fragment() { private var resultData: String = "" @@ -122,6 +128,10 @@ class DeviceProvisionFragment : Fragment() { deviceUpdateAvailable = false ) ) + encryptAndSaveToFile( + response.data.data?.credentials?.username.toString(), + response.data.data?.credentials?.password.toString() + ) // viewModel.addDeviceId(DeviceData(deviceId = sharedPreferences.getString(Constants.DEVICE_ID, "").toString())) Log.e("idpass", response.toString()) Log.e("idpass", response.data.data?.credentials?.username.toString()) @@ -222,8 +232,26 @@ class DeviceProvisionFragment : Fragment() { } } - private fun encryptAndSaveToFile(){ + private fun encryptAndSaveToFile(username: String, password: String) { + val messageToEncrypt = "deviceID\n$username\n$password" + val encryptionKey = + Settings.Secure.getString(context?.contentResolver, Settings.Secure.ANDROID_ID) + val encryptedString = Encryption.encrypt(messageToEncrypt, encryptionKey) + Log.d("DEVICE ID/encryptionKey", encryptionKey) + val target = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + val file = File(target, "credentials.txt") + + if (!file.exists()) { + file.createNewFile() + } + + val fos = FileOutputStream(file) + + file.writeText(encryptedString) + + Log.d("Encrypted Message", encryptedString) + fos.close() } +} -} \ No newline at end of file