@@ -2,12 +2,14 @@ package com.yenaly.han1meviewer.worker
22
33import android.annotation.SuppressLint
44import android.app.Notification
5+ import android.app.PendingIntent
56import android.content.Context
7+ import android.content.Intent
68import android.content.pm.ServiceInfo
79import android.os.Build
8- import android.util.Log
910import androidx.core.app.NotificationCompat
1011import androidx.core.app.NotificationManagerCompat
12+ import androidx.core.content.FileProvider
1113import androidx.core.net.toFile
1214import androidx.core.net.toUri
1315import androidx.work.Constraints
@@ -21,6 +23,7 @@ import androidx.work.WorkManager
2123import androidx.work.WorkerParameters
2224import androidx.work.workDataOf
2325import com.yenaly.han1meviewer.EMPTY_STRING
26+ import com.yenaly.han1meviewer.FILE_PROVIDER_AUTHORITY
2427import com.yenaly.han1meviewer.Preferences
2528import com.yenaly.han1meviewer.R
2629import com.yenaly.han1meviewer.UPDATE_NOTIFICATION_CHANNEL
@@ -30,6 +33,7 @@ import com.yenaly.han1meviewer.util.installApkPackage
3033import com.yenaly.han1meviewer.util.runSuspendCatching
3134import com.yenaly.han1meviewer.util.updateFile
3235import com.yenaly.yenaly_libs.utils.showShortToast
36+ import java.io.File
3337import java.util.Locale
3438import kotlin.random.Random
3539
@@ -114,6 +118,7 @@ class HUpdateWorker(
114118 if (inject.isSuccess) {
115119 val outputData = workDataOf(UPDATE_APK to file.toUri().toString())
116120 Preferences .updateNodeId = nodeId
121+ showInstallNotification(file)
117122 return Result .success(outputData)
118123 } else {
119124 inject.exceptionOrNull()?.printStackTrace()
@@ -123,9 +128,9 @@ class HUpdateWorker(
123128 }
124129 }
125130
126- private fun createNotification (progress : Int = 0, fileSizeMB : String = "0", downloadedSizeMB : String = "0"): Notification {
131+ private fun createNotification (progress : Int = 0, fileSizeMB : String = "0", downloadedSizeMB : String = "0", isPending : Boolean = true ): Notification {
127132 return NotificationCompat .Builder (context, UPDATE_NOTIFICATION_CHANNEL )
128- .setSmallIcon(R .mipmap.ic_launcher )
133+ .setSmallIcon(R .drawable.baseline_h_24 )
129134 .setOngoing(true )
130135 .setContentTitle(
131136 context.getString(
@@ -134,7 +139,7 @@ class HUpdateWorker(
134139 )
135140 .setPriority(NotificationCompat .PRIORITY_HIGH )
136141 .setOnlyAlertOnce(true )
137- .setProgress(100 , progress, false )
142+ .setProgress(100 , progress, isPending )
138143 .build()
139144 }
140145
@@ -151,7 +156,8 @@ class HUpdateWorker(
151156 createNotification(
152157 progress,
153158 fileSizeMB,
154- downloadedSizeMB
159+ downloadedSizeMB,
160+ false
155161 )
156162 )
157163 }
@@ -160,10 +166,44 @@ class HUpdateWorker(
160166 private fun createForegroundInfo (progress : Int = 0): ForegroundInfo {
161167 return ForegroundInfo (
162168 downloadId,
163- createNotification(progress),
169+ createNotification(progress, isPending = false ),
164170 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) {
165171 ServiceInfo .FOREGROUND_SERVICE_TYPE_DATA_SYNC
166172 } else 0
167173 )
168174 }
175+ @SuppressLint(" MissingPermission" )
176+ private fun showInstallNotification (file : File ) {
177+ val installIntent = Intent (Intent .ACTION_VIEW ).apply {
178+ val uri = FileProvider .getUriForFile(
179+ context,
180+ FILE_PROVIDER_AUTHORITY ,
181+ file
182+ )
183+ setDataAndType(uri, " application/vnd.android.package-archive" )
184+ addFlags(Intent .FLAG_GRANT_READ_URI_PERMISSION )
185+ addFlags(Intent .FLAG_ACTIVITY_NEW_TASK )
186+ }
187+
188+ val pendingIntent = PendingIntent .getActivity(
189+ context, 0 , installIntent,
190+ PendingIntent .FLAG_UPDATE_CURRENT or PendingIntent .FLAG_IMMUTABLE
191+ )
192+
193+ val notification = NotificationCompat .Builder (context, UPDATE_NOTIFICATION_CHANNEL )
194+ .setContentTitle(context.getString(R .string.download_complete))
195+ .setContentText(context.getString(R .string.click_to_install_update))
196+ .setSmallIcon(R .drawable.baseline_h_24)
197+ .setAutoCancel(true )
198+ .setContentIntent(pendingIntent)
199+ .addAction(
200+ NotificationCompat .Action .Builder (
201+ R .drawable.baseline_h_24,
202+ context.getString(R .string.install),
203+ pendingIntent
204+ ).build()
205+ )
206+ .build()
207+ notificationManager.notify(downloadId + 1 , notification)
208+ }
169209}
0 commit comments