Skip to content

Commit

Permalink
Merge pull request #19 from Steve-Mr/dev
Browse files Browse the repository at this point in the history
Target SDK 34
  • Loading branch information
Steve-Mr authored May 2, 2024
2 parents ebdfdb9 + 4b0d046 commit 0d7d5f8
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 57 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ keystoreProperties.load(new FileInputStream(keystorePropertiesFile))

android {
namespace 'com.maary.liveinpeace'
compileSdk 33
compileSdk 34

defaultConfig {
applicationId "com.maary.liveinpeace"
minSdk 31
targetSdk 33
targetSdk 34
versionCode 4
versionName "2.1_beta"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE"/>
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>

<application
android:name=".ConnectionsApplication"
Expand All @@ -22,7 +24,8 @@
<activity android:name=".HistoryActivity" android:theme="@style/Theme.LiveInPeace"/>

<service android:name=".service.ForegroundService"
android:exported="false">
android:exported="false"
android:foregroundServiceType="specialUse">
<intent-filter>
<action android:name="android.media.VOLUME_CHANGED_ACTION"/>
</intent-filter>
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/com/maary/liveinpeace/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ class Constants {
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"
const val PREF_WELCOME_FINISHED = "welcome_finished"
// 设置通知 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
const val ID_NOTIFICATION_WELCOME = 0
// 设置图像式图标 Action
const val ACTION_NAME_SET_IMG = "com.maary.liveinpeace.receiver.SettingsReceiver.SetIconImg"
// 设置字符式图标 Action
Expand All @@ -45,6 +47,7 @@ class Constants {
const val CHANNEL_ID_SETTINGS = "LIP_SETTINGS"
const val CHANNEL_ID_ALERT = "LIP_ALERT"
const val CHANNEL_ID_PROTECT = "LIP_PROTECT"
const val CHANNEL_ID_WELCOME = "LIP_WELCOME"
// 提醒时间
const val ALERT_TIME: Long = 2*60*60*1000
// 延后时间
Expand Down
168 changes: 124 additions & 44 deletions app/src/main/java/com/maary/liveinpeace/service/QSTileService.kt
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
package com.maary.liveinpeace.service

import android.Manifest
import android.annotation.SuppressLint
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.graphics.drawable.Icon
import android.net.Uri
import android.os.Build
import android.os.PowerManager
import android.provider.Settings
import android.service.quicksettings.Tile
import android.service.quicksettings.TileService
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import androidx.core.content.edit
import com.maary.liveinpeace.Constants
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.CHANNEL_ID_WELCOME
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.ID_NOTIFICATION_WELCOME
import com.maary.liveinpeace.Constants.Companion.PREF_WELCOME_FINISHED
import com.maary.liveinpeace.Constants.Companion.REQUESTING_WAIT_MILLIS
import com.maary.liveinpeace.Constants.Companion.SHARED_PREF
import com.maary.liveinpeace.R

class QSTileService: TileService() {
Expand All @@ -33,6 +46,54 @@ class QSTileService: TileService() {
val tile = qsTile
var waitMillis = REQUESTING_WAIT_MILLIS

val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (notificationManager.getNotificationChannel(CHANNEL_ID_DEFAULT) == null){
createNotificationChannel(
NotificationManager.IMPORTANCE_MIN,
CHANNEL_ID_DEFAULT,
resources.getString(R.string.default_channel),
resources.getString(R.string.default_channel_description)
)
}
if (notificationManager.getNotificationChannel(CHANNEL_ID_SETTINGS) == null) {
createNotificationChannel(
NotificationManager.IMPORTANCE_MIN,
CHANNEL_ID_SETTINGS,
resources.getString(R.string.channel_settings),
resources.getString(R.string.settings_channel_description)
)
}
if (notificationManager.getNotificationChannel(CHANNEL_ID_ALERT) == null) {
createNotificationChannel(
NotificationManager.IMPORTANCE_HIGH,
CHANNEL_ID_ALERT,
resources.getString(R.string.channel_alert),
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)
}
if (notificationManager.getNotificationChannel(CHANNEL_ID_WELCOME) == null){
createNotificationChannel(
NotificationManager.IMPORTANCE_MIN,
CHANNEL_ID_WELCOME,
resources.getString(R.string.welcome_channel),
resources.getString(R.string.welcome_channel_description)
)
}

val powerManager = getSystemService(Context.POWER_SERVICE) as PowerManager

while(ActivityCompat.checkSelfPermission(
applicationContext,
Manifest.permission.POST_NOTIFICATIONS
Expand All @@ -44,49 +105,27 @@ class QSTileService: TileService() {
waitMillis *= 2
}

val sharedPref = getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE)
while (!sharedPref.getBoolean(PREF_WELCOME_FINISHED, false)){
if ( powerManager.isIgnoringBatteryOptimizations(packageName) &&
ActivityCompat.checkSelfPermission(
applicationContext, Manifest.permission.POST_NOTIFICATIONS
) == PackageManager.PERMISSION_GRANTED) {
sharedPref.edit {
putBoolean(PREF_WELCOME_FINISHED, true)
}
break
} else {
createWelcomeNotification()
Thread.sleep(waitMillis.toLong())
waitMillis *= 2
}
}


val intent = Intent(this, ForegroundService::class.java)

if (!ForegroundService.isForegroundServiceRunning()){

val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (notificationManager.getNotificationChannel(CHANNEL_ID_DEFAULT) == null){
createNotificationChannel(
NotificationManager.IMPORTANCE_MIN,
CHANNEL_ID_DEFAULT,
resources.getString(R.string.default_channel),
resources.getString(R.string.default_channel_description)
)
}
if (notificationManager.getNotificationChannel(CHANNEL_ID_SETTINGS) == null) {
createNotificationChannel(
NotificationManager.IMPORTANCE_MIN,
CHANNEL_ID_SETTINGS,
resources.getString(R.string.channel_settings),
resources.getString(R.string.settings_channel_description)
)
}
if (notificationManager.getNotificationChannel(CHANNEL_ID_ALERT) == null) {
createNotificationChannel(
NotificationManager.IMPORTANCE_HIGH,
CHANNEL_ID_ALERT,
resources.getString(R.string.channel_alert),
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
tile.icon = Icon.createWithResource(this, R.drawable.icon_qs_one)
Expand All @@ -101,11 +140,12 @@ class QSTileService: TileService() {
tile.updateTile()
}

@RequiresApi(Build.VERSION_CODES.TIRAMISU)
override fun onStartListening() {
super.onStartListening()
val intentFilter = IntentFilter()
intentFilter.addAction(BROADCAST_ACTION_FOREGROUND)
registerReceiver(foregroundServiceReceiver, intentFilter)
registerReceiver(foregroundServiceReceiver, intentFilter, RECEIVER_NOT_EXPORTED)
}

override fun onStopListening() {
Expand Down Expand Up @@ -149,8 +189,48 @@ class QSTileService: TileService() {
notificationManager.createNotificationChannel(channel)
}

private fun requestNotificationsPermission() = Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
putExtra(Settings.EXTRA_APP_PACKAGE, packageName)
}.let(::startActivityAndCollapse)
private fun requestNotificationsPermission() {
val intent = Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
putExtra(Settings.EXTRA_APP_PACKAGE, packageName)
}
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
startActivityAndCollapse(pendingIntent)
}
}

@SuppressLint("BatteryLife")
private fun createWelcomeNotification() {
Log.v("MUTE_", "CREATING WELCOME")
val welcome = NotificationCompat.Builder(this, CHANNEL_ID_WELCOME)
.setOngoing(true)
.setSmallIcon(R.drawable.ic_baseline_settings_24)
.setShowWhen(false)
.setContentTitle(getString(R.string.welcome))
.setContentText(getString(R.string.welcome_description))
.setOnlyAlertOnce(true)
.setGroupSummary(false)
.setGroup(ID_NOTIFICATION_GROUP_SETTINGS)

val batteryIntent = Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
batteryIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.data = Uri.parse("package:$packageName")

val pendingBatteryIntent = PendingIntent.getActivity(this, 0, batteryIntent, PendingIntent.FLAG_IMMUTABLE)

val batteryAction = NotificationCompat.Action.Builder(
R.drawable.outline_battery_saver_24,
getString(R.string.request_permission_battery),
pendingBatteryIntent
).build()

welcome.addAction(batteryAction)

val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

notificationManager.notify(ID_NOTIFICATION_WELCOME, welcome.build())

}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/outline_battery_saver_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/white" android:pathData="M16,4h-2V2h-4v2H8C7.45,4 7,4.45 7,5v16c0,0.55 0.45,1 1,1h8c0.55,0 1,-0.45 1,-1V5C17,4.45 16.55,4 16,4zM15,14h-2v2h-2v-2H9v-2h2v-2h2v2h2V14z"/>

</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/outline_notifications_active_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/white" android:pathData="M12,22c1.1,0 2,-0.9 2,-2h-4c0,1.1 0.9,2 2,2zM18,16v-5c0,-3.07 -1.63,-5.64 -4.5,-6.32L13.5,4c0,-0.83 -0.67,-1.5 -1.5,-1.5s-1.5,0.67 -1.5,1.5v0.68C7.64,5.36 6,7.92 6,11v5l-2,2v1h16v-1l-2,-2zM16,17L8,17v-6c0,-2.48 1.51,-4.5 4,-4.5s4,2.02 4,4.5v6zM7.58,4.08L6.15,2.65C3.75,4.48 2.17,7.3 2.03,10.5h2c0.15,-2.65 1.51,-4.97 3.55,-6.42zM19.97,10.5h2c-0.15,-3.2 -1.73,-6.02 -4.12,-7.85l-1.42,1.43c2.02,1.45 3.39,3.77 3.54,6.42z"/>

</vector>
16 changes: 9 additions & 7 deletions app/src/main/res/layout/item_connection.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,25 @@
android:layout_width="48dp"
android:layout_height="48dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="@id/device_name"
app:layout_constraintBottom_toBottomOf="@id/device_connection_time"
android:contentDescription="@string/icon_of_device"
tools:src="@drawable/ic_headphone"
/>

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

tools:text="Headphone"/>
android:layout_marginEnd="16dp"
android:maxLines="2"
tools:text="HeadphoneHeadphoneHeadphoneHeadphoneHeadphone"/>

<ImageView
android:id="@+id/connection_time_prefix"
Expand All @@ -48,7 +50,7 @@
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_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/connection_time_prefix"
android:textSize="12sp"
tools:text="3hrs"/>
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,10 @@
<string name="ears_protected">保护了耳朵</string>
<string name="channel_protection">保护耳朵</string>
<string name="protection_channel_description">有新设备连接时自动将音量调节至 25 以下</string>
<string name="welcome_channel">初始设置</string>
<string name="welcome_channel_description">初次使用权限设置</string>
<string name="welcome">欢迎</string>
<string name="welcome_description">LiveInPeace 需要这些权限</string>
<string name="request_permission_battery">电池</string>
<string name="request_permission_notification">通知</string>
</resources>
6 changes: 6 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@
<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 name="welcome_channel">Initial Settings</string>
<string name="welcome_channel_description">Permission settings for first time use.</string>
<string name="welcome">Welcome</string>
<string name="welcome_description">LiveInPeace needs permissions to work.</string>
<string name="request_permission_battery">Battery</string>
<string name="request_permission_notification">Notification</string>
<string-array name="array_volume_comment">
<item>Shhhhhhh…</item>
<item>Perfectly Balanced, As All Things Should Be…</item>
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '8.0.2' apply false
id 'com.android.library' version '8.0.2' apply false
id 'com.android.application' version '8.3.1' apply false
id 'com.android.library' version '8.3.1' apply false
id 'org.jetbrains.kotlin.android' version '1.9.0-Beta' apply false
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Fri May 12 10:24:55 CST 2023
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

0 comments on commit 0d7d5f8

Please sign in to comment.