diff --git a/.gitignore b/.gitignore index 2665949..46e2387 100644 --- a/.gitignore +++ b/.gitignore @@ -14,5 +14,4 @@ .cxx local.properties deploymentTargetDropDown.xml -env.properties app/debug diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 94a25f7..35eb1dd 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index bc2e674..7417869 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -5,11 +5,11 @@ plugins { android { namespace 'com.logitech.vc.kirbytest' - compileSdk 32 + compileSdk 34 defaultConfig { applicationId "com.logitech.vc.kirbytest" - minSdk 29 + minSdk 27 targetSdk 32 versionCode 1 versionName "1.0" @@ -31,6 +31,29 @@ android { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } + + applicationVariants.all{ + variant -> + variant.outputs.each{ + output-> + // on below line we are specifying our app name. + project.ext { appName = 'kirbyTestApp' } + // on below line we are adding the formatted date to our apk file name. + def formattedDate = new Date().format('yyyyMMdd') + // on below line we are creating a new name for our apk. + def newName = output.outputFile.name + // on below line we are replacing our previous name with our app name. + newName = newName.replace("app-", "$project.ext.appName-") + // 當build type為 debug時觸發 + // on below line we are replacing -debug with our formatted date. + newName = newName.replace("-debug", "-debug-" + formattedDate) + // 當build type為 release時觸發 + // on below line we are replacing -release with our formatted date. + newName = newName.replace("-release", "-release-" + formattedDate) + // at last we are setting our apk name to it. + output.outputFileName = newName + } + } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 @@ -53,6 +76,7 @@ dependencies { implementation 'androidx.constraintlayout:constraintlayout:2.1.3' implementation 'androidx.navigation:navigation-fragment-ktx:2.5.2' implementation 'androidx.navigation:navigation-ui-ktx:2.5.2' + implementation 'androidx.activity:activity:1.8.0' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c5334d2..a6b75e6 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,8 +1,6 @@ - - - - - + - @@ -35,10 +29,15 @@ android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" + android:networkSecurityConfig="@xml/network_security_config" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.SensorTestingApp" + android:usesCleartextTraffic="true" tools:targetApi="31"> + - + \ No newline at end of file diff --git a/app/src/main/java/com/logitech/vc/kirbytest/DeviceListAdapter.kt b/app/src/main/java/com/logitech/vc/kirbytest/DeviceListAdapter.kt index 9f64146..e087323 100644 --- a/app/src/main/java/com/logitech/vc/kirbytest/DeviceListAdapter.kt +++ b/app/src/main/java/com/logitech/vc/kirbytest/DeviceListAdapter.kt @@ -114,7 +114,7 @@ class DeviceListAdapter( } } val inflater = popup.menuInflater - popup.setForceShowIcon(true) +// popup.setForceShowIcon(true) inflater.inflate(R.menu.device_menu, popup.menu) popup.show() } diff --git a/app/src/main/java/com/logitech/vc/kirbytest/KirbyDevice.kt b/app/src/main/java/com/logitech/vc/kirbytest/KirbyDevice.kt index f612a93..26a3a71 100644 --- a/app/src/main/java/com/logitech/vc/kirbytest/KirbyDevice.kt +++ b/app/src/main/java/com/logitech/vc/kirbytest/KirbyDevice.kt @@ -173,6 +173,7 @@ class KirbyDevice( Log.i("BleListener", "Char received: $payload") val base64Payload = Base64.getEncoder().encodeToString(characteristic.value) publishMeasurement(base64Payload) +// publishMeasurementAutoServer(measurement) loggerDb.writeLog(measurement) } @@ -216,6 +217,50 @@ class KirbyDevice( queue.add(request) } + private fun publishMeasurementAutoServer(measurement: DecoderIaq.Measurement) { + // Read url from SharedPreferences + val sharedPref = context.getSharedPreferences(context.getString(R.string.app_name), Context.MODE_PRIVATE) + val url = sharedPref.getString( + "kirby_data_post_url", + context.getString(R.string.kirby_data_post_url_default)) ?: context.getString(R.string.kirby_data_post_url_default) + val accessKey = BuildConfig.API_KEY + + if(url.isEmpty()) { + return + } + + val eui = "0000${bleDevice.address.replace(":", "")}" + + val postData = JSONObject() + + try { +// Log.i("POST", "Transmitting for $eui: $payload") + postData.put("accessKey", "${accessKey}_fromAndroid") + postData.put("eui", eui) + postData.put("deviceId", measurement.deviceId) + postData.put("msgType", measurement.msgType) + postData.put("co2", measurement.co2) + postData.put("voc", measurement.voc) + postData.put("humidity", measurement.humidity) + postData.put("temperature", measurement.temperature) + postData.put("pressure", measurement.pressure) + postData.put("occupancy", measurement.occupancy) + postData.put("pm25", measurement.pm25) + postData.put("pm10", measurement.pm10) + } catch (e: JSONException) { + e.printStackTrace() + } + + val request = JsonObjectRequest( + Request.Method.POST, url, postData, + { response -> + Log.i("sendDataResponse", "Response is: $response") + } + ) { error -> error.printStackTrace() } + + queue.add(request) + } + private val measurements = ArrayList() private val maxMeasurements = 20 diff --git a/app/src/main/java/com/logitech/vc/kirbytest/MainActivity.kt b/app/src/main/java/com/logitech/vc/kirbytest/MainActivity.kt index e025d8b..ad5f063 100644 --- a/app/src/main/java/com/logitech/vc/kirbytest/MainActivity.kt +++ b/app/src/main/java/com/logitech/vc/kirbytest/MainActivity.kt @@ -330,6 +330,13 @@ class MainActivity : AppCompatActivity() { return true } + R.id.action_server_setting -> { + // Goto server setting intent + val intent = Intent(this, ServerSettingActivity::class.java) + startActivity(intent) + return true + } + else -> super.onOptionsItemSelected(item) } } diff --git a/app/src/main/java/com/logitech/vc/kirbytest/ServerSettingActivity.kt b/app/src/main/java/com/logitech/vc/kirbytest/ServerSettingActivity.kt new file mode 100644 index 0000000..519eb8f --- /dev/null +++ b/app/src/main/java/com/logitech/vc/kirbytest/ServerSettingActivity.kt @@ -0,0 +1,49 @@ +package com.logitech.vc.kirbytest + +import android.os.Bundle +import android.util.Log +import android.widget.Button +import android.widget.EditText +import androidx.activity.enableEdgeToEdge +import androidx.appcompat.app.AppCompatActivity +import androidx.core.view.ViewCompat +import androidx.core.view.WindowInsetsCompat + +class ServerSettingActivity : AppCompatActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + enableEdgeToEdge() + setContentView(R.layout.activity_server_setting) + ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets -> + val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()) + v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom) + insets + } + + // Get current server setting from SharedPreferences + val sharedPref = getSharedPreferences(getString(R.string.app_name), MODE_PRIVATE) + var currentUrl = sharedPref.getString( + "kirby_data_post_url", + getString(R.string.kirby_data_post_url_default)) ?: getString(R.string.kirby_data_post_url_default) + val editTextServerSetting = findViewById(R.id.editTextServerSetting) + editTextServerSetting.setText(currentUrl) + + val doneButton = findViewById