@@ -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,34 @@ 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(applicationContext, R .string.app_update_unavailable_toast,
51+ Toast .LENGTH_SHORT ).show()
52+ }
53+ }
4554 return
4655 }
47- val app = App .getApp()
4856
4957 // A pending intent to open the apk location url in the browser.
5058 val intent = Intent (Intent .ACTION_VIEW , apkLocationUrl?.toUri())
5159 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)
60+ val pendingIntent = PendingIntentCompat .getActivity(
61+ applicationContext, 0 , intent, 0
62+ )
63+ val channelId = applicationContext.getString(R .string.app_update_notification_channel_id)
64+ val notificationBuilder = NotificationCompat .Builder (applicationContext, channelId)
5565 .setSmallIcon(R .drawable.ic_newpipe_update)
5666 .setVisibility(NotificationCompat .VISIBILITY_PUBLIC )
57- .setContentIntent(pendingIntent)
5867 .setAutoCancel(true )
59- .setContentTitle(app.getString(R .string.app_update_notification_content_title))
60- .setContentText(
61- app.getString(R .string.app_update_notification_content_text) +
62- " " + versionName
63- )
64- val notificationManager = NotificationManagerCompat .from(app)
68+ .setContentIntent(pendingIntent)
69+ .setContentTitle(applicationContext.getString(
70+ R .string.app_update_available_notification_title))
71+ .setContentText(applicationContext.getString(
72+ R .string.app_update_available_notification_text, versionName))
73+
74+ val notificationManager = NotificationManagerCompat .from(applicationContext)
6575 notificationManager.notify(2000 , notificationBuilder.build())
6676 }
6777
@@ -72,12 +82,14 @@ class NewVersionWorker(
7282 return
7383 }
7484
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
85+ if (! inputData.getBoolean(IS_MANUAL , false )) {
86+ val prefs = PreferenceManager .getDefaultSharedPreferences(applicationContext)
87+ // Check if the last request has happened a certain time ago
88+ // to reduce the number of API requests.
89+ val expiry = prefs.getLong(applicationContext.getString(R .string.update_expiry_key), 0 )
90+ if (! isLastUpdateCheckExpired(expiry)) {
91+ return
92+ }
8193 }
8294
8395 // Make a network request to get latest NewPipe data.
@@ -120,43 +132,42 @@ class NewVersionWorker(
120132 }
121133
122134 override fun doWork (): Result {
123- try {
135+ return try {
124136 checkNewVersion()
137+ Result .success()
125138 } catch (e: IOException ) {
126139 Log .w(TAG , " Could not fetch NewPipe API: probably network problem" , e)
127- return Result .failure()
140+ Result .failure()
128141 } catch (e: ReCaptchaException ) {
129142 Log .e(TAG , " ReCaptchaException should never happen here." , e)
130- return Result .failure()
143+ Result .failure()
131144 }
132- return Result .success()
133145 }
134146
135147 companion object {
136148 private val DEBUG = MainActivity .DEBUG
137149 private val TAG = NewVersionWorker ::class .java.simpleName
138150 private const val NEWPIPE_API_URL = " https://newpipe.net/api/data.json"
151+ private const val IS_MANUAL = " isManual"
139152
140153 /* *
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.
154+ * Start a new worker which checks if all conditions for performing a version check are met,
155+ * fetches the API endpoint [.NEWPIPE_API_URL] containing info about the latest NewPipe
156+ * version and displays a notification about an available update if one is available.
146157 * <br></br>
147- * Following conditions need to be met, before data is request from the server:
158+ * Following conditions need to be met, before data is requested from the server:
148159 *
149160 * * The app is signed with the correct signing key (by TeamNewPipe / schabi).
150161 * If the signing key differs from the one used upstream, the update cannot be installed.
151162 * * The user enabled searching for and notifying about updates in the settings.
152163 * * The app did not recently check for updates.
153164 * We do not want to make unnecessary connections and DOS our servers.
154- *
155165 */
156166 @JvmStatic
157- fun enqueueNewVersionCheckingWork (context : Context ) {
158- val workRequest: WorkRequest =
159- OneTimeWorkRequest .Builder (NewVersionWorker ::class .java).build()
167+ fun enqueueNewVersionCheckingWork (context : Context , isManual : Boolean ) {
168+ val workRequest = OneTimeWorkRequestBuilder <NewVersionWorker >()
169+ .setInputData(workDataOf(IS_MANUAL to isManual))
170+ .build()
160171 WorkManager .getInstance(context).enqueue(workRequest)
161172 }
162173 }
0 commit comments