@@ -3,16 +3,18 @@ package org.schabi.newpipe
33import android.content.Context
44import android.content.Intent
55import android.util.Log
6+ import android.widget.Toast
67import androidx.core.app.NotificationCompat
78import androidx.core.app.NotificationManagerCompat
9+ import androidx.core.content.ContextCompat
810import androidx.core.content.edit
911import androidx.core.net.toUri
1012import androidx.preference.PreferenceManager
11- import androidx.work.OneTimeWorkRequest
13+ import androidx.work.OneTimeWorkRequestBuilder
1214import androidx.work.WorkManager
13- import androidx.work.WorkRequest
1415import androidx.work.Worker
1516import androidx.work.WorkerParameters
17+ import androidx.work.workDataOf
1618import com.grack.nanojson.JsonParser
1719import com.grack.nanojson.JsonParserException
1820import org.schabi.newpipe.extractor.downloader.Response
@@ -42,26 +44,40 @@ class NewVersionWorker(
4244 versionCode : Int
4345 ) {
4446 if (BuildConfig .VERSION_CODE >= versionCode) {
47+ if (inputData.getBoolean(IS_MANUAL , false )) {
48+ // Show toast stating that the app is up-to-date if the update check was manual.
49+ ContextCompat .getMainExecutor(applicationContext).execute {
50+ Toast .makeText(
51+ applicationContext, R .string.app_update_unavailable_toast,
52+ Toast .LENGTH_SHORT
53+ ).show()
54+ }
55+ }
4556 return
4657 }
47- val app = App .getApp()
4858
4959 // A pending intent to open the apk location url in the browser.
5060 val intent = Intent (Intent .ACTION_VIEW , apkLocationUrl?.toUri())
5161 intent.addFlags(Intent .FLAG_ACTIVITY_NEW_TASK )
52- val pendingIntent = PendingIntentCompat .getActivity(app, 0 , intent, 0 )
53- val channelId = app.getString(R .string.app_update_notification_channel_id)
54- val notificationBuilder = NotificationCompat .Builder (app, channelId)
62+ val pendingIntent = PendingIntentCompat .getActivity(
63+ applicationContext, 0 , intent, 0
64+ )
65+ val channelId = applicationContext.getString(R .string.app_update_notification_channel_id)
66+ val notificationBuilder = NotificationCompat .Builder (applicationContext, channelId)
5567 .setSmallIcon(R .drawable.ic_newpipe_update)
5668 .setVisibility(NotificationCompat .VISIBILITY_PUBLIC )
57- .setContentIntent(pendingIntent)
5869 .setAutoCancel(true )
59- .setContentTitle(app.getString(R .string.app_update_notification_content_title))
70+ .setContentIntent(pendingIntent)
71+ .setContentTitle(
72+ applicationContext.getString(R .string.app_update_available_notification_title)
73+ )
6074 .setContentText(
61- app.getString(R .string.app_update_notification_content_text) +
62- " " + versionName
75+ applicationContext.getString(
76+ R .string.app_update_available_notification_text, versionName
77+ )
6378 )
64- val notificationManager = NotificationManagerCompat .from(app)
79+
80+ val notificationManager = NotificationManagerCompat .from(applicationContext)
6581 notificationManager.notify(2000 , notificationBuilder.build())
6682 }
6783
@@ -72,12 +88,14 @@ class NewVersionWorker(
7288 return
7389 }
7490
75- val prefs = PreferenceManager .getDefaultSharedPreferences(applicationContext)
76- // Check if the last request has happened a certain time ago
77- // to reduce the number of API requests.
78- val expiry = prefs.getLong(applicationContext.getString(R .string.update_expiry_key), 0 )
79- if (! isLastUpdateCheckExpired(expiry)) {
80- return
91+ if (! inputData.getBoolean(IS_MANUAL , false )) {
92+ val prefs = PreferenceManager .getDefaultSharedPreferences(applicationContext)
93+ // Check if the last request has happened a certain time ago
94+ // to reduce the number of API requests.
95+ val expiry = prefs.getLong(applicationContext.getString(R .string.update_expiry_key), 0 )
96+ if (! isLastUpdateCheckExpired(expiry)) {
97+ return
98+ }
8199 }
82100
83101 // Make a network request to get latest NewPipe data.
@@ -120,43 +138,42 @@ class NewVersionWorker(
120138 }
121139
122140 override fun doWork (): Result {
123- try {
141+ return try {
124142 checkNewVersion()
143+ Result .success()
125144 } catch (e: IOException ) {
126145 Log .w(TAG , " Could not fetch NewPipe API: probably network problem" , e)
127- return Result .failure()
146+ Result .failure()
128147 } catch (e: ReCaptchaException ) {
129148 Log .e(TAG , " ReCaptchaException should never happen here." , e)
130- return Result .failure()
149+ Result .failure()
131150 }
132- return Result .success()
133151 }
134152
135153 companion object {
136154 private val DEBUG = MainActivity .DEBUG
137155 private val TAG = NewVersionWorker ::class .java.simpleName
138156 private const val NEWPIPE_API_URL = " https://newpipe.net/api/data.json"
157+ private const val IS_MANUAL = " isManual"
139158
140159 /* *
141- * Start a new worker which
142- * checks if all conditions for performing a version check are met,
143- * fetches the API endpoint [.NEWPIPE_API_URL] containing info
144- * about the latest NewPipe version
145- * and displays a notification about ana available update.
160+ * Start a new worker which checks if all conditions for performing a version check are met,
161+ * fetches the API endpoint [.NEWPIPE_API_URL] containing info about the latest NewPipe
162+ * version and displays a notification about an available update if one is available.
146163 * <br></br>
147- * Following conditions need to be met, before data is request from the server:
164+ * Following conditions need to be met, before data is requested from the server:
148165 *
149166 * * The app is signed with the correct signing key (by TeamNewPipe / schabi).
150167 * If the signing key differs from the one used upstream, the update cannot be installed.
151168 * * The user enabled searching for and notifying about updates in the settings.
152169 * * The app did not recently check for updates.
153170 * We do not want to make unnecessary connections and DOS our servers.
154- *
155171 */
156172 @JvmStatic
157- fun enqueueNewVersionCheckingWork (context : Context ) {
158- val workRequest: WorkRequest =
159- OneTimeWorkRequest .Builder (NewVersionWorker ::class .java).build()
173+ fun enqueueNewVersionCheckingWork (context : Context , isManual : Boolean ) {
174+ val workRequest = OneTimeWorkRequestBuilder <NewVersionWorker >()
175+ .setInputData(workDataOf(IS_MANUAL to isManual))
176+ .build()
160177 WorkManager .getInstance(context).enqueue(workRequest)
161178 }
162179 }
0 commit comments