From 529a395cc9fd89198c1d49587c3117997e7d0f82 Mon Sep 17 00:00:00 2001 From: TwistedUmbrellaX Date: Mon, 18 Sep 2023 11:25:07 -0400 Subject: [PATCH] Optimize repositioning the NFC action icon --- .../hiddenramblings/tagmo/BrowserActivity.kt | 8 ++----- .../tagmo/bluetooth/GattService.kt | 23 ++++++------------- .../tagmo/eightbit/widget/FABulous.kt | 14 ++++++++--- .../tagmo/fragment/GattSlotFragment.kt | 6 ++--- app/src/main/res/values-de/strings.xml | 2 +- app/src/main/res/values-pl/strings.xml | 2 +- app/src/main/res/values-zh-rTW/string.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- 8 files changed, 27 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/com/hiddenramblings/tagmo/BrowserActivity.kt b/app/src/main/java/com/hiddenramblings/tagmo/BrowserActivity.kt index f05d58681..320bd83c0 100644 --- a/app/src/main/java/com/hiddenramblings/tagmo/BrowserActivity.kt +++ b/app/src/main/java/com/hiddenramblings/tagmo/BrowserActivity.kt @@ -1775,9 +1775,7 @@ class BrowserActivity : AppCompatActivity(), BrowserSettingsListener, private fun loadAmiiboFiles(rootFolder: File?, recursiveFiles: Boolean) { setSnackbarListener(object: SnackbarListener { override fun onSnackbarHidden(fakeSnackbar: AnimatedLinearLayout) { - viewPager.postDelayed({ - nfcFab.loadSavedPosition(prefs) - }, TagMo.uiDelay.toLong()) + nfcFab.loadSavedPosition(prefs) snackbarListener = null } }) @@ -1799,9 +1797,7 @@ class BrowserActivity : AppCompatActivity(), BrowserSettingsListener, private fun loadAmiiboDocuments(rootFolder: DocumentFile?, recursiveFiles: Boolean) { setSnackbarListener(object: SnackbarListener { override fun onSnackbarHidden(fakeSnackbar: AnimatedLinearLayout) { - viewPager.postDelayed({ - nfcFab.loadSavedPosition(prefs) - }, TagMo.uiDelay.toLong()) + nfcFab.loadSavedPosition(prefs) snackbarListener = null } }) diff --git a/app/src/main/java/com/hiddenramblings/tagmo/bluetooth/GattService.kt b/app/src/main/java/com/hiddenramblings/tagmo/bluetooth/GattService.kt index 2c4ba92bd..af55152e8 100644 --- a/app/src/main/java/com/hiddenramblings/tagmo/bluetooth/GattService.kt +++ b/app/src/main/java/com/hiddenramblings/tagmo/bluetooth/GattService.kt @@ -552,21 +552,6 @@ class GattService : Service() { private val mBinder: IBinder = LocalBinder() - /** - * Initializes a reference to the local Bluetooth adapter. - * - * @return Return true if the initialization is successful. - */ - fun initialize(): Boolean { - // For API level 18 and above, get a reference to BluetoothAdapter through BluetoothManager. - mBluetoothAdapter = if (Version.isJellyBeanMR2) { - with (getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager) { adapter } - } else { - @Suppress("deprecation") BluetoothAdapter.getDefaultAdapter() - } - return mBluetoothAdapter != null - } - /** * Connects to the GATT server hosted on the Bluetooth LE device. * @@ -577,10 +562,16 @@ class GattService : Service() { * callback. */ fun connect(address: String?): Boolean { + mBluetoothAdapter = if (Version.isJellyBeanMR2) { + with (getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager) { adapter } + } else { + @Suppress("deprecation") BluetoothAdapter.getDefaultAdapter() + } if (emptyAdapater || address == null) return false // Previously connected device. Try to reconnect. - if (address == mBluetoothDeviceAddress) mBluetoothGatt?.let { return it.connect() } + if (Version.isJellyBeanMR2 && address == mBluetoothDeviceAddress) + mBluetoothGatt?.let { return it.connect() } val device = mBluetoothAdapter?.getRemoteDevice(address) ?: return false // We want to directly connect to the device, so we are setting the autoConnect // parameter to false. diff --git a/app/src/main/java/com/hiddenramblings/tagmo/eightbit/widget/FABulous.kt b/app/src/main/java/com/hiddenramblings/tagmo/eightbit/widget/FABulous.kt index 634de3765..bccd9c244 100644 --- a/app/src/main/java/com/hiddenramblings/tagmo/eightbit/widget/FABulous.kt +++ b/app/src/main/java/com/hiddenramblings/tagmo/eightbit/widget/FABulous.kt @@ -26,6 +26,7 @@ import android.view.View.OnTouchListener import android.view.ViewGroup.MarginLayoutParams import com.google.android.material.floatingactionbutton.FloatingActionButton import com.hiddenramblings.tagmo.Preferences +import com.hiddenramblings.tagmo.TagMo import kotlin.math.abs class FABulous : FloatingActionButton, OnTouchListener { @@ -130,12 +131,19 @@ class FABulous : FloatingActionButton, OnTouchListener { override fun onConfigurationChanged(newConfig: Configuration?) { super.onConfigurationChanged(newConfig) - val bounds = getViewCoordinates(this, x, y) - animate().x(bounds[0]).y(bounds[1]).setDuration(0).start() + postDelayed({ + val bounds = getViewCoordinates(this, x, y) + animate().x(bounds[0]).y(bounds[1]).setDuration(0).start() + }, TagMo.uiDelay.toLong()) } fun loadSavedPosition(prefs: Preferences) { - animate().x(prefs.fabulousX(this)).y(prefs.fabulousY(this)).setDuration(0).start() + postDelayed({ + val bounds = getViewCoordinates( + this, prefs.fabulousX(this), prefs.fabulousY(this) + ) + animate().x(bounds[0]).y(bounds[1]).setDuration(0).start() + }, TagMo.uiDelay.toLong()) } companion object { diff --git a/app/src/main/java/com/hiddenramblings/tagmo/fragment/GattSlotFragment.kt b/app/src/main/java/com/hiddenramblings/tagmo/fragment/GattSlotFragment.kt index bfd43c393..70d75be65 100644 --- a/app/src/main/java/com/hiddenramblings/tagmo/fragment/GattSlotFragment.kt +++ b/app/src/main/java/com/hiddenramblings/tagmo/fragment/GattSlotFragment.kt @@ -685,9 +685,9 @@ open class GattSlotFragment : Fragment(), GattSlotAdapter.OnAmiiboClickListener, deviceDialog.dismiss() deviceProfile = device.name deviceAddress = device.address - dismissGattDiscovery() showConnectionNotice() startGattService() + dismissGattDiscovery() } return item } @@ -1276,7 +1276,7 @@ open class GattSlotFragment : Fragment(), GattSlotAdapter.OnAmiiboClickListener, override fun onServiceConnected(component: ComponentName, binder: IBinder) { val localBinder = binder as GattService.LocalBinder serviceGatt = localBinder.service.apply { - if (initialize() && connect(deviceAddress)) { + if (connect(deviceAddress)) { serviceType = deviceType maxSlotCount = when (serviceType) { Nordic.DEVICE.FLASK -> 85 @@ -1548,7 +1548,7 @@ open class GattSlotFragment : Fragment(), GattSlotAdapter.OnAmiiboClickListener, }) } else { stopGattService() - Toasty(requireContext()).Short(R.string.device_invalid) + Toasty(requireContext()).Short(R.string.gatt_connect_fail) } } } diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 4dcb7eae2..d1d506e92 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -292,7 +292,7 @@ Zurück zu TagMo Keine GATT-Geräte in der Nähe gefunden! - Kein gültiges GATT-Gerät! + Kein gültiges GATT-Gerät! Scannen nach Bluetooth GATT… Verbinde mit Bluetooth GATT… GATT-Geräteverbindung verloren! diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml index b8e393ca3..3c088c8a3 100644 --- a/app/src/main/res/values-pl/strings.xml +++ b/app/src/main/res/values-pl/strings.xml @@ -292,7 +292,7 @@ Powrót do TagMo Brak urządzeń GATT w pobliżu! - Nieprawidłowe urządzenie GATT! + Nieprawidłowe urządzenie GATT! Skanowanie w poszukiwaniu Bluetooth GATT… Łączenie z Bluetooth GATT… Utracono połączenie z urządzeniem GATT! diff --git a/app/src/main/res/values-zh-rTW/string.xml b/app/src/main/res/values-zh-rTW/string.xml index 856fcaa4b..65d4e2e26 100644 --- a/app/src/main/res/values-zh-rTW/string.xml +++ b/app/src/main/res/values-zh-rTW/string.xml @@ -298,7 +298,7 @@ 返回 TagMo 附近沒有可用的GATT設備! - 無效的GATT設備! + 無效的GATT設備! 正在掃描Bluetooth GATT… 正在連接Bluetooth GATT… GATT連線無法使用! diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8af82885c..9871147aa 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -321,7 +321,7 @@ Back to TagMo Shop amiibo hardware No nearby GATT devices found! - Not a valid GATT device! + Failed to connect GATT service! %1$s services not found! Characteristic not found! Empty collection can\'t be reduced