Skip to content

Commit

Permalink
Merge 9393d3c into fa92f09
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve-Mr authored Aug 16, 2023
2 parents fa92f09 + 9393d3c commit 900b3d9
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 1 deletion.
6 changes: 6 additions & 0 deletions app/src/main/java/com/maary/liveinpeace/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ class Constants {
const val PREF_ICON = "icon_type"
const val PREF_NOTIFY_TEXT_SIZE = "notification_text_size"
const val PREF_WATCHING_CONNECTING_TIME = "watching_connecting"
const val PREF_ENABLE_EAR_PROTECTION = "ear_protection_enabled"
// 设置通知 id
const val ID_NOTIFICATION_SETTINGS = 3
// 前台通知 id
const val ID_NOTIFICATION_FOREGROUND = 1
const val ID_NOTIFICATION_ALERT = 2
const val ID_NOTIFICATION_PROTECT = 4
// 设置图像式图标 Action
const val ACTION_NAME_SET_IMG = "com.maary.liveinpeace.receiver.SettingsReceiver.SetIconImg"
// 设置字符式图标 Action
Expand All @@ -27,6 +29,8 @@ class Constants {
const val ACTION_ENABLE_WATCHING = "com.maary.liveinpeace.receiver.SettingsReceiver.EnableWatching"
// 禁用长时间连接提醒 Action
const val ACTION_DISABLE_WATCHING = "com.maary.liveinpeace.receiver.SettingsReceiver.DisableWatching"
// toggle 设备连接调整音量 Action
const val ACTION_TOGGLE_AUTO_CONNECTION_ADJUSTMENT = "com.maary.liveinpeace.receiver.SettingsReceiver.ToggleAdjustment"
// 设置 Action
const val ACTION_NAME_SETTINGS = "com.maary.liveinpeace.receiver.SettingsReceiver"
// 静音广播名称
Expand All @@ -40,6 +44,7 @@ class Constants {
const val CHANNEL_ID_DEFAULT = "LIP_FOREGROUND"
const val CHANNEL_ID_SETTINGS = "LIP_SETTINGS"
const val CHANNEL_ID_ALERT = "LIP_ALERT"
const val CHANNEL_ID_PROTECT = "LIP_PROTECT"
// 提醒时间
const val ALERT_TIME: Long = 2*60*60*1000
// 延后时间
Expand All @@ -48,6 +53,7 @@ class Constants {
const val ID_NOTIFICATION_GROUP_FORE = "LIP_notification_group_foreground"
const val ID_NOTIFICATION_GROUP_SETTINGS = "LIP_notification_group_settings"
const val ID_NOTIFICATION_GROUP_ALERTS = "LIP_notification_group_alerts"
const val ID_NOTIFICATION_GROUP_PROTECT = "LIP_notification_group_protect"
const val PATTERN_DATE_DATABASE = "yyyy-MM-dd"
const val PATTERN_DATE_BUTTON = "MM/dd"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import com.maary.liveinpeace.Constants.Companion.ACTION_ENABLE_WATCHING
import com.maary.liveinpeace.Constants.Companion.ACTION_NAME_SETTINGS
import com.maary.liveinpeace.Constants.Companion.ACTION_NAME_SET_IMG
import com.maary.liveinpeace.Constants.Companion.ACTION_NAME_SET_NUM
import com.maary.liveinpeace.Constants.Companion.ACTION_TOGGLE_AUTO_CONNECTION_ADJUSTMENT
import com.maary.liveinpeace.Constants.Companion.CHANNEL_ID_SETTINGS
import com.maary.liveinpeace.Constants.Companion.ID_NOTIFICATION_GROUP_SETTINGS
import com.maary.liveinpeace.Constants.Companion.ID_NOTIFICATION_SETTINGS
import com.maary.liveinpeace.Constants.Companion.MODE_IMG
import com.maary.liveinpeace.Constants.Companion.MODE_NUM
import com.maary.liveinpeace.Constants.Companion.PREF_ENABLE_EAR_PROTECTION
import com.maary.liveinpeace.Constants.Companion.PREF_ICON
import com.maary.liveinpeace.Constants.Companion.PREF_WATCHING_CONNECTING_TIME
import com.maary.liveinpeace.Constants.Companion.SHARED_PREF
Expand Down Expand Up @@ -166,6 +168,24 @@ class SettingsReceiver: BroadcastReceiver() {
}
}

if (ACTION_TOGGLE_AUTO_CONNECTION_ADJUSTMENT == p1?.action){
val sharedPreferences = p0?.getSharedPreferences(
SHARED_PREF,
Context.MODE_PRIVATE
)
if (sharedPreferences != null) {
with(sharedPreferences.edit()){
putBoolean(PREF_ENABLE_EAR_PROTECTION,
!sharedPreferences.getBoolean(PREF_ENABLE_EAR_PROTECTION, false)
)
apply()
val foregroundServiceIntent = Intent(p0, ForegroundService::class.java)
p0.stopService(foregroundServiceIntent)
p0.startForegroundService(foregroundServiceIntent)
}
}
}

if (ACTION_CANCEL == p1?.action){
val notificationManager: NotificationManager =
p0?.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,21 @@ import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.graphics.drawable.IconCompat
import com.maary.liveinpeace.Constants.Companion.ACTION_NAME_SETTINGS
import com.maary.liveinpeace.Constants.Companion.ACTION_TOGGLE_AUTO_CONNECTION_ADJUSTMENT
import com.maary.liveinpeace.Constants.Companion.ALERT_TIME
import com.maary.liveinpeace.Constants.Companion.BROADCAST_ACTION_FOREGROUND
import com.maary.liveinpeace.Constants.Companion.BROADCAST_ACTION_MUTE
import com.maary.liveinpeace.Constants.Companion.BROADCAST_FOREGROUND_INTENT_EXTRA
import com.maary.liveinpeace.Constants.Companion.CHANNEL_ID_DEFAULT
import com.maary.liveinpeace.Constants.Companion.CHANNEL_ID_PROTECT
import com.maary.liveinpeace.Constants.Companion.ID_NOTIFICATION_ALERT
import com.maary.liveinpeace.Constants.Companion.ID_NOTIFICATION_FOREGROUND
import com.maary.liveinpeace.Constants.Companion.ID_NOTIFICATION_GROUP_FORE
import com.maary.liveinpeace.Constants.Companion.ID_NOTIFICATION_GROUP_PROTECT
import com.maary.liveinpeace.Constants.Companion.ID_NOTIFICATION_PROTECT
import com.maary.liveinpeace.Constants.Companion.MODE_IMG
import com.maary.liveinpeace.Constants.Companion.MODE_NUM
import com.maary.liveinpeace.Constants.Companion.PREF_ENABLE_EAR_PROTECTION
import com.maary.liveinpeace.Constants.Companion.PREF_ICON
import com.maary.liveinpeace.Constants.Companion.PREF_NOTIFY_TEXT_SIZE
import com.maary.liveinpeace.Constants.Companion.PREF_WATCHING_CONNECTING_TIME
Expand Down Expand Up @@ -159,6 +164,7 @@ class ForegroundService: Service() {
override fun onAudioDevicesAdded(addedDevices: Array<out AudioDeviceInfo>?) {

val connectedTime = System.currentTimeMillis()
val sharedPreferences = getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE)

// 在设备连接时记录设备信息和接入时间
addedDevices?.forEach { deviceInfo ->
Expand Down Expand Up @@ -187,10 +193,26 @@ class ForegroundService: Service() {
date = LocalDate.now().toString()
)
notifyDeviceMapChange()
if (sharedPreferences.getBoolean(PREF_ENABLE_EAR_PROTECTION, false)){
val audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
var boolProtected = false
while (getVolumePercentage(applicationContext)>25) {
boolProtected = true
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, 0)
}
while (getVolumePercentage(applicationContext)<10) {
boolProtected = true
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, 0)
}
if (boolProtected){
with(NotificationManagerCompat.from(applicationContext)){
notify(ID_NOTIFICATION_PROTECT, createProtectionNotification())
}
}
}
// 执行其他逻辑,比如将设备信息保存到数据库或日志中
}

val sharedPreferences = getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE)
if (sharedPreferences.getBoolean(PREF_WATCHING_CONNECTING_TIME, false)){
for ((productName, _) in deviceMap){
if (deviceTimerMap.containsKey(productName)) continue
Expand Down Expand Up @@ -341,6 +363,30 @@ class ForegroundService: Service() {
snoozePendingIntent
).build()

val sharedPreferences = getSharedPreferences(
SHARED_PREF,
Context.MODE_PRIVATE
)

var protectionActionTitle = R.string.protection
if (sharedPreferences != null){
if (sharedPreferences.getBoolean(PREF_ENABLE_EAR_PROTECTION, false)){
protectionActionTitle = R.string.dont_protect
}
}

val protectionIntent = Intent(this, SettingsReceiver::class.java).apply {
action = ACTION_TOGGLE_AUTO_CONNECTION_ADJUSTMENT
}
val protectionPendingIntent: PendingIntent =
PendingIntent.getBroadcast(this, 0, protectionIntent, PendingIntent.FLAG_IMMUTABLE)

val actionProtection : NotificationCompat.Action = NotificationCompat.Action.Builder(
R.drawable.ic_headphones_protection,
resources.getString(protectionActionTitle),
protectionPendingIntent
).build()

val historyIntent = Intent(this, HistoryActivity::class.java)
historyIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK

Expand Down Expand Up @@ -370,11 +416,22 @@ class ForegroundService: Service() {
.setPriority(NotificationCompat.PRIORITY_LOW)
.addAction(actionSettings)
.addAction(actionHistory)
.addAction(actionProtection)
.setGroup(ID_NOTIFICATION_GROUP_FORE)
.setGroupSummary(false)
.build()
}

fun createProtectionNotification(): Notification {
return NotificationCompat.Builder(this, CHANNEL_ID_PROTECT)
.setContentTitle(getString(R.string.ears_protected))
.setSmallIcon(R.drawable.ic_headphones_protection)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setGroup(ID_NOTIFICATION_GROUP_PROTECT)
.setTimeoutAfter(3000)
.build()
}

private val textBounds = Rect()

private fun generateNotificationIcon(context: Context, iconMode: Int): IconCompat {
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/com/maary/liveinpeace/service/QSTileService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.maary.liveinpeace.Constants.Companion.BROADCAST_ACTION_FOREGROUND
import com.maary.liveinpeace.Constants.Companion.BROADCAST_FOREGROUND_INTENT_EXTRA
import com.maary.liveinpeace.Constants.Companion.CHANNEL_ID_ALERT
import com.maary.liveinpeace.Constants.Companion.CHANNEL_ID_DEFAULT
import com.maary.liveinpeace.Constants.Companion.CHANNEL_ID_PROTECT
import com.maary.liveinpeace.Constants.Companion.CHANNEL_ID_SETTINGS
import com.maary.liveinpeace.Constants.Companion.REQUESTING_WAIT_MILLIS
import com.maary.liveinpeace.R
Expand Down Expand Up @@ -73,6 +74,18 @@ class QSTileService: TileService() {
resources.getString(R.string.alert_channel_description)
)
}
if (notificationManager.getNotificationChannel(CHANNEL_ID_PROTECT) == null) {
val channel = NotificationChannel(
CHANNEL_ID_PROTECT,
resources.getString(R.string.channel_protection),
NotificationManager.IMPORTANCE_LOW).apply {
description = resources.getString(R.string.protection_channel_description)
enableVibration(false)
setSound(null, null)
}
// Register the channel with the system
notificationManager.createNotificationChannel(channel)
}

applicationContext.startForegroundService(intent)
tile.state = Tile.STATE_ACTIVE
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_headphones_protection.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
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="M6.2,3.01C4.44,2.89 3,4.42 3,6.19L3,16c0,2.76 2.24,5 5,5h0c2.76,0 5,-2.24 5,-5V8c0,-1.66 1.34,-3 3,-3h0c1.66,0 3,1.34 3,3v7l-0.83,0c-1.61,0 -3.06,1.18 -3.17,2.79c-0.12,1.69 1.16,3.1 2.8,3.21c1.76,0.12 3.2,-1.42 3.2,-3.18L21,8c0,-2.76 -2.24,-5 -5,-5h0c-2.76,0 -5,2.24 -5,5v8c0,1.66 -1.34,3 -3,3l0,0c-1.66,0 -3,-1.34 -3,-3V9l0.83,0C7.44,9 8.89,7.82 9,6.21C9.11,4.53 7.83,3.11 6.2,3.01z"/>
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@
<string name="has_already_connected_for">已连接</string>
<string name="device_connection_state">设备连接状态指示</string>
<string name="select_date">选择日期</string>
<string name="protection">保护耳朵</string>
<string name="dont_protect">不保护耳朵</string>
<string name="ears_protected">保护了耳朵</string>
<string name="channel_protection">保护耳朵</string>
<string name="protection_channel_description">有新设备连接时自动将音量调节至 25 以下</string>
</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<string name="has_already_connected_for">Already connected</string>
<string name="device_connection_state">Device connection state indicator</string>
<string name="select_date">Select a Date</string>
<string name="protection">Protect My Ears</string>
<string name="dont_protect">Stop protecting ears</string>
<string name="ears_protected">Ears Protected</string>
<string name="channel_protection">Protecting ears</string>
<string name="protection_channel_description">Auto adjust volume below 25 when connecting to a new device.</string>
<string-array name="array_volume_comment">
<item>Shhhhhhh…</item>
<item>Perfectly Balanced, As All Things Should Be…</item>
Expand Down

0 comments on commit 900b3d9

Please sign in to comment.