mirror of
				https://github.com/mihonapp/mihon.git
				synced 2025-10-31 22:37:56 +01:00 
			
		
		
		
	
				
					committed by
					
						 GitHub
						GitHub
					
				
			
			
				
	
			
			
			
						parent
						
							2241a0b2de
						
					
				
				
					commit
					7fdd2cacd7
				
			| @@ -3,7 +3,6 @@ package eu.kanade.tachiyomi.data.updater | ||||
| import android.app.IntentService | ||||
| import android.content.Context | ||||
| import android.content.Intent | ||||
| import android.net.Uri | ||||
| import android.support.v4.app.NotificationCompat | ||||
| import eu.kanade.tachiyomi.Constants.NOTIFICATION_UPDATER_ID | ||||
| import eu.kanade.tachiyomi.R | ||||
| @@ -36,21 +35,6 @@ class UpdateDownloaderService : IntentService(UpdateDownloaderService::class.jav | ||||
|             } | ||||
|             context.startService(intent) | ||||
|         } | ||||
|  | ||||
|         /** | ||||
|          * Prompt user with apk install intent | ||||
|          * @param context context | ||||
|          * @param file file of apk that is installed | ||||
|          */ | ||||
|         fun installAPK(context: Context, file: File) { | ||||
|             // Prompt install interface | ||||
|             val intent = Intent(Intent.ACTION_VIEW).apply { | ||||
|                 setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive") | ||||
|                 // Without this flag android returned a intent error! | ||||
|                 flags = Intent.FLAG_ACTIVITY_NEW_TASK | ||||
|             } | ||||
|             context.startActivity(intent) | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /** | ||||
| @@ -106,7 +90,7 @@ class UpdateDownloaderService : IntentService(UpdateDownloaderService::class.jav | ||||
|                 throw Exception("Unsuccessful response") | ||||
|             } | ||||
|  | ||||
|             val installIntent = UpdateNotificationReceiver.installApkIntent(ctx, apkFile.absolutePath) | ||||
|             val installIntent = UpdateNotificationReceiver.installApkIntent(ctx, apkFile) | ||||
|  | ||||
|             // Prompt the user to install the new update. | ||||
|             NotificationCompat.Builder(this).update { | ||||
|   | ||||
| @@ -4,6 +4,10 @@ import android.app.PendingIntent | ||||
| import android.content.BroadcastReceiver | ||||
| import android.content.Context | ||||
| import android.content.Intent | ||||
| import android.net.Uri | ||||
| import android.os.Build | ||||
| import android.support.v4.content.FileProvider | ||||
| import eu.kanade.tachiyomi.BuildConfig | ||||
| import eu.kanade.tachiyomi.Constants.NOTIFICATION_UPDATER_ID | ||||
| import eu.kanade.tachiyomi.util.notificationManager | ||||
| import java.io.File | ||||
| @@ -12,34 +16,14 @@ class UpdateNotificationReceiver : BroadcastReceiver() { | ||||
|  | ||||
|     override fun onReceive(context: Context, intent: Intent) { | ||||
|         when (intent.action) { | ||||
|             ACTION_INSTALL_APK -> { | ||||
|                 UpdateDownloaderService.installAPK(context, | ||||
|                         File(intent.getStringExtra(EXTRA_FILE_LOCATION))) | ||||
|                 cancelNotification(context) | ||||
|             } | ||||
|             ACTION_DOWNLOAD_UPDATE -> UpdateDownloaderService.downloadUpdate(context, | ||||
|                     intent.getStringExtra(UpdateDownloaderService.EXTRA_DOWNLOAD_URL)) | ||||
|             ACTION_CANCEL_NOTIFICATION -> cancelNotification(context) | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     fun cancelNotification(context: Context) { | ||||
|         context.notificationManager.cancel(NOTIFICATION_UPDATER_ID) | ||||
|     } | ||||
|  | ||||
|     companion object { | ||||
|         // Install apk action | ||||
|         const val ACTION_INSTALL_APK = "eu.kanade.INSTALL_APK" | ||||
|  | ||||
|         // Download apk action | ||||
|         const val ACTION_DOWNLOAD_UPDATE = "eu.kanade.RETRY_DOWNLOAD" | ||||
|  | ||||
|         // Cancel notification action | ||||
|         const val ACTION_CANCEL_NOTIFICATION = "eu.kanade.CANCEL_NOTIFICATION" | ||||
|  | ||||
|         // Absolute path of apk file | ||||
|         const val EXTRA_FILE_LOCATION = "file_location" | ||||
|  | ||||
|         fun cancelNotificationIntent(context: Context): PendingIntent { | ||||
|             val intent = Intent(context, UpdateNotificationReceiver::class.java).apply { | ||||
|                 action = ACTION_CANCEL_NOTIFICATION | ||||
| @@ -47,20 +31,39 @@ class UpdateNotificationReceiver : BroadcastReceiver() { | ||||
|             return PendingIntent.getBroadcast(context, 0, intent, 0) | ||||
|         } | ||||
|  | ||||
|         fun installApkIntent(context: Context, path: String): PendingIntent { | ||||
|             val intent = Intent(context, UpdateNotificationReceiver::class.java).apply { | ||||
|                 action = ACTION_INSTALL_APK | ||||
|                 putExtra(EXTRA_FILE_LOCATION, path) | ||||
|         /** | ||||
|          * Prompt user with apk install intent | ||||
|          * | ||||
|          * @param context context | ||||
|          * @param file file of apk that is installed | ||||
|          */ | ||||
|         fun installApkIntent(context: Context, file: File): PendingIntent { | ||||
|             val intent = Intent(Intent.ACTION_VIEW).apply { | ||||
|                 val uri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) | ||||
|                     FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file) | ||||
|                 else Uri.fromFile(file) | ||||
|                 setDataAndType(uri, "application/vnd.android.package-archive") | ||||
|                 flags = Intent.FLAG_GRANT_READ_URI_PERMISSION | ||||
|             } | ||||
|             return PendingIntent.getBroadcast(context, 0, intent, 0) | ||||
|             cancelNotification(context) | ||||
|             return PendingIntent.getActivity(context, 0, intent, 0) | ||||
|         } | ||||
|  | ||||
|         /** | ||||
|          * Downloads a new update and let the user install the new version from a notification. | ||||
|          * | ||||
|          * @param context the application context. | ||||
|          * @param url the url to the new update. | ||||
|          */ | ||||
|         fun downloadApkIntent(context: Context, url: String): PendingIntent { | ||||
|             val intent = Intent(context, UpdateNotificationReceiver::class.java).apply { | ||||
|                 action = ACTION_DOWNLOAD_UPDATE | ||||
|             val intent = Intent(context, UpdateDownloaderService::class.java).apply { | ||||
|                 putExtra(UpdateDownloaderService.EXTRA_DOWNLOAD_URL, url) | ||||
|             } | ||||
|             return PendingIntent.getBroadcast(context, 0, intent, 0) | ||||
|             return PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) | ||||
|         } | ||||
|  | ||||
|         fun cancelNotification(context: Context) { | ||||
|             context.notificationManager.cancel(NOTIFICATION_UPDATER_ID) | ||||
|         } | ||||
|     } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user