Add ActivityExtensions for conciseness
Bug: 417855334
Test: m ScanningApp
Flag: EXEMPT ScanningApp only
Change-Id: Ie48ecc7f0d651c4cde5a74e7378a9089e0e95166
diff --git a/tools/ScanningApp/src/com/android/bluetooth/scanningapp/MainActivity.kt b/tools/ScanningApp/src/com/android/bluetooth/scanningapp/MainActivity.kt
index 91c19cf..4a4fc73 100644
--- a/tools/ScanningApp/src/com/android/bluetooth/scanningapp/MainActivity.kt
+++ b/tools/ScanningApp/src/com/android/bluetooth/scanningapp/MainActivity.kt
@@ -32,7 +32,6 @@
import android.view.ViewGroup
import android.widget.Button
import android.widget.CheckBox
-import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.PopupMenu
@@ -46,6 +45,7 @@
import androidx.recyclerview.widget.RecyclerView
import com.android.bluetooth.scanningapp.extensions.toScanErrorMessage
import com.android.bluetooth.scanningapp.extensions.toScanModeString
+import com.android.bluetooth.scanningapp.extensions.toast
import com.google.android.material.slider.Slider
private const val TAG = "MainActivity"
@@ -206,12 +206,6 @@
.setScanMode(scanMode)
.build()
- Toast.makeText(
- this@MainActivity,
- "Scan started: ${scanMode.toScanModeString()}",
- Toast.LENGTH_SHORT,
- )
- .show()
isScanning = true
scanButton.text = "Stop Scan"
scanButton.backgroundTintList =
@@ -232,7 +226,7 @@
return
}
- Toast.makeText(this@MainActivity, "Scan stopped", Toast.LENGTH_SHORT).show()
+ toast("Scan stopped")
isScanning = false
scanButton.text = "Start Scan"
scanButton.backgroundTintList =
@@ -259,12 +253,7 @@
Log.e(TAG, "Scan Failed with error code: $errorCode")
runOnUiThread {
- Toast.makeText(
- this@MainActivity,
- "Scan failed: ${errorCode.toScanErrorMessage()}",
- Toast.LENGTH_LONG,
- )
- .show()
+ toast("Scan failed: ${errorCode.toScanErrorMessage()}")
stopScan()
}
}
diff --git a/tools/ScanningApp/src/com/android/bluetooth/scanningapp/extensions/ActivityExtensions.kt b/tools/ScanningApp/src/com/android/bluetooth/scanningapp/extensions/ActivityExtensions.kt
new file mode 100644
index 0000000..233e8bb
--- /dev/null
+++ b/tools/ScanningApp/src/com/android/bluetooth/scanningapp/extensions/ActivityExtensions.kt
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2025 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.bluetooth.scanningapp.extensions
+
+import android.app.Activity
+import android.widget.Toast
+
+fun Activity.toast(text: String) {
+ Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
+}