Skip to content

Commit

Permalink
通知图标使用预设的 drawable
Browse files Browse the repository at this point in the history
空间换时间(理论上
  • Loading branch information
Steve-Mr committed May 29, 2024
1 parent 7f42a2d commit 8b5d917
Show file tree
Hide file tree
Showing 107 changed files with 1,243 additions and 77 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Quiet - Wishing for peace - Wishing for rest - Rest - RIP - Live in peace
1. ~~The notification permission (required) and nearby device permission (optional, only needed when monitoring volume changes caused by Bluetooth headset connection/disconnection) need to be manually enabled.~~
2. Notification channels and priority may need to be adjusted manually.
3. Notification icon may not be prominent enough.
4. There may be performance issues when using numeric notification icons, and when the volume is at maximum, it will display `!!` instead of `100`.
4. ~~There may be performance issues when using numeric notification icons, and~~ when the volume is at maximum, it will display `!!` instead of `100`.
5. "Disable Alert" and "Enable Alert" in the settings refer to the option of disabling or enabling the notification for prolonged headphone connection time. Due to the width limitation of the notification button, it may not clearly convey the meaning. There is a plan to implement a settings interface using an Activity in the future, but it's not guaranteed and may take a considerable amount of time.
6. Any kind of bug is possible

Expand Down
2 changes: 1 addition & 1 deletion README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
1. ~~需要手动启用通知权限(必需)和附近的设备权限(非必需,仅当需要监测蓝牙耳机的连接和断开导致的音量变化)~~
2. 通知渠道和通知优先级可能需要手动调整
3. 通知图标可能不够醒目
4. 使用数字式通知图标时可能有性能问题,且音量满时会显示`!!`而非 `100`
4. 使用数字式通知图标时~~可能有性能问题,且~~音量满时会显示`!!`而非 `100`
5. 设置中的「禁用提醒」和「启用提醒」指是否启用耳机连接时间过长的提醒,受限于通知按钮的宽度限制没有清楚表述,计划将来使用 Activity 实现设置界面,但并不能保证且可能需要很长的时间。
6. 随便什么 bug 都有可能

Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ android {
minSdk 31
targetSdk 34
versionCode 5
versionName "2.2_beta"
versionName "2.3_beta"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ import android.app.Service
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.media.AudioDeviceCallback
import android.media.AudioDeviceInfo
import android.media.AudioManager
Expand Down Expand Up @@ -42,12 +37,10 @@ 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
import com.maary.liveinpeace.Constants.Companion.SHARED_PREF
import com.maary.liveinpeace.DeviceMapChangeListener
import com.maary.liveinpeace.DeviceTimer
import com.maary.liveinpeace.HistoryActivity
import com.maary.liveinpeace.R
import com.maary.liveinpeace.SleepNotification.find
import com.maary.liveinpeace.database.Connection
Expand Down Expand Up @@ -197,7 +190,7 @@ class ForegroundService: Service() {
)
) { return@forEach }
val deviceName = deviceInfo.productName.toString().trim()
if (deviceName == android.os.Build.MODEL) return@forEach
if (deviceName == Build.MODEL) return@forEach
Log.v("MUTE_DEVICE", deviceName)
Log.v("MUTE_TYPE", deviceInfo.type.toString())
deviceMap[deviceName] = Connection(
Expand Down Expand Up @@ -410,17 +403,6 @@ class ForegroundService: Service() {
protectionPendingIntent
).build()

// val historyIntent = Intent(this, HistoryActivity::class.java)
// historyIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
//
// val pendingHistoryIntent = PendingIntent.getActivity(context, 0, historyIntent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
//
// val actionHistory: NotificationCompat.Action = NotificationCompat.Action.Builder(
// R.drawable.ic_action_history,
// resources.getString(R.string.history),
// pendingHistoryIntent
// ).build()

val sleepIntent = Intent(context, MuteMediaReceiver::class.java)
sleepIntent.action = BROADCAST_ACTION_SLEEPTIMER_TOGGLE
val pendingSleepIntent = PendingIntent.getBroadcast(context, 0, sleepIntent, PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
Expand Down Expand Up @@ -452,7 +434,6 @@ class ForegroundService: Service() {
.setContentIntent(pendingMuteIntent)
.setPriority(NotificationCompat.PRIORITY_LOW)
.addAction(actionSettings)
// .addAction(actionHistory)
.addAction(actionSleepTimer)
.addAction(actionProtection)
.setGroup(ID_NOTIFICATION_GROUP_FORE)
Expand All @@ -470,63 +451,13 @@ class ForegroundService: Service() {
.build()
}

private val textBounds = Rect()

@SuppressLint("DiscouragedApi")
private fun generateNotificationIcon(context: Context, iconMode: Int): IconCompat {
var currentVolume = getVolumePercentage(context)
val currentVolume = getVolumePercentage(context)
val currentVolumeLevel = getVolumeLevel(currentVolume)
if (iconMode == MODE_NUM) {

val iconSize =
resources.getDimensionPixelSize(android.R.dimen.notification_large_icon_width)
val background = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888)

val sharedPref = getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE)
val textSizePref = sharedPref.getFloat(
PREF_NOTIFY_TEXT_SIZE, 0.0f
)

val paint = Paint().apply {
color = Color.WHITE
typeface = context.resources.getFont(R.font.ndot_45)
isFakeBoldText = true
isAntiAlias = true
}

val canvas = Canvas(background)
val canvasWidth = canvas.width
val canvasHeight = canvas.height

if (textSizePref == 0.0f) {

paint.getTextBounds(99.toString(), 0, 99.toString().length, textBounds)
val textWidth = textBounds.width()
val textHeight = textBounds.height()
val textSize = (canvasWidth / textWidth * textHeight).coerceAtMost(canvasHeight)
paint.textSize = textSize.toFloat()
with(sharedPref.edit()) {
putFloat(PREF_NOTIFY_TEXT_SIZE, textSize.toFloat())
}
} else {
paint.textSize = textSizePref
}

var textToDraw = currentVolume.toString()
if (currentVolume == 100) {
currentVolume--
textToDraw = "!!"
}
paint.getTextBounds(
currentVolume.toString(), 0,
currentVolume.toString().length, textBounds)
canvas.drawText(
textToDraw,
(canvasWidth - textBounds.width()) / 2f,
(canvasHeight + textBounds.height()) / 2f,
paint
)

return IconCompat.createWithBitmap(background)
val resourceId = resources.getIdentifier("num_$currentVolume", "drawable", context.packageName)
return IconCompat.createWithResource(this, resourceId)
}
else {
return IconCompat.createWithResource(context, volumeDrawableIds[currentVolumeLevel])
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/num_0.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="400dp"
android:height="400dp"
android:viewportWidth="400"
android:viewportHeight="400">
<path
android:pathData="M164.23,325C159.35,325 155.16,323.29 151.66,319.88C148.16,316.38 146.41,312.14 146.41,307.17C146.41,302.3 148.16,298.11 151.66,294.61C155.16,291.11 159.35,289.36 164.23,289.36C169.19,289.36 173.42,291.11 176.92,294.61C180.42,298.11 182.17,302.3 182.17,307.17C182.17,312.14 180.42,316.38 176.92,319.88C173.42,323.29 169.19,325 164.23,325ZM200,325C195.03,325 190.84,323.29 187.42,319.88C183.92,316.38 182.17,312.14 182.17,307.17C182.17,302.3 183.92,298.11 187.42,294.61C190.84,291.11 195.03,289.36 200,289.36C204.97,289.36 209.16,291.11 212.58,294.61C216.08,298.11 217.83,302.3 217.83,307.17C217.83,312.14 216.08,316.38 212.58,319.88C209.08,323.29 204.88,325 200,325ZM235.77,325C230.8,325 226.57,323.29 223.08,319.88C219.58,316.38 217.83,312.14 217.83,307.17C217.83,302.3 219.58,298.11 223.08,294.61C226.57,291.11 230.8,289.36 235.77,289.36C240.65,289.36 244.84,291.11 248.34,294.61C251.84,298.11 253.59,302.3 253.59,307.17C253.59,312.14 251.84,316.38 248.34,319.88C244.84,323.29 240.65,325 235.77,325ZM128.59,271.53C123.63,271.53 119.39,269.78 115.89,266.28C112.47,262.78 110.77,258.55 110.77,253.59C110.77,248.71 112.47,244.52 115.89,241.02C119.39,237.52 123.63,235.77 128.59,235.77C133.47,235.77 137.66,237.52 141.16,241.02C144.66,244.52 146.41,248.71 146.41,253.59C146.41,258.55 144.66,262.78 141.16,266.28C137.66,269.78 133.47,271.53 128.59,271.53ZM271.41,271.53C266.45,271.53 262.26,269.78 258.84,266.28C255.34,262.78 253.59,258.55 253.59,253.59C253.59,248.71 255.34,244.52 258.84,241.02C262.34,237.52 266.53,235.77 271.41,235.77C276.38,235.77 280.57,237.52 283.98,241.02C287.48,244.52 289.23,248.71 289.23,253.59C289.23,258.55 287.48,262.78 283.98,266.28C280.57,269.78 276.38,271.53 271.41,271.53ZM128.59,217.94C123.63,217.94 119.39,216.19 115.89,212.69C112.47,209.2 110.77,204.97 110.77,200C110.77,195.12 112.47,190.92 115.89,187.42C119.39,183.92 123.63,182.17 128.59,182.17C133.47,182.17 137.66,183.92 141.16,187.42C144.66,190.92 146.41,195.12 146.41,200C146.41,204.97 144.66,209.2 141.16,212.69C137.66,216.19 133.47,217.94 128.59,217.94ZM271.41,217.94C266.45,217.94 262.26,216.19 258.84,212.69C255.34,209.2 253.59,204.97 253.59,200C253.59,195.12 255.34,190.92 258.84,187.42C262.34,183.92 266.53,182.17 271.41,182.17C276.38,182.17 280.57,183.92 283.98,187.42C287.48,190.92 289.23,195.12 289.23,200C289.23,204.97 287.48,209.2 283.98,212.69C280.57,216.19 276.38,217.94 271.41,217.94ZM128.59,164.36C123.63,164.36 119.39,162.61 115.89,159.11C112.47,155.61 110.77,151.42 110.77,146.53C110.77,141.57 112.47,137.34 115.89,133.84C119.39,130.34 123.63,128.59 128.59,128.59C133.47,128.59 137.66,130.34 141.16,133.84C144.66,137.34 146.41,141.57 146.41,146.53C146.41,151.42 144.66,155.61 141.16,159.11C137.66,162.61 133.47,164.36 128.59,164.36ZM271.41,164.36C266.45,164.36 262.26,162.61 258.84,159.11C255.34,155.61 253.59,151.42 253.59,146.53C253.59,141.57 255.34,137.34 258.84,133.84C262.34,130.34 266.53,128.59 271.41,128.59C276.38,128.59 280.57,130.34 283.98,133.84C287.48,137.34 289.23,141.57 289.23,146.53C289.23,151.42 287.48,155.61 283.98,159.11C280.57,162.61 276.38,164.36 271.41,164.36ZM164.23,110.77C159.35,110.77 155.16,109.02 151.66,105.52C148.16,102.02 146.41,97.82 146.41,92.94C146.41,87.98 148.16,83.75 151.66,80.25C155.16,76.75 159.35,75 164.23,75C169.19,75 173.42,76.75 176.92,80.25C180.42,83.75 182.17,87.98 182.17,92.94C182.17,97.82 180.42,102.02 176.92,105.52C173.42,109.02 169.19,110.77 164.23,110.77ZM200,110.77C195.03,110.77 190.84,109.02 187.42,105.52C183.92,102.02 182.17,97.82 182.17,92.94C182.17,87.98 183.92,83.75 187.42,80.25C190.84,76.75 195.03,75 200,75C204.97,75 209.16,76.75 212.58,80.25C216.08,83.75 217.83,87.98 217.83,92.94C217.83,97.82 216.08,102.02 212.58,105.52C209.08,109.02 204.88,110.77 200,110.77ZM235.77,110.77C230.8,110.77 226.57,109.02 223.08,105.52C219.58,102.02 217.83,97.82 217.83,92.94C217.83,87.98 219.58,83.75 223.08,80.25C226.57,76.75 230.8,75 235.77,75C240.65,75 244.84,76.75 248.34,80.25C251.84,83.75 253.59,87.98 253.59,92.94C253.59,97.82 251.84,102.02 248.34,105.52C244.84,109.02 240.65,110.77 235.77,110.77ZM235.77,110.77"
android:fillColor="#000000"/>
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/num_1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="400dp"
android:height="400dp"
android:viewportWidth="400"
android:viewportHeight="400">
<path
android:pathData="M128.59,325C123.63,325 119.39,323.29 115.89,319.88C112.47,316.38 110.77,312.14 110.77,307.17C110.77,302.3 112.47,298.11 115.89,294.61C119.39,291.11 123.63,289.36 128.59,289.36C133.47,289.36 137.66,291.11 141.16,294.61C144.66,298.11 146.41,302.3 146.41,307.17C146.41,312.14 144.66,316.38 141.16,319.88C137.66,323.29 133.47,325 128.59,325ZM164.23,325C159.35,325 155.16,323.29 151.66,319.88C148.16,316.38 146.41,312.14 146.41,307.17C146.41,302.3 148.16,298.11 151.66,294.61C155.16,291.11 159.35,289.36 164.23,289.36C169.19,289.36 173.42,291.11 176.92,294.61C180.42,298.11 182.17,302.3 182.17,307.17C182.17,312.14 180.42,316.38 176.92,319.88C173.42,323.29 169.19,325 164.23,325ZM200,325C195.03,325 190.84,323.29 187.42,319.88C183.92,316.38 182.17,312.14 182.17,307.17C182.17,302.3 183.92,298.11 187.42,294.61C190.84,291.11 195.03,289.36 200,289.36C204.97,289.36 209.16,291.11 212.58,294.61C216.08,298.11 217.83,302.3 217.83,307.17C217.83,312.14 216.08,316.38 212.58,319.88C209.08,323.29 204.88,325 200,325ZM235.77,325C230.8,325 226.57,323.29 223.08,319.88C219.58,316.38 217.83,312.14 217.83,307.17C217.83,302.3 219.58,298.11 223.08,294.61C226.57,291.11 230.8,289.36 235.77,289.36C240.65,289.36 244.84,291.11 248.34,294.61C251.84,298.11 253.59,302.3 253.59,307.17C253.59,312.14 251.84,316.38 248.34,319.88C244.84,323.29 240.65,325 235.77,325ZM271.41,325C266.45,325 262.26,323.29 258.84,319.88C255.34,316.38 253.59,312.14 253.59,307.17C253.59,302.3 255.34,298.11 258.84,294.61C262.34,291.11 266.53,289.36 271.41,289.36C276.38,289.36 280.57,291.11 283.98,294.61C287.48,298.11 289.23,302.3 289.23,307.17C289.23,312.14 287.48,316.38 283.98,319.88C280.57,323.29 276.38,325 271.41,325ZM200,271.53C195.03,271.53 190.84,269.78 187.42,266.28C183.92,262.78 182.17,258.55 182.17,253.59C182.17,248.71 183.92,244.52 187.42,241.02C190.84,237.52 195.03,235.77 200,235.77C204.97,235.77 209.16,237.52 212.58,241.02C216.08,244.52 217.83,248.71 217.83,253.59C217.83,258.55 216.08,262.78 212.58,266.28C209.08,269.78 204.88,271.53 200,271.53ZM200,217.94C195.03,217.94 190.84,216.19 187.42,212.69C183.92,209.2 182.17,204.97 182.17,200C182.17,195.12 183.92,190.92 187.42,187.42C190.84,183.92 195.03,182.17 200,182.17C204.97,182.17 209.16,183.92 212.58,187.42C216.08,190.92 217.83,195.12 217.83,200C217.83,204.97 216.08,209.2 212.58,212.69C209.08,216.19 204.88,217.94 200,217.94ZM200,164.36C195.03,164.36 190.84,162.61 187.42,159.11C183.92,155.61 182.17,151.42 182.17,146.53C182.17,141.57 183.92,137.34 187.42,133.84C190.84,130.34 195.03,128.59 200,128.59C204.97,128.59 209.16,130.34 212.58,133.84C216.08,137.34 217.83,141.57 217.83,146.53C217.83,151.42 216.08,155.61 212.58,159.11C209.08,162.61 204.88,164.36 200,164.36ZM164.23,110.77C159.35,110.77 155.16,109.02 151.66,105.52C148.16,102.02 146.41,97.82 146.41,92.94C146.41,87.98 148.16,83.75 151.66,80.25C155.16,76.75 159.35,75 164.23,75C169.19,75 173.42,76.75 176.92,80.25C180.42,83.75 182.17,87.98 182.17,92.94C182.17,97.82 180.42,102.02 176.92,105.52C173.42,109.02 169.19,110.77 164.23,110.77ZM200,110.77C195.03,110.77 190.84,109.02 187.42,105.52C183.92,102.02 182.17,97.82 182.17,92.94C182.17,87.98 183.92,83.75 187.42,80.25C190.84,76.75 195.03,75 200,75C204.97,75 209.16,76.75 212.58,80.25C216.08,83.75 217.83,87.98 217.83,92.94C217.83,97.82 216.08,102.02 212.58,105.52C209.08,109.02 204.88,110.77 200,110.77ZM200,110.77"
android:fillColor="#000000"/>
</vector>
Loading

0 comments on commit 8b5d917

Please sign in to comment.