Skip to content

Commit

Permalink
[fix]handle unexpected exit
Browse files Browse the repository at this point in the history
[feat]improve history activity
  • Loading branch information
Steve-Mr committed Jun 1, 2023
1 parent b07c0f6 commit b99a12f
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 7 deletions.
29 changes: 27 additions & 2 deletions app/src/main/java/com/maary/liveinpeace/HistoryActivity.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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) }
Expand All @@ -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<String, Connection>) {
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<String, Connection>) {
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()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<out AudioDeviceInfo>?) {
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_bluetooth_alt.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="?attr/colorSecondaryVariant"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M17,7l-4.29,-4.29c-0.63,-0.63 -1.71,-0.19 -1.71,0.7v6.18L7.11,5.7c-0.39,-0.39 -1.02,-0.39 -1.41,0 -0.39,0.39 -0.39,1.02 0,1.41L10.59,12 5.7,16.89c-0.39,0.39 -0.39,1.02 0,1.41 0.39,0.39 1.02,0.39 1.41,0L11,14.41v6.18c0,0.89 1.08,1.34 1.71,0.71L17,17c0.39,-0.39 0.39,-1.02 0,-1.41L13.41,12 17,8.42c0.39,-0.39 0.39,-1.03 0,-1.42zM13,5.83l1.88,1.88L13,9.59L13,5.83zM14.88,16.29L13,18.17v-3.76l1.88,1.88z"/>
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_bluetooth_round_alt.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/shape_circle_alt"/>
<item android:drawable="@drawable/ic_bluetooth_alt"
android:left="8dp"
android:right="8dp"
android:top="8dp"
android:bottom="8dp"/>
</layer-list>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_headphone_alt.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="?attr/colorSecondaryVariant"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="?android:attr/textColorPrimary" android:pathData="M12,3c-4.97,0 -9,4.03 -9,9v7c0,1.1 0.9,2 2,2h4v-8H5v-1c0,-3.87 3.13,-7 7,-7s7,3.13 7,7v1h-4v8h4c1.1,0 2,-0.9 2,-2v-7C21,7.03 16.97,3 12,3z"/>
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_headphone_round_alt.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/shape_circle_alt"/>
<item android:drawable="@drawable/ic_headphone_alt"
android:left="8dp"
android:right="8dp"
android:top="8dp"
android:bottom="8dp"/>
</layer-list>
8 changes: 8 additions & 0 deletions app/src/main/res/drawable/shape_circle_alt.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="?attr/colorSecondaryContainer"/>
<!-- <stroke-->
<!-- android:width="2dp"-->
<!-- android:color="?attr/colorTertiaryContainer"/>-->
</shape>
11 changes: 9 additions & 2 deletions app/src/main/res/layout/activity_history.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,19 @@

</com.google.android.material.appbar.AppBarLayout>

<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:color/transparent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" >
android:background="@android:color/transparent">

<TextView
android:id="@+id/title_current"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/current_connections"
Expand All @@ -88,6 +93,7 @@
tools:itemCount="2"/>

<TextView
android:id="@+id/title_history"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/connections_history"
Expand All @@ -105,6 +111,7 @@
tools:listitem="@layout/item_connection"/>

</LinearLayout>
</androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Expand Down
55 changes: 55 additions & 0 deletions app/src/main/res/layout/item_current_connections.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<ImageView
android:id="@+id/device_icon"
android:layout_width="48dp"
android:layout_height="48dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:contentDescription="@string/icon_of_device"
tools:src="@drawable/ic_headphone"
/>

<TextView
android:id="@+id/device_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toEndOf="@id/device_icon"
app:layout_constraintTop_toTopOf="@id/device_icon"
android:textSize="18sp"
android:textColor="?android:attr/textColorPrimary"
android:layout_marginStart="16dp"
tools:text="Headphone"/>

<ImageView
android:id="@+id/connection_time_prefix"
android:layout_width="8dp"
android:layout_height="8dp"
app:layout_constraintTop_toBottomOf="@id/device_name"
app:layout_constraintStart_toEndOf="@id/device_icon"
android:layout_marginStart="16dp"
app:layout_constraintBottom_toBottomOf="parent"
android:src="@drawable/shape_circle"
app:tint="@android:color/darker_gray"
android:contentDescription="@string/device_connection_state" />

<TextView
android:id="@+id/device_connection_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:textColor="?android:attr/textColorPrimary"
app:layout_constraintTop_toBottomOf="@id/device_name"
app:layout_constraintBottom_toBottomOf="@id/device_icon"
app:layout_constraintStart_toEndOf="@id/connection_time_prefix"
android:textSize="12sp"
tools:text="3hrs"/>

</androidx.constraintlayout.widget.ConstraintLayout>
4 changes: 4 additions & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@
<string name="duration_hour">%02d 小时 %02d 分钟</string>
<string name="enable_watching">启用提醒</string>
<string name="disable_watching">禁用提醒</string>
<string name="current_connections">当前连接</string>
<string name="connections_history">历史连接</string>
<string name="has_already_connected_for">已连接</string>
<string name="device_connection_state">设备连接状态指示</string>
</resources>
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<string name="device_connected_too_long">%1$s has been connected for over two hours.</string>
<string name="channel_alert">Alerts</string>
<string name="alert_channel_description">Notification channel for reminding about excessive device connection time.</string>
<string name="history">History</string>
<string name="history">Connections</string>
<string name="timeline">Timeline</string>
<string name="summary">Summary</string>
<string name="icon_of_device">Device icon</string>
Expand Down

0 comments on commit b99a12f

Please sign in to comment.