Skip to content

Commit 3722737

Browse files
committed
新增唤醒锁功能 & 去除代码警告
1 parent fe38967 commit 3722737

File tree

6 files changed

+87
-17
lines changed

6 files changed

+87
-17
lines changed

.idea/misc.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/AndroidManifest.xml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,23 @@
33
xmlns:tools="http://schemas.android.com/tools"
44
package="com.github.jing332.tts_server_android">
55

6-
<uses-permission android:name="android.permission.INTERNET"/>
7-
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
6+
<uses-permission android:name="android.permission.INTERNET" />
7+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
88
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
9+
<uses-permission android:name="android.permission.WAKE_LOCK" />
910

1011
<application
1112
android:allowBackup="true"
1213
android:icon="@mipmap/ic_launcher"
13-
android:roundIcon="@mipmap/ic_launcher_round"
1414
android:label="@string/app_name"
15+
android:roundIcon="@mipmap/ic_launcher_round"
1516
android:supportsRtl="true"
1617
android:theme="@style/Theme.TtsServer">
1718
<activity
1819
android:name="com.github.jing332.tts_server_android.MainActivity"
19-
android:screenOrientation="portrait"
20+
android:exported="true"
2021
android:launchMode="singleTop"
21-
android:exported="true">
22+
android:screenOrientation="portrait">
2223
<intent-filter>
2324
<action android:name="android.intent.action.MAIN" />
2425

@@ -32,10 +33,11 @@
3233
android:exported="false"
3334
tools:ignore="Instantiatable" />
3435

35-
<receiver android:name=".TtsIntentService$Receiver"
36+
<receiver
37+
android:name=".TtsIntentService$Receiver"
3638
android:exported="false">
3739
<intent-filter>
38-
<action android:name="quit_action"/>
40+
<action android:name="quit_action" />
3941
</intent-filter>
4042
</receiver>
4143
</application>

app/src/main/java/com/github/jing332/tts_server_android/MainActivity.kt

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import android.provider.Settings
1313
import android.text.Html
1414
import android.text.method.LinkMovementMethod
1515
import android.view.*
16-
import android.widget.*
16+
import android.widget.Button
17+
import android.widget.EditText
18+
import android.widget.TextView
19+
import android.widget.Toast
1720
import androidx.appcompat.app.AlertDialog
1821
import androidx.appcompat.app.AppCompatActivity
1922
import androidx.recyclerview.widget.LinearLayoutManager
@@ -121,6 +124,17 @@ class MainActivity : AppCompatActivity() {
121124
return true
122125
}
123126

127+
/* 准备菜单 */
128+
override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
129+
super.onPrepareOptionsMenu(menu)
130+
val item = menu?.findItem(R.id.menu_wakeLock)
131+
item?.isCheckable = true /* 设置{唤醒锁}菜单为可选中的 */
132+
/* 从配置文件读取并更新isWakeLock */
133+
TtsIntentService.isWakeLock = SharedPrefsUtils.getWakeLock(this)
134+
item?.isChecked = TtsIntentService.isWakeLock
135+
return true
136+
}
137+
124138
/*菜单点击事件*/
125139
@SuppressLint("BatteryLife")
126140
override fun onOptionsItemSelected(item: MenuItem): Boolean {
@@ -181,6 +195,13 @@ class MainActivity : AppCompatActivity() {
181195
}
182196
true
183197
}
198+
R.id.menu_wakeLock -> { /* 唤醒锁 */
199+
item.isChecked = !item.isChecked /* 更新选中状态 */
200+
TtsIntentService.isWakeLock = item.isChecked
201+
SharedPrefsUtils.setWakeLock(this, item.isChecked)
202+
Toast.makeText(this, "${item.isChecked} 重启服务以生效", Toast.LENGTH_SHORT).show()
203+
true
204+
}
184205

185206
else -> super.onOptionsItemSelected(item)
186207
}
@@ -221,6 +242,4 @@ class MainActivity : AppCompatActivity() {
221242
btnClose.isEnabled = true
222243
}
223244
}
224-
225-
226245
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.github.jing332.tts_server_android
2+
3+
import android.content.Context
4+
5+
class SharedPrefsUtils {
6+
companion object {
7+
@JvmStatic
8+
fun getWakeLock(ctx: Context): Boolean {
9+
val pref = ctx.getSharedPreferences("config", Context.MODE_PRIVATE)
10+
return pref.getBoolean("wakeLock", false)
11+
}
12+
13+
@JvmStatic
14+
fun setWakeLock(ctx: Context, isWakeLock: Boolean) {
15+
val editor = ctx.getSharedPreferences("config", Context.MODE_PRIVATE).edit()
16+
editor.putBoolean("wakeLock", isWakeLock)
17+
editor.apply()
18+
}
19+
20+
}
21+
22+
23+
}

app/src/main/java/com/github/jing332/tts_server_android/TtsIntentService.kt

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
package com.github.jing332.tts_server_android
22

