-
Notifications
You must be signed in to change notification settings - Fork 491
Expand file tree
/
Copy pathVersionService.kt
More file actions
325 lines (282 loc) · 11.1 KB
/
VersionService.kt
File metadata and controls
325 lines (282 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package com.allenliu.versionchecklib.v2.ui
import android.app.Service
import android.content.Context
import android.content.Intent
import android.content.pm.ServiceInfo
import android.os.Build
import android.os.IBinder
import androidx.annotation.WorkerThread
import com.allenliu.versionchecklib.R
import com.allenliu.versionchecklib.core.http.AllenHttp
import com.allenliu.versionchecklib.utils.ALog
import com.allenliu.versionchecklib.utils.AllenEventBusUtil
import com.allenliu.versionchecklib.utils.AppUtils
import com.allenliu.versionchecklib.v2.AllenVersionChecker
import com.allenliu.versionchecklib.v2.builder.BuilderManager
import com.allenliu.versionchecklib.v2.builder.DownloadBuilder
import com.allenliu.versionchecklib.v2.callback.DownloadListenerKt
import com.allenliu.versionchecklib.v2.eventbus.AllenEventType
import com.allenliu.versionchecklib.v2.eventbus.CommonEvent
import com.allenliu.versionchecklib.v2.net.DownloadMangerV2
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.io.File
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
class VersionService : Service() {
private var notificationHelper: NotificationHelper? = null
private var isServiceAlive = false
private var executors: ExecutorService? = null
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
if (!EventBus.getDefault().isRegistered(this)) {
EventBus.getDefault().register(this)
}
ALog.e("version service create")
init()
return START_REDELIVER_INTENT
}
companion object {
fun enqueueWork(context: Context, builder: DownloadBuilder) {
//清除之前的任务,如果有
// AllenVersionChecker.getInstance().cancelAllMission()
BuilderManager.init(context, builder)
val intent = Intent(context, VersionService::class.java)
//显示通知栏的情况 才设置为前台服务
if (builder.isRunOnForegroundService && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent)
} else {
context.startService(intent)
}
}
}
override fun onDestroy() {
super.onDestroy()
ALog.e("version service destroy")
BuilderManager.doWhenNotNull {
if (isRunOnForegroundService) {
stopForeground(true)
}
}
BuilderManager.destroy()
notificationHelper?.onDestroy()
isServiceAlive = false
executors?.shutdown()
AllenHttp.getHttpClient().dispatcher.cancelAll()
if (EventBus.getDefault().isRegistered(this)) {
EventBus.getDefault().unregister(this)
}
}
override fun onBind(intent: Intent): IBinder? {
return null
}
private fun onHandleWork() {
downloadAPK()
}
private fun downloadAPK() {
BuilderManager.doWhenNotNull {
if (versionBundle != null) {
if (isDirectDownload) {
startDownloadApk()
} else {
if (isSilentDownload) {
startDownloadApk()
} else {
showVersionDialog()
}
}
}
}
}
/**
* 开启UI展示界面
*/
private fun showVersionDialog() {
BuilderManager.doWhenNotNull {
val intent = Intent(this@VersionService, UIActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
}
}
private fun showDownloadingDialog() {
BuilderManager.doWhenNotNull {
if (isShowDownloadingDialog) {
val intent = Intent(this@VersionService, DownloadingActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
}
}
}
private fun updateDownloadingDialogProgress(progress: Int) {
val commonEvent: CommonEvent<Any> = CommonEvent()
commonEvent.eventType = AllenEventType.UPDATE_DOWNLOADING_PROGRESS
commonEvent.data = progress
commonEvent.isSuccessful = true
EventBus.getDefault().post(commonEvent)
}
private fun showDownloadFailedDialog() {
BuilderManager.doWhenNotNull {
val intent = Intent(this@VersionService, DownloadFailedActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
}
}
private fun requestPermissionAndDownload() {
//不再请求权限
startDownloadApk()
// BuilderManager.doWhenNotNull {
// val intent = Intent(this@VersionService, PermissionDialogActivity::class.java)
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
// startActivity(intent)
// }
}
private fun install() {
BuilderManager.doWhenNotNull {
AllenEventBusUtil.sendEventBus(AllenEventType.DOWNLOAD_COMPLETE)
val downloadPath = downloadFilePath
if (isSilentDownload) {
showVersionDialog()
} else {
AppUtils.installApk(applicationContext, File(downloadPath), customInstallListener)
BuilderManager.checkForceUpdate()
}
}
}
private val downloadFilePath: String
get() {
return BuilderManager.doWhenNotNull {
downloadAPKPath + getString(
R.string.versionchecklib_download_apkname,
if (apkName != null) apkName else packageName
)
} ?: ""
}
@WorkerThread
private fun startDownloadApk() {
//判断是否缓存并且是否强制重新下载
fun inner() {
BuilderManager.doWhenNotNull {
val downloadPath = downloadFilePath
if (AppUtils.checkAPKIsExists(
applicationContext,
downloadPath,
newestVersionCode
) && !isForceRedownload
) {
ALog.e("using cache")
install()
return@doWhenNotNull
}
BuilderManager.checkAndDeleteAPK()
var downloadUrl: String? = downloadUrl
if (downloadUrl == null && versionBundle != null) {
downloadUrl = versionBundle.downloadUrl
}
if (downloadUrl == null) {
AllenVersionChecker.getInstance().cancelAllMission()
throw RuntimeException("you must set a download url for download function using")
}
ALog.e("downloadPath:$downloadPath")
DownloadMangerV2.download(
downloadUrl,
downloadAPKPath,
getString(
R.string.versionchecklib_download_apkname,
if (apkName != null) apkName else packageName
),
downloadListener
)
}
}
executors?.submit { inner() }
}
private val downloadListener: DownloadListenerKt = object : DownloadListenerKt {
override fun onCheckerDownloading(progress: Int) {
BuilderManager.doWhenNotNull {
ALog.e("download progress $progress")
if (isServiceAlive) {
if (!isSilentDownload) {
if (isShowNotification)
notificationHelper?.updateNotification(progress)
updateDownloadingDialogProgress(progress)
}
apkDownloadListener?.onDownloading(progress)
}
}
}
override fun onCheckerDownloadSuccess(file: File) {
BuilderManager.doWhenNotNull {
if (isServiceAlive) {
if (!isSilentDownload && isShowNotification) notificationHelper?.showDownloadCompleteNotifcation(
file
)
apkDownloadListener?.onDownloadSuccess(file)
install()
}
}
}
override fun onCheckerDownloadFail() {
BuilderManager.doWhenNotNull {
ALog.e("download failed")
if (!isServiceAlive) return@doWhenNotNull
apkDownloadListener?.onDownloadFail()
if (!isSilentDownload) {
AllenEventBusUtil.sendEventBusStick(AllenEventType.CLOSE_DOWNLOADING_ACTIVITY)
if (isShowDownloadFailDialog) {
showDownloadFailedDialog()
}
if (isShowNotification)
notificationHelper?.showDownloadFailedNotification()
} else {
AllenVersionChecker.getInstance().cancelAllMission()
}
}
}
override fun onCheckerStartDownload() {
BuilderManager.doWhenNotNull {
ALog.e("start download apk")
if (!isSilentDownload) {
if (isShowNotification)
notificationHelper?.showNotification()
showDownloadingDialog()
}
}
}
}
@Subscribe(threadMode = ThreadMode.MAIN)
fun receiveEvent(commonEvent: CommonEvent<*>) {
when (commonEvent.eventType) {
AllenEventType.START_DOWNLOAD_APK -> startDownloadApk()
AllenEventType.STOP_SERVICE -> {
stopSelf()
EventBus.getDefault().removeStickyEvent(commonEvent)
}
}
}
// @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
// public synchronized void onReceiveDownloadBuilder(DownloadBuilder downloadBuilder) {
// builder = downloadBuilder;
// init();
// EventBus.getDefault().removeStickyEvent(downloadBuilder);
// }
private fun init() {
BuilderManager.doWhenNotNull {
//https://issuetracker.google.com/issues/76112072
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && isRunOnForegroundService) {
val notification = NotificationHelper.createSimpleNotification(this@VersionService)
val notificationId = NotificationHelper.NOTIFICATION_ID
// 指定服务类型
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startForeground(notificationId, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC)
} else {
startForeground(notificationId, notification)
}
Thread.sleep(500)
}
isServiceAlive = true
notificationHelper = NotificationHelper(applicationContext)
executors = Executors.newSingleThreadExecutor()
executors?.submit { onHandleWork() }
}
}
}