From b99a12f0eac60ab5383d0a8dbcb512d5081fd968 Mon Sep 17 00:00:00 2001 From: Maary <24504742+Steve-Mr@users.noreply.github.com> Date: Thu, 1 Jun 2023 21:50:53 +0800 Subject: [PATCH] [fix]handle unexpected exit [feat]improve history activity --- .../com/maary/liveinpeace/HistoryActivity.kt | 29 +++++++++- .../liveinpeace/service/ForegroundService.kt | 29 +++++++++- .../main/res/drawable/ic_bluetooth_alt.xml | 5 ++ .../res/drawable/ic_bluetooth_round_alt.xml | 9 +++ .../main/res/drawable/ic_headphone_alt.xml | 5 ++ .../res/drawable/ic_headphone_round_alt.xml | 9 +++ .../main/res/drawable/shape_circle_alt.xml | 8 +++ app/src/main/res/layout/activity_history.xml | 11 +++- .../res/layout/item_current_connections.xml | 55 +++++++++++++++++++ app/src/main/res/values-zh-rCN/strings.xml | 4 ++ app/src/main/res/values/strings.xml | 2 +- 11 files changed, 159 insertions(+), 7 deletions(-) create mode 100644 app/src/main/res/drawable/ic_bluetooth_alt.xml create mode 100644 app/src/main/res/drawable/ic_bluetooth_round_alt.xml create mode 100644 app/src/main/res/drawable/ic_headphone_alt.xml create mode 100644 app/src/main/res/drawable/ic_headphone_round_alt.xml create mode 100644 app/src/main/res/drawable/shape_circle_alt.xml create mode 100644 app/src/main/res/layout/item_current_connections.xml diff --git a/app/src/main/java/com/maary/liveinpeace/HistoryActivity.kt b/app/src/main/java/com/maary/liveinpeace/HistoryActivity.kt index e937259..16fbf6d 100644 --- a/app/src/main/java/com/maary/liveinpeace/HistoryActivity.kt +++ b/app/src/main/java/com/maary/liveinpeace/HistoryActivity.kt @@ -1,6 +1,8 @@ package com.maary.liveinpeace import android.os.Bundle +import android.util.Log +import android.view.View import androidx.activity.viewModels import androidx.appcompat.app.AppCompatActivity import androidx.core.view.WindowCompat @@ -56,6 +58,7 @@ class HistoryActivity : AppCompatActivity(), DeviceMapChangeListener { setContentView(binding.root) val connectionAdapter = ConnectionListAdapter() + binding.historyList.isNestedScrollingEnabled = false binding.historyList.adapter = connectionAdapter binding.historyList.layoutManager = LinearLayoutManager(this) @@ -65,10 +68,11 @@ class HistoryActivity : AppCompatActivity(), DeviceMapChangeListener { finish() } + binding.currentList.isNestedScrollingEnabled = false binding.currentList.adapter = currentAdapter binding.currentList.layoutManager = LinearLayoutManager(this) - currentAdapter.submitList(currentConnectionsDuration(ForegroundService.getConnections())) + updateCurrentAdapter() connectionViewModel.getAllConnectionsOnDate(LocalDate.now().toString()).observe(this) { connections -> connections.let { connectionAdapter.submitList(it) } @@ -80,19 +84,40 @@ class HistoryActivity : AppCompatActivity(), DeviceMapChangeListener { connectionViewModel.getAllConnectionsOnDate(LocalDate.now().toString()).observe(this) { connections -> connections.let { connectionAdapter.submitList(it) } } + updateCurrentAdapter() } if (checkedId == R.id.button_summary) { connectionViewModel.getSummaryOnDate(LocalDate.now().toString()).observe(this) { connections -> connections.let { connectionAdapter.submitList(it) } } + updateCurrentAdapter() } } } - override fun onDeviceMapChanged(deviceMap: Map) { + private fun updateCurrentAdapter(){ currentAdapter.submitList(currentConnectionsDuration(ForegroundService.getConnections())) + if (currentAdapter.itemCount == 0){ + Log.v("MUTE_", "GG") + binding.titleCurrent.visibility = View.GONE + }else{ + binding.titleCurrent.visibility = View.VISIBLE + } + } + + override fun onDeviceMapChanged(deviceMap: Map) { + if (deviceMap.isEmpty()){ + Log.v("MUTE_", "GGA") + + binding.titleCurrent.visibility = View.GONE + }else{ + Log.v("MUTE_", "GGB") + + binding.titleCurrent.visibility = View.VISIBLE + } + currentAdapter.submitList(currentConnectionsDuration(deviceMap.values.toMutableList())) } } \ No newline at end of file diff --git a/app/src/main/java/com/maary/liveinpeace/service/ForegroundService.kt b/app/src/main/java/com/maary/liveinpeace/service/ForegroundService.kt index deb07ba..90e23da 100644 --- a/app/src/main/java/com/maary/liveinpeace/service/ForegroundService.kt +++ b/app/src/main/java/com/maary/liveinpeace/service/ForegroundService.kt @@ -150,6 +150,31 @@ class ForegroundService: Service() { } } + private fun saveDateWhenStop(){ + val disconnectedTime = System.currentTimeMillis() + + for ( (deviceName, connection) in deviceMap){ + + val connectedTime = connection.connectedTime + val connectionTime = disconnectedTime - connectedTime!! + + CoroutineScope(Dispatchers.IO).launch { + connectionDao.insert( + Connection( + name = connection.name, + type = connection.type, + connectedTime = connection.connectedTime, + disconnectedTime = disconnectedTime, + duration = connectionTime, + date = connection.date + ) + ) + } + deviceMap.remove(deviceName) + } + return + } + private val audioDeviceCallback = object : AudioDeviceCallback() { @SuppressLint("MissingPermission") override fun onAudioDevicesAdded(addedDevices: Array?) { @@ -280,12 +305,12 @@ class ForegroundService: Service() { } override fun onDestroy() { - super.onDestroy() - + saveDateWhenStop() // 取消注册音量变化广播接收器 unregisterReceiver(volumeChangeReceiver) audioManager.unregisterAudioDeviceCallback(audioDeviceCallback) isForegroundServiceRunning = false + super.onDestroy() } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { diff --git a/app/src/main/res/drawable/ic_bluetooth_alt.xml b/app/src/main/res/drawable/ic_bluetooth_alt.xml new file mode 100644 index 0000000..462ac0d --- /dev/null +++ b/app/src/main/res/drawable/ic_bluetooth_alt.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/drawable/ic_bluetooth_round_alt.xml b/app/src/main/res/drawable/ic_bluetooth_round_alt.xml new file mode 100644 index 0000000..a47d618 --- /dev/null +++ b/app/src/main/res/drawable/ic_bluetooth_round_alt.xml @@ -0,0 +1,9 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_headphone_alt.xml b/app/src/main/res/drawable/ic_headphone_alt.xml new file mode 100644 index 0000000..4f8e69a --- /dev/null +++ b/app/src/main/res/drawable/ic_headphone_alt.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/drawable/ic_headphone_round_alt.xml b/app/src/main/res/drawable/ic_headphone_round_alt.xml new file mode 100644 index 0000000..513dec1 --- /dev/null +++ b/app/src/main/res/drawable/ic_headphone_round_alt.xml @@ -0,0 +1,9 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/shape_circle_alt.xml b/app/src/main/res/drawable/shape_circle_alt.xml new file mode 100644 index 0000000..4cb26ee --- /dev/null +++ b/app/src/main/res/drawable/shape_circle_alt.xml @@ -0,0 +1,8 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_history.xml b/app/src/main/res/layout/activity_history.xml index 82fa8c4..cc6eedf 100644 --- a/app/src/main/res/layout/activity_history.xml +++ b/app/src/main/res/layout/activity_history.xml @@ -62,14 +62,19 @@ + + + android:background="@android:color/transparent"> + diff --git a/app/src/main/res/layout/item_current_connections.xml b/app/src/main/res/layout/item_current_connections.xml new file mode 100644 index 0000000..f5c0e83 --- /dev/null +++ b/app/src/main/res/layout/item_current_connections.xml @@ -0,0 +1,55 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 3e3efc2..64b38ca 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -33,4 +33,8 @@ %02d 小时 %02d 分钟 启用提醒 禁用提醒 + 当前连接 + 历史连接 + 已连接 + 设备连接状态指示 \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1306b2a..63255ee 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -17,7 +17,7 @@ %1$s has been connected for over two hours. Alerts Notification channel for reminding about excessive device connection time. - History + Connections Timeline Summary Device icon