xiongweixp пре 2 месеци
родитељ
комит
160f1b3e01

+ 48 - 0
app/src/main/java/tech/xiongweixp/touchaction/MainActivity.kt

@@ -25,6 +25,8 @@ class MainActivity : ComponentActivity() {
     private var mainLayout: ConstraintLayout? = null
     private var computerTextView: TextView? = null
     private var compterCloseBtn: Button? = null
+    private var lightTextView: TextView? = null
+    private var lightCloseBtn: Button? = null
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
@@ -34,11 +36,18 @@ class MainActivity : ComponentActivity() {
         mainLayout = findViewById(R.id.mainLayout)
         computerTextView = findViewById(R.id.computerText)
         compterCloseBtn = findViewById(R.id.computerCloseButton)
+        lightTextView = findViewById(R.id.lightText)
+        lightCloseBtn = findViewById(R.id.lightCloseButton)
 
         compterCloseBtn!!.setOnClickListener {
             //退出APP
             finish()
         }
+
+        lightCloseBtn!!.setOnClickListener {
+            //退出APP
+            finish()
+        }
         enableEdgeToEdge()
         Log.i("MainActivity", "打开APP")
         Log.i("MainActivity", intent.data.toString())
@@ -60,14 +69,52 @@ class MainActivity : ComponentActivity() {
                 val ret = conn.responseCode
                 Log.i("MainActivity", "发送 $webhookID 请求成功,返回码:$ret")
                 conn.disconnect()
+                var device = 0;
+                when (webhookID) {
+                    "computer" -> {
+                        showMessage(0, ret, "电脑已打开")
+                    }
+                    "light" -> {
+                        showMessage(1, ret, "灯已打开")
+                    }
+                    else -> {
+                        device = 2
+                    }
+                }
+
             }
             catch (e: Exception) {
                 Log.i("MainActivity", "发送 $webhookID 请求失败")
                 Log.i("MainActivity", e.toString())
+                when(webhookID) {
+                    "computer" -> {
+                        showMessage(0, 0, "电脑操作失败")
+                    }
+                    "light" -> {
+                        showMessage(1, 0, "灯操作失败")
+                    }
+                }
             }
         }.start()
     }
 
+    private fun showMessage(device:Int, code:Int, msg:String) {
+        // 在UI线程中更新UI
+        runOnUiThread {
+            // 显示提示信息
+            when(device) {
+                0 -> {
+                    computerTextView!!.text = msg
+                }
+                1 -> {
+                    lightTextView!!.text = msg
+                }
+                else -> {
+                    mainLayout!!.visibility = ConstraintLayout.VISIBLE
+                }
+            }
+        }
+    }
     private fun dealAction() {
         Log.i("MainActivity", "感应到标签")
         var uri = intent.data
@@ -88,6 +135,7 @@ class MainActivity : ComponentActivity() {
                     computerLayout!!.visibility = ConstraintLayout.INVISIBLE
                     lightLayout!!.visibility = ConstraintLayout.VISIBLE
                     mainLayout!!.visibility = ConstraintLayout.INVISIBLE
+                    lightTextView!!.text = "操作灯开关"
                     hassioRequest("light")
                 }
                 else -> {

+ 30 - 0
app/src/main/res/layout/mainlayout.xml

@@ -95,5 +95,35 @@
             android:layout_width="match_parent"
             android:layout_height="match_parent" android:id="@+id/lightLayout" android:visibility="invisible">
 
+        <ImageView
+                android:layout_width="152dp"
+                android:layout_height="144dp"
+                android:id="@+id/lightImageView"
+                app:layout_constraintTop_toTopOf="parent"
+                android:layout_marginTop="173dp" app:layout_constraintStart_toStartOf="parent"
+                android:layout_marginStart="90dp" android:layout_marginEnd="100dp"
+                app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent"
+                android:layout_marginBottom="333dp" android:src="@mipmap/light"/>
+        <TextView
+                android:text="灯"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content" android:id="@+id/lightText"
+                android:textSize="24sp"
+                android:layout_marginTop="36dp"
+                app:layout_constraintTop_toBottomOf="@id/lightImageView"
+                app:layout_constraintStart_toStartOf="parent"
+                android:layout_marginStart="186dp" android:layout_marginEnd="177dp"
+                app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent"
+                android:layout_marginBottom="295dp"/>
+        <Button
+                android:text="关闭"
+                android:layout_width="316dp"
+                android:layout_height="57dp" android:id="@+id/lightCloseButton"
+                android:layout_marginTop="40dp"
+                app:layout_constraintTop_toBottomOf="@+id/lightText" app:layout_constraintStart_toStartOf="parent"
+                android:layout_marginStart="51dp" android:layout_marginEnd="44dp"
+                app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent"
+                android:layout_marginBottom="198dp" android:background="@drawable/button_shape"
+                android:textColor="@color/white"/>
     </androidx.constraintlayout.widget.ConstraintLayout>
 </FrameLayout>

BIN
app/src/main/res/mipmap-xxhdpi/computer.png