3+
import android.annotation.SuppressLint
34
import android.app.*
45
import android.content.BroadcastReceiver
56
import android.content.Context
67
import android.content.Intent
78
import android.graphics.Color
89
import android.os.Build
10+
import android.os.PowerManager
911
import android.util.Log
1012
import android.widget.Toast
1113
import tts_server_lib.LogCallback
1214
import tts_server_lib.Tts_server_lib
1315

16+
1417
class TtsIntentService(name: String = "TtsIntentService") : IntentService(name) {
1518
companion object {
19+
const val TAG = "TtsIntentService"
1620
var ACTION_SEND = "service.send" /*广播ID*/
21+
var isWakeLock = false
1722
var IsRunning = false /*服务是否在运行*/
1823
var Isinited = false /*已经初始化GoLib*/
1924
var port: Int = 1233 /*监听端口*/
@@ -29,6 +34,10 @@ class TtsIntentService(name: String = "TtsIntentService") : IntentService(name)
2934
}
3035
}
3136

37+
private lateinit var mWakeLock: PowerManager.WakeLock /* 唤醒锁 */
38+
39+
@Deprecated("Deprecated in Java")
40+
@SuppressLint("WakelockTimeout")
3241
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
3342
IsRunning = true
3443
port = intent?.getIntExtra("port", 1233)!!
@@ -62,7 +71,7 @@ class TtsIntentService(name: String = "TtsIntentService") : IntentService(name)
6271
val service = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
6372
service.createNotificationChannel(chan)
6473

65-
var builder = Notification.Builder(applicationContext, chanId)
74+
val builder = Notification.Builder(applicationContext, chanId)
6675
notification =
6776
builder
6877
.setContentTitle("TTS Server正在运行中...")
@@ -73,8 +82,8 @@ class TtsIntentService(name: String = "TtsIntentService") : IntentService(name)
7382
.build()
7483

7584
} else { /*SDK < Android 8*/
76-
var action = Notification.Action(0, "退出", closePendingIntent)
77-
var builder = Notification.Builder(applicationContext)
85+
val action = Notification.Action(0, "退出", closePendingIntent)
86+
val builder = Notification.Builder(applicationContext)
7887
notification = builder
7988
.setContentTitle("TTS Server正在运行中...")
8089
.setContentText("监听地址: localhost:$port")
@@ -85,21 +94,35 @@ class TtsIntentService(name: String = "TtsIntentService") : IntentService(name)
8594
}
8695
startForeground(1, notification) //启动前台服务
8796

97+
if (isWakeLock) { /* 启动唤醒锁 */
98+
val powerManager = getSystemService(POWER_SERVICE) as PowerManager
99+
mWakeLock = powerManager.newWakeLock(
100+
PowerManager.PARTIAL_WAKE_LOCK,
101+
"tts_server:ttsTag"
102+
)
103+
mWakeLock.acquire()
104+
}
105+
88106
return super.onStartCommand(intent, flags, startId)
89107
}
90108

109+
@Deprecated("Deprecated in Java")
91110
override fun onDestroy() {
92111
IsRunning = false
112+
if (isWakeLock) { /* 释放唤醒锁 */
113+
mWakeLock.release()
114+
}
93115
super.onDestroy()
94116
}
95117

118+
@Deprecated("Deprecated in Java")
96119
override fun onHandleIntent(intent: Intent?) {
97120
if (!Isinited) { /*初始化Go: 设置日志转发,注册Http.Server*/
98121
Tts_server_lib.init()
99122
Isinited = true
100123
}
101124
/*来自Go的日志*/
102-
var cb = LogCallback { s ->
125+
val cb = LogCallback { s ->
103126
Log.d("LogCallback", s)
104127
sendLog(s)
105128
}
@@ -110,14 +133,14 @@ class TtsIntentService(name: String = "TtsIntentService") : IntentService(name)
110133

111134
//发送日志给MainActivity
112135
private fun sendLog(msg: String) {
113-
var i = Intent(ACTION_SEND)
136+
val i = Intent(ACTION_SEND)
114137
i.putExtra("sendLog", msg)
115138
sendBroadcast(i)
116139
}
117140

118141
//发送关闭消息给MainActivity
119142
private fun sendClosedMsg() {
120-
var i = Intent(ACTION_SEND)
143+
val i = Intent(ACTION_SEND)
121144
i.putExtra("isClosed", true)
122145
sendBroadcast(i)
123146
}
@@ -128,5 +151,4 @@ class TtsIntentService(name: String = "TtsIntentService") : IntentService(name)
128151
closeServer(ctx!!)
129152
}
130153
}
131-
132154
}

app/src/main/res/menu/menu_main.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
<item
99
android:id="@+id/menu_checkUpdate"
1010
android:title="检查更新" />
11+
<item
12+
android:id="@+id/menu_wakeLock"
13+
android:title="唤醒锁" />
1114
<item
1215
android:id="@+id/menu_killBattery"
1316
android:title="电池优化" />

0 commit comments

Comments
 (0)